const _Http = getApp().globalData.http,
    file = require("../../../../utils/matchingFeilType");
let Counter = null;
Component({
    properties: {
        disabled: Boolean, //禁用
    },
    data: {
        viewIndex: 0,
        sa_salesforecastbillid: 0,
        "content": {
            nocache: true,
            pageSize: 999,
            total: null,
            "where": {
                "condition": ""
            }
        },
        param: {
            "id": 20220906154703,
            "version": 1,
            "content": {
                nocache: true,
                "sa_salesforecastbillid": "",
                pageNumber: 1,
                pageTotal: 1,
                "where": {
                    "condition": "",
                    "sa_projectid": ""
                }
            }
        },
        productList: [], //产品列表
    },
    methods: {
        /* 步进器数值改变 */
        stepperChange(e) {
            let discountrate = this.data.list[this.data.viewIndex].discountrate;
            switch (e.type) {
                case 'plus':
                    discountrate += 1;
                    break;
                case 'minus':
                    discountrate -= 1;
                    break;
                case 'blur':
                    discountrate = e.detail.value;
                    break;
            }
            if (discountrate > 100) discountrate = 100;
            if (discountrate < 0.1) discountrate = 0.1;
            this.data.list[this.data.viewIndex].discountrate = discountrate;
            this.setDiscountrate(this.data.list[this.data.viewIndex]);
        },

        /* 设置折扣 */
        setDiscountrate(item) {
            clearTimeout(Counter);
            let page = getCurrentPages().find(v => v.__route__ == 'packageA/salesForecasting/detail');
            let data = page.data.detail;
            Counter = setTimeout(() => {
                _Http.basic({
                    id: 20220913154403,
                    version: 1,
                    content: {
                        nocache: true,
                        sa_salesforecastmodelid: data.sa_salesforecastmodelid,
                        sa_salesforecastbillid: data.sa_salesforecastbillid,
                        sa_projectids: [{
                            sa_salesforecastprojectid: data.sa_salesforecastprojectid || item.sa_salesforecastprojectid,
                            sa_projectid: item.sa_projectid,
                            discountrate: (item.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 = false, isAdd = false) {
            let content = this.data.content;
            content.sa_salesforecastbillid = id;
            _Http.basic({
                "id": "20220916115203",
                "version": 1,
                content
            }).then(res => {
                console.log("项目清单", res)
                if (res.msg != '成功') return wx.showToast({
                    title: res.data,
                    icon: "none"
                })
                let page = getCurrentPages().find(v => v.__route__ == 'packageA/salesForecasting/detail');
                res.data = res.data.map(v => {
                    v.discountrate = v.discountrate * 100;
                    if (v.discountrate > 100) v.discountrate = 100;
                    return v
                })
                this.setData({
                    list: res.pageNumber == 1 ? res.data : this.data.list.concat(res.data),
                    "content.total": res.total,
                    sa_salesforecastbillid: id,
                    sa_salesforecastmodelid: page.data.detail.sa_salesforecastmodelid
                });
                if (res.data.length) {
                    this.setData({
                        'param.content.where.sa_projectid': res.data[isAdd ? res.data.length - 1 : this.data.viewIndex].sa_projectid,
                        'param.content.sa_salesforecastbillid': id,
                        viewIndex: isAdd ? res.data.length - 1 : this.data.viewIndex
                    })
                    this.getProduct(true);
                }
            })
        },
        /* 获取产品清单 */
        getProduct(init = false) {
            let param = this.data.param;
            if (init) param.content.pageNumber = 1;
            if (param.content.pageNumber > param.content.pageTotal) return;
            _Http.basic(param).then(res => {
                console.log("产品列表", res)
                if (res.msg != '成功') return wx.showToast({
                    title: res.msg,
                    icon: "none"
                });
                if (res.data.length == 1 && !res.data[0].itemname) {
                    res.data = []
                } else {
                    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.className = [];
                        value.brandName = [];
                        if (value.temclass.length != 0) value.temclass.forEach(v => {
                            if (v.brandname != "") value.brandName.push(v.brandname);
                            if (v.itemclassname != "") value.className.push(v.itemclassname);
                        })
                        return value;
                    })
                }
                this.setData({
                    productList: res.pageNumber == 1 ? res.data : this.data.productList.concat(res.data),
                    "param.content.pageNumber": res.pageNumber + 1,
                    "param.content.pageTotal": res.pageTotal,
                })
            })
        },
        /* 切换tab */
        onChange(e) {
            this.setData({
                "param.content.where.sa_projectid": e.detail.name,
                viewIndex: e.detail.index,
            });
            this.getProduct(true);
        },
        /* 添加产品 */
        addProduct() {
            wx.navigateTo({
                url: `/packageA/select/product/select?params=${JSON.stringify({
                    id: 20221208105403,
                    version: 1,
                    content: {
                        nocache:true,
                        sa_projectid: this.data.param.content.where.sa_projectid,
                        sa_salesforecastbillid: this.data.sa_salesforecastbillid,
                        "where": {
                            "condition": ""
                        }
                    }
                })}`
            });
            getApp().globalData.handleSelect = this.handleaddProduct.bind(this);
        },
        handleaddProduct({
            list
        }) {
            const that = this;
            wx.showModal({
                title: '提示',
                content: `是否确认添加${list.length}件产品`,
                complete: ({
                    confirm
                }) => {
                    if (confirm) _Http.basic({
                        "id": 20220906155003,
                        "version": 1,
                        "content": {
                            "sa_salesforecastmodelid": that.data.sa_salesforecastmodelid,
                            "sa_salesforecastbillid": that.data.sa_salesforecastbillid,
                            "sa_projectid": that.data.param.content.where.sa_projectid,
                            itemclassinfos: list.map(v => {
                                return {
                                    "sa_salesforecastid": 0,
                                    "itemid": v.itemid,
                                    "orderqty": 0,
                                    "orderamount": 0,
                                    "invoiceqty": 0,
                                    "invoiceamount": 0,
                                    "outqty": v.qty,
                                    "price": v.price
                                }
                            })
                        }
                    }).then(res => {
                        console.log("项目添加产品", res)
                        wx.showToast({
                            title: res.msg == '成功' ? '添加成功' : res.msg,
                            icon: "none"
                        })
                        if (res.msg == '成功') setTimeout(() => {
                            wx.navigateBack();
                            that.getProduct(true);
                            that.setAmount();
                        }, 300)
                    })
                }
            })
        },
        /* 生成修改队列 */
        changeQueue({
            detail
        }) {
            const that = this;
            _Http.basic({
                "id": 20220906155003,
                "version": 1,
                "content": {
                    "sa_salesforecastmodelid": that.data.sa_salesforecastmodelid,
                    "sa_salesforecastbillid": that.data.sa_salesforecastbillid,
                    "sa_projectid": that.data.param.content.where.sa_projectid,
                    itemclassinfos: [detail]
                }
            }).then(res => {
                console.log("项目修改产品", res)
                if (res.msg != '成功') return wx.showToast({
                    title: res.msg,
                    icon: "none"
                })
                that.setData({
                    [`productList[0].sumamount`]: res.data[0].sumamount,
                })
                this.setAmount();
            })
        },
        /* 删除 */
        deleteItem({
            detail
        }) {
            _Http.basic({
                "id": 20220906155103,
                "version": 1,
                "content": detail
            }).then(res => {
                console.log("删除产品", res);
                if (res.msg != '成功') return wx.showToast({
                    title: res.msg,
                    icon: "none"
                });
                this.setData({
                    productList: this.data.productList.filter(v => v.sa_salesforecastid != detail.sa_salesforecastid)
                });
                this.setAmount();
                wx.showToast({
                    title: '删除成功',
                    icon: "none"
                })
            })
        },
        setAmount() {
            let page = getCurrentPages().find(v => v.__route__ == 'packageA/salesForecasting/detail');
            if (page) page.getDetail(true)
        },
    }
})