var app = getApp();
var $ = require('../../../../js/util.js');
Page({
  data: {
    user: [],
    tabTxt: ['未使用', '已使用', '已过期'],//tab文案
    tab: [false, true, true],
    type: 0,
    list: [],//数据
    page: 1,//当前页码
    param: {
      type: 0
    },
    total_page:1,
    is_show: true,
    coupon_show:{},
    hasMore: false,//无更多数据提示
    dataNull: true//未获取到数据提示
  },
  onLoad: function (options) {//接收上个页面传的参数
    app.HCsetNavigationBarTitle('我的优惠券');
    if (options.invite_key) {
      app.globalData.invite_key = options.invite_key;
    }
  },
  onReady: function () {
    var self = this;
    app.getUserInfo(function (ret) {
      self.setData({
        user: ret
      });
      if (self.data.user != '') {
        self.setData({
          param: {
            type: self.data.type,
            page: self.data.page,
          }
        });
        self.getData();
      }
    });
  },
  // 选项卡
  filterTab: function (e) {
    var data = [true, true, true], index = e.currentTarget.dataset.index;
    if (this.data.tab[index] == true) {
      data[index] = false;
      this.setData({
        tab: data,
        page: 1,
        type: index,
        hasMore: false,
        dataNull: true,
      })
      if (this.data.user != '') {
        this.setData({
          param: {
            type: this.data.type,
            page: this.data.page,
          }
        });
        this.getData();
      }
    }
  },
  //加载数据
  getData: function (callback) {
    var self = this;
    app.showLoading('加载中...');
    var action = 'c=Member&a=mycoupon_list';
    app.ajaxRequest(action, 'GET', $.ObjectToStr(self.data.param), function (ret) {
      if (ret.status == 1) {
        $.init_page_list(self, ret.data);
        var initlist = self.data.modalhidden;
        for (var i = 0; i < ret.sn_hid.length; i++) {
          if (ret.sn_hid[i]) {
            initlist.push(ret.sn_hid[i]);
          }
        }
        self.setData({
          c_type: ret.c_type,
          modalhidden: initlist,
          total_page: ret.totalpage
        })
      } else if (ret.status == 2) {
        self.setData({
          total_page: 0
        })
        $.init_page_empty_list(self, ret.msg);
      }
    }, true);
  },
  onReachBottom: function () {
    var self = this;
    if (self.data.user != '') {
      self.data.page++;
      self.setData({
        param: {
          type: self.data.type,
          page: self.data.page,
        }
      });
      self.getData();
    }
  },
  //取消二维码弹窗
  lookconfirm: function (event) {
    this.setData({
      is_show: true
    })
  },
  onShareAppMessage: function () {
    return {
      title: '我的优惠券',
      desc: '我的优惠券!',
      path: '/' + $.getCurrentPageUrl() + '?invite_key=' + app.globalData.init_invite_key
    }
  },
  //跳转优惠券详情
  goToDetaile:function (event) {
    app.HCnavigateTo('../cont/cont?id=' + event.currentTarget.dataset.gid);
  },
  //立即使用
  goToUse: function (event) {
   app.HCswitchTab('../../../index/index'); 
  },
  onPullDownRefresh: function () {
    this.setData({
      list: [],
      page: 1,
      param: { type: 0},
      hasMore: false,
      dataNull: true,
      type: 0,
      tab: [false, true, true],
    });
    this.getData();
  },
});