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: "text",
            value: "",
            placeholder: "联系人所属部门",
            valueName: "depname",
            required: true,
            checking: "base"
        }, {
            label: "职位",
            error: false,
            errMsg: "",
            type: "text",
            value: "",
            placeholder: "联系人职位",
            valueName: "position",
            required: true,
            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: "homeaddress",
            required: false,
            checking: "base"
        }, {
            label: "偏好",
            error: false,
            errMsg: "",
            type: "textarea",
            value: "",
            placeholder: "请填写",
            valueName: "preference",
            required: false
        }, {
            label: "爱好",
            error: false,
            errMsg: "",
            type: "textarea",
            value: "",
            placeholder: "请填写",
            valueName: "hobby",
            required: false
        }, {
            label: "备注",
            error: false,
            errMsg: "",
            type: "textarea",
            value: "",
            placeholder: "请填写",
            valueName: "remarks",
            required: false,
            checking: "base"
        }],
        disabled: true,
        "content": {
            "sys_enterpriseid": 0, //绑定数据
            sys_phonebookid: 0,
            "isleader": 0, //默认0
            "isdefault": 0, //是否默认地址
            contactsid: 0,
            "isprimary": 0 //是否为主地址
        }
    },
    onLoad(options) {
        getApp().globalData.Language.getLanguagePackage(this, '联系人');
        if (options.sys_enterpriseid) this.data.content.sys_enterpriseid = options.sys_enterpriseid;
        if (options.enterprisename) this.data.content.enterprisename = options.enterprisename;
        if (options.data) {
            let item = JSON.parse(options.data),
                arr = item.phonenumber.split("-");
            this.data.content.contactsid = item.contactsid;
            item.isTelephone = arr.length == 2 ? "1" : "0";
            if (options.enterprisename) {
                item.sys_enterpriseid = [options.enterprisename, [options.sys_enterpriseid]]
            } else if (item.enterprisename) {
                item.sys_enterpriseid = [item.enterprisename, [item.sys_enterpriseid]]
            }
            this.data.content.enterprisename = item.enterprisename;
            if (typeof item.sys_enterpriseid == "object") {
                this.data.form.splice(6, 0, {
                    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",
                    disabled: true
                })
                this.data.content.sys_enterpriseid = item.sys_enterpriseid[1][0] || 0;
            }
            this.data.content.sys_phonebookid = item.sys_phonebookid || 0;
            // if (options.sys_enterpriseid) this.data.sys_phonebookid = item.sys_phonebookid || 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 = item[v.valueName] || v.value || '';
                    }
                    return v
                })
            });
            this.selectComponent("#Form").confirm();
        } else {
            this.data.form.splice(6, 0, {
                label: "关联企业",
                error: false,
                errMsg: "",
                type: "route",
                url: "/packageA/project/modules/contacts/select/index",
                value: [options.enterprisename, [options.sys_enterpriseid]],
                query: "&radio=true",
                placeholder: "选择关联企业",
                valueName: "sys_enterpriseid",
                required: true,
                checking: "base",
                disabled: true
            })
            this.setData({
                form: this.data.form
            })
        }
    },
    /* 打断 */
    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;
        const content = {
            ...this.data.content,
            ...data,
        };
        if (content.telephone) {
            content.phonenumber = content.telephone.join("-");
            delete(content.telephone)
        }
        content.sys_enterpriseid = this.data.content.sys_enterpriseid;
        this.handleSubmit(content);

        /*  if (this.data.sys_phonebookid == null) {
         } else {
             let that = this;
             wx.showModal({
                 title: getApp().globalData.Language.getMapText('提示'),
                 content: '是否删除原通讯录联系人信息',
                    cancelText: getApp().globalData.Language.getMapText('取消'),
                    confirmText: getApp().globalData.Language.getMapText('确定'),
                 complete({
                     confirm
                 }) {
                     content.sys_phonebookid = confirm ? content.sys_phonebookid : 0;
                     that.handleSubmit(content);
                 }
             })
         } */
    },
    handleSubmit(content) {
        _Http.basic({
            "id": "20240531151704",
            content
        }).then(res => {
            console.log("编辑联系人", res)
            wx.showToast({
                title: res.code != '1' ? res.msg : getApp().globalData.Language.getMapText('保存成功'),
                icon: "none",
                mask: true
            });
            if (res.code == '1') setTimeout(() => {
                wx.navigateBack()
                getCurrentPages().forEach(v => {
                    if (v.__route__ == 'packageA/setclient/modules/contacts/detail/index' || v.__route__ == 'packageA/contacts/detail' || v.__route__ == "packageA/publicCustomer/detail" || v.__route__ == "packageA/setclient/detail") {
                        v.getDetail();
                    } else if (v.selectComponent("#Contacts")) {
                        let page = v.selectComponent("#Contacts");
                        page.getList(page.data.content.sys_enterpriseid, true)
                    } else if (v.__route__ == 'packageA/project/modules/contacts/search/index') {
                        let index = v.data.list.findIndex(value => value.sys_phonebookid == res.data.sys_phonebookid);
                        if (index != -1) v.setData({
                            [`list[${index}]`]: res.data
                        })
                    }
                });
            }, 500)

        });
    },
    /* 表单是否填写完成 */
    onConfirm({
        detail
    }) {
        this.setData({
            disabled: detail
        })
    },
    /* 是否显示全部 */
    changefromShowAll({
        detail
    }) {
        this.setData({
            fromShowAll: detail
        })
    },
})