const _Http = getApp().globalData.http;
Component({
    options: {
        addGlobalClass: true
    },
    properties: {
        list: {
            type: Array
        },
        changeTotal: {
            type: Function
        },
        disabled: {
            type: Boolean
        },
        type: {
            type: String
        }
    },
    data: {
        radio: 0, //默认
    },
    lifetimes: {
        attached: function () {
            getApp().globalData.Language.getLanguagePackage(this)
        }
    },
    methods: {
        // 修改默认
        radioChange({
            detail
        }) {
            let list = this.data.list,
                data = list.find(v => v.contactsid == detail)
            _Http.basic({
                "id": "20221018144702",
                "content": {
                    "contactsid": data.contactsid,
                    "sys_enterpriseid": data.sys_enterpriseid
                }
            }, false).then(res => {
                if (res.code != '1') return wx.showToast({
                    title: res.data,
                    icon: "none"
                });

                getCurrentPages().forEach(v => {
                    //更新列表
                    if (["prsx/hospital/detail"].includes(v.__route__)) {
                        let page = v.selectComponent("#Address").selectComponent("#List");
                        const lists = page.data.list.map(value => {
                            value.isdefault = value.contactsid == data.contactsid ? 1 : 0
                            return value
                        });
                        page.setData({
                            list: lists
                        })
                        setTimeout(() => {
                            page.initRadio();
                        }, 200)
                    };
                })
            })
        }, //处理操作
        handleItem(e) {
            const {
                name,
                item
            } = e.target.dataset,
                that = this;
            if (!name) return;
            switch (name) {
                case 'call':
                    wx.makePhoneCall({
                        phoneNumber: item.phonenumber,
                    })
                    break;
                case 'copy':
                    let str = `${getApp().globalData.Language.getMapText('联系人')}：${item.name}\n${getApp().globalData.Language.getMapText('手机号码')}：${item.phonenumber}\n${getApp().globalData.Language.getMapText('所在地区')}：${item.province+item.city+item.county}\n${getApp().globalData.Language.getMapText('详细地址')}：${item.province+item.city+item.county+item.address}`
                    wx.setClipboardData({
                        data: str,
                    })
                    break;
                case 'edit':
                    wx.navigateTo({
                        url: '/prsx/hospital/address/add/index?data=' + JSON.stringify(item) + '&type=' + this.data.type,
                    })
                    break;
                case 'delete':
                    wx.showModal({
                        title: getApp().globalData.Language.getMapText('提示'),
                        content: getApp().globalData.Language.getMapText('是否确认删除') + `"${item.name}"`,
                        cancelText: getApp().globalData.Language.getMapText('取消'),
                        confirmText: getApp().globalData.Language.getMapText('确定'),
                        complete: (res) => {
                            if (res.confirm) {
                                _Http.basic({
                                    "id": "20221018145502",
                                    "content": {
                                        "contactsid": item.contactsid,
                                        "sys_enterpriseid": item.sys_enterpriseid
                                    }
                                }).then(res => {
                                    console.log("删除地址", res)
                                    if (res.code != '1') return wx.showToast({
                                        title: res.data,
                                        icon: "none"
                                    });
                                    wx.showToast({
                                        title: getApp().globalData.Language.getMapText('删除成功') + `！`,
                                        icon: "none"
                                    });
                                    getCurrentPages().forEach(v => {
                                        if (v.__route__ == "packageA/setclient/detail" || v.__route__ == "packageA/opponent/detail" || v.__route__ == "packageA/publicCustomer/detail") {
                                            let model = v.selectComponent("#Address");
                                            model.setData({
                                                list: model.data.list.filter(value => value.contactsid != item.contactsid)
                                            });
                                            model.changeTotal();
                                        } else if (v.__route__ == "packageA/setclient/modules/address/search/index") {
                                            v.setData({
                                                list: v.data.list.filter(value => value.contactsid != item.contactsid)
                                            });
                                        }
                                    })
                                })
                            }
                        }
                    })
                    break;
            }
        },
        initRadio() {
            let data = this.data.list.find(v => v.isdefault == 1);
            this.setData({
                radio: data ? data.contactsid : 0
            })
        },
    }
})