const _Http = getApp().globalData.http,
    currency = require("../../utils/currency"),
    CNY = (value, symbol = "", precision = 2) => currency(value, {
        symbol,
        precision
    }).format(),
    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)
            }
        }
    };


import {
    createElement
} from '@antv/f2';
import Chart from './modules/chart';
import {
    jsx as _jsx
} from "@antv/f2/jsx-runtime";
Page({
    data: {
        onRenderChart: () => {},
        showFiltrate: false,
        "where": {
            "begindate": "",
            "enddate": "",
            "departmentid": ""
        }
    },
    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: "departmentid", //返回Key
                value: "id", //选中值
                selectKey: "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]
            });
        });
    },
    onReady() {
        this.setChartData();
    },
    openFiltrate() {
        this.setData({
            showFiltrate: true
        })
    },
    handleFilter({
        detail
    }) {
        this.setData({
            where: {
                "begindate": detail.startdate,
                "enddate": detail.enddate,
                "departmentid": detail.departmentid
            }
        })
        this.setChartData();
    },
    setChartData() {
        _Http.basic({
            "id": 20230630151504,
            "content": {
                nocache: true,
                where: this.data.where
            }
        }).then(res => {
            console.log("漏斗数据", res)
            let full = Math.max(...res.data.map(v => v.projectqty));
            let statistics = res.data.map(v => {
                v.action = v.stagename
                v.percent = v.projectqty / full;
                v.totalinvestment = conversion(v.totalinvestment).show
                v.budgetary = conversion(v.budgetary).show
                v.signamount_due = conversion(v.signamount_due).show
                return v
            })
            let cHeight = statistics.length * 74;
            if (cHeight < 200) cHeight = 200;
            this.setData({
                statistics,
                cHeight: cHeight + 'rpx',
                onRenderChart: res.data.length == 0 ? () => {} : () => {
                    return this.renderChart();
                }
            })
        })
    },
    renderChart() {
        return createElement(Chart, {
            data: this.data.statistics
        });

    },
})