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: "",
            hint: "",
            type: "radio",
            value: "0",
            radioList: [{
                id: '0',
                name: '手机号'
            }, {
                id: '1',
                name: '座机电话'
            }],
            valueName: "isTelephone", //绑定的字段名称
            required: true, //必填
            interrupt: true,
            checking: `true`,
            phonenumber: "",
            telephone: ["", ""]
        }, {
            label: "手机号",
            error: false,
            errMsg: "",
            type: "number",
            value: "",
            placeholder: "联系人手机号码",
            valueName: "phonenumber",
            required: true,
            checking: "phone"
        }, {
            label: "微信",
            error: false,
            errMsg: "",
            type: "text",
            value: "",
            placeholder: "联系人微信",
            valueName: "wechatnum",
            required: false
        }, {
            label: "邮箱",
            error: false,
            errMsg: "",
            type: "textarea",
            value: "",
            placeholder: "请填写",
            valueName: "email",
            required: false,
            checking: "mail"
        }, {
            label: "传真",
            error: false,
            errMsg: "",
            type: "textarea",
            value: "",
            placeholder: "请填写",
            valueName: "fax",
            required: false,
        }, {
            label: "单位",
            error: false,
            errMsg: "",
            type: "route",
            url: "/packageA/project/modules/contacts/select/index",
            value: "",
            query: "&radio=true",
            placeholder: "选择单位",
            valueName: "sys_enterpriseid",
            required: true,
            checking: "base",
        }, {
            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: "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
            "sa_projectid": 0, //绑定数据
            "isleader": 0, //默认0
            "workaddress": 0,
            "isdefault": 0, //是否默认地址
            "isprimary": 0 //是否为主地址
        }
    },
    onLoad(options) {
        this.data.content.sa_projectid = options.sa_projectid;
        if (options.data) {
            let item = JSON.parse(options.data),
                arr = item.phonenumber.split("-");
            console.log(item)
            item.sys_enterpriseid = item.enterprisename ? [item.enterprisename, [item.sys_enterpriseid]] : "";
            item.isTelephone = arr.length == 2 ? "1" : "0";
            this.setData({
                form: this.data.form.map(v => {
                    if (v.valueName == 'phonenumber') {
                        v = arr.length == 2 ? {
                            label: "电话",
                            error: false,
                            errMsg: "",
                            type: "telephone",
                            value: arr,
                            placeholder: "联系人手机号码",
                            valueName: "telephone",
                            required: true,
                            checking: "telephone"
                        } : {
                            label: "手机号",
                            error: false,
                            errMsg: "",
                            type: "number",
                            value: arr[0],
                            placeholder: "联系人手机号码",
                            valueName: "phonenumber",
                            required: true,
                            checking: "phone"
                        }
                    } else {
                        v.value = Object.hasOwn(item, v.valueName) ? item[v.valueName] : v.value;
                    }
                    return v
                })
            });
            this.data.content = item;
            this.selectComponent("#Form").confirm();
            if (item.sys_phonebookid) this.setData({
                sys_phonebookid: item.sys_phonebookid
            })
        };
    },
    /* 打断 */
    interrupt({
        detail
    }) {
        let {
            data,
            form
        } = detail;
        if (data.label == '联系方式') {
            if (data.value == 0) {
                data.telephone = form[2].value;
                form[2] = {
                    label: "手机号",
                    error: false,
                    errMsg: "",
                    type: "number",
                    value: data.phonenumber,
                    placeholder: "联系人手机号码",
                    valueName: "phonenumber",
                    required: true,
                    checking: "phone"
                }
            } else {
                data.phonenumber = form[2].value;
                form[2] = {
                    label: "电话",
                    error: false,
                    errMsg: "",
                    type: "telephone",
                    value: data.telephone,
                    placeholder: "联系人手机号码",
                    valueName: "telephone",
                    required: true,
                    checking: "telephone"
                }
            };
            this.setData({
                form
            })
        }
    },
    /* 提交数据 */
    submit() {
        let data = this.selectComponent("#Form").submit();
        if (!data || this.data.disabled) return;
        data.sys_enterpriseid = data.sys_enterpriseid[1] ? data.sys_enterpriseid[1][0] : 0;
        const content = {
            ...this.data.content,
            ...data,
            sys_phonebookid: ""
        };
        if (content.telephone) {
            content.phonenumber = content.telephone.join("-");
            delete(content.telephone)
        }
        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"
            });
            let that = this;
            let page = getCurrentPages()[getCurrentPages().length - 2];
            if (that.data.content.sa_project_contactsid) {
                wx.showToast({
                    title: '保存成功',
                    icon: "none",
                    mask: true
                });
                setTimeout(() => {
                    wx.navigateBack();
                    let model = page.selectComponent("#Contacts")
                    model.getList(model.data.sa_projectid, true)
                }, 300)
            } else {
                _Http.basic({
                    "id": 20221111131104,
                    "content": {
                        "sa_project_contactsid": 0,
                        contactsids: [res.data.contactsid],
                        "sa_projectid": page.data.sa_projectid,
                        "remarks": ""
                    }
                }).then(s => {
                    console.log("项目添加联系人", s)
                    if (s.msg != '成功') return wx.showToast({
                        title: s.data,
                        icon: "none"
                    });
                    wx.showToast({
                        title: '添加成功',
                        icon: "none",
                        mask: true
                    });
                    setTimeout(() => {
                        wx.navigateBack();
                        let model = page.selectComponent("#Contacts")
                        model.getList(model.data.sa_projectid, true)
                    }, 300)
                })
            }
        });
    },
    /* 表单是否填写完成 */
    onConfirm({
        detail
    }) {
        this.setData({
            disabled: detail
        })
    },
    /* 是否显示全部 */
    changefromShowAll({
        detail
    }) {
        this.setData({
            fromShowAll: detail
        })
    },
})