import currency from "../../../../../utils/currency";

Component({
	properties: {
		list: {
			type: Array
		},
		deleteItem: {
			type: Function
		},
		changeProduct: {
			type: Function
		}, //修改队列
		disabled: {
			type: Boolean
		}
	},
	options: {
		addGlobalClass: true
	},
	data: {
		hidePrice: wx.getStorageSync('hidePrice'),
	},
	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 = -1;
				if (data.sa_promotion_itemsid) {
					i = this.data.list.findIndex(v => v.sa_promotion_itemsid == data.sa_promotion_itemsid);
				} else {
					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,
				sa_promotion_itemsid: data.sa_promotion_itemsid || 0,
				qty: data.qty,
				remarks: name == 'remarks' ? e.detail.value : data.remarks,
				needdate: data.needdate || "",
				deliverydate: data.deliverydate || ""
			})
		},
		changeDate(e) {
			let {
				data
			} = e.currentTarget.dataset;
			this.triggerEvent("changeProduct", {
				sa_orderitemsid: data.sa_orderitemsid,
				itemid: data.itemid,
				qty: data.qty,
				remarks: data.remarks,
				needdate: e.detail.value || "",
				deliverydate: data.deliverydate || ""
			})
		},
		deleteProduct(e) {
			this.triggerEvent("deleteItem", e.currentTarget.dataset.item)
		},
		isEdit() {
			if (this.data.disabled) wx.showToast({
				title: '当前状态不可编辑！',
				icon: "none"
			})
		}
	}
})