const _Http = getApp().globalData.http;
Page({
    data: {
        sys_phonebookid: null, //是否为快捷选择创建的账号
        fromShowAll: false,
        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: "dw",
            required: false,
            checking: "base",
            disabled: true
        }, {
            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) {
        this.setData({
            "content.sys_enterpriseid": options.sys_enterpriseid
        });
        if (options.data) {
            let item = JSON.parse(options.data),
                form = this.data.form.map(v => {
                    if (v.valueName == 'region') {
                        v.value = item.province ? [item.province, item.city, item.county] : []
                    } else {
                        v.value = Object.hasOwn(item, v.valueName) ? item[v.valueName] : v.value;
                    }
                    return v
                });
            let page = getCurrentPages()[getCurrentPages().length - 2];
            form[3].value = page.data.detail.enterprisename;
            this.setData({
                form,
                "disabled": false,
                "content.contactsid": page.__route__ == 'packageA/setclient/detail' ? 0 : item.contactsid
            });
            if (item.sys_phonebookid) this.setData({
                sys_phonebookid: item.sys_phonebookid
            })
        };
    },
    /* 提交数据 */
    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] || "",
            sys_phonebookid: ""
        };
        delete(content.region);
        if (this.data.sys_phonebookid == null) {
            this.handleSubmit(content);
        } else {
            let that = this;
            wx.showModal({
                title: '提示',
                content: '是否删除原通讯录联系人信息',
                complete({
                    confirm
                }) {
                    content.sys_phonebookid = confirm ? that.data.sys_phonebookid : "";
                    that.handleSubmit(content);
                }
            })
        }
    },
    handleSubmit(content) {
        _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(() => {
                wx.navigateBack()
                getCurrentPages().forEach(v => {
                    if (v.__route__ == 'packageA/setclient/modules/contacts/detail/index') {
                        v.getDetail();
                    } else if (v.selectComponent("#Contacts")) {
                        let page = v.selectComponent("#Contacts"),
                            list = page.data.list,
                            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
                            });
                            wx.navigateTo({
                                url: '/packageA/setclient/modules/contacts/detail/index?contactsid=' + res.data.contactsid
                            })
                        }
                    } else if (v.__route__ == 'packageA/project/modules/contacts/search/index') {
                        let index = v.data.list.findIndex(value => value.contactsid == res.data.contactsid);
                        if (index != -1) v.setData({
                            [`list[${index}]`]: res.data
                        })
                    }
                });
            }, 500)
        });
    },
    /* 表单是否填写完成 */
    onConfirm({
        detail
    }) {
        this.setData({
            disabled: detail
        })
    },
    /* 是否显示全部 */
    changefromShowAll({
        detail
    }) {
        this.setData({
            fromShowAll: detail
        })
    },
})