const _Http = getApp().globalData.http,
    deleteMark = require("../../utils/deleteMark");
Page({
    data: {
        sys_phonebookid: 0,
        from1: [{
            label: "姓名",
            error: false,
            errMsg: "",
            type: "text",
            value: "",
            placeholder: "请填写",
            valueName: "name", //绑定的字段名称
            required: true, //必填
        }, {
            label: "手机号码",
            error: false,
            errMsg: "",
            type: "text",
            value: "",
            placeholder: "请填写",
            valueName: "phonenumber", //绑定的字段名称
            required: true, //必填
            callback: null,
        }, {
            label: "性别",
            error: false,
            errMsg: "",
            type: "sex",
            value: "",
            placeholder: "请填写",
            valueName: "sex",
            required: false, //必填
        }, {
            label: "生日",
            error: false,
            errMsg: "",
            type: "date",
            value: "",
            placeholder: "请填写",
            valueName: "birthday",
            required: false, //必填
        }, {
            label: "家庭住址",
            error: false,
            errMsg: "",
            type: "text",
            value: "",
            placeholder: "请填写",
            valueName: "homeaddress",
            required: false, //必填
        }, {
            label: "邮箱",
            error: false,
            errMsg: "",
            type: "text",
            value: "",
            placeholder: "请填写",
            valueName: "email",
            required: false, //必填
        }],
        from2: [{
            label: "单位",
            error: false,
            errMsg: "",
            type: "text",
            value: "",
            placeholder: "请填写",
            valueName: "company",
            required: false, //必填
        }, {
            label: "部门",
            error: false,
            errMsg: "",
            type: "text",
            value: "",
            placeholder: "请填写",
            valueName: "depname",
            required: false, //必填
        }, {
            label: "职位",
            error: false,
            errMsg: "",
            type: "text",
            value: "",
            placeholder: "请填写",
            valueName: "position",
            required: false, //必填
        }],
        from3: [{
            label: "标签",
            error: false,
            errMsg: "",
            type: "slot",
            value: "",
            placeholder: "请填写",
            valueName: "tag",
            required: false, //必填
        }, {
            label: "群组",
            error: false,
            errMsg: "",
            type: "custom",
            value: "",
            placeholder: "请填写",
            valueName: "sys_phonebookgroupid",
            required: false, //必填
        }, {
            label: "备注",
            error: false,
            errMsg: "",
            type: "text",
            value: "",
            placeholder: "请填写",
            valueName: "remarks",
            required: false, //必填
        }],
        disabled: true, //按钮禁用
        groupList: [],
        groupNameList: [],
        groupName: null,
        popupShow: false,
        checked: false,
        radio: '',
        loading: false
    },
    onLoad(options) {
        if (options.group) {
            const group = JSON.parse(options.group);
            this.setData({
                groupName: group.groupname,
                'from3[1].value': group.sys_phonebookgroupid
            })
        };
        if (options.data) {
            const item = JSON.parse(options.data);
            this.setData({
                'from1[0].value': item.name,
                'from1[1].value': item.phonenumber,
                'from1[2].value': item.sex,
                'from1[3].value': item.birthday,
                'from1[4].value': item.homeaddress,
                'from1[5].value': item.email,
                'from2[0].value': item.company,
                'from2[1].value': item.depname,
                'from2[2].value': item.position,
                'from3[0].value': item.tag1,
                'from3[1].value': item.sys_phonebookgroupid,
                'from3[2].value': item.remarks,
                groupName: item.groupname,
                sys_phonebookid: item.sys_phonebookid,
                disabled: false
            });
            if (item.tag1.length) {
                let checked = false,
                    radio = '';
                item.tag1.forEach(v => {
                    if (v == '关键决策人') {
                        checked = true;
                    } else {
                        radio = v;
                    }
                })
                this.setData({
                    checked,
                    radio
                })
            }
        };
        this.getGroup();
        wx.setNavigationBarTitle({
            title: this.data.sys_phonebookid == 0 ? '添加联系人' : '修改联系人',
        })
    },
    /* 监听表单必填项 */
    form1CompletedOrNot({
        detail
    }) {
        this.setData({
            disabled: !detail
        })
    },
    /* 提交表单 */
    submit() {
        if (this.data.disabled) return wx.showToast({
            title: '请完成必填项',
            icon: "none"
        })
        let from = {
            ...this.selectComponent("#from1").getData().returnData,
            ...this.selectComponent("#from2").getData().returnData,
            ...this.selectComponent("#from3").getData().returnData,
        }
        if (!deleteMark.CheckPhoneNumber(from.phonenumber)) return;
        if (from.email && !deleteMark.CheckEmail(from.email)) return;
        if (from.sys_phonebookgroupid == '') from.sys_phonebookgroupid = 0;
        if (from.tag == '') from.tag = [];
        this.setData({
            loading: true
        })
        _Http.basic({
            "id": "20220831164503",
            "version": 1,
            "content": {
                "sys_phonebookid": this.data.sys_phonebookid, //sys_phonebookid<=0时 为新增
                ...from
            }
        }).then(res => {
            this.setData({
                loading: false
            })
            if (res.msg != '成功') return wx.showToast({
                title: res.data,
                icon: "none"
            })
            try {
                res.data.tag1 = JSON.parse(res.data.tag)
                getCurrentPages().forEach(v => {
                    if (v.route === 'packageA/contacts/index') {
                        v.getGroup();
                    } else if (v.route === 'packageA/contacts/list') {
                        let arr = v.data.phonebook,
                            index = arr.findIndex(value => value.sys_phonebookid == res.data.sys_phonebookid);
                        (index === -1) ? arr.push(res.data): arr[index] = res.data;
                        v.setData({
                            phonebook: arr
                        })
                    } else if (v.route === 'packageA/contacts/details') {
                        v.handleData(res.data);
                    }
                });
            } catch (e) {
                wx.showToast({
                    title: `错误!${e}`,
                    icon: "none"
                })
            };
            wx.showToast({
                title: this.data.sys_phonebookid == 0 ? '添加成功' : '保存成功'
            })
            this.setData({
                disabled: true,
            })
            setTimeout(() => {
                wx.navigateBack({
                    delta: 0
                })
            }, 700);
        })
    },
    /* 获取分组 */
    getGroup() {
        _Http.basic({
            "id": "20220831164603",
            "version": 1,
            "content": {
                "nocache": true,
                "where": {
                    "condition": ""
                }
            }
        }).then(res => {
            if (res.msg != '成功') return wx.showToast({
                title: res.msg,
                icon: "none"
            })
            const data = res.data.filter(v => v.groupname != '客户联系人' && v.groupname != '项目联系人');
            this.setData({
                groupList: data,
                groupNameList: data.map(v => v.groupname)
            })
        });
    },
    /* 选择部门 */
    bindPickerChange(e) {
        const {
            returnData
        } = this.selectComponent("#from3").getData()
        this.setData({
            'from3[1].value': this.data.groupList[e.detail.value - 0].sys_phonebookgroupid,
            groupName: this.data.groupNameList[e.detail.value - 0],
            'from3[0].value': returnData.tag,
            'from3[2].value': returnData.remarks
        });
    },
    /* 打开弹出层 */
    openPopup() {
        this.setData({
            popupShow: true
        })
    },
    /* 关闭弹出层 */
    closePopup() {
        this.setData({
            popupShow: false
        })
    },
    /* 确定标签 */
    handleTag() {
        let tagArr = [];
        if (this.data.checked) tagArr.push("关键决策人");
        if (this.data.radio) tagArr.push(this.data.radio);
        const {
            returnData
        } = this.selectComponent("#from3").getData()
        this.setData({
            'from3[0].value': tagArr,
            'from3[1].value': returnData.sys_phonebookgroupid,
            'from3[2].value': returnData.remarks
        });
        this.closePopup()
    },
    /* 是否关键决策人 */
    changeChecked() {
        this.setData({
            checked: !this.data.checked
        })
    },
    /* 单选 */
    onChange(e) {
        this.setData({
            radio: e.currentTarget.dataset.name
        })
    },
    onShareAppMessage() {}
})