import {
    ApiModel
} from "../../utils/api";
const _Http = new ApiModel();
import {
    TestVerify
} from "../../utils/verify"
const _Verify = new TestVerify();
const utilMd5 = require('../../utils/md5')
let countDown = null;
Page({
    /**
     * 页面的初始数据
     */
    data: {
        fname: '', //用户名称
        frole: '', //身份/职位
        fphonenumber: '', //手机号码
        subusers: [], //权限
        tenterprise_userid: 0, //用户id
        checked: true, //是否启用
        fisused: 1,
        throttle: false, //节流阀
        makeOver: false,
        showModel: false,
        codeChange: "", //验证码
        codeCount: 60,
        verificationCode: "",
        /* 必填项 */
        errTips: {
            fname: false,
            frole: false,
            fphonenumber: false
        },
        tispText: "", //验证码提示文本
    },
    /* input事件剔除特殊字符 */
    eliminate(value) {
        const {
            name
        } = value.target.dataset;
        this.setData({
            [name]: _Verify.Eliminate(value.detail)
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if (options.data) {
            const memberMessage = JSON.parse(options.data);
            console.log(memberMessage)

            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,
                fisadministrator: memberMessage.fisadministrator
            })
        }
    },
    /* 获取验证码 */
    getAuthCode() {
        if (this.data.codeCount != 60) return wx.showToast({
            title: '已发送验证码，请勿重复获取',
            icon: "none"
        })
        _Http.basic({
            "accesstoken": wx.getStorageSync('userData').token,
            "classname": "customer.usercenter.teammsg.teammsg",
            "method": "getPassword",
            "content": {
                "tenterprise_userid": this.data.tenterprise_userid
            }
        }).then(res => {
            if (res.data != '成功') return;
            const that = this;
            /* 已将验证码发送到受让方 */
            wx.showToast({
                title: res.msg,
                icon: "none",
                duration: 3000
            })
            this.setData({
                tispText: "已发送到对方手机号"
            })
            that.setData({
                codeCount: this.data.codeCount - 1
            });
            countDown = setInterval(() => {
                if (this.data.codeCount == 0) {
                    that.setData({
                        codeCount: 60
                    });
                    clearInterval(countDown)
                    return
                }
                that.setData({
                    codeCount: this.data.codeCount - 1
                });
            }, 1000)
        })
    },
    /* 二次确认回调 */
    callBack({
        detail
    }) {
        clearInterval(countDown)
        this.setData({
            codeCount: 60,
            tispText: ''
        });
        if (detail == 'true') {
            if (this.data.verificationCode == '') return wx.showToast({
                title: '验证码不能为空',
                icon: "none"
            })
            _Http.basic({
                "accesstoken": wx.getStorageSync('userData').token,
                "classname": "customer.usercenter.teammsg.teammsg",
                "method": "changeAdministrator",
                "content": {
                    "tenterprise_userid": this.data.tenterprise_userid,
                    "fpassword": utilMd5.hexMD5(this.data.verificationCode)
                }
            }).then(res => {
                console.log(res)
                if (res.msg != '成功') return wx.showToast({
                    title: res.msg,
                    icon: "none"
                })
                wx.showToast({
                    title: '转让成功！请您重新登录',
                    icon: "none",
                });
                _Http.logout({
                    "accesstoken": wx.getStorageSync('userData').token
                }, false)
                setTimeout(() => {
                    wx.reLaunch({
                        url: '/pages/login/index',
                    })
                }, 1000)
            })
        } else {

        }
    },
    /* 转让主账号 */
    makeOverChange() {
        this.setData({
            showModel: true
        })
    },
    /* 验证码输入 */
    codeChange(e) {
        this.setData({
            verificationCode: e.detail.value
        })
    },
    /* 提交 */
    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() {
        if (this.data.fisadministrator == 1) return wx.showToast({
            title: '商户主账号不可停用',
            icon: "none"
        });
        const that = this;
        if (this.data.tenterprise_userid != 0) {
            wx.showModal({
                title: "提示",
                content: (this.data.checked) ? '是否停用“' + this.data.fname + '”，停用后该角色将暂时无法登陆' : '是否启用“' + this.data.fname + '”',
                success(res) {
                    if (res.confirm) {
                        const checked = !that.data.checked,
                            fisused = (checked) ? 1 : 0
                        that.setData({
                            checked,
                            fisused
                        })
                    }
                }
            })
        } else {
            const checked = !that.data.checked,
                fisused = (checked) ? 1 : 0
            that.setData({
                checked,
                fisused
            })
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

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

    },

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

    },

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

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

    },

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

    },

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

    }
})