import {
    ApiModel
} from "../../utils/api";
const _Http = new ApiModel();
import {
    TestVerify
} from "../../utils/verify";
const _Verify = new TestVerify();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        attinfos: [], //头像
        fname: "",
        fphonenumber: "",
        fsex: "",
        fbirthdate: "",
        femail: "",
        fwechatno: "",
        faddress: "",
        frole: "",
        YMD: "", //日期
        /* 错误提示 */
        errTips: {
            fname: false, //用户名
            frole: false, //身份/职位
            fphonenumber: false, //手机号
            femail: false, //邮箱
        },
    },
    /* input事件剔除特殊字符 */
    eliminate(value) {
        const {
            name
        } = value.target.dataset;
        this.setData({
            [name]: _Verify.Eliminate(value.detail)
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        /* 日期 */
        let time = new Date(Date.parse(new Date()) - 86400000);
        let d = time.getDate();
        if (d < 10) d = '0' + d;
        this.setData({
            YMD: time.getFullYear() + '-' + (time.getMonth() + 1) + '-' + d
        });
        const userMessage = JSON.parse(options.data);
        let attinfos = [];
        if (userMessage.attinfos.length >= 1) {
            attinfos = [{
                url: userMessage.attinfos[0].fobsurl,
                ownerid: userMessage.attinfos[0].ownerid,
                tattachmentid: userMessage.attinfos[0].tattachmentid,
                ownertable: userMessage.attinfos[0].ownertable,
                fdocument: userMessage.attinfos[0].fdocument
            }];
        }
        this.setData({
            attinfos,
            fname: userMessage.fname,
            fphonenumber: userMessage.fphonenumber,
            fsex: userMessage.fsex,
            fbirthdate: userMessage.fbirthdate,
            femail: userMessage.femail,
            fwechatno: userMessage.fwechatno,
            faddress: userMessage.faddress,
            frole: userMessage.frole
        })
    },
    timeChange({
        detail
    }) {
        this.setData({
            fbirthdate: detail.value
        })
    },
    /* 修改性别 */
    sexOnChange(event) {
        this.setData({
            fsex: event.detail,
        });
    },
    /* 表单验证 */
    formVerify() {
        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 (this.data.errTips.femail && this.data.femail != '') {
            wx.showToast({
                title: '请检查邮箱格式！',
                icon: "none"
            })
            verify = false;
        }
        if (!verify) {
            this.setData({
                errTips
            })
        }
        return verify;
    },
    /* 提交 */
    submit() {
        if (!this.formVerify()) return wx.showToast({
            title: '请检查表单内容',
            icon: "none"
        });
        _Http.basic({
            "accesstoken": wx.getStorageSync('userData').token,
            "classname": "customer.usercenter.usermsg.usermsg",
            "method": "update_usermsg",
            "content": {
                "fname": this.data.fname,
                "fsex": this.data.fsex,
                "fbirthdate": this.data.fbirthdate,
                "femail": this.data.femail,
                "fwechatno": this.data.fwechatno,
                "faddress": this.data.faddress,
                "frole": this.data.frole
            }
        }).then(res => {
            console.log(res)
            if (res.msg == "成功") {
                wx.showToast({
                    title: '保存成功',
                })
                setTimeout(() => {
                    wx.navigateBack({
                        delta: 1
                    })
                }, 500)
            } else {
                wx.showToast({
                    title: res.data,
                    icon: 'none'
                })
            }
        })
    },
    /* 获取焦点 */
    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 == 'femail') {
            var reg = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/;
            if (!reg.test(value.trim())) {
                wx.showToast({
                    title: '请检查邮箱格式！',
                    icon: "none"
                })
                let errTips = this.data.errTips;
                errTips[name] = true;
                this.setData({
                    errTips
                })
            }
            return;
        }
        if (value.trim() == "") {
            let errTips = this.data.errTips;
            errTips[name] = true;
            this.setData({
                errTips
            })
        }
    },
    /* 修改用户头像 */
    userImageChange(data) {
        this.setData({
            attinfos: data.detail.fileList
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    }
})