// pages/mine/vip/vipcenter.js
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        name: '用户名称',
        userIcon: '/images/ic_user_default.png',
        curPage: 1,
        isRefreshing: false, //是否在刷新中
        hasMore: true, //是否有更多数据
        isLoadMore: false, //是否正在加载更多
        recordList: [],
        isDredge: true, //是否开通过会员
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {},
    onShow(e) {
        var name = wx.getStorageSync('name')
        var userIcon = wx.getStorageSync('userIcon')
        if (name) {
            this.setData({
                name: name,
                userIcon: userIcon
            })
        }
        wx.startPullDownRefresh({})
        // this.getPayState()
        // this.getPayHistoryList()
    },
    getPayState() {
        wx.showLoading({
            title: '加载中...',
        })
        var _self = this
        app.http.get(app.urls.getPayState, {
                header: {
                    token: app.globalData.token
                }
            })
            .then(res => {
                wx.hideLoading({})
                //支付过跳转名片创建页面,没有支付跳转支付页面
                //第一次开通
                _self.setData({
                    isDredge: res.data.cardCharge && res.data.cardChargeEnd == ''
                })
                if (res.data.cardChargeEnd && res.data.cardChargeEnd.length > 0) {
                    res.data.cardChargeEnd = _self.formatDate(res.data.cardChargeEnd)
                }
                _self.setData({
                    payState: res.data
                })
            })
            .catch(err => {

            })
    },
    formatDate(date) {
        if (date.length > 0) {
            return date.slice(0, 11)
        } else {
            return ''
        }
    },
    //获取支付历史列表
    getPayHistoryList() {
        var _self = this
        wx.showLoading({
            title: '加载中...',
        })
        app.http.get(app.urls.getPayHistoryList, {
                header: {
                    token: app.globalData.token
                },
                data: {
                    page: _self.data.curPage,
                    rows: '10'
                }
            })
            .then(res => {
                wx.stopPullDownRefresh({})
                wx.hideLoading({})
                _self.data.recordList = _self.data.recordList.concat(res.data.rows)
                var more = _self.data.recordList.length < res.data.total
                _self.setData({
                    recordList: _self.data.recordList,
                    isRefreshing: false,
                    isLoadMore: false,
                    hasMore: more
                })
            })
            .catch(err => {
                wx.stopPullDownRefresh({})
                _self.setData({
                    isRefreshing: false,
                    isLoadMore: false,
                    hasMore: true
                })
            })
    },

    //立即续费
    doRenew() {
        wx.navigateTo({
            url: '/packagecard/paypage/paypage',
        })
    },
    //加载更多
    doLoadMore() {
        var _self = this
        if (_self.data.hasMore) {
            if (_self.data.isLoadMore) {
                return
            }
            this.setData({
                isLoadMore: true,
                curPage: ++_self.data.curPage
            })
            this.getPayHistoryList()
        }
    },
    onReachBottom() {
        this.doLoadMore()
    },
    //下拉刷新
    onPullDownRefresh() {
        this.setData({
            isLoadMore: false,
            isRefreshing: true,
            hasMore: true,
            recordList: [],
            curPage: 1
        })
        this.getPayState()
        this.getPayHistoryList()
    }
})