const _Http = getApp().globalData.http;
Page({
    data: {
        fromShowAll: true,
        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: "text",
            value: "",
            placeholder: "联系人所属部门",
            valueName: "depname",
            required: false,
            checking: "base"
        }, {
            label: "职位",
            error: false,
            errMsg: "",
            type: "text",
            value: "",
            placeholder: "联系人职位",
            valueName: "position",
            required: false,
            checking: "base"
        }, {
            label: "性别",
            error: false,
            errMsg: "",
            type: "sex",
            value: "",
            placeholder: "联系人性别",
            valueName: "sex",
            required: false,
            checking: "base"
        }, {
            label: "生日",
            error: false,
            errMsg: "",
            type: "date",
            value: "",
            placeholder: "联系人生日",
            valueName: "birthday",
            required: false
        }, {
            label: "邮箱",
            error: false,
            errMsg: "",
            type: "textarea",
            value: "",
            placeholder: "请填写",
            valueName: "email",
            required: false,
            checking: "mail"
        }, {
            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"
        }, {
            label: "备注",
            error: false,
            errMsg: "",
            type: "textarea",
            value: "",
            placeholder: "请填写",
            valueName: "remarks",
            required: false,
            checking: "base"
        }],
        disabled: true,
        "content": {
            "contactsid": 0, //地址id
            "sys_enterpriseid": 0, //绑定数据
            "isleader": 0, //默认0
            "workaddress": 0,
            "isdefault": 0, //是否默认地址
            "isprimary": 0 //是否为主地址
        }
    },
    onLoad(options) {
        if (options.data) {
            let item = JSON.parse(options.data),
                form = this.data.form.map(v => {
                    if (v.valueName == 'region') {
                        v.value = [item.province, item.city, item.county]
                    } else {
                        v.value = Object.hasOwn(item, v.valueName) ? item[v.valueName] : v.value;
                    }
                    return v
                });
            this.setData({
                form,
                "disabled": false,
                "content.contactsid": item.contactsid
            })
        }
    },
    /* 提交数据 */
    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)
        });
    },
    /* 表单是否填写完成 */
    onConfirm({
        detail
    }) {
        this.setData({
            disabled: detail
        })
    },
    /* 是否显示全部 */
    changefromShowAll({
        detail
    }) {
        this.setData({
            fromShowAll: detail
        })
    },
})