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

Page({
	data: {
		loading: true,
		sa_aftersalesmagid: 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: "#toolBill"
		}],
		tabbarsList: []
	},
	onLoad(options) {
		this.setData({
			sa_aftersalesmagid: options.id
		});
		this.getDetail(true);
	},
	/* 获取详情 */
	getDetail(init = false, show = true) {
		_Http.basic({
			"id": 20230104160603,
			"content": {
				nocache: true,
				"sa_aftersalesmagid": this.data.sa_aftersalesmagid
			}
		}, 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-tabgenjinjilu"
			}]
			if (res.data.status == '审核') tabbarsList = []

			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.billdate
				}, {
					label: "状态",
					value: data.status
				},
				{
					label: '是否实物返回',
					value: data.isitemreturn ? '是' : '否'
				}, {
					label: "提交日期",
					value: data.submitdate
				}, {
					label: "审核日期",
					value: data.checkdate
				}, {
					label: "备注",
					value: data.remarks
				}
			],
			list2 = [{
				label: "创建人",
				value: data.createby
			}, {
				label: "创建时间",
				value: data.createdate
			}, {
				label: "审核人",
				value: data.checkby
			}, {
				label: "审核时间",
				value: data.checkdate
			}];
		this.setData({
			list1,
			list2
		})
	},
	/* 审核选择弹出框关闭 */
	onClose() {
		this.setData({
			show: false
		})
	},
	/* 审核提交 */
	async checkSubmit({
		detail
	}) {
		let res = await _Http.basic({
			"id": "20221226153304",
			"content": {
				"sa_aftersalesmagid": this.data.sa_aftersalesmagid,
				"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/toolBill/update?rowData=${JSON.stringify(this.data.detail)}`,
				})
				break;
			case "提交":
				wx.showModal({
					title: '提示',
					content: `是否确认提交该工具归还单？`,
					complete: ({
						confirm
					}) => {
						if (confirm) _Http.basic({
							"id": "20230105161603",
							"content": {
								"sa_aftersalesmagid": this.data.sa_aftersalesmagid
							}
						}).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.showModal({
					title: '提示',
					content: `是否确认取消提交该工具归还单？`,
					complete: ({
						confirm
					}) => {
						if (confirm) _Http.basic({
							"id": "20230105161703",
							"content": {
								"sa_aftersalesmagid": this.data.sa_aftersalesmagid
							}
						}).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.showModal({
					title: '提示',
					content: `是否删除该工具归还单？`,
					complete: ({
						confirm
					}) => {
						if (confirm) _Http.basic({
							"id": 20230104160803,
							"content": {
								sa_aftersalesmagids: [this.data.sa_aftersalesmagid]
							}
						}).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_aftersalesmagid;
			if (total == null || init) {
				Component.getList(id, init);
			} else if (pageNumber < pageTotal) {
				Component.getList(id, false);
			}
		}
	},
	onReachBottom() {
		this.partialRenewal();
	},
	onUnload() {
		console.log("更新列表数据")
		let page = getCurrentPages().find(v => v.__route__ == 'packageA/toolBill/index');
		let content = JSON.parse(JSON.stringify(page.data.content));
		content.pageNumber = 1;
		content.pageSize = (page.data.content.pageNumber - 1) * page.data.content.pageSize;
		_Http.basic({
			"id": 20230105161503,
			content
		}).then(res => {
			console.log("更新订单列表", res)
			if (res.msg == '成功') page.setData({
				list: res.data
			})
		})
	},
})