import currency from "../../../../../utils/currency";
let {
	formatTime
} = require("../../../../../utils/getTime")
const _Http = getApp().globalData.http;
Component({
	properties: {
		list: {
			type: Array
		},
		deleteItem: {
			type: Function
		},
		changeProduct: {
			type: Function
		}, //修改队列
		disabled: {
			type: Boolean
		}
	},
	data: {
		popupShow: false
	},
	options: {
		addGlobalClass: true
	},
	methods: {
		onBlur(e) {
			let {
				data,
				name
			} = e.currentTarget.dataset;
			if (name == 'qty') {
				if (data.orderminqty > e.detail.value) {
					wx.showToast({
						title: '输入数量低于最低起订量！',
						icon: "none"
					})
					data.qty = data.orderminqty;
				} else if (data.orderminqty < e.detail.value) {
					var currencyRounding = value => currency(value, {
						increment: data.orderaddqty
					});
					data.qty = currency(currencyRounding(currency(e.detail.value).subtract(data.orderminqty)).format()).add(data.orderminqty).value;
				} else {
					data.qty = e.detail.value;
				}
				let i = this.data.list.findIndex(v => v.sa_orderitemsid == data.sa_orderitemsid);
				if (i != -1) this.setData({
					[`list[${i}].qty`]: data.qty
				})
			}
			this.triggerEvent("changeProduct", {
				sa_orderitemsid: data.sa_orderitemsid,
				itemid: data.itemid,
				qty: data.qty,
				remarks: name == 'remarks' ? e.detail.value : data.remarks,
				needdate: data.needdate || "",
				deliverydate: data.deliverydate || ""
			})
		},
		async calculate(e) {
			this.isEdit()
			if (this.data.disabled) return;
			let {
				item
			} = e.currentTarget.dataset;
			item.minData = Date.now();
			if (item.delivery != 0 && item.delivery != "") {
				let date = Date.now() + (item.delivery * 86400000);
				item.minDate = await exclusion(formatTime(new Date(date), "-").split(" ")[0]);

				this.setData({
					changeDate: item,
					popupShow: true
				})

				function exclusion(begindate) {
					return new Promise((resolve) => {
						_Http.basic({
							"id": 20231008134404,
							"content": {
								"isExport": 0,
								"isnext": '0',
								"pageNumber": 1,
								"pageSize": 100,
								"where": {
									begindate,
									"enddate": formatTime(new Date(date + (31 * 86400000)), "-").split(" ")[0]
								}
							},
						}).then(res => {
							console.log("查询节假日", begindate, res)
							if (res.data.some(v => v.date == begindate)) {
								function day() {
									date += 86400000;
									if (res.data.some(v => v.date == formatTime(new Date(date), "-").split(" ")[0])) {
										day()
									} else {
										resolve(date)
									}
								}
								day()
							} else {
								resolve(date)
							}
						})
					})
				}
			} else {
				this.setData({
					changeDate: item,
					popupShow: true
				})
			}
		},
		closePopup() {
			this.setData({
				popupShow: false,
				changeDate: null
			})
		},
		onConfirm({
			detail
		}) {
			let data = this.data.changeDate,
				that = this;
			let needdate = formatTime(new Date(detail), "-").split(' ')[0];
			wx.showModal({
				content: `是否确定期望交期为${needdate}日`,
				title: '提示',
				success: (result) => {
					that.triggerEvent("changeProduct", {
						sa_orderitemsid: data.sa_orderitemsid,
						itemid: data.itemid,
						qty: data.qty,
						remarks: data.remarks,
						needdate,
						deliverydate: data.deliverydate || ""
					})
					that.closePopup();
				},
			})

		},
		deleteProduct(e) {
			this.triggerEvent("deleteItem", e.currentTarget.dataset.item)
		},
		isEdit() {
			if (this.data.disabled) wx.showToast({
				title: '当前状态不可编辑！',
				icon: "none"
			})
		}
	}
})