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: {
        mode: {
            type: String
        }
    },
    data: {
        list: [],
        dateType: "本年",
        "content": {
            type: 1, // 0全部 1本年 2本季 3本月
            "nocache": true,
            "pageNumber": 1,
            "pageTotal": 1,
            "total": null,
            "where": {
                begdate: "",
                enddate: "",
            }
        }
    },
    methods: {
        getList(id, init) {
            let content = this.data.content;
            content.hrid = id;
            if (init) {
                content.pageNumber = 1;
                content.total = null;
            }
            if (content.pageNumber > content.pageTotal) return;
            _Http.basic({
                "id": 20230717100404,
                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(item => {
                    const num = wx.getStorageSync('languagecode') == 'ZH' ? 10000 : 1000;
                    item.target_l = CNY(item.target_l / num)
                    item.amount = CNY(item.amount / num)
                    item.outamount = CNY(item.outamount / num)
                    item.writeoffamount = CNY(item.writeoffamount / num)
                    item.invoiceamount = CNY(item.invoiceamount / num)
                    return item
                })

                this.setData({
                    list: res.pageNumber == 1 ? res.data : this.data.list.concat(res.data),
                    content,
                    id: id,
                });
                this.selectComponent("#TimeRange").onCancel()
            })
        },
        changeDate({
            detail
        }) {
            let type = 99;
            if (detail.dateType == "本年") {
                type = 1
            } else if (detail.dateType == "本季") {
                type = 2
            } else if (detail.dateType == "本月") {
                type = 3
            } else if (detail.dateType == "全部") {
                type = 0
            }
            this.setData({
                "dateType": detail.dateType,
                "content.type": type,
                "content.where.begdate": detail.begdate || "",
                "content.where.enddate": detail.enddate || ""
            })
            this.getList(this.data.id, true)
        },
        viewInstructions() {
            getApp().globalData.Language.modeBoxPrompts('定义:每次平均跟进天数;若没有跟进次数，则跟进频率-0天/次;若有跟进次数，则按跟进频率=(首次跟进时间到当前时间的天数-节假日天数)÷跟进次数')
        }
    }
})