const _Http = getApp().globalData.http;

Page({
	data: {
		loading: true,
		showAll: false,
		disabled: true,
		form: [{
			label: "发票种类",
			error: false,
			errMsg: "",
			type: "radio",
			radioList: [{
					name: 's/专用发票',
					id: 's'
				},
				{
					name: 'p/普通发票(电票)',
					id: 'p'
				},
			],
			value: "s",
			valueName: "invoiceline",
			required: false,
		}, {
			label: "发票限额",
			error: false,
			errMsg: "",
			type: "number", //digit
			value: "",
			placeholder: "发票限额",
			valueName: "quota",
			required: false
		}, {
			label: "发票复核人",
			error: false,
			errMsg: "",
			type: "text",
			value: "王蒋霞",
			placeholder: "发票复核人",
			valueName: "invoicechecker",
			required: false
		}, {
			label: "发票收款人",
			error: false,
			errMsg: "",
			type: "text",
			value: "孙惠吉",
			placeholder: "发票收款人",
			valueName: "invoicepayee",
			required: false
		}, {
			label: "发票开票员",
			error: false,
			errMsg: "",
			type: "text",
			value: "冯云霞",
			placeholder: "发票开票员",
			valueName: "invoiceclerk",
			required: false
		}, {
			label: "备注",
			error: false,
			errMsg: "",
			type: "textarea",
			value: "",
			placeholder: "备注",
			valueName: "remarks",
			required: false
		}, ],
		content: {
			sa_invoiceapplyid: 0,
			byhand: false,
			enterprisename: "", //抬头
			taxno: "", //税号
			bank: "", //开户行
			phonenumber: "", //手机号
			bankcardno: "", //开户账号
			address: "", //开票地址
			quota: 0,
		}
	},
	onLoad(options) {
		console.log(wx.getStorageSync('userrole'))
		if (wx.getStorageSync('userrole') == '业务员') {
			this.data.form.unshift({
				label: "开票企业",
				error: false,
				errMsg: "",
				type: "route",
				url: "/select/agent/index",
				params: {
					"id": "20221216145803",
					"version": 1,
					"content": {
						nocache: true,
						"where": {
							"condition": ""
						}
					}
				},
				interrupt: true,
				query: '&radio=true',
				value: "",
				placeholder: "选择开票企业",
				valueName: "sys_enterpriseid",
				required: true
			})
		} else {
			this.data.form.unshift({
				label: "开票抬头",
				error: false,
				errMsg: "",
				type: "route",
				url: "/select/finance/index",
				params: {
					"id": 20221013160602,
					"content": {
						nocache: true,
						"pageSize": 20,
						"where": {
							"condition": "",
						},
					},
				},
				interrupt: true,
				query: '&radio=true',
				value: "",
				placeholder: "选择开票抬头信息",
				valueName: "principal",
				checking: "base",
				required: true
			})
		}
		this.setData({
			form: this.data.form
		})
	},
	/* 打断处理form */
	interrupt({
		detail
	}) {
		console.log("打断", detail)
		if (detail.temporary.item.label == "开票抬头") {
			let item = detail.data.item;
			detail.data.value[0] = `抬头:${item.enterprisename}\n税号:${item.taxno}\n地址:${item.address}`;
			detail.form[detail.temporary.index].value = detail.data.value;
			this.setData({
				"content.enterprisename": item.enterprisename,
				"content.taxno": item.taxno,
				"content.bank": item.bank,
				"content.phonenumber": item.phonenumber,
				"content.bankcardno": item.bankcardno,
				"content.address": item.address,
			})
		} else if (detail.temporary.item.label == "开票企业") {
			detail.form[0].value = detail.data.value;
			detail.form.splice(1, detail.form[1].label == '发票种类' ? 0 : 1, {
				label: "开票抬头",
				error: false,
				errMsg: "",
				type: "route",
				url: "/select/finance/index",
				params: {
					"id": 20221013160602,
					"content": {
						nocache: true,
						sys_enterpriseid: detail.data.id[0],
						"pageSize": 20,
						"where": {
							"condition": "",
						},
					},
				},
				interrupt: true,
				query: '&radio=true',
				value: "",
				placeholder: "选择开票抬头信息",
				valueName: "principal",
				checking: "base",
				required: true
			})
		}
		this.setData({
			form: detail.form,
		})
		this.selectComponent("#Form").confirm();
		wx.navigateBack()
	},
	submit() {
		let that = this;
		let data = this.selectComponent("#Form").submit();
		let content = Object.assign(this.data.content, data);
		content.sys_enterpriseid && (content.sys_enterpriseid = data.sys_enterpriseid[1][0]);
		delete(content.principal)
		wx.showModal({
			title: '提示',
			content: '是否确定创建开票申请单?',
			complete: ({
				confirm
			}) => {
				if (confirm) {
					that.setData({
						loading: true
					})
					_Http.basic({
						"id": "20221216142903",
						"version": 1,
						content
					}).then(res => {
						that.setData({
							loading: false
						})
						console.log("创建开票申请单", res)
						wx.showToast({
							title: res.msg == '成功' ? '创建成功' : res.msg,
							icon: "none",
							mask: true
						});
						if (res.msg == '成功') setTimeout(() => wx.redirectTo({
							url: '/packageA/invoice/detail?id=' + res.data.sa_invoiceapplyid,
						}), 500)
					})
				}
			}
		})

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