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: "contactsid",
                required: false,
                checking: "base",
            }, {
                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,
                checking: "base"
            },
            {
                label: "爱好",
                error: false,
                errMsg: "",
                type: "textarea",
                value: "",
                placeholder: "请填写",
                valueName: "hobby",
                required: false,
                checking: "base"
            },
            {
                label: "备注",
                error: false,
                errMsg: "",
                type: "textarea",
                value: "",
                placeholder: "请填写",
                valueName: "remarks",
                required: false,
                checking: "base"
            }
        ],
        disabled: true,
        "content": {
            groupname: "项目联系人",
            sys_phonebookid: 0,
            sa_project_contactsid: 0,
            "sa_projectid": 0, //绑定数据
            "isleader": 0, //默认0
            "workaddress": 0,
            "isdefault": 0, //是否默认地址
            "isprimary": 0 //是否为主地址
        }
    },
    onLoad(options) {
        if (options.sa_projectid) this.data.content.sa_projectid = options.sa_projectid;
        if (options.data) {
            let item = JSON.parse(options.data),
                arr = item.phonenumber.split("-");
            item.contactsid = item.enterprisename ? [item.enterprisename, [item.contactsid]] : '';
            item.isTelephone = arr.length == 2 ? "1" : "0";
            if (item.sa_projectid) this.data.content.sa_projectid = item.sa_projectid;
            this.data.content.sys_phonebookid = item.sys_phonebookid || 0;
            this.data.content.sa_project_contactsid = item.sa_project_contactsid || 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 || '';
                        if (v.valueName == 'contactsid') {
                            v.query = v.query + ('&sys_phonebookid=' + (options.sys_phonebookid ? options.sys_phonebookid : item.sys_phonebookid || 0))
                            console.log(v)
                        }
                    }
                    return v
                })
            });
            this.selectComponent("#Form").confirm();
            if (item.sa_project_contactsid) return;
            _Http.basic({
                "id": "20231215134204",
                "content": {
                    "sys_phonebookid": item.sys_phonebookid,
                    "nocache": true,
                    "pageSize": 1,
                    "where": {
                        "condition": ""
                    },
                    "sa_projectid": options.sa_projectid,
                    "total": 1
                },
            }).then(res => {
                if (res.code == 1 && res.total) {
                    let value = [res.data[0].enterprisename, [res.data[0].sys_enterpriseid]]
                    let index = this.data.form.findIndex(v => v.label == '关联企业')
                    if (index != -1) this.setData({
                        [`form[${index}].value`]: value
                    })
                }
                console.log("获取一条关联企业", res)
            })

        }
        getApp().globalData.Language.getLanguagePackage(this, '联系人');
    },
    /* 打断 */
    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.enterprisename = data.contactsid[0] ? data.contactsid[0] : "";
        data.contactsid = data.contactsid[1] ? data.contactsid[1][0] : 0;
        const content = {
            ...this.data.content,
            ...data
        };
        content.ownertable = "sa_project";
        content.ownerid = content.sa_projectid;
        if (content.telephone) {
            content.phonenumber = content.telephone.join("-");
            delete(content.telephone)
        }
        this.handleSubmit(content);
    },
    handleSubmit(content) {
        console.log("提交", content)
        _Http.basic({
            "id": "20221219135502",
            content
        }).then(res => {
            console.log("编辑联系人", res)
            if (res.code != '1') return wx.showModal({
                title: getApp().globalData.Language.getMapText('提示'),
                content: res.msg,
                confirmText: getApp().globalData.Language.getMapText('确定'),
                showCancel: false
            })
            let that = this;
            let page = getCurrentPages()[getCurrentPages().length - 2];
            if (that.data.content.sa_project_contactsid) {
                wx.showToast({
                    title: getApp().globalData.Language.getMapText('保存成功'),
                    icon: "none",
                    mask: true
                });
                setTimeout(() => {
                    wx.navigateBack();
                    let model = page.selectComponent("#Contacts")
                    model.getList(model.data.sa_projectid, true)
                }, 300)
            } else {
                wx.showToast({
                    title: getApp().globalData.Language.getMapText('添加成功'),
                    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
        })
    },
})