const _Http = getApp().globalData.http;
Component({
    options: {
        addGlobalClass: true,
    },
    properties: {
        update: {
            type: Function
        }
    },
    lifetimes: {
        attached: function () {
            getApp().globalData.Language.getLanguagePackage(this)
            this.data.organization = this.selectComponent("#organization");
            this.data.organization.initDepAndUser().then(active => {
                let userid = active.userid || wx.getStorageSync("userMsg").userid;
                this.setData({
                    "content.dataid": userid,
                    userid,
                })
                this.triggerEvent('update', "Contacts")
                this.setFiltratelist();
            });

        }
    },
    data: {
        list: [],
        total: null,
        showFiltrate: false,
        searchShow: false,
        searchValue: "",
        filtratelist: [{
            label: "状态",
            index: 1,
            default: 1,
            showName: "label", //显示字段
            valueKey: "isleave", //返回Key
            selectKey: "isleave", //传参 代表选着字段 不传参返回整个选择对象
            value: 1, //选中值
            list: [{
                label: "全部",
                isleave: 0
            }, {
                label: "在职",
                isleave: 1
            }, {
                label: "离职",
                isleave: 2
            }]
        }],
        "content": {
            "type": 0,
            "pageNumber": 1,
            "pageSize": 20,
            "where": {
                "condition": "",
                "begdate": "", //开始时间
                "enddate": "", //结束时间
                "tag": [], //标签
                "isleave": 1 //0全部 1在职 2离职
            }
        }
    },
    methods: {
        getList(init = false) {
            _Http.init(this.data.content, init).then(content => {
                _Http.basic({
                    "id": 20240603162604,
                    content
                }).then(res => {
                    console.log("联系人画像", res)
                    this.selectComponent('#ListBox').automaticSetHei();
                    this.selectComponent('#ListBox').RefreshToComplete();
                    if (res.code != '1') return wx.showToast({
                        title: res.data,
                        icon: "none"
                    })
                    this.setData({
                        list: res.pageNumber == 1 ? res.data : this.data.list.concat(res.data),
                        "content.pageNumber": res.pageNumber + 1,
                        "content.pageSize": res.pageSize,
                        "content.pageTotal": res.pageTotal,
                        "total": res.total,
                    })
                })
            })
        },
        setFiltratelist() {
            _Http.basic({
                "id": 20220929085401,
                "content": {
                    "ownertable": "sys_phonebook",
                    "ownerid": "0"
                },
            }).then(res => {
                console.log("查询标签", res)
                if (res.code != 1) return;
                this.data.filtratelist.unshift({
                    label: "标签",
                    index: null,
                    type: "checkbox",
                    showName: "tag", //显示字段
                    valueKey: "tag", //返回Key
                    selectKey: "tag", //传参 代表选着字段 不传参返回整个选择对象
                    value: "", //选中值
                    list: res.data.option
                });
                this.setData({
                    filtratelist: this.data.filtratelist
                })
            })
        },
        handleFilter({
            detail
        }) {
            if (!detail.name) return;
            let content = this.data.content,
                organization = this.data.organization;
            if (detail.name == "reset") {
                organization.setData({
                    isleave: 1
                })
                organization.initDepAndUser()
                content.dataid = this.data.userid || 0;
                content.where = {
                    "condition": content.where.condition || '',
                    "tag": [],
                    "begdate": "",
                    "enddate": "",
                    isleave: 1
                }
            } else if (detail.name == "confirm") {
                const res = organization.data.result
                content.type = res.sa_saleareaid ? 1 : 0;
                content.dataid = res.sa_saleareaid || res.userid;
                content.where = {
                    "condition": content.where.condition || '',
                    "tag": detail.tag || [],
                    "begdate": detail.startdate || '',
                    "enddate": detail.enddate || '',
                    isleave: detail.isleave
                }
            }
            this.setData({
                content
            });
            this.getList(true);
        },
        changeSearchShow() {
            this.setData({
                searchShow: !this.data.searchShow
            })
            setTimeout(() => {
                this.selectComponent('#ListBox').automaticSetHei();
                if (this.data.searchShow) {
                    if (this.selectComponent("#Yl_Filtrate1").data.show) return
                    this.setData({
                        focus: true
                    })
                } else {
                    this.setData({
                        focus: false
                    })
                }
            }, 350)
        },
        onSearch() {
            this.data.content.where.condition = this.data.searchValue;
            this.getList(true);
        },
        onChange(event) {
            this.setData({
                searchValue: event.detail
            })
        },
        onClear() {
            this.setData({
                searchValue: "",
                "content.where.condition": ""
            })
            this.getList(true);
        },
    }
})