const Parse = getApp().Parse
    // const company = getApp().globalData.company
const dateF = require('../../../utils/date')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        consume: 0,
        activeColor: "",
        titleColor: "",
        currentDate: new Date().getTime(),
        minDate: (new Date().getTime() - 70 * 12 * 30 * 24 * 60 * 60 * 1000),
        account: null,
        mobile: null,
        nickname: null,
        actions: [{
                name: '男',
            },
            {
                name: '女',
            },
        ],
        images: []
    },
    onSelect(event) {
        console.log(event.detail);
        let {
            name
        } = event.detail
        let {
            user
        } = this.data
        user.sex = name
        this.setData({
            user,
            show2: false
        })
    },
    async getUser() {
        let id = Parse.User.current() ? Parse.User.current().id : ""
        if (!id) {
            wx.showToast({
                title: '您还未登录请先登录',
                icon: 'error'
            })
            setTimeout(() => {
                wx.navigateBack({
                    delta: 1
                });
            }, 1000)
            return
        }
        let new_user = new Parse.Query('_User')
        new_user.include('invite')
        new_user.include('agentLevel')
        new_user.select('objectId', 'avatar', 'authentication', 'nickname', 'mobile', 'invite.nickname',
            'agentLevel.name')
        let res = await new_user.get(id)
        let user = res.toJSON()
        user.joinTime = dateF.formatTime("YYYY-mm-dd", user.createdAt)
        if (user.authentication && user.authentication != {}) {
            user.auth = { isAuth: true, label: "已认证" }
        } else {
            user.auth = { isAuth: false, label: "未认证" }
        }
        console.log(user.avatar)
        this.setData({
            user: user,
            images: [user.avatar]
        })
    },
    // 会员头像，会员昵称，手机号,会员性别，实名认证,会员等级,会员ID, 邀请人,
    async getAccount() {
        let Account = new Parse.Query('Account')
        Account.equalTo('user', Parse.User.current().id)
        Account.select('bank', 'wechat')
        let account = await Account.first()
        let a = account.toJSON()
        if (a.bank && a.bank.length > 0) {
            a.isBank = { isBank: true, label: "已绑定" }
        } else {
            a.isBank = { isBank: false, label: "未绑定" }
        }
        if (a.wechat) {
            a.isWechat = { isWechat: true, label: "已绑定" }
        } else {
            a.isWechat = { isWechat: false, label: "未绑定" }
        }
        if (account && account.id) {
            this.setData({
                account: a
            })
        }
    },

    editMobile(e) {
        let mobile = e.detail.value
        let a = /^1[3456789]\d{9}$/;
        if (!mobile.match(a)) {
            wx.showToast({
                icon: "none",
                title: "请填写正确的手机记号格式",
            });
            return;
        } else {
            this.setData({
                mobile: mobile
            })
        }
    },
    changeImage(e) {
        if (e.detail && e.detail.length > 0) {
            this.setData({
                image: [e.detail[0].url]
            })
        } else {
            this.setData({
                image: []
            })
        }
    },
    editNickname(e) {
        let nickname = e.detail
        this.setData({
            nickname: nickname
        })
    },
    onInput(event) {
        this.setData({
            currentDate: event.detail,
        });
    },
    showPicker() {
        this.setData({
            show: true
        })
    },
    //取消日期选择
    onClose() {
        this.setData({
            show: false,
            show2: false
        })
    },
    showSelect() {
        this.setData({
            show2: true
        })
    },
    async submit() {
        let {
            sex
        } = this.data.user
        console.log(this.data.user)
        let id = Parse.User.current().id
        let new_user = new Parse.Query('_User')
        let res = await new_user.get(id)
        if (sex) {
            res.set("sex", sex)
        }
        if (this.data.mobile) {
            res.set("mobile", this.data.mobile)
        }
        if (this.data.nickname) {
            res.set("nickname", this.data.nickname)
        }
        let updateuser = await res.save()
        if (updateuser) {
            console.log(updateuser)
            wx.showToast({
                title: '修改成功',
                icon: 'success',
                image: '',
                duration: 1000,
                mask: false,
                success: (result) => {
                    setTimeout(() => {
                        wx.navigateBack({
                            delta: 1
                        });
                    }, 1000);
                },
                fail: () => {},
                complete: () => {}
            });
        }

    },

    toAuth() {
        wx.navigateTo({
            url: '/common-page/pages/info/cauth/cauth'
        })
    },

    bindWechat(e) {
        wx.navigateTo({
            url: `/common-page/pages/info/bindWechat/index?way='wechat'`,
        })
    },
    bindBank() {
        wx.navigateTo({
            url: `/common-page/pages/info/bindBank/index`,
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.getUser()
        this.getAccount()

        console.log(getApp().globalData.activeColor, 'getApp().globalData.activeColor')
        this.setData({
            activeColor: getApp().globalData.activeColor,
            titleColor: getApp().globalData.titleColor
        })
        console.log(this.data.activeColor);
    },

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

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        this.getUser()
        this.getAccount()
    },

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

    },

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

    },

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

    },

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

    },

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

    }
})