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({
    properties: {
        disabled: Boolean, //禁用
    },
    data: {
        sa_contractid: 0,
        "content": {
            nocache: true,
            pageNumber: 1,
            pageTotal: 1,
            total: null
        },
    },
    lifetimes: {
        detached: function () {
            if (downCounter) {
                clearTimeout(downCounter);
                this.changeItem(queue)
            }
        },
    },
    methods: {
        /* 获取产品列表 */
        getList(id, init) {
            let content = this.data.content;
            content.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.msg != '成功') 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,
                    sa_contractid: id
                })
            })
        },
        /* 去选择产品 */
        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: "",
                        }
                    }
                })
            });
            getApp().globalData.handleSelect = this.handleSelectProduct.bind(this);
        },
        /* 处理选择产品 */
        handleSelectProduct({
            list
        }) {
            let that = this;
            wx.showModal({
                title: '提示',
                content: `是否确认添加${list.length}件产品?`,
                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": 1,
                                    "marketprice": v.marketprice,
                                    "remarks": "",
                                    type: "指定单价"
                                }
                            })
                        }
                    }).then(res => {
                        console.log("产品明细添加产品", res);
                        wx.showToast({
                            title: res.msg != '成功' ? res.msg : '添加成功!',
                            icon: "none"
                        });
                        if (res.msg != '成功') 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.msg == '成功' ? '删除成功!' : res.msg,
                    icon: "none"
                })
                if (res.msg == '成功') 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.msg != '成功') {
                    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
                    }
                });
            })
        }
    }
})