const _Http = getApp().globalData.http;
Component({
    options: {
        addGlobalClass: true
    },
    data: {
        sys_enterpriseid: 0,
        list: [],
        radio: 0, //默认
        content: {
            "nacache": true,
            "pageNumber": 1,
            "pageSize": 10,
            "pageTotal": 1,
            "total": null,
            "where": {
                "condition": ""
            }
        }
    },
    methods: {
        /* 获取地址列表 */
        getList(id, init) {
            let content = this.data.content;
            content.sys_enterpriseid = id;
            if (init) {
                content.pageNumber = 1
                content.pageTotal = 1
            }
            _Http.basic({
                "id": "20221018153302",
                content
            }).then(res => {
                console.log("地址列表", res)
                if (res.msg != '成功') return wx.showToast({
                    title: res.data,
                    icon: "none"
                })
                this.setData({
                    list: res.pageNumber == 1 ? res.data : this.data.list.concat(res.data),
                    "content.pageNumber": res.pageNumber + 1,
                    "content.pageSize": res.pageSize,
                    "content.pageTotal": res.pageTotal,
                    "content.total": res.total,
                    sys_enterpriseid: id
                })
                let index = this.data.list.findIndex(v => v.isdefault == 1);
                this.setData({
                    radio: index != -1 ? res.data[index].contactsid : 0
                })
            })
        },
        // 修改默认
        radioChange({
            detail
        }) {
            let list = this.data.list,
                index = list.findIndex(v => v.contactsid == detail)
            _Http.basic({
                "id": "20221018144702",
                "content": {
                    "contactsid": list[index].contactsid,
                    "sys_enterpriseid": list[index].sys_enterpriseid
                }
            }, false).then(res => {
                if (res.msg != '成功') return wx.showToast({
                    title: res.data,
                    icon: "none"
                });
                list = list.map((v, i) => {
                    v.isdefault = i == index ? 1 : 0
                    return v
                })
                this.setData({
                    list
                })
                console.log(list)
            })
        },
        //处理操作
        handleItem(e) {
            const {
                name,
                item
            } = e.target.dataset,
                that = this;
            if (!name) return;
            switch (name) {
                case 'copy':
                    let str = `收货人：${item.name}\n手机号码：${item.phonenumber}\n所在地区：${item.province+item.city+item.county}\n详细地址：${item.province+item.city+item.county+item.address}`
                    wx.setClipboardData({
                        data: str,
                    })
                    break;
                case 'edit':
                    wx.navigateTo({
                        url: '/packageA/setclient/modules/address/add/index?data=' + JSON.stringify(item),
                    })
                    break;
                case 'delete':
                    wx.showModal({
                        title: '提示',
                        content: `是否确认删除"${item.name}"`,
                        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.msg != '成功') return wx.showToast({
                                        title: res.data,
                                        icon: "none"
                                    });
                                    that.setData({
                                        list: this.data.list.filter(v => v.contactsid != item.contactsid)
                                    });
                                    wx.showToast({
                                        title: `删除成功！`,
                                        icon: "none"
                                    })
                                })
                            }
                        }
                    })
                    break;
            }
        }
    }
})