const _Http = getApp().globalData.http,
    currency = require("../../../../utils/currency"),
    file = require("../../../../utils/matchingFeilType"),
    CNY = value => currency(value, {
        symbol: "￥",
        precision: 2
    }).format();
Component({
    properties: {
        disabled: {
            type: Boolean
        },
        toAdd: {
            type: Function
        }
    },
    options: {
        addGlobalClass: true
    },
    lifetimes: {
        attached: function () {
            getApp().globalData.Language.getLanguagePackage(this)
        }
    },
    data: {
        sa_salesforecastbillid: 0,
        list: [],
        sa_projectid: 0,
        "content": {
            "nocache": true,
            pageSize: 9999,
            "total": null,
            "where": {
                "condition": ""
            }
        },
        project: null
    },
    methods: {
        getList(id, init, update = false) {
            let content = this.data.content;
            content.sa_salesforecastbillid = id;
            content.where.condition = update ? this.data.project.projectnum : "";
            _Http.basic({
                "id": 20230705144904,
                content
            }).then(res => {
                console.log("预测项目列表", res)
                if (res.code != '1') return wx.showToast({
                    title: res.data,
                    icon: "none"
                })
                res.data = res.data.map(v => {
                    v.zk = currency(v.discountrate).multiply(100).value;
                    v.shouAmount = CNY(v.sumprojectamount)
                    return v
                });
                if (update) {
                    const index = this.data.list.findIndex(v => v.projectnum == this.data.project.projectnum);
                    if (index != -1) {
                        this.setData({
                            [`list[${index}]`]: res.data[0],
                            project: res.data[0]
                        });
                        this.getProduct();
                    }
                } else {
                    content.total = res.total;
                    this.setData({
                        list: res.pageNumber == 1 ? res.data : this.data.list.concat(res.data),
                        content
                    });
                    if (res.data.length) {
                        this.setData({
                            sa_projectid: res.data[0].sa_projectid,
                            project: res.data[0]
                        });
                        this.getProduct();
                    } else {
                        this.setData({
                            sa_projectid: 0,
                            project: null
                        })
                    }
                }
                this.setData({
                    sa_salesforecastbillid: id
                })
            })
        },
        onChange({
            detail
        }) {
            this.setData({
                sa_projectid: detail.name,
                project: this.data.list[detail.index]
            });
            this.getProduct();
        },
        addProject() {
            this.triggerEvent("toAdd")
        },
        /* 折扣失焦 */
        onBlur(e) {
            let zk = e.detail.value,
                project = JSON.parse(JSON.stringify(this.data.project));
            if (zk > 100) {
                zk = 100
            } else if (zk < 0.01) {
                zk = project.zk;
                wx.showToast({
                    title: getApp().globalData.Language.getMapText('折扣过小'),
                    icon: "none"
                })
            }
            if (zk == project.zk) return;
            project.zk = currency(zk).multiply(1).value;
            project.discountrate = currency(zk, {
                precision: 4
            }).divide(100).value;
            _Http.basic({
                id: 20230705145104,
                content: {
                    sa_salesforecastbillid: this.data.sa_salesforecastbillid,
                    "ownertable": "sa_project",
                    salesforecastproject: [{
                        ownerid: project.sa_projectid,
                        ownertable: "sa_project",
                        discountrate: project.discountrate,
                        sa_salesforecastprojectid: project.sa_salesforecastprojectid
                    }]
                }
            }).then(res => {
                wx.showToast({
                    title: res.code == '1' ? getApp().globalData.Language.getMapText('折扣修改成功') : res.msg,
                    icon: "none"
                })
                if (res.code == '1') this.updateAmount();
            })
        },
        getProduct() {
            _Http.basic({
                "id": 20230705145004,
                "content": {
                    nocache: true,
                    "sa_salesforecastprojectid": this.data.project.sa_salesforecastprojectid,
                    "sa_salesforecastbillid": this.data.project.sa_salesforecastbillid,
                    pageSize: 9999,
                    "where": {
                        "condition": ""
                    }
                }
            }).then(res => {
                if (res.code != '1') return wx.showToast({
                    title: res.data,
                    icon: "none"
                })
                console.log("产品", res)
                this.setData({
                    productList: 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.itemclass.length == 0 ? "暂无类目" : value.itemclass.map(v => v.itemclassname);
                        value.brandName = value.brand.length == 0 ? "暂无品牌" : value.brand.map(v => v.brandname);
                        value.showMP = CNY(value.marketprice)
                        value.showOrderamount = CNY(value.orderamount)
                        return value
                    })
                })
            })
        },
        toAddProduct() {
            let project = this.data.project;
            wx.navigateTo({
                url: `/packageA/select/product/select?params=${JSON.stringify({
                "accesstoken": "1ede5d2594d778a5e652b5a267c90308",
                "id": 20230705145704,
                "content": {
                    "pageNumber": 1,
                    "pageSize": 20,
                    "sa_salesforecastbillid":project.sa_salesforecastbillid,
                    "sa_salesforecastprojectid":project.sa_salesforecastprojectid,
                    "sa_projectid":project.sa_projectid,
                    "where": {
                        "condition": ""
                    }
                }
            })}`,
            });
            getApp().globalData.handleSelect = this.handleAdd.bind(this);
        },
        handleAdd({
            list
        }) {
            let that = this,
                project = this.data.project;
            console.log(list)
            wx.showModal({
                title: getApp().globalData.Language.getMapText('提示'),
                content: getApp().globalData.Language.joint([{
                    t: 1,
                    v: '是否确认添加',
                    r: " "
                }, {
                    v: list.length,
                    r: " "
                }, {
                    t: 1,
                    v: '件产品',
                    r: "?"
                }]),
                cancelText: getApp().globalData.Language.getMapText('取消'),
                confirmText: getApp().globalData.Language.getMapText('确定'),
                complete: (res) => {
                    if (res.confirm) _Http.basic({
                        "id": 20230705145204,
                        "content": {
                            "sa_salesforecastbillid": project.sa_salesforecastbillid,
                            "sa_salesforecastprojectid": project.sa_salesforecastprojectid,
                            "salesforecast": list.map(v => {
                                return {
                                    "itemid": v.itemid,
                                    "orderqty": v.qty,
                                    "orderamount": v.qty * v.price,
                                    "price": v.price,
                                    "sa_salesforecastid": 0
                                }
                            })
                        }
                    }).then(res => {
                        console.log("添加产品", res)
                        if (res.code != '1') return wx.showToast({
                            title: res.data,
                            icon: "none"
                        });
                        wx.showToast({
                            title: getApp().globalData.Language.getMapText('添加成功'),
                            icon: "none",
                            mask: true
                        });
                        setTimeout(() => {
                            wx.navigateBack()
                            that.updateAmount()
                        }, 300)
                    })
                }
            })
        },
        /* 更新金额 */
        updateAmount() {
            const page = getCurrentPages().find(v => v.__route__ == 'packageA/finishforecast/detail');
            if (page) page.getDetail();
            this.getList(this.data.sa_salesforecastbillid, true, true)
        },
    }
})