const currency = require("../../../../../utils/currency") 
Component({
    properties: {
        list: {type:Array},
        deleteItem: {type:Function},
        changeQueue: {type:Function}, //修改队列
        disabled: {type:Boolean}
    },
    options: {
        addGlobalClass: true
    },
    methods: {
        /* 修改产品数量/单价 */
        onBlur(e) {
            let {
                name,
                index
            } = e.currentTarget.dataset;
            let item = this.data.list[index];
            console.log(item)
            if (e.detail.value <= 0) {
                wx.showToast({
                    title: '非法数值',
                    icon: "none"
                });
            } else {
                if (item[name] == e.detail.value) return;
                let v = e.detail.value - 0;
                //处理起订量和增量
                if (name == 'outqty') {
                    if (v < item.orderminqty) {
                        wx.showToast({
                            title: `输入数量小于起订量${item.orderminqty}`,
                            icon: "none",
                            mask: true
                        });
                        item.outqty = item.orderminqty;
                    } else if (item.orderminqty < v) {
                        var currencyRounding = value => currency(value, {
                            increment: item.orderaddqty
                        });
                        item.outqty = currency(currencyRounding(currency(v).subtract(item.orderminqty)).format()).add(item.orderminqty).value;
                        if (item.outqty != v) wx.showToast({
                            title: `输入数量不符合增减量规则，已为您取最接近的值`,
                            icon: "none",
                            mask: true
                        })
                    } else {
                        item.outqty = v;
                    }
                } else {
                    item[name] = v;
                }
                item.outamount = item.outqty * item.price;
                if (name == "price" && item[name] > item.marketprice) item[name] = item.marketprice;
                let obj = {};
                ["sa_salesforecastid", "itemid", "orderqty", "orderamount", "invoiceqty", "price", "invoiceamount", "outqty"].forEach(v => obj[v] = item[v]);
                this.triggerEvent("changeQueue", obj)
            }
            this.data.list[index] = item;
            this.setData({
                list: this.data.list
            })
        },
        deleteProduct(e) {
            const {
                sa_salesforecastbillid,
                sa_salesforecastid,
                itemname
            } = e.currentTarget.dataset.item,
                that = this;
            wx.showModal({
                title: '提示',
                content: `是否确认删除“${itemname}”?`,
                complete: ({
                    confirm
                }) => {
                    if (confirm) that.triggerEvent("deleteItem", {
                        sa_salesforecastbillid,
                        sa_salesforecastid,
                        sa_projectid: 0
                    });
                }
            })
        },
    }
})