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

    /**
     * 页面的初始数据
     */
    data: {
        userMessage: {}, //头像
        fname: "",
        fphonenumber: "",
        fsex: "",
        fbirthdate: "",
        femail: "",
        fwechatno: "",
        faddress: "",
        frole: "",
        /* 错误提示 */
        errTips: {
            fname: false, //用户名
            frole: false, //身份/职位
            fphonenumber: false, //手机号
        },
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        const userMessage = JSON.parse(options.data);
        const attinfos = {
            attinfos: userMessage.attinfos
        }
        this.setData({
            userMessage: 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
        })
    },
    /* 打开时间选择器 */
    setDate() {
        this.selectComponent("#SetDate").dateOnClose()
    },
    /* 设置生日 */
    setBbirthday(date) {
        this.setData({
            fbirthdate: date.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 (!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 => {
            if (res.msg == "成功") {
                wx.showToast({
                    title: '保存成功',
                })
                setTimeout(() => {
                    wx.navigateBack({
                        delta: 1
                    })
                }, 500)
            }
        })
    },
    /* 获取焦点 */
    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 (value.trim() == "") {
            let errTips = this.data.errTips;
            errTips[name] = true;
            this.setData({
                errTips
            })
        }
    },
    /* 修改用户头像 */
    userImageChange(data) {
        const attinfos = [{
            ownerid: data.detail.fileList[0].ownerid,
            tattachmentid: data.detail.fileList[0].tattachmentid,
            fobsurl: data.detail.fileList[0].url
        }]
        this.setData({
            'userMessage.attinfos': attinfos
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    }
})