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

Component({
    properties: {
        list: {
            type: Array
        },
        changeAmount: { //修改产品
            type: Function
        },
        disabled: {
            type: Boolean
        },
        sa_salesforecastprojectid: {
            type: String
        }
    },
    options: {
        addGlobalClass: true
    },
    methods: {
        /* 修改产品数量/单价 */
        onBlur(e) {
            let {
                name,
                index
            } = e.currentTarget.dataset;
            let item = this.data.list[index];
            if (e.detail.value <= 0) {
                wx.showToast({
                    title: getApp().globalData.Language.getMapText('非法数值'),
                    icon: "none"
                });
            } else {
                if (item[name] == e.detail.value) return;
                let v = e.detail.value - 0;
                //处理起订量和增量
                if (name == 'orderqty') {
                    if (v < item.orderminqty) {
                        wx.showToast({
                            title: getApp().globalData.Language.getMapText('输入数量小于起订量') + item.orderminqty,
                            icon: "none",
                            mask: true
                        });
                        item.orderqty = item.orderminqty;
                    } else if (item.orderminqty < v) {
                        var currencyRounding = value => currency(value, {
                            increment: item.orderaddqty
                        });
                        item.orderqty = currency(currencyRounding(currency(v).subtract(item.orderminqty)).format()).add(item.orderminqty).value;
                        if (item.orderqty != v) wx.showToast({
                            title: getApp().globalData.Language.getMapText('输入数量不符合增减量规则，已为您取最接近的值'),
                            icon: "none",
                            mask: true
                        })
                    } else {
                        item.orderqty = v;
                    }
                } else {
                    item[name] = v;
                }
                _Http.basic({
                    "id": 20230705145204,
                    "content": {
                        "sa_salesforecastbillid": item.sa_salesforecastbillid,
                        "sa_salesforecastprojectid": this.data.sa_salesforecastprojectid,
                        "salesforecast": [{
                            "itemid": item.itemid,
                            "orderqty": item.orderqty,
                            "orderamount": item.orderqty * item.price,
                            "price": item.price,
                            "sa_salesforecastid": item.sa_salesforecastid
                        }]
                    }
                }).then(res => {
                    console.log("修改产品", res)
                    this.triggerEvent("changeAmount")
                })
            }
            this.data.list[index] = item;
            this.setData({
                list: this.data.list
            })
        },
        deleteProduct(e) {
            const {
                item
            } = e.currentTarget.dataset,
                that = this;
            wx.showModal({
                title: getApp().globalData.Language.getMapText('提示'),
                content: getApp().globalData.Language.getMapText('是否确认删除') + `“${item.itemname}”?`,
                cancelText: getApp().globalData.Language.getMapText('取消'),
                confirmText: getApp().globalData.Language.getMapText('确定'),
                complete: ({
                    confirm
                }) => {
                    if (confirm) _Http.basic({
                        "accesstoken": "7cbf3f38611c090666f8de7b9582a6ae",
                        "id": 20230705145404,
                        "content": {
                            "sa_salesforecastprojectid": this.data.sa_salesforecastprojectid,
                            "sa_salesforecastbillid": item.sa_salesforecastbillid,
                            "sa_salesforecastids": [item.sa_salesforecastid]
                        }
                    }).then(res => {
                        console.log("删除项目产品", res)
                        wx.showToast({
                            title: res.code == '1' ? getApp().globalData.Language.getMapText('删除成功') : res.msg,
                            icon: "none"
                        })
                        if (res.code == '1') that.triggerEvent("changeAmount")
                    })
                }
            })
        },
    }
})