let _Http = getApp().globalData.http,
    currency = require("../../utils/currency"),
    CNY = value => currency(value, {
        symbol: "￥",
        precision: 2
    }).format();

Page({
    data: {
        showFiltrate: false,
        pageIndex: 0,
        admin: false,
        tabsActive: 0,
        active: null,
        "content": {
            "type": 1,
            "dataid": wx.getStorageSync('userMsg').sa_saleareaid,
            "baseonproject": 0,
            "title": ""
        },
        isonlydep: false
    },
    onLoad(options) {
        this.setData({
            sa_salesforecastbillid: options.id
        })
        if (options.admin == 'true') this.setData({
            admin: true
        })
        let tabsList = [{
            label: "订单",
            icon: "icon-E-dingdan",
            model: "#OrderForm"
        }, {
            label: "项目",
            icon: "icon-tabxiangmu",
            model: "#Project"
        }, {
            label: "客户",
            icon: "icon-tabkehu",
            model: "#Client"
        }, {
            label: "附件",
            icon: "icon-tabfujian1",
            model: "#Files"
        }, {
            label: "操作记录",
            icon: "icon-tabcaozuojilu1",
            model: "#Record"
        }];

        const {
            isdatafollowup,
            istask,
            isdatatag,
            isdatateam
        } = wx.getStorageSync('auth')[options.admin == 'true' ? 'invoiceforecastadmin' : 'invoiceforecast'];

        if (isdatafollowup) tabsList.push({
            label: "跟进动态",
            icon: "icon-tabgenjinjilu",
            model: "#Trace"
        })
        if (istask) tabsList.push({
            label: "任务",
            icon: "icon-tabrenwu",
            model: "#Work"
        })
        this.setData({
            tabsList,
            isdatatag,
            isdatateam
        })
        this.getDetail();
        if (isdatatag) this.getTags();
        if (isdatateam) this.getGroup()
        this.partialRenewal();
        getApp().globalData.Language.getLanguagePackage(this, '预测填报');
    },
    switchOnChange({
        detail
    }) {
        this.setData({
            isonlydep: detail
        });
        if (detail) this.setData({
            active: null,
            "content.type": 1,
            "content.dataid": wx.getStorageSync('userMsg').sa_saleareaid,
        });
        this.selectComponent("#organization").initDepAndUser();
        this.getRests();
    },
    /* 切换 */
    onChange(e) {
        this.setData({
            pageIndex: e.detail.index
        })
    },
    openFiltrate() {
        this.setData({
            showFiltrate: true
        })
    },
    onReady() {
        if (this.data.admin) this.selectComponent("#organization").initDepAndUser();
    },
    handleFilter({
        detail
    }) {
        if (detail.name == 'reset') {
            this.selectComponent("#organization").initDepAndUser()
            this.setData({
                active: null,
                "content.dataid": wx.getStorageSync('userMsg').sa_saleareaid,
                'content.type': 1,
                isonlydep: false
            })
        } else {
            let active = this.selectComponent("#organization").data.result;
            let type = active.userid ? 0 : 1,
                dataid = type == 0 ? active.userid : active.sa_saleareaid
            this.setData({
                active,
                'content.dataid': dataid,
                'content.type': type
            })
        }
        this.getRests();
    },
    getRests() {
        let content = this.data.content;
        content.isonlydep = this.data.isonlydep ? 1 : 0
        _Http.basic({
            "id": 20230705144804,
            content
        }).then(res => {
            console.log("其他预测单", res)
            if (res.code != '1') wx.showToast({
                title: res.msg,
                icon: "none"
            });
            res.data.allorderinvoamount = CNY(res.data.allorderinvoamount)
            res.data.allorderoutamount = CNY(res.data.allorderoutamount)
            res.data.data = res.data.data.filter(v => v.billnum).map(v => {
                v.orderoutamount = CNY(v.orderoutamount)
                v.orderinvoamount = CNY(v.orderinvoamount)
                return v
            })
            this.setData({
                rests: res.data
            })
        })
    },
    getDetail() {
        _Http.basic({
            "id": 20230705144704,
            "content": {
                "sa_salesforecastbillid": this.data.sa_salesforecastbillid
            }
        }).then(res => {
            console.log("预测详情", res)
            if (res.code != '1') wx.showToast({
                title: res.msg,
                icon: "none"
            });
            res.data.orderoutamount = CNY(res.data.orderoutamount)
            res.data.orderinvoamount = CNY(res.data.orderinvoamount)
            this.setData({
                detail: res.data,
                'content.title': res.data.title,
                isLeader: res.data.userid == wx.getStorageSync('userMsg').userid
            });
            if (!this.data.isLeader) getApp().agentOrNot("sa_salesforecastbill", this.data.sa_salesforecastbillid).then(s => {
                console.log("代理人数据", s)
                this.setData({
                    isLeader: s.data.editable == 1 || this.selectComponent("#Group").data.editable == 1
                })
            })
            if (this.data.admin) this.getRests();
        })
    },
    //tabs 切换
    tabsChange({
        detail
    }) {
        this.setData({
            tabsActive: detail
        });
        this.partialRenewal();
    },
    //更新标签
    getTags() {
        this.selectComponent("#Tags").getTags();
    },
    //更新团队成员
    getGroup() {
        this.selectComponent("#Group").getList().then(res => {
            if (!this.data.isLeader) this.setData({
                isLeader: this.selectComponent("#Group").data.editable == 1
            })
        });
    },
    //局部数据更新 tabs
    partialRenewal(init = false) {
        let model = this.data.tabsList[this.data.tabsActive].model;
        if (model) {
            let Component = this.selectComponent(model),
                {
                    total,
                    pageNumber,
                    pageTotal
                } = Component.data.content,
                id = this.data.sa_salesforecastbillid;
            if (model == "#Files") init = true;
            if (total == null || init) {
                Component.getList(id, init);
            } else if (pageNumber <= pageTotal) {
                Component.getList(id, false);
            }
        }
    },
    onReachBottom() {
        this.partialRenewal();
    },
    onUnload() {
        let page = getCurrentPages()[getCurrentPages().length - 2];
        if (page.getRests) return page.getRests();
        let content = JSON.parse(JSON.stringify(page.data.content));
        content.pageSize = ((content.pageNumber - 1) * content.pageSize) || 20;
        content.pageNumber = 1;
        _Http.basic({
            id: page.data.admin ? 20230705144604 : 20230706092304,
            content
        }).then(res => {
            console.log("更新预测", res);
            res.data = res.data.map(v => {
                v.orderoutamount = CNY(v.orderoutamount)
                v.orderinvoamount = CNY(v.orderinvoamount)
                return v
            })
            if (res.code == '1') page.setData({
                list: res.data,
                "content.total": res.total
            })
        })
    }
})