// @ts-nocheck
import { request } from '../../../utils/request'
import { locationItemType } from './location-item'
Component({
    properties: {
        item: {} as locationItemType,
        selectLocation: {
            type: Boolean,
            value: true
        }
    },
    data: {
        locationStr: ''
    },
    observers: {
        'item.location': function (location: string[]) {
            if (location && Array.isArray(location)) {
                this.setData({
                    locationStr: location.join('   ')
                })
            }
        },
    },
    methods: {
        handleEdit() {
            console.log("this.data.item", this.data.item)
            this.triggerEvent('edit', { item: this.data.item })
        },
        handleDelete() {
            wx.showModal({
                title: '地址设置',
                content: '要将此收货地址删除吗?',
                success: async (res) => {
                    if (res.confirm) {
                        wx.showLoading({ title: "加载中" })
                        try {
                            await request({
                                options: {
                                    url: "/app/member-address/remove",
                                    data: {
                                        // mbuId: Number(wx.getStorageSync("mbuId")),
                                        id: this.data.item.id
                                    }
                                }
                            }).then(() => {
                                wx.showToast({ title: "删除成功" })
                                if (this.data.item.defaultStatus) {
                                    wx.removeStorageSync("addressId");
                                    wx.removeStorageSync("defualtAddress");
                                }
                                this.triggerEvent("refresh")
                            })
                        } catch {

                        }
                    }
                }
            })
        },
        handleSelect() {
            // console.log(this.data.item)
            if (this.data.selectLocation) {
                wx.showModal({
                    title: '地址设置',
                    content: '要选择此收货地址吗?',
                    success: (res) => {
                        if (res.confirm) {
                            this.triggerEvent("select", {
                                id: this.data.item.id,
                                item: this.data.item
                            });
                        }
                    },
                    // success: async (res) => {
                    //     if (res.confirm) {
                    //         const value: any = {
                    //             location: this.data.item.location,
                    //             address: this.data.item.address,
                    //             mobile: this.data.item.mobile,
                    //             name: this.data.item.name,
                    //             id: this.data.item.id,
                    //             defaultStatus: 1,
                    //             mbuId: wx.getStorageSync('mbuId')
                    //         }
                    //         try {
                    //             await request<any[]>({
                    //                 options: {
                    //                     url: "/app/member-address/editor",
                    //                     data: value
                    //                 }, loadingTitle: "保存中"
                    //             })
                    //                 .then(() => {
                    //                     // wx.showToast({ title: "设置地址成功！", duration: 1000 })
                    //                     wx.setStorage({ key: "addressId", data: this.data.item.id })
                    //                     if (this.data.item.location) wx.setStorage({ key: "defualtAddress", data: `${this.data.item.location.join("")}${this.data.item.address}` })
                    //                     this.triggerEvent("refresh")
                    //                 }).then(() => {
                    //                     wx.navigateBack()
                    //                 })
                    //         } catch (e) {
                    //             console.log(e, '出现错误')
                    //         }
                    //     } else if (res.cancel) {
                    //         console.log('用户点击了取消')
                    //     }
                    // },
                    fail: (e) => {
                        console.log(e, '错误')
                    }
                })
            }
        },
        stopTap() { },//阻止点击穿透
        handleSwitch(e) {
            const checked = e.detail;

            if (!checked) {
                wx.showToast({ title: "不能取消默认地址,请切换到其他默认地址~", icon: "none" });
                return;
            }

            wx.showModal({
                title: '地址设置',
                content: '要将此地址设置为默认收货地址吗?',
                success: async (res) => {
                    if (res.confirm) {
                        const value = {
                            location: this.data.item.location,
                            address: this.data.item.address,
                            mobile: this.data.item.mobile,
                            name: this.data.item.name,
                            id: this.data.item.id,
                            defaultStatus: 1,
                            mbuId: wx.getStorageSync('mbuId')
                        };

                        await request({
                            options: {
                                url: "/app/member-address/editor",
                                data: value
                            },
                            loadingTitle: "保存中"
                        });

                        wx.setStorageSync("addressId", this.data.item.id);
                        if (this.data.item.location) {
                            wx.setStorageSync(
                                "defualtAddress",
                                `${this.data.item.location.join("")}${this.data.item.address}`
                            );
                        }

                        this.triggerEvent("refresh");
                    }
                }
            });
        },

    }
})
