const _Http = getApp().globalData.http,
    file = require("../../../../../utils/matchingFeilType"),
    currency = require("../../../../../utils/currency"),
    CNY = value => currency(value, {
        symbol: "￥",
        precision: 2
    }).format();
let queue = [],
    downCounter = null;

Component({
    options: {
        addGlobalClass: true
    },
    properties: {
        disabled: {
            type: Boolean
        }, //禁用
        type: {
            type: String,
            value: "指定单价"
        },
        isSync: { //居间同步
            type: Boolean,
            value: false
        }
    },
    data: {
        sa_contractid: 0,
        "content": {
            nocache: true,
            pageNumber: 1,
            pageTotal: 1,
            total: null
        },
    },
    lifetimes: {
        detached: function () {
            if (downCounter) {
                clearTimeout(downCounter);
                if (queue.length) this.changeItem(queue)
            }
        },
        attached: function () {
            getApp().globalData.Language.getLanguagePackage(this)
        }
    },
    methods: {
        /* 获取产品列表 */
        getList(id, init) {
            let content = this.data.content;
            content.sa_contractid = id;
            this.data.sa_contractid = id;
            content.sys_enterpriseid = getCurrentPages().find(v => v.__route__ == 'packageA/contract/detail').data.detail.sys_enterpriseid;
            if (init) content.pageNumber = 1;
            _Http.basic({
                "id": "20221122165302",
                content
            }).then(res => {
                console.log("产品折扣明细列表", res)
                if (res.code != '1') return wx.showToast({
                    title: res.data,
                    icon: "none"
                })
                res.data = res.data.map(value => {
                    if (value.attinfos.length != 0) {
                        value.attinfos = file.fileList(value.attinfos)
                        let image = value.attinfos.find(v => v.fileType == "image");
                        value.cover = image ? image.cover : "";
                    }
                    value.showMarketprice = CNY(value.marketprice)
                    value.className = value.itemclass.length == 0 ? "暂无类目" : value.itemclass.map(v => v.itemclassname);
                    value.brandName = value.brand.length == 0 ? "暂无品牌" : value.brand.map(v => v.brandname);
                    value.discountrate = (value.discountrate * 100).toFixed(2);
                    return value;
                })
                this.setData({
                    list: res.pageNumber == 1 ? res.data : this.data.list.concat(res.data),
                    "content.pageNumber": res.pageNumber + 1,
                    "content.pageSize": res.pageSize,
                    "content.pageTotal": res.pageTotal,
                    "content.total": res.total
                })
            })
        },
        /* 去选择产品 */
        addProduct() {
            wx.navigateTo({
                url: '/packageA/select/product/select?params=' + JSON.stringify({
                    "id": 20221124093602,
                    "version": 1,
                    "content": {
                        nocache: true,
                        sa_contractid: this.data.sa_contractid,
                        where: {
                            condition: "",
                            istool: 0
                        }
                    }
                })
            });
            getApp().globalData.handleSelect = this.handleSelectProduct.bind(this);
        },
        /* 处理选择产品 */
        handleSelectProduct({
            list
        }) {
            let that = this;
            wx.showModal({
                title: getApp().globalData.Language.getMapText('提示'),
                content: getApp().globalData.Language.joint([{
                    t: 1,
                    v: '是否确认添加',
                    r: " "
                }, {
                    v: list.length,
                    r: " "
                }, {
                    t: 1,
                    v: '件产品',
                    r: "?"
                }]),
                cancelText: getApp().globalData.Language.getMapText('取消'),
                confirmText: getApp().globalData.Language.getMapText('确定'),
                complete: ({
                    confirm
                }) => {
                    if (confirm) _Http.basic({
                        "id": 20221123164402,
                        "content": {
                            "sa_contractid": that.data.sa_contractid,
                            items: list.map(v => {
                                return {
                                    "itemid": v.itemid,
                                    "price": v.marketprice,
                                    "qty": v.orderminqty || 1,
                                    "marketprice": v.marketprice,
                                    "remarks": "",
                                    type: this.data.type
                                }
                            })
                        }
                    }).then(res => {
                        console.log("产品明细添加产品", res);
                        wx.showToast({
                            title: res.code != '1' ? res.msg : getApp().globalData.Language.getMapText('添加成功') + '!',
                            icon: "none"
                        });
                        if (res.code != '1') return;
                        setTimeout(() => {
                            wx.navigateBack();
                            setTimeout(() => {
                                that.getList(that.data.sa_contractid, true)
                            }, 300)
                        }, 300)
                    })
                }
            })

        },
        /* 删除产品 */
        deleteItems({
            detail
        }) {
            _Http.basic({
                "id": 20221123164502,
                "version": 1,
                "content": {
                    "sa_contractid": this.data.sa_contractid,
                    "sa_contract_itemsids": detail
                }
            }).then(res => {
                console.log("批量删除产品", res);
                wx.showToast({
                    title: res.code == '1' ? getApp().globalData.Language.getMapText('删除成功') + '!' : res.msg,
                    icon: "none"
                })
                if (res.code == '1') this.setData({
                    list: this.data.list.filter(v => detail.indexOf(v.sa_contract_itemsid) == -1)
                })
            })
        },
        /* 生成修改队列 */
        changeQueue({
            detail
        }) {
            let i = queue.findIndex(v => v.sa_contract_itemsid == detail.sa_contract_itemsid);
            i == -1 ? queue.push(detail) : queue[i] = detail;
            clearTimeout(downCounter)
            downCounter = setTimeout(() => {
                this.changeItem(queue)
            }, 0);
        },
        changeItem(items) {
            console.log("修改", items)
            _Http.basic({
                "id": 20221122200302,
                "version": 1,
                "content": {
                    "sa_contractid": this.data.sa_contractid,
                    items
                }
            }, false).then(res => {
                console.log("合同批量修改产品清单", res)
                queue = [];
                downCounter = null;
                let list = this.data.list;
                if (res.code != '1') {
                    wx.showToast({
                        title: res.msg,
                        icon: "none"
                    })
                    this.setData({
                        list
                    })
                    return;
                }
                items.forEach(v => {
                    v.discountrate = (v.discountrate * 100).toFixed(2);
                    let i = list.findIndex(value => v.sa_contract_itemsid == value.sa_contract_itemsid);
                    if (i != -1) this.data.list[i] = {
                        ...list[i],
                        ...v
                    }
                });
            })
        },
        brokerSync() {
            let that = this;
            wx.showModal({
                title: getApp().globalData.Language.getMapText('同步协议'),
                content: getApp().globalData.Language.getMapText('是否添加关联的项目或客户合同中的产品明细同步更新到列表'),
                cancelText: getApp().globalData.Language.getMapText('取消'),
                confirmText: getApp().globalData.Language.getMapText('确定'),
                complete: ({
                    confirm
                }) => {
                    if (confirm) _Http.basic({
                        "id": 20240518155604,
                        "content": {
                            "sa_contractid": that.data.sa_contractid
                        },
                    }).then(res => {
                        console.log("同步", res)
                        wx.showToast({
                            title: res.code == '1' ? getApp().globalData.Language.getMapText('同步成功') : res.msg,
                            icon: "none"
                        })
                        if (res.code != '1') return
                        that.getList(that.data.sa_contractid, true)
                    })
                }
            })
        }
    }
})