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

Page({
	data: {
		loading: false,
		showAll: false,
		disabled: true,
		content: {
			"sa_aftersalesmagid": 0,
			"sa_orderid": "",
			"reason": "",
			"billdate": "",
			"type": "退货单"
		},

	},
	onLoad(options) {
		let form = [{
			label: "返退原因",
			error: false,
			errMsg: "",
			type: "texteara",
			value: "",
			placeholder: "请输入返退原因",
			valueName: "reason",
			required: false,

		}, {
			label: "是否实物退回",
			error: false,
			errMsg: "",
			type: "radio",
			radioList: [{
					name: '是',
					id: 1
				},
				{
					name: '否',
					id: 0
				},
			],
			value: 1,
			valueName: "isitemreturn",
			required: true,
		}];
		form.unshift(wx.getStorageSync('userrole') == '业务员' ? {
			label: "所属企业",
			error: false,
			errMsg: "",
			type: "route",
			url: "/select/agent/index",
			params: {
				"id": 20220920083901,
				"content": {
					nocache: true,
					"where": {
						"condition": "",
						"type": 9,
					}
				}
			},
			query: '&radio=true&idname=sys_enterpriseid',
			value: "",
			placeholder: "选择企业",
			valueName: "sys_enterpriseid",
			checking: "base",
			required: true,
			interrupt: true
		} : {
			label: "订单号",
			error: false,
			errMsg: "",
			type: "route",
			url: "/packageA/toolBill/modules/selectOrder/index",
			params: {
				"id": 20230105110003,
				"content": {
					nocache: true,
					"pageNumber": 1,
					"pageSize": 20,
					"where": {
						"condition": ""
					}
				}
			},
			query: '&radio=true',
			value: "",
			placeholder: "选择订单号",
			valueName: "sa_orderid",
			checking: "base",
			required: true
		})
		this.setData({
			form
		})
		if (options.rowData) {
			let data = JSON.parse(options.rowData);
			console.log(data);
			this.setData({
				form: form.map(v => {
					if (v.valueName != 'region') {
						if (v.valueName == 'enterprisename') {
							v.value = [data[v.valueName]]
						} else {
							v.value = data[v.valueName];
						}
					} else {
						v.value = data.province ? [data.province, data.city, data.county] : []
					}
					return v
				}),
				'form[0].value': [data.sonum, [data.sa_orderid]],
				'content.sa_aftersalesmagid': data.sa_aftersalesmagid
			})
			this.selectComponent('#Form').confirm()
		}
	},
	/* 打断处理form */
	interrupt({
		detail
	}) {
		detail.form[detail.temporary.index].value = detail.data.value;
		detail.form.splice(1, detail.form.some(v => v.label == '订单号') ? 1 : 0, {
			label: "订单号",
			error: false,
			errMsg: "",
			type: "route",
			url: "/packageA/toolBill/modules/selectOrder/index",
			params: {
				"id": 20230105110003,
				"content": {
					nocache: true,
					"pageNumber": 1,
					"pageSize": 20,
					"where": {
						"condition": ""
					},
					sys_enterpriseid: detail.data.id[0]
				}
			},
			query: '&radio=true',
			value: "",
			placeholder: "选择订单号",
			valueName: "sa_orderid",
			checking: "base",
			required: true
		})
		this.setData({
			form: detail.form
		})
		wx.navigateBack()
	},
	async submit() {
		let content = {
			...this.data.content,
			...this.selectComponent("#Form").submit()
		}
		content.sa_orderid = content.sa_orderid[1][0] || '';
		content.sys_enterpriseid = content.sys_enterpriseid[1][0] || '';

		this.setData({
			loading: true
		})
		let res = await _Http.basic({
			"id": "20230104160503",
			content
		})
		this.setData({
			loading: false
		})
		console.log(res)
		wx.showToast({
			title: res.msg == '成功' ? '创建成功' : res.msg,
			icon: "none",
			mask: true
		})

		setTimeout(() => {
			wx.redirectTo({
				url: '/packageA/returnOne/detail?id=' + res.data.sa_aftersalesmagid,
			})
		}, 500)
	},
	// 是否显示全部
	onChange({
		detail
	}) {
		this.setData({
			showAll: detail
		})
	},
	/* 表单必填项是否完成 */
	onConfirm({
		detail
	}) {
		this.setData({
			disabled: detail
		})
	}
})