<template>
	<app-layout>
		<view class="cash">
			<view class="search">
                <view v-if="searchBool" style="position: relative">
    				<input placeholder="请输入昵称搜索" type="text" class="input" focus v-model="inputText" @focus="getFocus=true" @blur="searchText">
                    <image v-if="getFocus && inputText.length > 0" @click="clearSearch" class="search-clear" src="../image/clear.png"></image>
                </view>
				<view class="view dir-left-nowrap main-center cross-center" v-else @click="searchBool = true">
					<image src="../image/icon-search.png"></image>
					<view>{{type === 1 ? '请输入订单号/店铺名/昵称搜索':'请输入昵称搜索'}}</view>
				</view>
			</view>
			<view class="bar dir-left-nowrap" v-if="mch === '1' && share === '1'">
				<view class="bar-item">
					<app-form-id @click="mchShare(1)">
						<text :class="{'text-active': type === 1}">多商户</text>
					</app-form-id>
				</view>
				<view class="bar-item">
					<app-form-id @click="mchShare(2)">
						<text :class="{'text-active': type === 2}">分销商</text>
					</app-form-id>
				</view>
			</view>
			<view class="reply main-center cross-center reply-margin-search" :class="{'reply-margin-bar': mch === '1' && share === '1'}">
				<view class="reply-item unreviewed " :class="{'reply-item-active': status === 0}">
					<app-form-id @click="setStatus(0)">
						未审核
					</app-form-id>
				</view>
				<view class="reply-item unpaid" :class="{'reply-item-active': status === 1}">
					<app-form-id @click="setStatus(1)">
						未打款
					</app-form-id>
				</view>
			</view>
			<view class="content content-margin-search" :class="{'content-margin-bar': mch === '1' && share === '1'}">
				<view class="content-item dir-left-nowrap" v-for="(item, index) in list" :key="index">
					<view class="image">
						<image v-if="type === 2" :src="item.user.avatar"></image>
						<image v-if="type === 1" :src="item.mch.user.userInfo.avatar"></image>
					</view>
					<view class="app-content dir-top-nowrap">
						<view class="name" v-if="type === 1">{{item.mch.user.nickname }}</view>
						<view class="name" v-if="type === 2">{{item.user.nickname}}</view>
						<view class="amount" v-if="type === 2">提现金额：￥{{item.cash.price}}</view>
						<view class="amount" v-if="type === 1">提现金额：￥{{item.money}}</view>
                        <view class="amount" v-if="type === 2">手续费：￥{{item.cash.service_charge}}</view>
						<view class="pay dir-left-nowrap">
							<view class="pay-price-text">打款金额：</view>
							<view class="pay-price-number" v-if="type === 2">￥{{item.cash.actual_price}}</view>
							<view class="pay-price-number" v-if="type === 1">￥{{item.money}}</view>
						</view>
						<view class="button-men dir-left-nowrap main-right cross-center">
							<view class="button ordinary">
								<app-form-id @click="refuse(item)">
									拒绝
								</app-form-id>
							</view>
							<view class="button can" v-if="item.status == '0'">
								<app-form-id @click="byGo(item)">
									通过
								</app-form-id>
							</view>
							<view class="button can" v-if="item.status == '1'">
								<app-form-id @click="pay(item)">
									打款
								</app-form-id>
							</view>
						</view>
					</view>
				</view>
			</view>
			<view class="no-tip" v-if="list.length === 0">
				<image src="https://v4test.zjhejiang.com/web/statics/img/app/app_admin/no-apply.png"></image>
				<view>没有任何申请哦~</view>
			</view>
			<view @touchmove.stop.prevent="" class="model" v-if="model">
				<view class="model-content">
					<view class="header">{{modelType === 2 ? '通过申请' : modelType === 3 ? '打款' : modelType === 1 ? '拒绝申请' :''}}</view>
					<view class="center">
						<template v-if="modelType === 1">
							<view class="free">
								<textarea class="textarea" placeholder="请填写拒绝理由" v-model="textarea"></textarea>
							</view>
						</template>
						<template v-if="modelType === 2">
							<view class="i-modal-body">
								<view>是否确认通过提现申请</view>
							</view>
						</template>
						<template v-if="modelType === 3">
							<view class="i-modal-body-free">
								<view class="price" v-if="type === 1">￥{{setItem.money}}</view>
								<view class="price" v-if="type === 2">￥{{setItem.cash.actual_price}}</view>
								<view class="text">是否确认打款</view>
							</view>
						</template>
					</view>
					<view class="bottom dir-left-nowrap cross-center">
						<view class="but cancel">
							<app-form-id @click="model=false; modelType=-1">取消</app-form-id>
						</view>
						<view class="line"></view>
						<view class="but confirm">
							<app-form-id @click="confirm">确认</app-form-id>
						</view>
					</view>
				</view>
			</view>
		</view>
	</app-layout>
</template>

