const _Http = getApp().globalData.http,
    currency = require("../../utils/currency"),
    CNY = (value, symbol = "", precision = 2) => currency(value, {
        symbol,
        precision
    }).format();
Page({
    data: {
        dropdownItem: 0,
        dropdown: [{
                text: '作业看板',
                value: 0
            },
            {
                text: '线索',
                value: 1
            },
            {
                text: '客户',
                value: 2
            },
            {
                text: '项目商机',
                value: 3
            },
            {
                text: '作业单据',
                value: 4
            }
        ],
        filtratelist: [],
        showFiltrate: false
    },
    onLoad(options) {
        _Http.basic({
            "id": 20230620102004,
            "content": {}
        }).then(res => {
            console.log("获取下级部门和下级人员", res)
            if (res.msg != '成功') return wx.showToast({
                title: res.data,
                icon: "none"
            })
            let data = {
                label: "查询范围",
                index: 0,
                showName: "name", //显示字段
                valueKey: "active", //返回Key
                value: "id", //选中值
                list: res.data.hr.map(v => {
                    v.id = v.userid;
                    // v.key = v.name + v.userid;
                    v.type = '人员'
                    delete(v.rowindex)
                    delete(v.userid)
                    return v
                })
            };
            const mflat = (v, prefix) => {
                v.subdep.forEach(s => mflat(s, v.depname));
                v.name = v.depname; //prefix ? prefix + '/' + v.depname : v.depname
                v.type = '部门';
                v.id = v.departmentid;
                // v.key = v.name + v.id;
                delete(v.depname)
                delete(v.departmentid)
                delete(v.parentid)
                delete(v.rowindex)
                delete(v.subdep)
                data.list.unshift(v)
            };
            res.data.dep.forEach(v => mflat(v))
            data.list.map((v, i) => {
                v.index = i;
                if (v.type == '人员' && v.id == wx.getStorageSync('userMsg').userid) data.index = i;
                return v
            })
            this.setData({
                "filtratelist[0]": data,
                active: data.list[data.index]
            });
            this.refreshModel();
        });
        this.refreshData();
    },
    openFiltrate() {
        this.setData({
            showFiltrate: true
        })
    },
    handleFilter(e) {
        let {
            active
        } = e.detail;
        this.setData({
            active
        });
        this.refreshData(active.type == '人员' ? 0 : 1, active.id);
        this.refreshModel();
    },
    refreshData(type = 0, dataid = wx.getStorageSync('userMsg').userid) {
        const conversion = (n) => {
            const integer = (n + '').split(".")[0] + '',
                length = integer.length,
                regexp = /(?:\.0*|(\.\d+?)0+)$/
            if (length <= 4) {
                const index = 4 - length;
                return { //元
                    show: CNY(n, "", index).replace(regexp, '$1'),
                    value: CNY(n, '￥')
                }
            } else if (length <= 8) {
                return { //万-千万
                    show: CNY(currency(n).divide(10000)).replace(regexp, '$1') + '万',
                    value: CNY(n)
                }
            } else {
                return { //亿
                    show: CNY(currency(n).divide(100000000)).replace(regexp, '$1') + '亿',
                    value: CNY(n)
                }
            }
        };
        const sortOut = (obj, data, isConversion = false) => {
            for (const key in obj) {
                if (key == 'tab') continue;
                for (const k in obj[key]) {
                    obj[key][k] = isConversion ? conversion(data[key + k]) : data[key + k]
                }
            }
            return obj
        };
        const getData = (dataType) => {
            return _Http.basic({
                "id": 20230616131404,
                "content": {
                    nocache: true,
                    dataType, // 1 作业数据 2 销售数据 3业绩数据
                    type,
                    dataid
                }
            }).then(res => {
                let data = {};
                switch (dataType) {
                    case 1:
                        data = {
                            tab: {
                                active: "bz",
                                list: [{
                                    name: "本周",
                                    id: "bz"
                                }, {
                                    name: "本月",
                                    id: "by"
                                }, {
                                    name: "本年",
                                    id: "bn"
                                }]
                            },
                            bz: {
                                khgj: 0,
                                khxz: 0,
                                xmgj: 0,
                                xmxz: 0
                            },
                            by: {
                                khgj: 0,
                                khxz: 0,
                                xmgj: 0,
                                xmxz: 0
                            },
                            bn: {
                                khgj: 0,
                                khxz: 0,
                                xmgj: 0,
                                xmxz: 0
                            }
                        }
                        break;
                    case 2:
                        data = {
                            tab: {
                                active: "bz",
                                list: [{
                                    name: "本周",
                                    id: "bz"
                                }, {
                                    name: "本月",
                                    id: "by"
                                }, {
                                    name: "本年",
                                    id: "bn"
                                }]
                            },
                            bz: {
                                chje: 0,
                                kpje: 0,
                                skje: 0,
                                xsje: 0
                            },
                            by: {
                                chje: 0,
                                kpje: 0,
                                skje: 0,
                                xsje: 0
                            },
                            bn: {
                                chje: 0,
                                kpje: 0,
                                skje: 0,
                                xsje: 0
                            }
                        }
                        break;
                    default:
                        data = res.data
                        data.tab = {
                            active: "by",
                            list: [{
                                name: "本月",
                                id: "by"
                            }, {
                                name: "本季",
                                id: "bj"
                            }, {
                                name: "本年",
                                id: "bn"
                            }]
                        }
                        break;
                }
                return dataType == 3 ? data : sortOut(data, res.data, dataType == 2)
            })
        };
        getData(1).then(res => {
            this.setData({
                zysj: res
            })
        });
        getData(2).then(res => {
            this.setData({
                xssj: res
            })
        });
        getData(3).then(res => {
            for (const key in res) {
                if (key == 'tab') continue;
                res[key].target_l = conversion(res[key].target_l);
                //实际
                res[key].amount = conversion(res[key].amount);
                res[key].outamount = conversion(res[key].outamount);
                res[key].invoiceamount = conversion(res[key].invoiceamount);
                //差额
                res[key].unamount = CNY(res[key].unamount, '￥');
                res[key].unamountcolor = res[key].unamount >= 0 ? '#5AB73F' : '#EB4B5C';
                res[key].unoutamount = CNY(res[key].unoutamount, '￥');
                res[key].unoutamountcolor = res[key].unoutamount >= 0 ? '#5AB73F' : '#EB4B5C';
                res[key].uninvoiceamount = CNY(res[key].uninvoiceamount, '￥');
                res[key].uninvoiceamountcolor = res[key].uninvoiceamount >= 0 ? '#5AB73F' : '#EB4B5C';
                //达成率
                res[key].wcamount = currency(res[key].wcamount).multiply(100) + '%';
                res[key].wcoutamount = currency(res[key].wcoutamount).multiply(100) + '%';
                res[key].wcinvoiceamount = currency(res[key].wcinvoiceamount).multiply(100) + '%';
            }
            this.setData({
                yjsj: res
            })
        });
    },
    dropdownItemChange({
        detail
    }) {
        this.setData({
            dropdownItem: detail
        })
        this.refreshModel();
    },
    changeId(e) {
        const {
            id,
            name
        } = e.currentTarget.dataset;
        if (this.data[name].tab.active == id) return;
        this.setData({
            [`${name}.tab.active`]: id
        })
    },
    refreshModel() {
        const dropdownItem = this.data.dropdownItem;
        if (dropdownItem == 0) return;
        const model = this.selectComponent("#model" + dropdownItem);
        if (!model) return;
        const active = this.data.active;
        model.init(active.type == '人员' ? '0' : '1', active.id);
    },
    onReady(){
        this.selectComponent("#ListBox").setHeight(".head", this);
    }
})