Page({
    data: {
        active: "",
        page: null,
        ops: [],
        unfolds: [],
        showFiltrate: false,
        type: 0,
        isleave: 1,
    },
    onLoad(options) {
        getApp().globalData.Language.getLanguagePackage(this, '数据总览');
        let organization = this.selectComponent("#organization");
        organization.initDepAndUser()
        const ops = wx.getStorageSync('auth').wdataoverview.options.filter(v => ["Clue", "Client", "Project", "Cost"].includes(v))
        this.setData({
            active: ops[0],
            unfolds: [ops[0]],
            dataid: wx.getStorageSync('userMsg').userid,
            username: wx.getStorageSync('userMsg').name,
        })
        this.setData({
            ops: ops.map(v => {
                return {
                    name: v,
                    title: {
                        "Clue": "线索",
                        "Client": "客户",
                        "Project": "项目",
                        "Cost": "营销费用"
                    } [v]
                }
            }),
        })
    },
    onChange(event) {
        const active = event.detail.name;
        if (!this.data.unfolds.includes(active)) {
            this.data.unfolds.push(active);
        }
        this.setData({
            active,
            unfolds: this.data.unfolds
        });
        this.getList({
            detail: active
        });
    },
    getList({
        detail
    }) {
        if (detail && this.data.active == "") return setTimeout(() => {
            this.getList({
                detail: detail
            })
        })
        if (detail && this.data.active != detail) return;
        const page = this.selectComponent("#" + this.data.active);
        if (page) {
            const {
                dataid,
                type,
                username,
                isleave
            } = this.data;
            let content = {};
            try {
                content = page.data.model.data.content;
            } catch (error) {
                content = page.getContent()
            }
            if (content.dataid != dataid || content.type != type || content.where.isleave != isleave) page.data.init = false
            content.dataid = dataid;
            content.type = type;
            content.username = username;
            content.where.isleave = isleave;
            if (!page.data.init) page.getList();
        }
    },
    openFiltrate() {
        this.setData({
            showFiltrate: true
        })
    },
    handleFilter({
        detail
    }) {
        if (detail.name == "close") return;
        if (detail.name == 'reset') {
            this.selectComponent("#organization").setData({
                isleave: 1
            })
            this.selectComponent("#organization").initDepAndUser()
            this.setData({
                dataid: wx.getStorageSync('userMsg').userid,
                'type': 0,
                isleave: 1,
                username: wx.getStorageSync('userMsg').name
            })
        } else {
            let active = this.selectComponent("#organization").data.result,
                isleave = this.selectComponent("#organization").data.isleave;
            let type = active.userid ? 0 : 1,
                dataid = type == 0 ? active.userid : active.sa_saleareaid
            this.setData({
                isleave,
                dataid,
                type,
                username: active.name
            })
        }
        this.getList({
            detail: this.data.active
        });
    },
})