// pages/my-account/index.js
import {
    getProductHot
} from '../../api/store.js';
import {
    coinTransfer
} from '../../api/mine.js'
import {
    openRechargeSubscribe
} from '../../utils/SubscribeMessage.js';
import {
    getUserInfo,
    userActivity
} from '../../api/user.js';
import d from '../../utils/d.js';

const app = getApp();

Page({
    /**
     * 页面的初始数据
     */
    data: {
        parameter: {
            'navbar': '1',
            'return': '1',
            'title': '钱包',
            'color': false,
        },
        showPopup: false,
        transferAmount: 0, // int 转出数量
        transferSymbol: '', // 转的种类
        iShidden: false,
        userInfo: {},
        host_product: [],
        isClose: false,
        recharge_switch: 0,
        activity: {
            is_pink: false,
            is_seckill: false,
            is_bargin: false
        }
    },

    /**
     * 登录回调
     */
    onLoadFun: function () {
        this.getUserInfo();
        this.get_host_product();
        // this.get_activity();
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if (app.globalData.token) this.setData({
            iShidden: true
        });
    },

    openSubscribe: function (e) {
        let page = e.currentTarget.dataset.url;
        wx.showLoading({
            title: '正在加载',
        })
        openRechargeSubscribe().then(res => {
            wx.hideLoading();
            wx.navigateTo({
                url: page,
            });
        }).catch(() => {
            wx.hideLoading();
        });
    },

    // 点击币
    onTapCoinItem: function (ev) {
        // get amount
        let amount = 0;
        for (let i = 0; i < this.data.userInfo.coins.length; i++) {
            if (this.data.userInfo.coins[i].symbol === ev.currentTarget.dataset.id) {
                amount = this.data.userInfo.coins[i].total;
            }
        }
        // check total to be transfered
        if (amount < 1) {
            wx.showToast({
                title: '金额不足以转入',
            })
            return
        }
        this.setData({
            transferAmount: Math.floor(amount),
            transferSymbol: ev.currentTarget.dataset.id,
            showPopup: true,
        })
    },

    onConfirmTransfer: function () {
        this.onCancelTransfer()
        // check if input value valid
        const regex = new RegExp('^[0-9]+$')
        if (!regex.test(this.data.transferAmount)) {
            wx.showToast({
                title: '您的输入有误',
            })
            return
        }
        // check if value great than total
        let total = 0
        for (let i = 0; i < this.data.userInfo.coins.length; i++) {
            if (this.data.userInfo.coins[i].symbol === this.data.transferSymbol) {
                total = this.data.userInfo.coins[i].total;
            }
        }
        if (total < 1 || this.data.transferAmount > total) {
            wx.showToast({
                title: '输入错误',
            })
            return
        }
        // call API
        let that = this
        coinTransfer(this.data.transferAmount, this.data.transferSymbol).then(res => {
            wx.showToast({
                title: '转出成功',
            })
            that.getUserInfo();
        }).catch(err => {
            wx.showToast({
                title: err,
            })
        })
    },

    onCancelTransfer: function () {
        this.setData({
            showPopup: false,
        })
    },

    /**
     * 获取用户详情
     */
    getUserInfo: function () {
        let that = this;
        getUserInfo().then(res => {
            that.setData({
                userInfo: res.data,
                recharge_switch: res.data.recharge_switch
            });
            // app.globalData.unread = res.data.notice
            app.setUnread(res.data.notice)
        });
    },

    /**
     * 获取活动可参与否
     */
    get_activity: function () {
        var that = this;
        userActivity().then(res => {
            that.setData({
                activity: res.data
            });
        })
    },

    /**
     * 获取我的推荐
     */
    get_host_product: function () {
        var that = this;
        getProductHot().then(res => {
            that.setData({
                host_product: res.data
            });
        })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        if (app.globalData.isLog && this.data.isClose) {
            this.getUserInfo();
            this.get_host_product();
            // this.get_activity();
            this.setData({
                iShidden: true
            });
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
        this.setData({
            isClose: true
        });
    },
})