// nova-travel/page/cauth/cauth.js
var qiniuUploader = require("../../../../utils/qiniuUploader");
var Parse = getApp().Parse;
var company = getApp().globalData.company


Page({

    /**
     * 页面的初始数据
     */
    data: {
        focus: false,
        fileList: [],
        fileList_1: [],
        uploadURL: "", //七牛参数
        domain: "", //七牛参数
        uptokenURL: "", //七牛参数
        optionId: "",
        auth: true,
        activeColor:getApp().globalData.activeColor,
        titleColor:getApp().globalData.titleColor
    },
    bindKeyInput(e) {
        this.setData({
            [e.currentTarget.dataset.prop]: e.detail.value
        })
    },
    submit() {
        const _this = this
        if (!this.data.name || !this.data.mobile || !this.data.id) {
            wx.showToast({
                title: '请填写完整信息',
                icon: 'none',
                duration: 2000
            })
            return
        }
        let name = /[\u4e00-\u9fa5]/,
            text = this.data.name;
        if (!name.test(text)) {
            wx.showToast({
                title: '请输入您的真实姓名',
                icon: 'none',
                duration: 2000
            })
            return
        }
        let str = this.data.mobile;
        let exist = /^1[34578]\d{9}$/.test(str);
        if (exist == false) {
            wx.showToast({
                title: '手机号码格式有误',
                icon: 'none',
                duration: 2000
            })
            return
        }
        let idReg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/gi
        let id = this.data.id
        if (!idReg.test(id)) {
            wx.showToast({
                title: '请输入正确的身份证号',
                icon: 'none',
                duration: 2000
            })
            return
        }
        wx.request({
            url: "https://server.fmode.cn/api/apig/idcard", //请求接口的url
            method: "POST", //请求方式
            data: {
                company: company,
                cardNo: id,
                realName: text
            }, //请求参数
            header: {
                "content-type": "application/json", // 默认值
            },
            success: (res) => {
                console.log(res.data);
                console.log(res.data.data.result);
                if (res.data.data.result.isok) {
                    let User = Parse.Object.extend("_User")
                    let user = new User()
                    user.id = Parse.User.current().id
                    user.set("authentication", {
                        "id": _this.data.id,
                        "name": _this.data.name,
                        "mobile": _this.data.mobile
                    })
                    user.set('realname',_this.data.name)
                    user.set('idcard',_this.data.id)
                    user.save().then(res => {
                        // 弹窗
                        wx.showToast({
                            title: '提交成功',
                            icon: 'none',
                            duration: 2000
                        })
                        // 重新拉取数据
                        _this.getUserInfo()
                    })
                } else {
                    wx.showToast({
                        title: '请输入正确的身份证号和姓名',
                        icon: 'none',
                        duration: 2000
                    })
                }
            },
        })
    },
    getUserInfo() {
        const _this = this
        let User = new Parse.Query("_User")
        User.equalTo("objectId", Parse.User.current().id)
        User.first().then(res => {
            let user = res.toJSON()
            console.log(user)
            if (user.authentication && user.authentication.id) {
                console.log(user.authentication)
                user.authentication.id = user.authentication.id.replace(/^(.{3})(?:\d+)(.{4})$/, "$1***********$2")
                user.authentication.mobile = user.authentication.mobile.replace(/^(.{3})(?:\d+)(.{4})$/, "$1****$2")
                _this.setData({
                    userInfo: user.authentication,
                    auth: true
                })
            } else {
                _this.setData({
                    auth: false
                })
            }
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getUserInfo()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    }
})