<script>
    export default {
        name: "cash",
	    data() {
            return {
                searchBool: false,
	            list: [],
                mch: '0',
	            share: '0',
	            type: 1,
                getFocus: false,
                status: 0,
	            over: false,
                model: false,
                modelType: -1,
                textarea: '',
	            setItem: {},
                page: 1,
                inputText: '',
            }
	    },
	    onLoad(options) {
            this.mch = options.mch;
            this.share = options.share;
            if (this.mch === '0') {
                this.type = 2;
            }
            this.$request({
	            url: this.$api.app_admin.cash,
	            data: {
                    page: 1,
                    type: this.type,
		            status: 0,
                    keyword: '',
	            }
            }).then(response => {
                if (response.code === 0) {
                    this.list = response.data.list;
                }
            })
	    },
        onReachBottom() {
            if (!this.over) {
                this.page++;
                this.bottomRequest();
            }
        },
	    methods: {
            searchText() {
                let that = this;
                setTimeout(v=>{
                    that.page = 1;
                    that.over = false;
                    that.getFocus=false;
                    if(that.inputText == '') {
                        that.searchBool = false;
                    }
                    switch (that.type) {
                        case 1:
                            that.switchRequest({type: 1});
                            break;
                        case 2:
                            that.switchRequest({type: 2});
                            break;
                    }
                },300)
            },
            clearSearch() {
                this.inputText = '';
            },
            bottomRequest() {
                if (this.type === 1 && this.status === 0) {
                    this.$request({
	                    url: this.$api.app_admin.cash,
	                    data: {
	                        type: 1,
                            page: this.page,
		                    status: 0,
		                    keyword: '',
	                    }
                    }).then(response => {
                        if (response.code === 0) {
                            if (response.data.list.length === 0) {
                                this.over = true;
                            } else {
                                this.list = [...this.list, ...response.data.list];
                            }
                        }
                    })
                } else if (this.type === 1 && this.status === 1) {
                    this.$request({
                        url: this.$api.app_admin.cash,
                        data: {
                            type: 1,
                            page: this.page,
                            status: 1,
                            keyword: '',
                        }
                    }).then(response => {
                        if (response.code === 0) {
                            if (response.data.list.length === 0) {
                                this.over = true;
                            } else {
                                this.list = [...this.list, ...response.data.list];
                            }
                        }
                    })
                } else if (this.type === 2 && this.status === 0) {
                    this.$request({
                        url: this.$api.app_admin.cash,
                        data: {
                            type: 2,
                            page: this.page,
                            status: 0,
                            keyword: '',
                        }
                    }).then(response => {
                        if (response.code === 0) {
                            if (response.data.list.length === 0) {
                                this.over = true;
                            } else {
                                this.list = [...this.list, ...response.data.list];
                            }
                        }
                    })
                } else if (this.type === 2 && this.status === 1) {
                    this.$request({
                        url: this.$api.app_admin.cash,
                        data: {
                            type: 2,
                            page: this.page,
                            status: 1,
                            keyword: '',
                        }
                    }).then(response => {
                        if (response.code === 0) {
                            if (response.data.list.length === 0) {
                                this.over = true;
                            } else {
                                this.list = [...this.list, ...response.data.list];
                            }
                        }
                    })
                }
            },
            setStatus(data) {
                this.page = 1;
                this.status = data;
                this.over = false;
                this.inputText = '';
                this.searchBool = false;
                this.$request({
	                url: this.$api.app_admin.cash,
	                data: {
	                    page: 1,
		                type: this.type,
		                status: data,
		                keyword: this.inputText,
	                }
                }).then(response => {
                    if (response.code === 0) {
                        this.list = response.data.list;
                    }
                });
            },
            mchShare(data) {
                this.page = 1;
                this.status = 0;
                this.over = false;
                this.type = data;
                this.inputText = '';
                this.searchBool = false;
                this.list = [];
                this.$request({
	                url: this.$api.app_admin.cash,
	                data: {
                        page: 1,
		                type: data,
                        status: 0,
		                keyword: this.inputText,
	                }
                }).then(response => {
                    if (response.code === 0) {
                        this.list = response.data.list;
                    }
                });
            },
            confirm() {
                // 拒绝
	            if (this.modelType === 1) {
	                if (this.textarea === '') return;
	                if (this.type === 1 && this.status === 0) {
                        this.$request({
                            url: this.$api.app_admin.verify,
                            method: 'post',
                            data: {
                                status: 0,
                                id: this.setItem.id,
                                content: this.textarea,
                                type: 1,
                            }
                        }).then(response => {
                            if (response.code === 0) {
                                for (let i = 0; i < this.list.length; i++) {
                                    if (this.list[i].id === this.setItem.id) {
                                        this.$delete(this.list, i);
                                        this.model = false;
                                        this.textarea = '';
                                    }
                                }
                            }
                        });
	                } else if (this.type === 1 && this.status === 1) {
                        this.$request({
                            url: this.$api.app_admin.user_cash,
                            method: 'post',
                            data: {
                                id: this.setItem.id,
                                content: this.textarea,
                                type: this.type,
                                transfer_type: 3,
                            }
                        }).then(response => {
                            if (response.code === 0) {
                                for (let i = 0; i < this.list.length; i++) {
                                    if (this.list[i].id === this.setItem.id) {
                                        this.$delete(this.list, i);
                                        this.model = false;
                                        this.textarea = '';
                                    }
                                }
                            }
                        })
	                } else if (this.type === 2 && this.status === 0) {
                        this.$request({
                            url: this.$api.app_admin.verify,
                            method: 'post',
                            data: {
                                status: 3,
                                id: this.setItem.id,
                                content: this.textarea,
                                type: 2,
                            }
                        }).then(response => {
                            if (response.code === 0) {
                                for (let i = 0; i < this.list.length; i++) {
                                    if (this.list[i].id === this.setItem.id) {
                                        this.$delete(this.list, i);
                                        this.model = false;
                                        this.textarea = '';
                                    }
                                }
                            }
                        });
	                } else if (this.type === 2 && this.status === 1) {
                        this.$request({
                            url: this.$api.app_admin.user_cash,
                            method: 'post',
                            data: {
                                id: this.setItem.id,
                                content: this.textarea,
                                type: 2,
                                status: 3,
                            }
                        }).then(response => {
                            if (response.code === 0) {
                                for (let i = 0; i < this.list.length; i++) {
                                    if (this.list[i].id === this.setItem.id) {
                                        this.$delete(this.list, i);
                                        this.model = false;
                                        this.textarea = '';
                                    }
                                }
                            }
                        })
	                }
	            } else if (this.modelType === 3) {
	                if (this.type === 1) {
                        this.$request({
                            url: this.$api.app_admin.user_cash,
                            method: 'post',
                            data: {
                                id: this.setItem.id,
                                content: '小程序端操作',
                                type: 1,
                                transfer_type: 1,
                            }
                        }).then(response => {
                            if (response.code === 0) {
                                for (let i = 0; i < this.list.length; i++) {
                                    if (this.list[i].id === this.setItem.id) {
                                        this.$delete(this.list, i);
                                        this.model = false;
                                    }
                                }
                            }
                        })
	                } else if (this.type === 2) {
                        this.$request({
                            url: this.$api.app_admin.user_cash,
                            method: 'post',
                            data: {
                                id: this.setItem.id,
                                content: '小程序端操作',
                                type: 2,
                                status: 2,
                            }
                        }).then(response => {
                            if (response.code === 0) {
                                for (let i = 0; i < this.list.length; i++) {
                                    if (this.list[i].id === this.setItem.id) {
                                        this.$delete(this.list, i);
                                        this.model = false;
                                    }
                                }
                            } else if (response.code === 1) {
                                uni.showModal({
                                    title: '提示',
                                    content: response.msg,
                                });
                                this.model = false;
                            }
                        })
	                }
	            } else if (this.modelType === 2) {
	                if (this.type === 1) {
                        this.$request({
                            url: this.$api.app_admin.verify,
                            method: 'post',
                            data: {
                                status: 1,
                                id: this.setItem.id,
                                content: '小程序端操作',
                                type: 1,
                            }
                        }).then(response => {
                            if (response.code === 0) {
                                for (let i = 0; i < this.list.length; i++) {
                                    if (this.list[i].id === this.setItem.id) {
                                        this.$delete(this.list, i);
                                        this.model = false;
                                        this.textarea = '';
                                    }
                                }
                            }
                        });
	                } else if (this.type === 2) {
                        this.$request({
                            url: this.$api.app_admin.verify,
                            method: 'post',
                            data: {
                                status: 1,
                                id: this.setItem.id,
                                content: '小程序端操作',
                                type: 2,
                            }
                        }).then(response => {
                            if (response.code === 0) {
                                for (let i = 0; i < this.list.length; i++) {
                                    if (this.list[i].id === this.setItem.id) {
                                        this.$delete(this.list, i);
                                        this.model = false;
                                        this.textarea = '';
                                    }
                                }
                            }
                        });
	                }
	            }
            },
            refuse(data) {
                this.setItem = data;
                this.model = true;
                this.modelType = 1;
            },
            byGo(data) {
                this.setItem = data;
                this.model = true;
                this.modelType = 2;
            },
            pay(data) {
                this.setItem = data;
                this.model = true;
                this.modelType = 3;
            },
		    async request(data) {
                const response = await this.$request({
                    url: this.$api.app_admin.cash,
                    data: {
                        type: data.type,
                        page: data.page,
                        status: data.status,
                        keyword: this.inputText,
                    }
                });
                if (response.code === 0) {
                    this.list = response.data.list;
                }
		    },
		    switchRequest(data) {
                if (this.status === 0) {
                    this.request({
                        type: data.type,
                        page: this.page,
                        status: 0,
                    });
                } else if (this.status === 1) {
                    this.request({
                        type: data.type,
                        page: this.page,
                        status: 1,
                    });
                }
		    }
	    }
    }
</script>

<style scoped lang="scss">
	@import "./cash.scss";
</style>