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,
    Counter = null;
Component({
    properties: {
        disabled: Boolean,
        signamount_due: String,
        discountrate: Number, //折扣
    },
    data: {
        sa_projectid: 0,
        content: {
            nocache: true,
            pageNumber: 1,
            pageTotal: 1,
            total: null
        }
    },
    lifetimes: {
        detached: function () {
            if (downCounter) {
                clearTimeout(downCounter);
                this.changeItem()
            }
        },
    },
    methods: {
        /* 步进器数值改变 */
        stepperChange(e) {
            switch (e.type) {
                case 'plus':
                    this.data.discountrate += 1;
                    break;
                case 'minus':
                    this.data.discountrate -= 1;
                    break;
                case 'blur':
                    this.data.discountrate = e.detail.value;
                    break;
            }
            if (this.data.discountrate > 100) this.data.discountrate = 100;
            if (this.data.discountrate < 0.5) this.data.discountrate = 0.5;
            this.setDiscountrate();
        },
        /* 设置折扣 */
        setDiscountrate() {
            clearTimeout(Counter);
            let page = getCurrentPages().find(v => v.__route__ == 'packageA/project/detail');
            let data = page.data.detail;
            Counter = setTimeout(() => {
                _Http.basic({
                    "id": 20230207090702,
                    "content": {
                        "sa_projectid": data.sa_projectid,
                        "discountrate": (this.data.discountrate / 100).toFixed(4) //预计折扣率
                    }
                }).then(res => {
                    console.log("修改折扣", res)
                    if (res.msg != '成功') wx.showToast({
                        title: res.msg,
                        icon: "none"
                    });
                    page.getDetail(true);
                })
            }, 300)
        },
        /* 获取产品列表 */
        getList(id, init) {
            let content = this.data.content;
            content.sa_projectid = id;
            if (init) content.pageNumber = 1
            _Http.basic({
                "id": "20221021145702",
                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) return value;
                    value.attinfos = file.fileList(value.attinfos)
                    let image = value.attinfos.find(v => v.fileType == "image");
                    value.cover = image ? image.cover : "";
                    value.amount = CNY(value.amount);
                    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;
                    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_projectid: id
                })
            })
        },
        /* 去选择产品 */
        addProduct() {
            wx.navigateTo({
                url: '/packageA/select/product/select?params=' + JSON.stringify({
                    "id": 20221021171802,
                    "version": 1,
                    "content": {
                        nocache: true,
                        pageNumber: 1,
                        pageTotal: 1,
                        total: null,
                        sa_projectid: this.data.sa_projectid,
                        where: {
                            condition: ""
                        }
                    }
                })
            });
            getApp().globalData.handleSelect = this.handleSelect.bind(this);
        },
        /* 处理选择产品 */
        handleSelect({
            list
        }) {
            let that = this,
                discountrate = this.data.discountrate / 100;
            wx.showModal({
                title: '提示',
                content: `是否确认添加${list.length}件产品?`,
                complete: ({
                    confirm
                }) => {
                    if (confirm) {
                        _Http.basic({
                            "id": 20221021145502,
                            "content": {
                                "sa_projectid": that.data.sa_projectid,
                                "items": list.map(v => {
                                    return {
                                        "sa_project_itemsid": 0,
                                        "itemid": v.itemid,
                                        "qty": 1,
                                        "remarks": "",
                                        "marketprice": v.marketprice, //市场价
                                        "price": (v.marketprice * discountrate).toFixed(2) //单价
                                    }
                                })
                            }
                        }).then(res => {
                            console.log("项目商机添加产品", res);
                            wx.showToast({
                                title: res.msg != '成功' ? res.msg : '添加成功!',
                                icon: "none"
                            });
                            if (res.msg == '成功') setTimeout(() => {
                                wx.navigateBack()
                                that.getList(that.data.sa_projectid, true)
                                this.setAmount();
                            }, 300)
                        })
                    }
                }
            })

        },
        /* 删除产品 */
        deleteItems({
            detail
        }) {
            _Http.basic({
                "id": 20221021145602,
                "content": {
                    sa_projectid: this.data.sa_projectid,
                    "deletereason": "",
                    "sa_project_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_project_itemsid) == -1)
                    })
                    this.setAmount();
                }
            })
        },
        /* 生成修改队列 */
        changeQueue({
            detail
        }) {
            let i = queue.findIndex(v => v.sa_project_itemsid == detail.sa_project_itemsid);
            if (i == -1) {
                queue.push(detail)
            } else {
                queue[i] = detail;
            };
            clearTimeout(downCounter)
            downCounter = setTimeout(() => {
                this.changeItem()
            }, 300);
        },
        changeItem() {
            _Http.basic({
                "id": 20221021145502,
                "content": {
                    "sa_projectid": this.data.sa_projectid,
                    items: queue
                }
            }, false).then(res => {
                console.log("批量修改产品清单", res)
                queue = [];
                downCounter = null;
                if (res.msg == '成功') {
                    this.setAmount();
                } else {
                    wx.showToast({
                        title: res.msg,
                        icon: "none"
                    })
                    that.getList(that.data.sa_projectid, true)
                }
            })
        },
        setAmount() {
            getCurrentPages().find(v => v.__route__ == 'packageA/project/detail').getDetail();
        },
    }
})