const _Http = getApp().globalData.http;
import Dialog from '@vant/weapp/dialog/dialog';
Page({
    data: {
        active: "群组",
        allGroup: [],
        defaultGroup: [],
        myGroup: [],
        updateGroup: {
            "sys_phonebookgroupid": 0,
            "groupname": ""
        }
    },
    onLoad(options) {
        getApp().globalData.Language.getLanguagePackage(this, '联系人')
        this.setData({
            active: "联系人"
        })
        this.getList(true);
        this.getGroup(true);
        _Http.getGroup = this.getGroup.bind(this);
    },
    onInput(e) {
        this.data.updateGroup.groupname = e.detail.value;
    },
    deleteGroup(e) {
        let {
            item
        } = e.currentTarget.dataset,
            that = this;
        wx.showModal({
            title: getApp().globalData.Language.getMapText('提示'),
            content: getApp().globalData.Language.getMapText('是否确定删除') + `“${item.groupname}”`,
            cancelText: getApp().globalData.Language.getMapText('取消'),
            confirmText: getApp().globalData.Language.getMapText('确定'),
            complete: ({
                confirm
            }) => {
                if (confirm) _Http.basic({
                    "id": "20220831164403",
                    "version": 1,
                    "content": {
                        "sys_phonebookgroupid": item.sys_phonebookgroupid
                    }
                }).then(res => {
                    console.log("删除群组", res)
                    wx.showToast({
                        title: res.code != '1' ? res.msg : getApp().globalData.Language.getMapText('删除成功'),
                        icon: "none"
                    })
                    if (res.code == '1') that.setData({
                        myGroup: that.data.myGroup.filter(v => v.sys_phonebookgroupid != item.sys_phonebookgroupid)
                    })
                })
            }
        })

    },
    editGroup(e) {
        this.setData({
            updateGroup: e.currentTarget.dataset.item
        })
        this.showDialog();
    },
    insertGroup() {
        this.setData({
            updateGroup: {
                "sys_phonebookgroupid": 0,
                "groupname": ""
            }
        });
        this.showDialog();
    },
    showDialog() {
        Dialog.confirm({
            beforeClose: (action) =>
                new Promise((resolve) => {
                    if (action === 'confirm') {
                        let content = this.data.updateGroup;
                        if (content.groupname == "") {
                            wx.showToast({
                                title: getApp().globalData.Language.getMapText('群组名称不可为空'),
                                icon: "none"
                            })
                            resolve(false);
                        } else {
                            _Http.basic({
                                "id": "20220831164203",
                                "version": 1,
                                content
                            }).then(res => {
                                let operate = content.sys_phonebookgroupid == 0 ? "新建" : '编辑';
                                console.log(operate + '群组', res)
                                if (res.code != '1') {
                                    wx.showToast({
                                        title: res.msg,
                                        icon: "none"
                                    })
                                    resolve(false);
                                } else {
                                    this.getGroup(true).then(() => {
                                        resolve(true)
                                        wx.showToast({
                                            title: operate + getApp().globalData.Language.getMapText('成功'),
                                            icon: "none"
                                        })
                                    })
                                }
                            })
                        }
                    } else {
                        resolve(true);
                    }
                })
        });
    },
    onChange(e) {
        const active = e.detail.name;
        if (active == this.data.active) return;
        this.setData({
            active
        });
        this.updatedb(active == '群组' && this.data.defaultGroup.length == 0)
    },
    async updatedb(init = false) {
        if (init.detail != undefined) init = init.detail;
        await this[this.data.active == '联系人' ? 'getList' : 'getGroup'](init)
        this.selectComponent('#ListBox').RefreshToComplete();
    },
    getList(init) {
        return this.selectComponent("#list").getList(init);
    },
    getGroup(init) {
        return new Promise((resolve) => {
            if (!init) return resolve();
            _Http.basic({
                "id": "20220831164303",
                "content": {
                    nocache: true,
                    type: 1,
                    pageSize: 9999,
                    where: {
                        condition: ""
                    }
                }
            }).then(res => {
                resolve();
                console.log("群组", res)
                if (res.code != '1') return wx.showToast({
                    title: res.msg,
                    icon: "none"
                });
                let myGroup = [],
                    defaultGroup = [];
                myGroup = res.data.filter(v => {
                    if (v.sys_phonebookgroupid == 0) defaultGroup.push(v)
                    return v.sys_phonebookgroupid
                })
                this.setData({
                    defaultGroup,
                    myGroup
                })
                let page = this.selectComponent("#list");
                if (page) {
                    let index = page.data.filtratelist.findIndex(v => v.label == '群组');

                    if (index == -1) {
                        page.data.filtratelist.unshift({
                            label: "群组",
                            index: null,
                            showName: "groupname", //显示字段
                            valueKey: "group", //传参 代表选着字段 不传参返回整个选择对象
                            value: "", //选中值
                            list: res.data
                        });
                    } else {
                        page.data.filtratelist[index].list = res.data;
                    }

                    page.setData({
                        filtratelist: page.data.filtratelist
                    })
                }
            })
        })
    },
    onReady() {
        this.selectComponent("#ListBox").setHeight(".linear", this);
    },
    onUnload() {
        delete(_Http.updateList)
        delete(_Http.getGroup)
    },
})