const _Http = getApp().globalData.http;
Component({
    options: {
        addGlobalClass: true
    },
    properties: {
        list: {
            type: Array
        },
        changeTotal: {
            type: Function
        },
        disabled: {
            type: Boolean
        }
    },
    lifetimes: {
        attached: function () {
            getApp().globalData.Language.getLanguagePackage(this)
        }
    },
    data: {
        range: ['高层', '中层', '项目负责人']
    },
    methods: {
        handleItem(e) {
            const {
                name,
                item
            } = e.target.dataset,
                that = this;
            console.log(item)
            switch (name) {
                case "call":
                    if (!item.phonenumber) return getApp().globalData.Language.showToast('该用户联系方式为空')
                    wx.makePhoneCall({
                        phoneNumber: item.phonenumber,
                    })
                    break;
                case "delete":
                    wx.showModal({
                        title: '提示',
                        content: `是否确认删除"${item.name}"`,
                        complete: (res) => {
                            if (res.confirm) _Http.basic({
                                "id": "20240923153104",
                                "content": {
                                    "sa_contractid": item.sa_contractid,
                                    "sys_phonebookid": item.sys_phonebookid
                                }
                            }).then(res => {
                                console.log("删除联系人", res)
                                getApp().globalData.Language.showToast(res.code == 1 ? `删除成功！` : res.data)
                                getCurrentPages().forEach(v => {
                                    if (v.selectComponent("#Contacts")) {
                                        const page = v.selectComponent("#Contacts");
                                        if (page.data.list.some(v => v.sys_phonebookid == item.sys_phonebookid)) {
                                            page.setData({
                                                list: page.data.list.filter(v => v.sys_phonebookid != item.sys_phonebookid)
                                            });
                                            page.changeTotal();
                                        }
                                    }
                                })
                            })
                        }
                    })
                    break;
                case "edit":
                    wx.navigateTo({
                        url: `/packageA/project/modules/contacts/add/index?data=${JSON.stringify(item)}`
                    })
                    break;
            }
        },
        bindPickerChange(e) {
            let item = e.currentTarget.dataset.item,
                tag = this.data.range[e.detail.value];
            if (item.tag == tag) return;
            _Http.basic({
                "id": 20221119103902,
                "content": {
                    "sa_project_contactsid": item.sa_project_contactsid,
                    tag,
                },
            }).then(res => {
                console.log("修改关联医院类型", res)
                if (res.code != '1') return wx.showToast({
                    title: res.msg,
                    icon: "none"
                });
                this.setData({
                    [`list[${this.data.list.findIndex(v=>v.sa_project_contactsid==item.sa_project_contactsid)}].tag[0]`]: tag
                });
                wx.showToast({
                    title: getApp().globalData.Language.joint([{
                        t: 1,
                        v: '已更改',
                    }, {
                        v: item.name,
                        f: "“",
                        r: "”"
                    }, {
                        t: 1,
                        v: '联系人类型为',
                    }, {
                        t: 1,
                        v: tag,
                        f: "“",
                        r: "”"
                    }]),
                    icon: "none"
                })
            })
        },
    }
})