const _Http = getApp().globalData.http;
Page({
    data: {
        form: [{
            label: "联系人",
            error: false,
            errMsg: "",
            type: "text",
            value: "",
            placeholder: "请填写",
            valueName: "name",
            required: true,
            checking: "base"
        }, {
            label: "手机号",
            error: false,
            errMsg: "",
            type: "number",
            value: "",
            placeholder: "收货人手机号码",
            valueName: "phonenumber",
            required: false,
            checking: "phone"
        }, {
            label: "地区",
            error: false,
            errMsg: "",
            type: "region",
            value: [],
            placeholder: "省,市,区",
            valueName: "region",
            required: false
        }, {
            label: "详细地址",
            error: false,
            errMsg: "",
            type: "textarea",
            value: "",
            placeholder: "例: 科创园11栋1103室",
            valueName: "address",
            required: false,
            checking: "base"
        }],
        disabled: true,
        "content": {
            "contactsid": 0, //地址id
            "sys_enterpriseid": 0, //绑定数据
            "sex": "",
            "depname": "",
            "position": "",
            "isleader": 0,
            "birthday": "",
            "email": "",
            "remarks": "",
            "workaddress": 1,
            "isdefault": 0, //是否默认地址
            "isprimary": 0 //是否为主地址
        }
    },
    onLoad(options) {
        if (options.sys_enterpriseid) this.setData({
            "content.sys_enterpriseid": options.sys_enterpriseid
        })
        if (options.data) {
            let item = JSON.parse(options.data)
            this.setData({
                content: {
                    ...this.data.content,
                    ...item
                },
                "content.isdefault": item.isdefault,
                'form[0].value': item.name,
                'form[1].value': item.phonenumber,
                'form[2].value': [item.province, item.city, item.county],
                'form[3].value': item.address,
                'content.isdefault': item.isdefault,
                "disabled": false
            })
        };
    },
    /* 提交数据 */
    submit() {
        let data = this.selectComponent("#Form").submit();
        if (!data || this.data.disabled) return;
        const content = {
            ...this.data.content,
            ...data,
            "province": data.region[0],
            "city": data.region[1],
            "county": data.region[2],
        };
        delete(content.region);
        _Http.basic({
            "id": "20221018141802",
            content
        }).then(res => {
            if (res.msg != '成功') return wx.showToast({
                title: res.data,
                icon: "none"
            });
            wx.showToast({
                title: '保存成功',
                icon: "none"
            });
            setTimeout(() => {
                getCurrentPages().forEach(v => {
                    //更新列表
                    if (v.selectComponent("#Address")) {
                        let page = v.selectComponent("#Address"),
                            list = res.data.isdefault == 0 ? page.data.list : page.data.list.map(v => {
                                v.isdefault = 0;
                                return v;
                            }),
                            index = list.findIndex(value => value.contactsid == res.data.contactsid);
                        if (index != -1) {
                            //列表中存在说明是编辑，返回上一级页面并更新数据
                            list[index] = res.data;
                            page.setData({
                                list
                            });
                        } else {
                            //列表中不存在说明是新增，返回上一级页面更新数据 并进入详情
                            list.push(res.data);
                            page.setData({
                                list,
                                "content.total": page.data.content.total + 1
                            });
                        }
                        if (res.data.isdefault == 1) setTimeout(() => {
                            page.selectComponent("#List").initRadio();
                        }, 200)
                        wx.navigateBack();
                    };
                    if ("packageA/setclient/modules/address/search/index" == v.__route__) v.getList(true);
                })
            }, 300)
        })
    },
    /* 设置默认 */
    checkboxChange() {
        this.setData({
            "content.isdefault": this.data.content.isdefault == 1 ? 0 : 1
        })
    },
    /* 表单是否填写完成 */
    onConfirm({
        detail
    }) {
        this.setData({
            disabled: detail
        })
    },
    onShareAppMessage() {}
})