<template>
    <view class="p10">
        <view class="bgf tc br8" style="height: calc(100vh - 20px)">
            <i class="iconfont icon-zhanghuyue"></i>
            <view class="mt30 f16 c3">账户余额</view>
            <view class="wxMoney">
                <text class="moneyFu">￥</text>
                {{ balance }}
            </view>
            <view>
                <!-- <navigator  url="/pages/withdraw/index" class="loginOut dib" style="font-weight:400;margin-top:70px;">
            提现
        </navigator> -->
                <button class="loginOut dib" @click.stop="goWithDraw" style="font-weight: 400; margin-top: 70px">提现</button>
                <button
                    class="loginOut dib"
                    v-if="isLogin && (openId == '' || openId == null || openId == 'undefined')"
                    open-type="getUserInfo"
                    @click.stop="getUserInfoBtn"
                    style="font-weight: 400; margin-top: 80px"
                >
                    提现
                </button>
            </view>
            <navigator url="/pages/withdrawDetail/index" class="txmx" hover-class="thover">提现明细</navigator>
        </view>

        <!-- <navigator wx:if="{{isLogin && openId != '' && openId != null && openId != 'undefined'}}" url="/pages/withdraw/index" class="moneyCash">提现</navigator>
      <button wx:if="{{isLogin && (openId == '' || openId == null || openId == 'undefined')}}" type="primary" class="moneyCashBtn" hover-class="thover" open-type="getUserInfo" catchtap="getUserInfoBtn">提现</button> -->
    </view>
</template>

<script>
// pages/wxMoney/index.js
const app = getApp();
const commonUtil = require('../../../utils/commonUtil.js');
export default {
    data() {
        return {
            balance: '0.00',
            auth: 0,
            openId: '',
            isLogin: false,
            hasUserInfo: {},
            agencyStatus: 0,
            user: '',
            wxCode: ''
        };
    }
    /**
     * 生命周期函数--监听页面加载
     */,
    onLoad: function (options) {},
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        uni.showLoading({
            title: '加载中...'
        });
        this.wxLogin();
        if (app.globalData.isLogin) {
            this.setData({
                isLogin: app.globalData.isLogin,
                hasUserInfo: app.globalData.hasUserInfo,
                agencyStatus: app.globalData.agencyStatus,
                user: app.globalData.user
            });
            this.getWalletInfo();
        } else {
            // 由于 userLogin 是网络请求，可能会在 Page.onLoad 之后才返回
            // 所以此处加入 callback 以防止这种情况
            app.globalData.userLoginCallback = (res) => {
                this.setData({
                    isLogin: app.globalData.isLogin,
                    hasUserInfo: app.globalData.hasUserInfo,
                    agencyStatus: app.globalData.agencyStatus,
                    user: app.globalData.user
                });
                this.getWalletInfo();
            };
        }
        uni.hideLoading();
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {},
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {},
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {},
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {},
    methods: {
        getWalletInfo: function () {
            var that = this;
            uni.request({
                url: app.globalData.ip + '/wallet/info',
                data: {},
                header: app.globalData.headers,
                method: 'GET',
                success: function (res) {
                    console.log(res);
                    if (res.data.status == 200) {
                        console.log(res);
                        //绑定成功
                        // console.log(new Number(res.data.data.balance / 100).toFixed(2));
                        // console.log( commonUtil.moneyToFixed(res.data.data.balance) + '');

                        that.setData({
                            balance: new Number(res.data.data.balance / 100).toFixed(2),
                            auth: res.data.data.auth,
                            openId: res.data.data.openId
                        });
                        uni.hideLoading({
                            success: (res) => {}
                        });
                    } else {
                        uni.showToast({
                            icon: 'none',
                            title: res.data.msg
                        });
                    }
                },
                fail: function (res) {
                    console.log('操作失败');
                }
            });
        },

        wxLogin() {
            var that = this;
            uni.login({
                success(res) {
                    if (res.code) {
                        that.setData({
                            wxCode: res.code
                        });
                    } else {
                        console.log('获取code失败！' + res.errMsg);
                    }
                }
            });
        },

        goWithDraw() {
            uni.navigateTo({
                url: '/pages/withdraw/index'
            });
        },

        getUserInfoBtn: function (e) {
            console.log(e);
            let that = this;
            uni.getUserProfile({
                desc: '用于完善会员资料',
                // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
                success: (res) => {
                    console.log(res);
                    let encryptedData = res.encryptedData;
                    let iv = res.iv;
                    //发起网络请求
                    uni.request({
                        url: app.globalData.ip + '/decodeUserInfo',
                        data: {
                            encryptedData: encryptedData,
                            iv: iv,
                            code: that.wxCode
                            // type: 'dtdl',
                        },
                        method: 'POST',
                        header: app.globalData.headers,
                        success: function (res) {
                            console.log(res);
                            if (res.data.status == 200) {
                                //设置头像昵称成功，进去提现页面
                                uni.navigateTo({
                                    url: '/pages/withdraw/index'
                                });
                            } else {
                                uni.showToast({
                                    icon: 'none',
                                    title: res.data.msg
                                });
                            }
                        },
                        fail: function (res) {}
                    });
                },
                fail: (res) => {
                    console.log(res);
                    uni.showToast({
                        icon: 'none',
                        title: '获取授权失败，将影响您的提现。'
                    });
                }
            });
        }
    }
};
</script>
<style>
@import './index.css';
</style>
