const _Http = getApp().globalData.http,
    currency = require("../../utils/currency"),
    CNY = (value, symbol = "￥", precision = 2) => currency(value, {
        symbol,
        precision
    }).format();
Component({
    options: {
        addGlobalClass: true
    },
    lifetimes: {
        attached: function () {
            getApp().globalData.Language.getLanguagePackage(this)
        }
    },
    properties: {

    },
    data: {
        followSize: 0,
        dateType: "本年",
        showList: false,
        list: [],
        "type": 1,
        "content": {
            "nocache": true,
            "pageNumber": 1,
            "pageTotal": 1,
            "total": null,
            "where": {
                begdate: "",
                enddate: "",
            }
        }
    },
    methods: {
        getList(id, init) {
            let content = this.data.content;
            content.hrid = id;
            content.type = this.data.type;
            if (init) {
                content.pageNumber = 1;
                content.total = null;
            }
            if (!this.data.showList && content.total != null) return;
            if (content.pageNumber > content.pageTotal) return;
            _Http.basic({
                "id": 20230717101004,
                content
            }).then(res => {
                console.log("业务员关联项目", res)
                if (res.code != '1') return wx.showToast({
                    title: res.data,
                    icon: "none"
                })
                content.pageNumber = res.pageNumber + 1;
                content.pageSize = res.pageSize;
                content.pageTotal = res.pageTotal;
                content.total = res.total;
                res.data = res.data.map(v => {
                    try {
                        v.signamount_due = CNY(v.signamount_due);
                    } catch (error) {

                    }
                    return v
                })
                this.setData({
                    list: res.pageNumber == 1 ? res.data : this.data.list.concat(res.data),
                    content,
                    id: id,
                });
                this.selectComponent("#TimeRange").onCancel()
            })
            _Http.basic({
                "id": 20230717101104,
                "content": {
                    ...content,
                    pageSize: 0
                }
            }).then(res => {
                try {
                    this.setData({
                        followSize: res.data[0].followSize
                    })
                } catch (error) {

                }
            })
        },
        changeDate({
            detail
        }) {
            let type = 99;
            switch (detail.dateType) {
                case '全部':
                    type = 0;
                    break;
                case '本年':
                    type = 1;
                    break;
                case '本季':
                    type = 2;
                    break;
                case '本月':
                    type = 3;
                    break;
            }
            this.setData({
                dateType: detail.dateType,
                type,
                "content.where.begdate": detail.begdate || "",
                "content.where.enddate": detail.enddate || ""
            })
            this.getList(this.data.id, true)
        },
        shrinkChange({
            detail
        }) {
            this.setData({
                showList: detail
            })
        },
        upDateList() {
            let content = JSON.parse(JSON.stringify(this.data.content));
            try {
                content.hrid = this.data.id;
                content.type = this.data.type;
                content.pageSize = (content.pageNumber - 1) * content.pageSize;
                content.pageNumber = 1;
            } catch (error) {
                console.log("error", error)
            }
            _Http.basic({
                id: '20230717101004',
                content
            }).then(res => {
                console.log("更新业务员关联项目", res);
                if (res.code == '1') {
                    res.data = res.data.map(v => {
                        try {
                            v.signamount_due = CNY(v.signamount_due);
                        } catch (error) {

                        }
                        return v
                    })
                    this.setData({
                        list: res.data,
                        "content.total": res.total
                    })
                }
            })
            _Http.basic({
                "id": 20230717101104,
                "content": {
                    ...content,
                    pageSize: 0
                }
            }).then(res => {
                try {
                    this.setData({
                        followSize: res.data[0].followSize
                    })
                } catch (error) {

                }
            })
        }
    }
})