const _Http = getApp().globalData.http,
    deleteMark = require("../../utils/deleteMark");

Page({
    data: {
        update: true, // 修改权限
        sa_agent_hrid: 0,
        remarks: "", //备注
        fromList: [{
            label: "姓名",
            error: false,
            errMsg: "",
            type: "text",
            value: "",
            placeholder: "请填写",
            valueName: "name", //绑定的字段名称
            required: true, //必填
        }, {
            label: "职位",
            error: false,
            errMsg: "",
            type: "text",
            value: "",
            placeholder: "请填写",
            valueName: "position", //绑定的字段名称
            required: false, //必填
        }, {
            label: "邮箱",
            error: false,
            errMsg: "",
            type: "text",
            value: "",
            placeholder: "请填写",
            valueName: "email", //绑定的字段名称
            required: true, //必填
        }, {
            label: "手机号码",
            error: false,
            errMsg: "",
            type: "text",
            value: "",
            placeholder: "请填写",
            valueName: "phonenumber", //绑定的字段名称
            required: true, //必填
        }, {
            label: "角色配置",
            error: false,
            errMsg: "",
            type: "custom",
            value: "",
            placeholder: "请填写",
            valueName: "rolenames", //绑定的字段名称
            required: false, //必填
        }],
        roleValue: [],
        roleids: [], //已选角色ID
        roleList: [], //角色列表
        disabled: true, //按钮禁用
        loading: false, //按钮加载
    },
    onLoad(options) {
        if (options.item) {
            let item = JSON.parse(options.item)
            this.setData({
                sa_agent_hrid: item.sa_agent_hrid,
                ['fromList[0].value']: item.name,
                ['fromList[1].value']: item.position,
                ['fromList[2].value']: item.email,
                ['fromList[3].value']: item.phonenumber,
                ['fromList[4].value']: item.rolenames,
                roleValue: item.rolenames,
                remarks: item.remarks,
                roleids: item.roleids.map(v => v + ''),
                disabled: true,
                update: options.update
            })
        };
        this.queryRole();
    },
    /* 查询角色列表 */
    queryRole() {
        _Http.basic({
            "classname": "sale.team.team",
            "method": "queryRole",
            "content": {
                "sa_agent_hrid": this.data.sa_agent_hrid
            }
        }).then(res => {
            if (res.msg != '成功') return wx.showToast({
                title: res.msg,
                icon: "none"
            })
            this.setData({
                roleList: res.data
            })
        })
    },
    /* 表单是否完成 */
    formCompletedOrNot({
        detail
    }) {
        this.setData({
            disabled: !detail
        })
    },
    /* 提交数据 */
    submitRole() {
        if (this.data.disabled || this.data.loading) return;
        let {
            isReturn,
            returnData
        } = this.selectComponent("#form").getData();
        if (!deleteMark.CheckPhoneNumber(returnData.phonenumber.trim() - 0)) return;
        if (!deleteMark.CheckEmail(returnData.email.trim())) return;
        delete(returnData.rolenames);
        this.setData({
            loading: true
        })
        _Http.basic({
            "classname": "sale.team.team",
            "method": "insertormodify_team",
            "content": {
                "sa_agent_hrid": this.data.sa_agent_hrid,
                ...returnData,
                "remarks": this.data.remarks,
                "roleids": this.data.roleids
            }
        }).then(res => {
            this.setData({
                loading: false
            })
            if (res.msg != '成功') return wx.showToast({
                title: res.msg,
                icon: "none"
            })
            this.setData({
                disabled: true
            })
            let pages = getCurrentPages(),
                prevPage = pages[pages.length - 2];
            prevPage.getList(true);
            setTimeout(() => {
                wx.navigateBack({
                    delta: 0
                })
                wx.showToast({
                    title: '保存成功!'
                })
            }, 300)
        })
    },
    /* 得到多选结果 */
    getResult({
        detail
    }) {
        this.setData({
            roleids: detail.roleids,
            roleValue: detail.rolenames
        })
    },
    /* 文本域输入 */
    textareaInput(e) {
        this.setData({
            remarks: deleteMark.queryStr(e.detail.value).trim()
        })
    },
    onShareAppMessage(res) { }
})