const _Http = getApp().globalData.http,
	getTime = require("../../utils/getTime")

Page({
	data: {
		loading: true,
		sa_paybillid: null,
		tabsActive: 1,
		currentDate: new Date().getTime(),
		formatter(type, value) {
			if (type === 'year') {
				return `${value}年`;
			}
			if (type === 'month') {
				return `${value}月`;
			}
			return value;
		},
		show: false,
		tabsList: [{
			label: "详细信息",
			icon: "icon-tabchanpin"
		}, {
			label: "打款明细",
			icon: "icon-tabcaozuojilu1",
			model: "#remitVoucher"
		}],
		tabbarsList: []
	},
	onLoad(options) {
		this.setData({
			sa_paybillid: options.id
		});
		this.getDetail(true);
	},
	/* 获取详情 */
	getDetail(init = false, show = true) {
		_Http.basic({
			"id": 20221226153404,
			"content": {
				nocache: true,
				"sa_paybillid": this.data.sa_paybillid
			}
		}, show).then(res => {
			console.log("出货详情", res)
			if (res.msg != '成功') return wx.showToast({
				title: res.msg,
				icon: "none"
			});

			let tabbarsList = []
			if (res.data.status == '新建') tabbarsList = [{
				label: "编辑",
				icon: "icon-dibu-bianji"
			}, {
				label: "提交",
				icon: "icon-tijiao",
			}, {
				label: "删除",
				icon: "icon-guanlian-shanchu"
			}]
			if (res.data.status == '提交') tabbarsList = [{
				label: "审核",
				icon: "icon-shenhe"
			}, {
				label: "退回",
				icon: "icon-a-fanshenhetuihui"
			}]
			if (res.data.status == '审核') tabbarsList = [{
				label: "退回",
				icon: "icon-a-fanshenhetuihui"
			}]

			this.setPreview(res.data);
			this.setData({
				detail: res.data,
				tabbarsList,
				loading: false
			});
			if (init) this.partialRenewal(true)
		})
	},
	/* 设置详情信息 */
	setPreview(data) {
		let list1 = [{
				label: "凭证单号",
				value: data.billno
			}, {
				label: "状态",
				value: data.status
			}, {
				label: "经销商编号",
				value: data.agentnum
			}, {
				label: "经销商简称",
				value: data.enterprisename
			}, {
				label: "回款归属月份",
				value: data.period
			}, {
				label: "打款金额",
				value: data.amount
			}],
			list2 = [{
				label: "创建人",
				value: data.createby
			}, {
				label: "创建时间",
				value: data.createdate
			}, {
				label: "审核人",
				value: data.checkby
			}, {
				label: "审核时间",
				value: data.checkdate
			}, {
				label: "付款人",
				value: data.payer
			}, {
				label: "付款时间",
				value: data.paydate
			}];
		this.setData({
			list1,
			list2
		})
	},
	/* 审核选择弹出框关闭 */
	onClose() {
		this.setData({
			show: false
		})
	},
	/* 审核提交 */
	async checkSubmit({
		detail
	}) {
		let res = await _Http.basic({
			"id": "20221226153304",
			"content": {
				"sa_paybillid": this.data.sa_paybillid,
				"period": getTime.formatTime(new Date(detail), '-').split(' ')[0]
			}
		})
		console.log(res);
		if (res.msg == '成功') {
			this.setData({
				show: false
			})
			this.getDetail(true)
		}
	},
	tabbarOnClick({
		detail
	}) {
		switch (detail.label) {
			case "编辑":
				wx.navigateTo({
					url: `/packageA/remitVoucher/update?rowData=${JSON.stringify(this.data.detail)}`,
				})
				break;
			case "提交":
				wx.showModal({
					title: '提示',
					content: `是否确认提交该打款凭证？`,
					complete: ({
						confirm
					}) => {
						if (confirm) _Http.basic({
							"id": "20221226153204",
							"content": {
								"sa_paybillid": this.data.sa_paybillid
							}
						}).then(res => {
							wx.showToast({
								title: res.msg == '成功' ? `已提交改打款凭证` : res.msg,
								icon: "none"
							});
							if (res.msg == '成功') {
								this.getDetail()
								let pages = getCurrentPages(),
									page = pages[pages.length - 2]
								page.getList(true)
							}
						})
					}
				})
				break;
			case "审核":
				wx.showToast({
					title: '请选择回款日期',
					icon: 'none'
				})
				this.setData({
					show: true
				})
				break;
			case "退回":
				wx.showModal({
					title: '提示',
					content: `是否确认退回该打款凭证？`,
					complete: ({
						confirm
					}) => {
						if (confirm) _Http.basic({
							"id": 20221227110104,
							"content": {
								sa_paybillid: this.data.sa_paybillid
							}
						}).then(res => {
							wx.showToast({
								title: res.msg == '成功' ? `已退回该凭证` : res.msg,
								icon: "none"
							});
							if (res.msg == '成功') {
								this.getDetail()
							}
						})
					}
				})
				break;
			case "删除":
				wx.showModal({
					title: '提示',
					content: `是否删除该凭证？`,
					complete: ({
						confirm
					}) => {
						if (confirm) _Http.basic({
							"id": 20221226153104,
							"content": {
								sa_paybillids: [this.data.sa_paybillid]
							}
						}).then(res => {
							wx.showToast({
								title: res.msg == '成功' ? `已删除该凭证` : res.msg,
								icon: "none"
							});
							if (res.msg == '成功') {
								setTimeout(() => {
									wx.navigateBack()
									getCurrentPages()[getCurrentPages().length - 2].getList(true)
								}, 300)
							}
						})
					}
				})
				break;
		}
	},
	onInput(detail) {

	},
	//tabs 切换
	tabsChange({
		detail
	}) {
		this.setData({
			tabsActive: detail
		});
		console.log(this.data.tabsActive);
		this.partialRenewal();
	},
	//局部数据更新 tabs
	partialRenewal(init = false) {
		let model = this.data.tabsList[this.data.tabsActive].model;
		if (model) {
			let Component = this.selectComponent(model),
				{
					total,
					pageNumber,
					pageTotal
				} = Component.data.content,
				id = this.data.detail.sa_paybillid;
			if (total == null || init) {
				Component.getList(id, init);
			} else if (pageNumber <= pageTotal) {
				Component.getList(id, false);
			}
		}
	},
	onReachBottom() {
		this.partialRenewal();
	},
	onUnload() {
		let page = getCurrentPages().find(v => v.__route__ == 'packageA/remitVoucher/index');
		let content = JSON.parse(JSON.stringify(page.data.content));
		content.pageSize = (page.data.content.pageNumber - 1) * page.data.content.pageSize;
		content.pageNumber = 1;
		_Http.basic({
			"id": 20221226152904,
			content
		}).then(res => {
			console.log("更新订单列表", res)
			if (res.msg == '成功') page.setData({
				list: res.data
			})
		})
	},
})