const _Http = getApp().globalData.http;
Component({
    options: {
        addGlobalClass: true
    },
    lifetimes: {
        attached: function () {
            getApp().globalData.Language.getLanguagePackage(this)
            let dateTypes = [];
            if (this.data.mode == "联系人") {
                dateTypes = ["全部", "本年", "本季", "本月"]
            } else {
                dateTypes = ["全部", "本年"]
            }
            this.setData({
                dateTypes
            })
        }
    },
    properties: {
        mode: {
            type: String
        }
    },
    data: {
        dateTypes: [],
        showList: false,
        list: [],
        dateType: "本年", //联系人用
        "isAll": 0, //客户和项目用 1全部 0本年 99时间范围
        "content": {
            "nocache": true,
            "pageNumber": 1,
            "pageTotal": 1,
            "total": null,
            "where": {
                begdate: "",
                enddate: "",
            }
        }
    },
    methods: {
        getList(id, init) {
            let content = this.data.content;
            if (init) {
                content.pageNumber = 1;
                content.total = null;
            }
            if (!this.data.showList && content.total != null) return;
            if (content.pageNumber > content.pageTotal) return;
            const types = {
                    联系人: {
                        id: 20240605110904,
                        idName: "sys_phonebookid"
                    },
                    客户: {
                        id: 20230713103904,
                        idName: "sa_customersid"
                    },
                    项目: {
                        id: 20230715111804,
                        idName: "sa_projectid"
                    }
                },
                mode = this.data.mode;
            content[types[mode].idName] = id;
            if (mode == "联系人") {
                content.dateType = this.data.dateType;
            } else {
                content.isAll = this.data.isAll;
            }
            _Http.basic({
                "id": types[mode].id,
                content
            }).then(res => {
                console.log(this.data.mode + "跟进", 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;
                try {
                    res.data = res.data.map(item => {
                        try {
                            if (!item.followname.length) item.followname = ' --'
                        } catch (error) {
                            item.followname = ' --'
                        }
                        item.cons = item.content.split('；').filter(v => v).map(v => {
                            let arr = v.split('：')
                            return {
                                label: arr[0] || "--",
                                value: arr[1] || "--"
                            }
                        })
                        return item
                    })
                } catch (error) {
                    console.log("error", error)
                }
                this.setData({
                    list: res.pageNumber == 1 ? res.data : this.data.list.concat(res.data),
                    content,
                    id: id,
                });
                this.selectComponent("#TimeRange").onCancel()
            })
        },
        changeDate({
            detail
        }) {
            console.log(detail)
            this.setData({
                dateType: detail.dateType,
                isAll: detail.isAll,
                "content.where.begdate": detail.begdate || "",
                "content.where.enddate": detail.enddate || ""
            })
            this.getList(this.data.id, true)
        },
        shrinkChange({
            detail
        }) {
            this.setData({
                showList: detail
            })
        },
        viewInstructions() {
            getApp().globalData.Language.modeBoxPrompts('定义:每次平均跟进天数;若没有跟进次数，则跟进频率-0天/次;若有跟进次数，则按跟进频率=(首次跟进时间到当前时间的天数-节假日天数)÷跟进次数')
        }
    }
})