const _Http = getApp().globalData.http;
Component({
    options: {
        addGlobalClass: true
    },
    properties: {
        groupname: {
            type: String,
            value: ""
        },
        sys_phonebookgroupid: {
            type: [String, Number],
            value: ''
        }
    },
    data: {
        list: [],
        "content": {
            nocache: true,
            "type": 1,
            "pageNumber": 1,
            "pageTotal": 1,
            "pageSize": 20,
            "where": {
                "condition": ""
            }
        },
        navList: [{
            label: "我负责的",
            icon: "icon-webxialaxuanxiangjiantou",
            color: "",
            width: "",
            id: "1"
        }, {
            label: "筛选",
            icon: "icon-shaixuan",
            color: "",
            width: "",
            id: "2"
        }],
        classShow: false,
        classActions: [],
        filtratelist: [{
            label: "标签",
            index: null,
            type: "checkbox",
            showName: "tag", //显示字段
            valueKey: "tag", //返回Key
            selectKey: "tag", //传参 代表选着字段 不传参返回整个选择对象
            value: "", //选中值
            list: [{
                tag: "关键决策人"
            }, {
                tag: "反对者"
            }, {
                tag: "支持者"
            }]
        }, {
            label: "关联",
            index: null,
            showName: "value", //显示字段
            valueKey: "contactstype",
            selectKey: "value", //传参 代表选着字段 不传参返回整个选择对象
            value: "", //选中值
            list: [{
                value: "关联客户"
            }, {
                value: "关联项目"
            }]
        }],
        total: 0
    },
    lifetimes: {
        attached: function () {
            getApp().globalData.Language.getLanguagePackage(this)
        }
    },
    methods: {
        getList(init = false) {
            if (init.detail != undefined) init = init.detail;
            let content = this.data.content;
            if (init) {
                content.pageNumber = 1;
                content.pageTotal = 1;
            }
            if (content.pageNumber > content.pageTotal) return;
            content.groupname = this.data.groupname;
            content.sys_phonebookgroupid = this.data.sys_phonebookgroupid;
            if (this.data.classActions.length == 0) {
                let templetList = wx.getStorageSync('templetList');
                if (wx.getStorageSync('userMsg').usertype != 0) templetList = templetList.filter(v => v.templetid != '99')
                this.setData({
                    classActions: templetList.map((v, i) => {
                        return {
                            name: v.templetname,
                            index: v.templetid,
                            color: i == 0 ? '#3874F6' : '',
                            i: i,
                        }
                    }),
                    'navList[0].label': templetList[0].templetname
                });
                content.type = templetList[0].templetid;
                this.selectComponent("#ListBox").setHeight(".global-total", this);
            }
            return new Promise((resolve) => {
                _Http.basic({
                    "id": 20231220085804,
                    content
                }).then(res => {
                    console.log("联系人列表", res)
                    this.selectComponent('#ListBox').RefreshToComplete();
                    resolve()
                    if (res.code != '1') return wx.showToast({
                        title: res.msg,
                        icon: "none"
                    })
                    content.pageTotal = res.pageTotal;
                    content.pageNumber = res.pageNumber + 1;
                    this.setData({
                        content,
                        list: res.pageNumber == 1 ? res.data : this.data.list.concat(res.data),
                        total: res.total
                    })
                })
            })
        },
        insertRows() {
            wx.navigateTo({
                url: `/packageA/contacts/insert?id=${this.data.sys_phonebookgroupid||0}&name=${this.data.groupname||'默认群组'}`,
            });
            this.updateList()
        },
        toDetail(e) {
            let {
                sys_phonebookid,
                groupname
            } = e.currentTarget.dataset.item;
            console.log("groupname", groupname)
            wx.navigateTo({
                url: `/packageA/contacts/detail?id=${sys_phonebookid||0}&groupname=${groupname||'默认分组'}`,
            });
            this.updateList()
        },
        updateList() {
            _Http.updateList = () => {
                let content = JSON.parse(JSON.stringify(this.data.content));
                content.pageSize = (content.pageNumber - 1) * (content.pageSize || 20);
                console.log(content.pageSize)
                content.pageNumber = 1;
                _Http.basic({
                    "id": 20231220085804,
                    content
                }).then(res => {
                    console.log("更新联系人列表", res)
                    this.setData({
                        list: res.data,
                        total: res.total
                    })
                })
            }
        },
        classSelect({
            detail
        }) {
            if (this.data.content.type == detail.index) return this.classClose();
            this.setData({
                "content.type": detail.index,
                'navList[0].label': detail.name,
                classActions: this.data.classActions.map(v => {
                    v.color = detail.i == v.i ? '#3874F6' : ''
                    return v
                })
            })
            this.classClose();
            this.getList(true)
        },
        classClose() {
            this.setData({
                classShow: false
            })
        },
        /* 顶部条件导航回调 */
        navClick({
            detail
        }) {
            switch (detail.id) {
                case '1':
                    this.setData({
                        classShow: true
                    })
                    break;
                case '2':
                    this.setData({
                        'filterShow': true
                    })
                    break;
            }
        },
        onSearch({
            detail
        }) {
            this.data.content.where.condition = detail;
            this.getList(true)
        },
        handleFilter({
            detail
        }) {
            console.log("筛选项", detail)
            detail.condition = this.data.content.where.condition;
            if (getCurrentPages()[getCurrentPages().length - 1].__route__ == 'packageA/contacts/index') {
                if (detail.group) {
                    this.data.groupname = detail.group.groupname;
                    this.data.sys_phonebookgroupid = detail.group.sys_phonebookgroupid;
                } else {
                    this.data.groupname = "";
                    this.data.sys_phonebookgroupid = '';
                }
                delete(detail.group)
            }
            this.data.content.where = detail;
            this.getList(true);
        }
    }
})