const _Http = getApp().globalData.http,
	getTime = require("../../utils/getTime");
let sa_paybilldetailid = 0;
Page({
	data: {
		showAll: false,
		disabled: true,
		content: {
			"sa_paybillid": 0,
			enterprisename: "",
			period: ""
		},
		form: [{
				label: "转出账号",
				error: false,
				errMsg: "",
				type: "route",
				url: "/pages/bankAccount/index",
				params: {},
				value: "",
				placeholder: "转出账号",
				valueName: "bankcardno",
				interrupt: true,
				required: true
			}, {
				label: "打款人",
				error: false,
				errMsg: "",
				type: "text",
				value: "",
				placeholder: "打款人",
				disabled: true,
				valueName: "payer",
				required: true
			}, {
				label: "转出银行",
				error: false,
				errMsg: "",
				type: "text",
				value: "",
				placeholder: "转出银行",
				disabled: true,
				valueName: "bank",
				required: true
			}, {
				label: "打款日期",
				error: false,
				errMsg: "",
				type: "date",
				value: getTime.formatTime(new Date(), '-').split(' ')[0],
				placeholder: "选择打款日期",
				valueName: "paydate",
				required: true,
			}, {
				label: "转入银行",
				error: false,
				errMsg: "",
				type: "selector",
				range: [],
				value: "",
				rangeKey: "remarks",
				rangeIndex: "",
				placeholder: "选择转入银行",
				valueName: "inbank",
				required: true,
				interrupt: true
			},
			{
				label: "银行账号",
				error: false,
				errMsg: "",
				type: "text",
				value: "",
				placeholder: "选择转入银行后自动带入",
				valueName: "inbankcardno",
				required: true,
			}, {
				label: "转入账户",
				error: false,
				errMsg: "",
				type: "selector",
				range: [],
				value: "",
				rangeKey: "accountname",
				rangeIndex: "",
				placeholder: "选择转入账户",
				valueName: "sa_accountclassid",
				required: true,
				interrupt: true
			}, {
				label: "转入金额",
				error: false,
				errMsg: "",
				type: "digit",
				value: "",
				placeholder: "转入金额",
				valueName: "amount",
				required: true,
			}, {
				label: "备注",
				error: false,
				errMsg: "",
				type: "text",
				value: "",
				placeholder: "输入备注",
				valueName: "remarks",
				required: false,
			},
		]
	},
	onLoad(options) {
		let sa_paybillid = 0;
		if (options.rowData) {
			let data = JSON.parse(options.rowData);
			sa_paybillid = data.sa_paybillid;
			sa_paybilldetailid = data.sa_paybilldetailid;
			this.setData({
				form: this.data.form.map(v => {
					if (v.valueName == 'bankcardno') {
						v.value = [data[v.valueName], data[v.valueName]];
					} else if (v.valueName == "sa_accountclassid") {
						v.id = data[v.valueName];
						v.value = data.accountname;
					} else {
						v.value = data[v.valueName];
					}
					return v
				}),
				'content.sa_paybillid': data.sa_paybillid
			})
			this.selectComponent('#Form').confirm()
		}
		_Http.basic({
			"classname": "sysmanage.develop.optiontype.optiontype",
			"method": "optiontypeselect",
			"content": {
				"pageNumber": 1,
				"pageSize": 1000,
				"typename": "inbank",
				"parameter": {}
			},
		}).then(res => {
			console.log("银行列表", res)
			if (res.msg == '成功') {
				let item = this.data.form.find(v => v.label == '转入银行')
				item.range = res.data;
				this.setData({
					form: this.data.form
				})
			}
		});

		_Http.basic({
			"id": 20221228085004,
			"content": {
				"pageNumber": 1,
				"pageSize": 9999,
				sa_paybillid,
				"where": {
					"ispay": 1
				}
			},
		}).then(res => {
			console.log("获取可选择账户", res)
			if (res.msg == '成功') {
				let item = this.data.form.find(v => v.label == '转入账户')
				item.range = res.data;
				if (sa_paybillid) item.disabled = true;
				this.setData({
					form: this.data.form
				})
			}
		})
	},
	/* 打断处理form */
	interrupt({
		detail
	}) {
		if (detail.data.label == '转入银行') {
			detail.form[4] = detail.data;
			detail.form[5].value = detail.result.value;
		} else if (detail.temporary.item.label == '转出账号') {
			detail.form[0].value = [detail.data.bankcardno, detail.data.bankcardno];
			detail.form[1].value = detail.data.name;
			detail.form[2].value = detail.data.bank;
			wx.navigateBack()
		} else if (detail.temporary.item.label == '转入账户') {
			detail.form[6].value = detail.result.accountname;
			detail.form[6].id = detail.result.sa_accountclassid;
		}
		this.setData({
			form: detail.form
		})
		this.selectComponent('#Form').confirm()
	},
	async submit() {
		let content = {
			...this.data.content,
			...this.selectComponent("#Form").submit()
		}
		content.sa_accountclassid = this.data.form.find(v => v.label == '转入账户').id;
		content.bankcardno = content.bankcardno[0]
		console.log(content)
		let res = await _Http.basic({
			"id": "20221226153004",
			content,
			version: 1
		})
		console.log("创建打款凭证", res);
		if (res.msg == '成功') {
			_Http.basic({
				"id": 20221227092904,
				"content": {
					"paybilldetails": [{
						"amount": content.amount,
						"sa_accountclassid": content.sa_accountclassid,
						sa_paybilldetailid
					}],
					"sa_paybillid": res.data.sa_paybillid
				}
			}).then(s => {
				console.log("修改明细", s)
				if (s.msg == '成功') {
					let page = getCurrentPages()[getCurrentPages().length - 2];
					if (page.__route__ == 'packageA/remitVoucher/index') {
						wx.redirectTo({
							url: '/packageA/remitVoucher/detail?id=' + res.data.sa_paybillid,
						})
					} else if (page.__route__ == 'packageA/remitVoucher/detail') {
						wx.navigateBack();
						page.getDetail(true)
					}
				} else {
					wx.showToast({
						title: res.msg,
						icon: "none",
						mask: true
					})
				}

			})
		} else {
			wx.showToast({
				title: res.msg,
				icon: "none",
				mask: true
			})
		}

	},
	// 是否显示全部
	onChange({
		detail
	}) {
		this.setData({
			showAll: detail
		})
	},
	/* 表单必填项是否完成 */
	onConfirm({
		detail
	}) {
		this.setData({
			disabled: detail
		})
	}
})