// packageA/contacts/index.js
let dowmCount = null,
    _Http = getApp().globalData.http,
    dMark = require('../../utils/deleteMark')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        systemGroup: [], //系统分组
        myGroup: [], //自定义分组
        groupName: '',
        sys_phonebookgroupid: 0,
        dialogShow: false
    },
    onLoad(options) {
        this.getGroup();
    },
    toAddContacts() {
        wx.navigateTo({
            url: './edit',
        })
    },
    /* 获取分组 */
    getGroup(condition = '') {
        return _Http.basic({
            "id": "20220831164603",
            "version": 1,
            "content": {
                "nocache": true,
                "where": {
                    condition
                }
            }
        }).then(res => {
            if (res.msg != '成功') return wx.showToast({
                title: res.msg,
                icon: "none"
            })
            console.log(res.data)
            if (condition != '') return res.data;
            let systemGroup = [],
                myGroup = [];
            res.data.forEach(v => ['默认群组', '客户联系人', '项目联系人'].includes(v.groupname) ? systemGroup.push(v) : myGroup.push(v));
            this.setData({
                systemGroup,
                myGroup
            })
        })
    },
    toCheckList(e) {
        const {
            item
        } = e.currentTarget.dataset;
        wx.navigateTo({
            url: './list?item=' + JSON.stringify(item),
        })
    },
    /* 删除自定义分组 */
    deleteGroup(e) {
        const {
            item
        } = e.currentTarget.dataset,
            that = this;
        if (item.count) return wx.showToast({
            title: '当前状态不可删除',
            icon: "none"
        });
        wx.showModal({
            title: "提示",
            content: `是否确认删除 "${item.groupname}"`,
            success({
                confirm
            }) {
                if (confirm) _Http.basic({
                    "id": "20220831164403",
                    "version": 1,
                    "content": {
                        "sys_phonebookgroupid": item.sys_phonebookgroupid
                    }
                }).then(res => {
                    if (res.msg != '成功') return wx.showToast({
                        title: res.msg,
                        icon: "none"
                    })
                    const myGroup = that.data.myGroup.filter(v => v.sys_phonebookgroupid != item.sys_phonebookgroupid);
                    that.setData({
                        myGroup
                    })
                    wx.showToast({
                      title: '删除成功',
                    })
                })
            }
        })

    },
    /* 开始搜索 */
    startSearch({
        detail
    }) {
        this.getGroup(detail.trim()).then(res => {
            let item = {
                groupname: "搜索",
                phonebook: [],
                sys_phonebookgroupid: 0
            };
            res.forEach(v => {
                if (v.phonebook.length != 0) {
                    item.phonebook = item.phonebook.concat(v.phonebook)
                }
            });
            if (item.phonebook.length == 0) {
                wx.showToast({
                    title: `未找到与'${detail.trim()}'有关内容`,
                    icon: "none"
                })
            } else {
                wx.navigateTo({
                    url: './list?item=' + JSON.stringify(item),
                })
            }
        })
    },
    onClose(event) {
        const {
            instance
        } = event.detail;
        instance.close();
    },
    openDialog(e) {
        console.log(e)
        if (e.currentTarget.dataset.item) {
            this.setData({
                dialogShow: true,
                groupName: e.currentTarget.dataset.item.groupname,
                sys_phonebookgroupid: e.currentTarget.dataset.item.sys_phonebookgroupid
            })
        } else {
            this.setData({
                dialogShow: true,
                groupName: "",
                sys_phonebookgroupid: 0
            })
        }
    },
    inputName({
        detail
    }) {
        this.setData({
            groupName: dMark.queryStr(detail.value)
        })
    },
    handleAddGroup() {
        let name = this.data.groupName.trim()
        if (name.length == 0) return wx.showToast({
            title: '群组名称不可为空',
            icon: "none"
        })
        _Http.basic({
            "id": "20220831164203",
            "version": 1,
            "content": {
                "sys_phonebookgroupid": this.data.sys_phonebookgroupid,
                "groupname": name
            }
        }).then(res => {
            if (res.msg != '成功') {
                wx.showToast({
                    title: res.data,
                    icon: "none"
                })
            };
            this.getGroup();
            wx.showToast({
                title: this.data.sys_phonebookgroupid == 0 ? '添加成功' : '修改成功',
            })
            this.setData({
                groupName: "",
                sys_phonebookgroupid: 0
            })
        })
    },
    onCancel() {
        this.setData({
            dialogShow: false
        })
    },
    onShareAppMessage() {

    }
})