const _Http = getApp().globalData.http,
	getTime = require("../../utils/getTime");
let source = 0,
	sa_quotedpriceid = 0,
	quotedpricenotes = [],
	address = "";
Page({
	data: {
		loading: false,
		subLoading: false,
		showAll: false,
		accessory: true,
		form: [{
			label: "项目",
			error: false,
			errMsg: "",
			type: "route",
			url: "/packageA/select/project/select",
			value: "",
			params: {
				"id": 20221020143503,
				content: {
					nocache: true,
					"type": 1,
					"where": {
						"condition": "",
					}
				},
			},
			interrupt: true,
			query: "&radio=true",
			placeholder: "选择项目",
			valueName: "sa_projectid",
			checking: "base",
			required: true
		}, {
			label: "报价金额(元)",
			error: false,
			errMsg: "",
			type: "digit",
			value: "",
			placeholder: "请填写金额",
			valueName: "quotedpriceamount",
			checking: "twoDecimalPlaces",
			required: true
		}, {
			label: "产品系列",
			error: false,
			errMsg: "",
			type: "option",
			optionNmae: "itemtype",
			optionType: "checkbox", //复选   radio 单选
			value: "",
			placeholder: "请选择产品系列",
			valueName: "itemtype",
			checking: "base",
			required: true
		}, {
			label: "报价日期",
			error: false,
			errMsg: "",
			type: "date",
			value: getTime.formatTime(new Date(), '-').split(' ')[0],
			placeholder: "报价日期",
			valueName: "billdate",
			checking: "base",
			required: false
		}, {
			label: "有效期",
			error: false,
			errMsg: "",
			type: "dateRange",
			value: [getTime.formatTime(new Date(), '-').split(' ')[0] + "", getTime.formatTime(new Date(Date.now() + (86400000 * 30)), '-').split(' ')[0] + ""],
			placeholder: ['生效日期', '截止日期'],
			valueName: "invaliddate",
			checking: "base",
			required: true
		}, {
			label: "备注",
			error: false,
			errMsg: "",
			type: "textarea",
			value: "",
			placeholder: "报价单说明",
			valueName: "remarks",
			required: false, //必填
		}],
		disabled: true,
		sa_quotedpriceid: 0
	},
	changeState({
		detail
	}) {
		this.setData({
			loading: detail,
			subLoading: detail
		})
	},
	onLoad(options) {
		source = 0;
		sa_quotedpriceid = 0;
		quotedpricenotes = [];
		address = "";
		let form = this.data.form;
		/* 项目商机进入 */
		if (options.sa_projectid) {
			let data = JSON.parse(options.sa_projectid);
			if (data.sa_projectid) {
				form[0].value = data.sa_projectid;
				form[0].disabled = true;
				source = 1;
				form.splice(1, 0, {
					label: "选择客户",
					error: false,
					errMsg: "",
					type: "route",
					url: "/packageA/select/setclient/select",
					value: "",
					params: {
						id: 20221027143702,
						"content": {
							nocache: true,
							"sa_projectid": data.sa_projectid[1][0],
							"where": {
								"condition": ""
							}
						},
					},
					query: "&radio=true&idname=sys_enterpriseid",
					placeholder: "选择客户",
					valueName: "sys_enterpriseid",
					checking: "base",
					required: true,
					interrupt: true
				});
				//业务员
				let hr = {
					label: "业务员",
					error: false,
					errMsg: "",
					type: "route",
					url: "/packageA/select/contacts/select",
					value: "",
					params: {
						"id": "20230306144402",
						"content": {
							nocache: true,
							"type": 2, //type:1客户2:项目
							"sa_projectid": data.sa_projectid[1][0],
							"where": {
								"condition": ""
							}
						}
					},
					query: "&radio=true&idname=hrid",
					placeholder: "选择业务员",
					valueName: "saler_hrid",
					checking: "base",
					required: true
				};
				_Http.basic({
					"id": "20230306144402",
					"content": {
						"nocache": true,
						"type": 2, //type:1客户2:项目
						"sa_projectid": data.sa_projectid[1][0],
						"where": {
							"condition": ""
						},
						"pageNumber": 1,
						"pageSize": 1,
					},
				}).then(res => {
					console.log('获取一个业务员', res)
					if (res.code == 1 && res.data.length) {
						hr.value = [res.data[0].name, [res.data[0].hrid]]
						if (form.some(v => v.label == '业务员')) {
							form.splice(form.findIndex(v => v.label == '备注') - 1, 1, hr);
						} else {
							form.splice(form.findIndex(v => v.label == '备注'), 0, hr);
						}
						this.setData({
							form
						})
					}
				})
				if (form.some(v => v.label == '业务员')) {
					form.splice(form.findIndex(v => v.label == '备注') - 1, 1, hr);
				} else {
					form.splice(form.findIndex(v => v.label == '备注'), 0, hr);
				}
			};
			this.setData({
				disabled: false,
				form
			});
		};
		if (options.data) {
			let data1 = JSON.parse(options.data);
			sa_quotedpriceid = data1.sa_quotedpriceid;
			quotedpricenotes = data1.quotedpricenotes;
			address = data1.address;
			data1.sa_projectid = [data1.projectname, [data1.sa_projectid]]
			form = form.map(v => {
				if (v.valueName == "invaliddate") {
					v.value = [data1.begdate, data1.enddate]
				} else {
					v.value = data1[v.valueName] || "";
					if (v.valueName == "quotedpriceamount") v.disabled = data1.iseditamount == 0
				}
				return v
			})
			form.splice(1, 0, {
				label: "选择客户",
				error: false,
				errMsg: "",
				type: "route",
				url: "/packageA/select/setclient/select",
				value: [data1.enterprisename, [data1.sys_enterpriseid]],
				params: {
					id: 20221027143702,
					"content": {
						nocache: true,
						"sa_projectid": data1.sa_projectid[1][0],
						"where": {
							"condition": ""
						}
					},
				},
				interrupt: true,
				query: "&radio=true&idname=sys_enterpriseid",
				placeholder: "选择客户",
				valueName: "sys_enterpriseid",
				checking: "base",
				required: true,
				interrupt: true
			});
			form.splice(2, 0, {
				label: "联系人",
				error: false,
				errMsg: "",
				type: "route",
				url: "/packageA/select/contacts/select",
				value: [data1.contactsname, [data1.sys_phonebookid]],
				params: {
					"id": "20240614151004",
					"content": {
						nocache: true,
						"sys_enterpriseid": data1.sys_enterpriseid,
						sa_projectid: data1.sa_projectid[1][0],
						pageNumber: 1,
						pageSize: 10,
						pageTotal: 1,
						"where": {
							"condition": "",
							"workaddress": 0
						}
					}
				},
				query: "&radio=true&idname=sys_phonebookid",
				placeholder: "选择客户联系人",
				valueName: "sys_phonebookid",
				checking: "base",
				required: true
			});
			form.splice(form.findIndex(v => v.label == '备注'), 0, {
				label: "业务员",
				error: false,
				errMsg: "",
				type: "route",
				url: "/packageA/select/contacts/select",
				value: [data1.name, [data1.saler_hrid]],
				params: {
					"id": "20230306144402",
					"content": {
						nocache: true,
						"type": 2, //type:1客户2:项目
						"sa_projectid": data1.sa_projectid[1][0],
						"where": {
							"condition": ""
						}
					}
				},
				query: "&radio=true&idname=hrid",
				placeholder: "选择业务员",
				valueName: "saler_hrid",
				checking: "base",
				required: true
			});
			this.setData({
				form,
				accessory: false,
				sa_quotedpriceid
			})
		} else {
			_Http.basic({
				"classname": "sysmanage.develop.optiontype.optiontype",
				"method": "optiontypeselect",
				"content": {
					"pageNumber": 1,
					"pageSize": 1,
					"typename": "itemtype",
					"parameter": {
						"siteid": wx.getStorageSync('siteP').siteid
					}
				},
			}).then(res => {
				console.log(res)
				if (res.code == 1 && res.data.length) {
					this.data.form[this.data.form.findIndex(v => v.label == '产品系列')].value = [res.data[0].value]
					this.setData({
						form: this.data.form
					})
				}
			})
		}
		getApp().globalData.Language.getLanguagePackage(this, '项目报价');
	},
	/* 打断处理form */
	interrupt(e) {
		let {
			data,
			form,
			temporary
		} = e.detail;
		if (temporary.item.value[0] == data.value[0]) return wx.navigateBack();
		let obj = {};
		temporary.item.value = data.value;
		switch (temporary.item.valueName) {
			case "sa_projectid":
				obj = {
					label: "选择客户",
					error: false,
					errMsg: "",
					type: "route",
					url: "/packageA/select/setclient/select",
					value: "",
					params: {
						id: 20221027143702,
						"content": {
							nocache: true,
							"sa_projectid": data.value[1][0],
							"where": {
								"condition": ""
							}
						},
					},
					interrupt: true,
					query: "&radio=true&idname=sys_enterpriseid",
					placeholder: "选择客户",
					valueName: "sys_enterpriseid",
					checking: "base",
					required: true
				};
				form.splice(1, form[1].label == "选择客户" ? 1 : 0, obj);
				address = data.item.province + data.item.city + data.item.county + data.item.address
				//业务员
				let hr = {
					label: "业务员",
					error: false,
					errMsg: "",
					type: "route",
					url: "/packageA/select/contacts/select",
					value: "",
					params: {
						"id": "20230306144402",
						"content": {
							nocache: true,
							"type": 2, //type:1客户2:项目
							"sa_projectid": data.value[1][0],
							"where": {
								"condition": ""
							}
						}
					},
					query: "&radio=true&idname=hrid",
					placeholder: "选择业务员",
					valueName: "saler_hrid",
					checking: "base",
					required: true
				};
				_Http.basic({
					"id": "20230306144402",
					"content": {
						"nocache": true,
						"type": 2, //type:1客户2:项目
						"sa_projectid": data.value[1][0],
						"where": {
							"condition": ""
						},
						"pageNumber": 1,
						"pageSize": 1,
					},
				}).then(res => {
					console.log('获取一个业务员', res)
					if (res.code == 1 && res.data.length) {
						hr.value = [res.data[0].name, [res.data[0].hrid]]
						if (form.some(v => v.label == '业务员')) {
							form.splice(form.findIndex(v => v.label == '备注') - 1, 1, hr);
						} else {
							form.splice(form.findIndex(v => v.label == '备注'), 0, hr);
						}
						this.setData({
							form
						})
					}
				})
				if (form.some(v => v.label == '业务员')) {
					form.splice(form.findIndex(v => v.label == '备注') - 1, 1, hr);
				} else {
					form.splice(form.findIndex(v => v.label == '备注'), 0, hr);
				}
				break;
			case "sys_enterpriseid":
				obj = {
					label: "联系人",
					error: false,
					errMsg: "",
					type: "route",
					url: "/packageA/select/contacts/select",
					value: "",
					params: {
						"id": "20240614151004",
						"content": {
							nocache: true,
							"sys_enterpriseid": data.value[1][0],
							sa_projectid: form.find(v => v.valueName == 'sys_enterpriseid').params.content.sa_projectid,
							pageNumber: 1,
							pageSize: 10,
							pageTotal: 1,
							"where": {
								"condition": "",
								"workaddress": 0
							}
						}
					},
					query: "&radio=true&idname=sys_phonebookid",
					placeholder: "选择客户联系人",
					valueName: "sys_phonebookid",
					checking: "base",
					required: true
				};
				form.splice(2, form.some(v => v.label == '联系人') ? 1 : 0, obj);
				break;
		}
		form[temporary.index] = temporary.item;
		wx.navigateBack()
		this.setData({
			form
		})
	},
	/* 绑定媒体 */
	insertImgEdit({
		detail
	}) {
		this.handleFileLink(detail)
	},
	handleFileLink(attachmentids, ownertable = "temporary", ownerid = 1, data) {
		_Http.basic({
			"classname": "system.attachment.Attachment",
			"method": "createFileLink",
			"content": {
				ownertable,
				ownerid,
				usetype: 'default',
				attachmentids
			}
		}).then(res => {
			console.log('跟进记录绑定附件', res)
			if (res.code != '1') return wx.showToast({
				title: res.msg,
				icon: "none"
			})
			if (ownertable == 'temporary') {
				this.selectComponent("#Yl_files").handleFiles(res.data)
			} else {
				if (res.data.length) data.attinfos = res.data;
				this.changeItem(data)
			}
		})
	},
	changeItem(data) {
		this.setData({
			loading: false,
			subLoading: false
		})
		let pages = getCurrentPages(),
			page = pages[pages.length - 2];
		if (page.__route__ == 'packageA/offers/detail') {
			page.getDetail(true);
			wx.navigateBack()
		} else {
			if (page.__route__ == 'packageA/offers/index') {
				page.getList(true);
			} else if (page.__route__ == 'packageA/project/detail') {
				let model = page.selectComponent("#Offers");
				model.getList(model.data.sa_projectid, true);
			}
			wx.redirectTo({
				url: '/packageA/offers/detail?sa_quotedpriceid=' + data.sa_quotedpriceid
			})
		}
	},
	submit(e) {
		const {
			submit
		} = e.currentTarget.dataset;
		console.log(submit)
		if (submit == 1) {
			this.setData({
				subLoading: true
			})
		} else {
			this.setData({
				loading: true
			})
		}
		let content = {
			...this.selectComponent("#Form").submit(),
			sa_quotedpriceid,
			quotedpricenotes,
			address,
			source
		};
		content.sys_enterpriseid = content.sys_enterpriseid[1] ? content.sys_enterpriseid[1][0] : 0;
		content.projectname = content.sa_projectid[0] || "";
		content.sa_projectid = content.sa_projectid[1] ? content.sa_projectid[1][0] : 0;
		content.sys_phonebookid = content.sys_phonebookid ? content.sys_phonebookid[1][0] : 0;
		content.saler_hrid = content.saler_hrid[1] ? content.saler_hrid[1][0] : 0;
		content.discountrate = ''
		content.begdate = content.invaliddate[0];
		content.enddate = content.invaliddate[1];
		delete(content.invaliddate)
		_Http.basic({
			"id": 20221020164803,
			"version": 1,
			content
		}).then(res => {
			console.log("添加项目报价单", res)
			wx.showToast({
				title: res.code != '1' ? res.msg : getApp().globalData.Language.getMapText('保存成功'),
				icon: "none",
				mask: true
			})
			if (res.code != '1') return this.setData({
				loading: false,
				subLoading: false
			});
			if (submit == 1) {
				_Http.basic({
					"id": 20221020165303,
					"version": 1,
					"content": {
						"sa_quotedpriceid": res.data.sa_quotedpriceid
					}
				}).then(s => {
					console.log("提交报价单", s)
					getApp().globalData.Language.showToast(s.code == '1' ? "创建成功" : s.msg)
					try {
						let attachmentids = this.selectComponent("#Yl_files").getFiles().attachmentids;
						if (attachmentids.length) {
							this.handleFileLink(attachmentids, 'sa_quotedprice', res.data.sa_quotedpriceid, res.data)
						} else {
							setTimeout(() => {
								this.changeItem(res.data)
							}, 500)
						}
					} catch (error) {
						setTimeout(() => {
							this.changeItem(res.data)
						}, 500)
					}

				})
			} else {
				try {
					let attachmentids = this.selectComponent("#Yl_files").getFiles().attachmentids;
					if (attachmentids.length) {
						this.handleFileLink(attachmentids, 'sa_quotedprice', res.data.sa_quotedpriceid, res.data)
					} else {
						setTimeout(() => {
							this.changeItem(res.data)
						}, 500)
					}
				} catch (error) {
					setTimeout(() => {
						this.changeItem(res.data)
					}, 500)
				}
			}
		})
	},
	// 是否显示全部
	onChange({
		detail
	}) {
		this.setData({
			showAll: detail
		})
	},
	/* 表单必填项是否完成 */
	onConfirm({
		detail
	}) {
		this.setData({
			disabled: detail
		})
	}
})