const _Http = getApp().globalData.http,
    currency = require("../../../../utils/currency"),
    CNY = value => currency(value, {
        symbol: "￥",
        precision: 2
    }).format();
Component({
    properties: {
        disabled: {
            type: Boolean
        }, //禁用
    },
    options: {
        addGlobalClass: true
    },
    data: {
        sa_salesforecastbillid: 0,
        list: [],
        sa_projectid: 0,
        allinvoiceamount: "",
        alloutamount: "",
        "content": {
            "nocache": true,
            "pageNumber": 1,
            "pageTotal": 1,
            "pageSize": 20,
            "type": 1, // 1 订单 2项目 3客户
            "total": null,
            "where": {
                "condition": ""
            }
        },
    },
    lifetimes: {
        attached: function () {
            getApp().globalData.Language.getLanguagePackage(this)
            this.setData({
                siteid: wx.getStorageSync('userMsg').siteid
            })
        }
    },
    methods: {
        getList(id, init) {
            let content = this.data.content;
            content.sa_salesforecastbillid = id;
            if (init) content.pageNumber = 1
            _Http.basic({
                "id": 20230705145504,
                content
            }).then(res => {
                console.log("预测订单列表", res)
                if (res.code != '1') return wx.showToast({
                    title: res.data,
                    icon: "none"
                })
                content.total = res.total;
                content.pageNumber = res.pageNumber + 1;
                content.pageTotal = res.pageTotal;

                res.data = res.data.map(v => {
                    v.defaultamount = CNY(v.defaultamount);
                    v.amount = CNY(v.amount);
                    v.showunoutamount = CNY(v.unoutamount);
                    v.showuninvoiceamount = CNY(v.uninvoiceamount);
                    return v
                })

                this.setData({
                    list: res.pageNumber == 1 ? res.data : this.data.list.concat(res.data),
                    sa_salesforecastbillid: id,
                    content
                });

                if (res.data.length) this.setData({
                    allinvoiceamount: CNY(res.data[0].allinvoiceamount),
                    alloutamount: CNY(res.data[0].alloutamount),
                })
            })
        },
        add() {
            wx.navigateTo({
                url: `/packageA/select/orderForm/select?params=${JSON.stringify({
                "id": 20230706090904,
                "content": {
                    nocache: true,
                    "sa_salesforecastbillid": this.data.sa_salesforecastbillid,
                    "where": {
                        "condition": ""
                    }
                }
            })}&aitText=暂无数据，请添加订单明细`,
            })
            getApp().globalData.handleSelect = this.handleAdd.bind(this);
        },
        handleAdd({
            result
        }) {
            const that = this;
            wx.showModal({
                title: getApp().globalData.Language.getMapText('提示'),
                content: getApp().globalData.Language.joint([{
                    t: 1,
                    v: '是否确认添加',
                    r: " "
                }, {
                    v: result.length,
                    r: " "
                }, {
                    t: 1,
                    v: '个订单',
                    r: "?"
                }]),
                cancelText: getApp().globalData.Language.getMapText('取消'),
                confirmText: getApp().globalData.Language.getMapText('确定'),
                complete: (res) => {
                    if (res.confirm) _Http.basic({
                        "id": 20230705145104,
                        "content": {
                            "sa_salesforecastbillid": that.data.sa_salesforecastbillid,
                            "ownertable": "sa_order", // sa_order 订单  sa_project 项目 sa_customers  客户
                            "salesforecastproject": result.map(v => {
                                return {
                                    "sa_salesforecastprojectid": 0,
                                    "ownerid": v,
                                    discountrate: 1
                                }
                            })
                        }
                    }).then(res => {
                        console.log("添加订单", res)
                        wx.showToast({
                            title: res.code == '1' ? getApp().globalData.Language.getMapText('添加成功') : res.msg,
                            icon: "none"
                        });
                        getCurrentPages()[getCurrentPages().length - 2].getDetail();
                        if (res.code == '1') {
                            that.getList(that.data.sa_salesforecastbillid, true)
                            setTimeout(() => {
                                wx.navigateBack()
                            }, 300)
                        }
                    })
                }
            })

        },
        deleteItem(e) {
            const {
                item
            } = e.currentTarget.dataset,
                that = this;
            wx.showModal({
                title: getApp().globalData.Language.getMapText('提示'),
                content: getApp().globalData.Language.joint([{
                    t: 1,
                    v: '是否确定删除',
                }, {
                    v: `"${item.sonum}"`,
                }, {
                    t: 1,
                    v: '订单',
                    r: "?"
                }]),
                cancelText: getApp().globalData.Language.getMapText('取消'),
                confirmText: getApp().globalData.Language.getMapText('确定'),
                complete: (res) => {
                    if (res.confirm) _Http.basic({
                        "id": 20230705145304,
                        "content": {
                            "sa_salesforecastbillid": that.data.sa_salesforecastbillid,
                            "sa_salesforecastprojectids": [item.sa_salesforecastprojectid]
                        }
                    }).then(res => {
                        console.log("删除订单行", res)
                        wx.showToast({
                            title: res.code == '1' ? getApp().globalData.Language.getMapText('删除成功') : res.msg,
                            icon: "none"
                        });
                        if (res.code == '1') that.updateList()
                    })
                }
            })
        },
        updateList(sonum) {
            let content = JSON.parse(JSON.stringify(this.data.content));
            if (sonum) {
                content.where.condition = sonum;
                content.pageSize = 1;
            } else {
                content.pageSize = (content.pageNumber - 1) * content.pageNumber;
            }
            content.pageNumber = 1;
            _Http.basic({
                "id": 20230705145504,
                content
            }).then(res => {
                console.log("更新数据", res)
                if (res.code != '1') return wx.showToast({
                    title: res.msg,
                    icon: "none"
                })
                res.data = res.data.map(v => {
                    v.defaultamount = CNY(v.defaultamount);
                    v.amount = CNY(v.amount);
                    v.showunoutamount = CNY(v.unoutamount);
                    v.showuninvoiceamount = CNY(v.uninvoiceamount);
                    return v
                })

                if (sonum) {
                    let index = this.data.list.findIndex(v => v.sonum == sonum);
                    if (index != -1) this.setData({
                        [`list[${index}]`]: res.data[0]
                    })
                } else {
                    this.setData({
                        list: res.data
                    })
                }
                this.setData({
                    allinvoiceamount: res.data.length ? CNY(res.data[0].allinvoiceamount) : CNY(0),
                    alloutamount: res.data.length ? CNY(res.data[0].alloutamount) : CNY(0),
                })
            })
            getCurrentPages()[getCurrentPages().length - 1].getDetail();
        },
        onBlur(e) {
            const {
                item,
                name
            } = e.currentTarget.dataset;
            let value = e.detail.value;
            if (item[name] == value) return;
            item[name] = Number((value - 0).toFixed(2))
            _Http.basic({
                "id": 20230705145104,
                "content": {
                    "sa_salesforecastbillid": this.data.sa_salesforecastbillid,
                    "ownertable": "sa_order", // sa_order 订单  sa_project 项目 sa_customers  客户
                    "salesforecastproject": [{
                        "sa_salesforecastprojectid": item.sa_salesforecastprojectid || 0,
                        "sa_salesforecastid": item.sa_salesforecastid || 0,
                        "outamount": item.outamount,
                        "ownerid": item.sa_orderid,
                        "invoiceamount": item.invoiceamount
                    }]
                }
            }).then(res => {
                console.log("修改行", res);
                wx.showToast({
                    title: res.code == '1' ? getApp().globalData.Language.getMapText('修改成功') : res.msg,
                    icon: "none"
                });
                if (res.code == '1') this.updateList(item.sonum)
            })

        }
    }
})