const _Http = getApp().globalData.http;
Page({
    data: {
        sourceData: null,
        isEdit: null,
        ProductList: null,
        disabled: false,
        loading: false
    },
    onLoad(options) {
        this.setData({
            sourceData: JSON.parse(options.item),
            isEdit: options.isEdit == 'true'
        })
        wx.setNavigationBarTitle({
            title: options.isEdit == 'true' ? '预测报表' : '预测填报',
        })
        this.getList();
        this.getProductList();
    },
    /* 提交 */
    submit() {
        if (this.data.disabled && this.data.loading) return;
        this.setData({
            loading: true
        })
        _Http.basic({
            "id": 20220906155003,
            "version": 1,
            "content": {
                "sa_salesforecastmodelid": this.data.sourceData.sa_salesforecastmodelid,
                "sa_salesforecastbillid": this.data.sourceData.sa_salesforecastbillid,
                "sa_projectid": this.data.sourceData.sa_projectid,
                "itemclassinfos": this.data.list
            }
        }).then(res => {
            this.setData({
                loading: false
            })
            if (res.msg != '成功') return wx.showToast({
                title: res.data,
                icon: "none"
            });
            this.setData({
                disabled: true
            })
            this.randerData()
            setTimeout(() => {
                wx.navigateBack({
                    delta: 0,
                })
            }, 500)
            wx.showToast({
                title: '保存成功',
                icon: "none"
            });
        })
    },
    randerData() {
        let pages = getCurrentPages();
        pages[pages.length - 2].getList(true);
    },
    /* 获取列表 */
    getList() {
        let parem = {
            "id": 20220906154703,
            "version": 1,
            "content": {
                "nocheca": true,
                "sa_salesforecastbillid": this.data.sourceData.sa_salesforecastbillid,
                'pageSize': 9999,
                "where": {}
            }
        };
        if (this.data.sourceData.sa_projectid != 0) parem.content.where = {
            "sa_projectid": this.data.sourceData.sa_projectid
        }
        _Http.basic(parem).then(res => {
            console.log(321321321,res)
            if (res.msg != '成功') return wx.showToast({
                title: res.msg,
                icon: "none"
            })
            for (let i = 0; i < res.data.length; i++) {
                res.data[i].complete = true;
            }
            this.setData({
                list: res.data.filter(v => v.itemclassname)
            });
        })
    },
    /* 获取产品列表 */
    getProductList() {
        _Http.basic({
            "id": 20220906154903,
            "version": 1,
            "content": {}
        }).then(res => {
            console.log("产品列表", res)
            if (res.msg != '成功') return wx.showToast({
                title: res.data,
                icon: "none"
            })
            this.setData({
                ProductList: res.data
            })
        })
    },
    /* 打开添加产品 */
    addProduct() {
        let list = this.data.ProductList.filter(v => !this.data.list.some(value => value.itemclassnum == v.itemclassnum))
        this.selectComponent('#Product').setData({
            show: true,
            list
        })
    },
    /* 得到添加产品 */
    getResult({
        detail
    }) {
        const list = detail.map(v => {
            const data = this.data.ProductList.find(value => value.itemclassnum == v);
            return {
                "sa_salesforecastid": 0,
                "itemclassnum": data.itemclassnum,
                "itemclassname": data.itemclassname,
                "orderqty": 0,
                "orderamount": 0,
                "invoiceqty": 0,
                "invoiceamount": 0,
                "outqty": 0,
                "outamount": 0,
                "complete": false
            }
        })
        this.setData({
            list: list.concat(this.data.list)
        });
        wx.pageScrollTo({
            scrollTop: 0,
        })
        this.isDisabled();
    },
    /* 输入框改变 */
    inputChange(e) {
        const {
            index,
            name
        } = e.currentTarget.dataset,
            value = e.detail.value.trim();
        this.setData({
            [`list[${index}].${name}`]: value
        })
        if (['orderamount', 'outamount', 'invoiceamount'].includes(name)) {
            const data = this.data.list[index];
            this.setData({
                [`list[${index}].complete`]: data.orderamount != "" && data.outamount != "" && data.invoiceamount != ""
            })
        };
        this.isDisabled();
    },
    /* 删除产品 */
    deleteProduct(e) {
        const {
            item
        } = e.currentTarget.dataset,
            that = this;
        wx.showModal({
            title: "提示",
            content: `是否确认删除${item.itemclassname}`,
            success({
                confirm
            }) {
                if (confirm) {
                    if (item.sa_salesforecastid == 0) {
                        that.setData({
                            list: that.data.list.filter(v => v.itemclassnum != item.itemclassnum)
                        })
                        wx.showToast({
                            title: '删除成功',
                            icon: "none"
                        })
                        that.isDisabled();
                        that.randerData()
                    } else {
                        _Http.basic({
                            "id": 20220906155103,
                            "version": 1,
                            "content": {
                                "sa_salesforecastid": item.sa_salesforecastid,
                                "sa_projectid": 0,
                                "sa_salesforecastbillid": item.sa_salesforecastbillid
                            }
                        }).then(res => {
                            if (res.msg != '成功') return wx.showToast({
                                title: res.data,
                                icon: "none"
                            });
                            that.setData({
                                list: that.data.list.filter(v => v.itemclassnum != item.itemclassnum)
                            })
                            that.isDisabled();
                            that.randerData()
                            wx.showToast({
                                title: '删除成功',
                                icon: "none"
                            })
                        })
                    }
                }
            }
        })
    },
    /* 判断是否禁用保存按钮 */
    isDisabled() {
        let count = 0;
        this.data.list.forEach(v => v.complete ? count += 1 : '')
        this.setData({
            disabled: !(count == this.data.list.length)
        })
    },
    onShareAppMessage() {}
})