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

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: "digit",
				value: "",
				placeholder: "请输入打款总金额",
				valueName: "amount",
				required: true,

			}, {
				label: "转入银行",
				error: false,
				errMsg: "",
				type: "selector",
				range: [],
				value: "",
				rangeKey: "remarks",
				rangeIndex: "",
				placeholder: "请选择转入银行",
				valueName: "inbank",
				required: false,
				interrupt: true
			},
			{
				label: "转入账户",
				error: false,
				errMsg: "",
				type: "text",
				value: "",
				placeholder: "请选择转入银行后自动带入",
				valueName: "inbankcardno",
				required: false,
			},
			{
				label: "备注",
				error: false,
				errMsg: "",
				type: "text",
				value: "",
				placeholder: "请输入备注",
				valueName: "remarks",
				required: false,
			},
		]
	},
	onLoad(options) {
		if (options.rowData) {
			let data = JSON.parse(options.rowData);
			this.setData({
				form: this.data.form.map(v => {
					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
				})
			}
		});
	},
	/* 打断处理form */
	interrupt({
		detail
	}) {
		if (detail.data.label == '转入银行') {
			detail.form[5] = detail.data;
			detail.form[6].value = detail.result.value;
			this.setData({
				form: detail.form
			})
		} 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;
			this.setData({
				form: detail.form
			})
			wx.navigateBack()
		}
		this.selectComponent('#Form').confirm()
	},
	async submit() {
		let content = {
			...this.data.content,
			...this.selectComponent("#Form").submit()
		}
		content.bankcardno = content.bankcardno[0]
		let res = await _Http.basic({
			"id": "20221226153004",
			content,
			version: 1
		})
		console.log("创建打款凭证", res);
		if (res.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
			})
		}
	},
	// 是否显示全部
	onChange({
		detail
	}) {
		this.setData({
			showAll: detail
		})
	},
	/* 表单必填项是否完成 */
	onConfirm({
		detail
	}) {
		this.setData({
			disabled: detail
		})
	}
})