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

const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        stateType   : 'ALL',   //卡券分类
        couponList  : [],
        status      : 1,
        type        : '',
        statusTab   : 0,
        currentTab  : 0,
        statusMenu  : [
            {
                type: 1,
                text: '未使用'
            },
            {
                type: 2,
                text: '已使用'
            },
            {
                type: 3,
                text: '已失效'
            }
        ],
        couponMenu  : [
            {
                type: '',
                text: '全部'
            },
            {
                type: 1,
                text: '免费券'
            },
            {
                type: 2,
                text: '代金券'
            },
            {
                type: 3,
                text: '提货卡'
            }
        ],
        coupontype  : '',
        couponstatus: '1',
        total_page  :'',
        current     :'',
        page        :'',
        default     :'',
        nomore      :false
    },

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

    couponInfo() {
        if (wx.getStorageSync("token") == "") {
            wx.navigateTo({
                url: '/pages/login/login'
            })
            return
        }
        wx.$api.mall.coupons({
            type    : this.data.coupontype || '',
            status  : this.data.couponstatus || '',
            page    : this.data.page || ''
        }).then(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:''
                })
            }
        })
    },
    statusChange(e) {
        let cur          = e.target.dataset.current;
        let couponstatus = e.target.dataset.type;
        this.setData({
            statusTab   : cur,
            couponstatus:couponstatus,
            coupontype  :'',
            page        :'',
            couponList  :[],
            currentTab  :0,
            nomore      :false
        })
        this.couponInfo()
    },

    couponChange(e){
        let cur  = e.target.dataset.current;
        let type = e.target.dataset.type;
        this.setData({
            currentTab  : cur,
            coupontype  :type,
            page        :'',
            couponList  :[],
            nomore      :false
        })
        this.couponInfo();
    },
    detailLink(e){
        let verification    = e.target.dataset.verification;
        let itemtype        = e.target.dataset.itemtype;
        let couponid        = e.target.dataset.couponid;
        if(itemtype==1){
            var type='free'
        }else if(itemtype==2){
            var type='cash'
        }else if(itemtype==3){
            var type='rebate'
        }
        if(verification==2){
            wx.navigateTo({
                url: '/pages/user/user_coupon_data/user_coupon_data?user_coupon_id='+couponid+'&type='+type
            })
        }else if(verification==1){
            wx.navigateTo({
                url: '/pages/ticket/goods/goods?user_coupon_id='+couponid
            })
        }


    },
    
    /**
     * 页面上拉触底事件的处理函数
     */
    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();
        }
    }
})