import {
    ApiModel
} from "../../utils/api";
const _Http = new ApiModel();
import {
    TestVerify
} from "../../utils/verify"
const _Verify = new TestVerify();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        fname: '', //用户名称
        frole: '', //身份/职位
        fphonenumber: '', //手机号码
        subusers: [], //权限
        tenterprise_userid: 0, //用户id
        checked: true, //是否启用
        fisused: 1,
        throttle:false,//节流阀
        /* 必填项 */
        errTips: {
            fname: false,
            frole: false,
            fphonenumber: false
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if (options.data) {
            const memberMessage = JSON.parse(options.data);
            const checked = (memberMessage.fisused == 1) ? true : false
            this.setData({
                fname: memberMessage.fname,
                frole: memberMessage.frole,
                fphonenumber: memberMessage.fphonenumber,
                fname: memberMessage.fname,
                subusers: memberMessage.subusers,
                fisused: memberMessage.fisused,
                tenterprise_userid: memberMessage.tenterprise_userid,
                checked
            })
        }
    },
    /* 提交 */
    submit() {
        if (!this.submitVerify()) return wx.showToast({
            title: '请检查表单内容',
            icon: "none"
        });
        if(this.data.throttle) return;
        _Http.basic({
            "accesstoken": wx.getStorageSync('userData').token,
            "classname": "customer.usercenter.teammsg.teammsg",
            "method": "update_userMsg",
            "content": {
                "tenterprise_userid": this.data.tenterprise_userid,
                "fname": this.data.fname,
                "frole": this.data.frole,
                "fphonenumber": this.data.fphonenumber,
                "subusers": this.data.subusers,
                "fisused": this.data.fisused
            }
        }).then(res => {
            console.log(res)
            if (res.msg != "成功") return wx.showToast({
                title: res.data,
                icon: "none"
            });
            //节流阀
            this.setData({
                throttle:true
            })
            wx.showToast({
                title: '保存成功',
            });
            setTimeout(() => {
                wx.navigateBack({
                    delta: 1
                })
            }, 500)

        })
    },
    /* 商户提交前验证表单 */
    submitVerify() {
        let errTips = this.data.errTips,
            verify = true;
        //账户名称
        if (!_Verify.required(this.data.fname)) {
            errTips.fname = true;
            verify = false;
        };
        //身份/职位
        if (!_Verify.required(this.data.frole)) {
            errTips.frole = true;
            verify = false;
        };
        //验证联系方式
        if (!_Verify.phoneNumber(this.data.fphonenumber)) {
            errTips.fphonenumber = true;
            verify = false;
        }
        this.setData({
            errTips
        })
        return verify;
    },
    /* 获取焦点 */
    inputFocus(e) {
        const {
            name
        } = e.currentTarget.dataset;
        let errTips = this.data.errTips;
        errTips[name] = false;
        this.setData({
            errTips
        })
    },
    /* 失去焦点 */
    inputBlur(e) {
        const {
            name
        } = e.currentTarget.dataset;
        const {
            value
        } = e.detail;
        if (name == 'fphonenumber') {
            if (!_Verify.phoneNumber(this.data.fphonenumber, 1)) return this.setData({
                "errTips.fphonenumber": true
            })
        };
        if (value.trim() == "") {
            let errTips = this.data.errTips;
            errTips[name] = true;
            this.setData({
                errTips
            })
        }
    },
    /* 开关 */
    onChange() {
        const checked = !this.data.checked,
            fisused = (checked) ? 1 : 0
        this.setData({
            checked,
            fisused
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {},

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})