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: true,
            checking: "phone"
        }, {
            label: "地区",
            error: false,
            errMsg: "",
            type: "region",
            value: [],
            placeholder: "省,市,区",
            valueName: "region",
            required: true
        }, {
            label: "详细地址",
            error: false,
            errMsg: "",
            type: "textarea",
            value: "",
            placeholder: "例: 科创园11栋1103室",
            valueName: "address",
            required: true,
            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.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);
        let pages = getCurrentPages();
        content.sys_enterpriseid = pages[pages.length - 2].data.detail.sys_enterpriseid;
        _Http.basic({
            "id": "20221018141802",
            content
        }).then(res => {
            console.log("添加地址", res)
            if (res.msg != '成功') return wx.showToast({
                title: res.data,
                icon: "none"
            });
            wx.showToast({
                title: '保存成功',
                icon: "none"
            });
            setTimeout(() => {
                let pages = getCurrentPages();
                pages[pages.length - 2].partialRenewal(true);
                wx.navigateBack();
            }, 500)
        })
    },
    /* 设置默认 */
    checkboxChange() {
        this.setData({
            "content.isdefault": this.data.content.isdefault == 1 ? 0 : 1
        })
    },
    /* 表单是否填写完成 */
    onConfirm({
        detail
    }) {
        this.setData({
            disabled: detail
        })
    },
    onShareAppMessage() {}
})