const Parse = getApp().Parse;
const company = getApp().globalData.company
Page({

    /**
     * 页面的初始数据
     */
    data: {
        hasInfo: false,
        name: '',
        accountId: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(options)
        this.setData({
            way: options.way
        })
        this.getAuthInfo(options.way)
    },
    bindKeyInput(e) {
        this.setData({
            [e.currentTarget.dataset.prop]: e.detail.value
        })
    },
    getAuthInfo(type) {
        const _this = this
        let account = new Parse.Query("Account")
        account.equalTo("user", Parse.User.current().id)
        account.first().then(res => {
            console.log(res)
            let info = res.toJSON()
            console.log(type, info)
            if (type == 'alipay' && info.ali) {
                console.log(222)
                _this.setData({
                    name: info.ali.name,
                    accountId: info.ali.accountId,
                    hasInfo: true
                })
            }
            if (type == 'wechat' && info.wechat) {
                _this.setData({
                    name: info.wechat.name,
                    accountId: info.wechat.accountId,
                    hasInfo: true
                })
            }
            _this.setData({
                account: res.id
            })
        })
    },
    submit() {
        const _this = this
        if (!this.data.name || !this.data.accountId) {
            wx.showToast({
                icon: 'none',
                title: '请填写完整信息',
                duration: 2000
            })
            return
        }
        try {
            let Account = Parse.Object.extend("Account")
            let account = new Account()
            account.id = this.data.account
            this.data.way == 'alipay' ? account.set('ali', {
                name: _this.data.name,
                accountId: _this.data.accountId
            }) : account.set('wechat', {
                name: _this.data.name,
                accountId: _this.data.accountId
            })
            account.save().then(res => {
                wx.showToast({
                    icon: 'none',
                    title: '保存成功',
                    duration: 2000
                })
                _this.setData({
                    hasInfo: true
                })
            })
        } catch (error) {
            console.log(error)
            wx.showToast({
                icon: 'none',
                title: '保存失败',
                duration: 2000
            })
        }
    },
    change() {
        this.setData({
            hasInfo: false
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    }
})