/*
 * 手太欠
 * 企获客商城
 */

const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        couponList  : [],
        couponMenu  : [
            {
                type: 'all',
                text: '全部'
            },
            {
                type: 1,
                text: '免费券'
            },
            {
                type: 2,
                text: '代金券'
            },
            {
                type: 3,
                text: '提货卡'
            }
        ],
        currentTab  : 0,
        type        :'',
        total_page  :'',
        current     :'',
        page        :'',
        default     :'',
        nomore      : false
    },

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

        this.couponInfo();
    },
    couponInfo() {

        wx.$api.mall.coupon({
            type: this.data.type || '',
            page: this.data.page || ''
        } ).then(res => {
            console.log(res)
            let newrcoupon =  res.data;
            this.setData({
                couponList: this.data.couponList.concat(newrcoupon), //优惠券数组
                total_page:res.page.total_page,
                current:res.page.current,
            })
            if(this.data.couponList.concat(newrcoupon).length==0){
                this.setData({
                    default:true
                })
            }else{
                this.setData({
                    default:''
                })
            }
        })

    },
    couponChange(e) {
        let cur = e.target.dataset.current;
        let type = e.target.dataset.type;
        this.setData({
            currentTab: cur,
            type:type,
            page:'',
            couponList:[],
            nomore: false
        })
        this.couponInfo();
    },
    receive(e) {
        if (wx.getStorageSync("token") == "") {
            wx.navigateTo({
                url: '/pages/login/login'
            })
            return
        }
        console.log('e', e)
        let coupon_id = e.target.dataset.id;
        wx.$api.mall.grant({
            coupon_id: coupon_id
        }).then(res => {
            console.log(res)
            wx.showToast({
              title: res,
              icon : 'none'
            })
        })
    },
    linkMy(){
        wx.navigateTo({
            url: '/pages/user_coupon/user_coupon'
        })
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        let total_page = this.data.total_page;
        let current = parseInt(this.data.current) + 1;
        this.setData({
            page: current
        })
        if(current > total_page){
            this.setData({
                nomore: true
              })
        }else{
            this.couponInfo();
        }
    }
})