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 {
                index,
                name
            } = e.currentTarget.dataset,
                detail = name == 'qty' ? e.detail.value - 0 : (e.detail.value - 0).toFixed(2),
                item = this.data.list[index];
            if (item[name] == detail) return;
            //处理起订量和增量
            if (name == 'qty') {
                if (detail < item.orderminqty) {
                    wx.showToast({
                        title: getApp().globalData.Language.getMapText('输入数量小于起订量') + item.orderminqty,
                        icon: "none",
                        mask: true
                    });
                    item.qty = item.orderminqty;
                } else if (item.orderminqty < detail) {
                    var currencyRounding = value => currency(value, {
                        increment: item.orderaddqty
                    });
                    item.qty = currency(currencyRounding(currency(detail).subtract(item.orderminqty)).format()).add(item.orderminqty).value;
                    if (item.qty != detail) wx.showToast({
                        title: getApp().globalData.Language.getMapText('输入数量不符合增减量规则，已为您取最接近的值'),
                        icon: "none",
                        mask: true
                    })
                } else {
                    item.qty = detail;
                }
            } else {
                item[name] = currency(detail).value;
            }

            if (name == 'price') {
                if (item.price <= 0) item.price = 1
                item.discountrate = this.getPercent(item.price, item.marketprice).toFixed(2);
            } else if (name == 'discountrate') {
                if (item.discountrate <= 0) item.discountrate = 1;
                item.price = currency(item.marketprice).multiply((item.discountrate / 100)).value;
            }
            item.showAmount = CNY(currency(item.price).multiply(item.qty));
            item.amount = currency(item.price).multiply(item.qty).value;
            item.discountDifferenceAmount = CNY(currency(item.marketprice).subtract(item.price));
            this.setData({
                [`list[${index}]`]: item
            })
            let obj = {};
            ["sa_quotedprice_itemsid", "itemid", "price", "discountrate", "qty", "mindiscountrate", "discountDifferenceAmount", "amount"].forEach(v => obj[v] = item[v]);
            obj.discountrate = (obj.discountrate / 100).toFixed(4);
            this.triggerEvent("changeQueue", obj)
        },
        /* 计算百分比 */
        getPercent(num, total) {
            num = parseFloat(num);
            total = parseFloat(total);
            if (isNaN(num) || isNaN(total)) {
                return "-";
            }
            return total <= 0 ? "0%" : Math.round((num / total) * 10000) / 100;
        },
        deleteProduct(e) {
            const {
                sa_quotedprice_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_quotedprice_itemsid]);
                }
            })
        },
    }
})