const currency = require("../../../../../utils/currency"),
    CNY = value => currency(value, {
        symbol: "￥",
        precision: 2
    }).format();
Component({
    properties: {
        list: {
            type: Array
        },
        deleteItem: {
            type: Function
        },
        changeQueue: {
            type: Function
        }, //修改队列
        disabled: {
            type: Boolean
        }
    },
    options: {
        addGlobalClass: true
    },
    lifetimes: {
        attached: function () {
            getApp().globalData.Language.getLanguagePackage(this)
            this.setData({
                siteid: wx.getStorageSync('userMsg').siteid
            })
        }
    },
    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",
                    mask: true
                });
            } else {
                if (item[name] == e.detail.value) return;
                // orderminqty 起订量
                // orderaddqty 增量
                let v = e.detail.value - 0;
                //处理起订量和增量
                if (name == 'qty') {
                    if (v < item.orderminqty) {
                        wx.showModal({
                            title: getApp().globalData.Language.getMapText('提示'),
                            content: getApp().globalData.Language.getMapText('输入数量小于起订量') + item.orderminqty,
                            showCancel: false,
                            confirmText: getApp().globalData.Language.getMapText('确定'),
                        })
                        item.qty = item.orderminqty;
                    } else if (item.orderminqty < v) {
                        var currencyRounding = value => currency(value, {
                            increment: item.orderaddqty
                        });
                        item.qty = currency(currencyRounding(currency(v).subtract(item.orderminqty)).format()).add(item.orderminqty).value;
                        if (item.qty != v) wx.showModal({
                            title: getApp().globalData.Language.getMapText('提示'),
                            content: getApp().globalData.Language.getMapText('输入数量不符合增减量规则，已为您取最接近的值'),
                            showCancel: false,
                            confirmText: getApp().globalData.Language.getMapText('确定'),
                        })
                    } else {
                        item.qty = v;
                    }
                } else {
                    item[name] = v;
                }
                if (name == "price") item[name] = (item[name] - 0).toFixed(2) - 0;
                item.amount = CNY(currency(item.price).multiply(item.qty));
                let obj = {};
                ["sa_project_itemsid", "itemid", "qty", "remarks", "marketprice", "price"].forEach(v => obj[v] = item[v]);
                this.triggerEvent("changeQueue", obj)
            }
            this.setData({
                list: this.data.list
            })
        },
        deleteProduct(e) {
            const {
                sa_project_itemsid,
                itemname
            } = e.currentTarget.dataset.item,
                that = this;
            wx.showModal({
                title: getApp().globalData.Language.getMapText('提示'),
                content: getApp().globalData.Language.getMapText('是否确认删除') + `“${itemname}”?`,
                cancelText: getApp().globalData.Language.getMapText('取消'),
                confirmText: getApp().globalData.Language.getMapText('确定'),
                complete: ({
                    confirm
                }) => {
                    if (confirm) that.triggerEvent("deleteItem", [sa_project_itemsid]);
                }
            })
        },
    }
})