const _Http = getApp().globalData.http;
let queue = [],
    downCounter = null;

Component({
    properties: {
        disabled: Boolean, //禁用
        type: {
            type: String,
            value: "合同"
        }
    },
    data: {
        sa_contractid: 0,
        "content": {
            nocache: true,
            pageNumber: 1,
            pageTotal: 1,
            total: null
        },
    },
    lifetimes: {
        detached: function () {
            if (downCounter) {
                clearTimeout(downCounter);
                this.changeItem(queue)
            }
        },
    },
    methods: {
        addType() {
            let page = getCurrentPages()[getCurrentPages().length - 1];
            if (page.route == 'packageA/contract/detail') {
                if (page.data.detail.type == '经销商合作协议') {
                    wx.navigateTo({
                        url: '/packageA/contract/modules/discount/type/add/index?sa_contractid=' + this.data.sa_contractid
                    })
                } else {
                    wx.navigateTo({
                        url: '/packageA/select/offers/select?params=' + JSON.stringify({
                            "id": 20221222151302,
                            "content": {
                                nocache: true,
                                "sa_projectid": page.data.detail.sa_projectid,
                                "where": {
                                    "condition": ""
                                }
                            }
                        }) + '&radio=true'
                    })
                    getApp().globalData.handleSelect = this.selectOffers.bind(this);
                }
            } else {
                wx.navigateTo({
                    url: '/packageA/contract/modules/discount/type/add/index?sa_contractid=' + this.data.sa_contractid
                })
            }
        },
        /* 选择报价单 */
        selectOffers({
            item
        }) {
            wx.redirectTo({
                url: '/packageA/contract/modules/discount/type/add/index?sa_quotedpriceid=' + item.sa_quotedpriceid
            })
        },
        /* 获取产品列表 */
        getList(id, init) {
            let content = this.data.content;
            content.sa_contractid = id;
            content.sa_quotedpriceid = id;
            if (init) content.pageNumber = 1;
            _Http.basic({
                "id": this.data.type == "合同" ? 20221124135602 : 20230219161403,
                content
            }).then(res => {
                console.log("类别折扣明细列表", res)
                if (res.msg != '成功') return wx.showToast({
                    title: res.data,
                    icon: "none"
                })
                res.data = res.data.map(value => {
                    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,
                    sa_quotedpriceid: id,
                    idname: this.data.type == "合同" ? "sa_contract_itemsaleclassids" : "sa_quotedprice_itemclassids"
                })
            })
        },
        /* 处理新增 */
        handleAdd(items) {
            let that = this;
            wx.showModal({
                title: '提示',
                content: '是否确认添加产品类别?',
                complete: ({
                    confirm
                }) => {
                    if (confirm) _Http.basic({
                        "id": this.data.type == "合同" ? 20221124110202 : 20230219161303,
                        "content": {
                            sa_contractid: that.data.sa_contractid,
                            sa_quotedpriceid: that.data.sa_contractid,
                            items
                        }
                    }).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": this.data.type == "合同" ? 20221124111202 : 20230219161503,
                "content": {
                    "sa_contract_itemsaleclassids": detail,
                    "sa_quotedprice_itemclassids": 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[this.data.idname]) == -1)
                })
            })
        },
        /* 生成修改队列 */
        changeQueue({
            detail
        }) {
            let i = queue.findIndex(v => v[this.data.idname] == detail[this.data.idname]);
            i == -1 ? queue.push(detail) : queue[i] = detail;
            clearTimeout(downCounter)
            downCounter = setTimeout(() => {
                this.changeItem(queue)
            }, 0);
        },
        changeItem(items) {
            _Http.basic({
                "id": this.data.type == "合同" ? 20221124110202 : 20230219161303,
                "version": 1,
                "content": {
                    "sa_contractid": this.data.sa_contractid,
                    sa_quotedpriceid: this.data.sa_contractid,
                    items
                }
            }, false).then(res => {
                console.log("修改类型折扣", res)
                queue = [];
                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[this.data.idname] == value[this.data.idname]);
                    if (i != -1) this.data.list[i] = {
                        ...list[i],
                        ...v
                    }
                })
            })
        },
    }
})