const _Http = getApp().globalData.http;
Page({
    data: {
        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"
        }, {
            disabled: false,
            errMsg: "",
            error: false,
            interrupt: false,
            label: "关联客户",
            params: {
                "content": {
                    "pageNumber": 1,
                    "pageSize": 20,
                    "where": {
                        "condition": ""
                    }
                },
                "id": 2025041710422802,
            },
            placeholder: "选择医院管理",
            query: "&showName=enterprisename&idname=sys_enterpriseid",
            required: false,
            type: "route",
            url: "/packageA/select/setclient/select",
            value: [],
            valueName: "sys_enterpriseids"
        }, {
            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: "route",
            url: "/packageA/select/group/select",
            value: "",
            query: "&radio=true",
            placeholder: "选择群组",
            valueName: "group",
            required: false,
            checking: "base",
            interrupt: false
        }, {
            label: "偏好",
            error: false,
            errMsg: "",
            type: "text",
            value: "",
            valueName: "preference",
            required: false,
            checking: "base"
        }, {
            label: "爱好",
            error: false,
            errMsg: "",
            type: "text",
            value: "",
            valueName: "hobby",
            required: false,
            checking: "base"
        }, {
            label: "备注",
            error: false,
            errMsg: "",
            type: "textarea",
            value: "",
            placeholder: "请填写",
            valueName: "remarks",
            required: false,
            checking: "base"
        }],
        disabled: true,
        "content": {
            sys_phonebookid: 0,
        },
        loading: false
    },
    onLoad(options) {
        getApp().globalData.Language.getLanguagePackage(this, '联系人')
        let form = this.data.form;
        if (options.data) {
            let item = JSON.parse(options.data),
                arr = item.phonenumber.split("-");
            item.isTelephone = arr.length == 2 ? "1" : "0";
            item.group = [item.groupname, [item.sys_phonebookgroupid]]
            this.data.content.sys_phonebookid = item.sys_phonebookid;
            this.setData({
                form: 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 || '';
                        try {
                            if (v.valueName == 'sys_enterpriseids') {
                                v.params.content.sys_phonebookid = item.sys_phonebookid
                            }
                        } catch (error) {

                        }
                    }
                    if (v.valueName == 'group') v.disabled = item.leaderid != wx.getStorageSync('userMsg').userid
                    return v
                }),
                "disabled": false
            });
        } else if (options.name) {
            let index = form.findIndex(v => v.label == '群组');
            form[index].value = [options.name, [options.id || 0]]
            this.setData({
                form
            })
        }
    },
    /* 提交数据 */
    submit() {
        this.setData({
            loading: true
        })
        try {
            const content = {
                ...this.data.content,
                ...this.selectComponent("#Form").submit()
            };
            if (content.telephone) {
                content.phonenumber = content.telephone.join("-");
                delete(content.telephone)
            }
            content.groupname = content.group[0] || '';
            content.sys_phonebookgroupid = content.group[1][0] || 0;
            content.sys_enterpriseids = content.sys_enterpriseids[1] || [];
            delete(content.group)
            _Http.basic({
                "id": "20220831164503",
                content
            }).then(res => {
                this.setData({
                    loading: false
                })
                console.log("编辑联系人", res)
                wx.showToast({
                    title: res.code != '1' ? res.msg : getApp().globalData.Language.getMapText('保存成功'),
                    icon: "none",
                    mask: true
                });
                if (res.code == '1') {
                    if (content.sys_phonebookid == 0) {
                        setTimeout(() => {
                            const {
                                sys_phonebookid,
                                sa_project_contactsid,
                                contactsid,
                            } = res.data;
                            wx.redirectTo({
                                url: `/packageA/contacts/detail?id=${sys_phonebookid||0}&sa_project_contactsid=${sa_project_contactsid||0}&contactsid=${contactsid||0}`,
                            })
                        }, 500)
                    } else {
                        getCurrentPages().forEach(v => {
                            if (v.__route__ == 'packageA/contacts/detail') {
                                v.getDetail(true);
                            } else if (v.__route__ == 'packageA/project/detail') {
                                let model = v.selectComponent("#Contacts");
                                if (model) model.getList(model.data.sa_projectid, true)
                            } else if (v.__route__ == 'packageA/setclient/detail') {
                                let model = v.selectComponent("#Contacts");
                                if (model) model.getList(model.data.sys_enterpriseid, true)
                            } else if (v.__route__ == 'packageA/saleClue/detail') {
                                let model = v.selectComponent("#Contacts");
                                if (model) model.getList(model.data.sat_orderclueid, true)
                            }
                        })
                        setTimeout(() => {
                            wx.navigateBack();
                        }, 500)
                    }
                }
            });
        } catch (error) {
            this.setData({
                loading: false
            })
        }
    },
    /* 打断 */
    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
            })
        } else if (data.label == '群组') {
            console.log(data, form)
        }
    },
    /* 表单是否填写完成 */
    onConfirm({
        detail
    }) {
        this.setData({
            disabled: detail
        })
    },
    /* 是否显示全部 */
    changefromShowAll({
        detail
    }) {
        this.setData({
            fromShowAll: detail
        })
    },
})