var app = getApp();
var $ = require('../../../js/util.js');
Page({
  data: {
    list: [],
    page: 1,//当前页码
    param: {
      discount_num:0, // 优惠方式
      receive_num:0,// 领取方式
    },
    is_show_popmask:0,
    isshow_discount:0,
    isshow_receive:0,
    //公共提示
    hasMore: false,//无更多数据提示
    dataNull: true//未获取到数据提示
  },
  onLoad: function (options) {
    var self = this; 
    if (options.invite_key) {
      app.globalData.invite_key = options.invite_key;
    } 
  },
  onReady: function () {
    var self = this;
    app.getUserInfo(function (ret) {
        self.getData();
    });
  },
  getData: function (callback) {
    var self = this;
    app.showLoading('加载中...');
    var action = 'c=Coupon&a=index';
    var param = self.data.param;
    param.page = self.data.page;
    self.setData({
      param: param,
    })
    app.ajaxRequest(action, 'GET', $.ObjectToStr(self.data.param), function (ret) {
      if (ret.status == 1) {
        $.init_page_list(self, ret.data);
      } else if (ret.status == 2) {
        $.init_page_empty_list(self, ret.msg);
      }
      self.close_mask();
    })
  },
  onReachBottom: function () {
    var self = this;
    self.data.page++;
    self.getData();
  },
  //领取优惠券
  goToGet: function (event) {
    var self = this;
    var cid = event.currentTarget.dataset.cid;
    //console.log(cid);
    app.showLoading('加载中...');
    var action = 'c=Coupon&a=get_coupon_wx';
    var param = new Object;
    param.id = cid;
    app.ajaxRequest(action, 'POST', param, function (ret) {
      if (ret.status == 1) {
        app.HCtoast('领取成功');
        if (ret.data.cardId) {
          var cardlist = new Array;
          cardlist.push(ret.data);
          var id = ret.data.add_id;
          app.HCaddCard(cardlist, function (ret) {
            if (ret.errMsg == "addCard:ok") {
              var code = ret.cardList[0].code;
              self.save_coupon(id,code);
            }
          });
        }
      } else {
        app.HCerror('提示', ret.msg, false);
      }
    }, true);
  },
  // 积分兑换优惠券
  goToPoints:function(event){
    var self = this;
    app.HCerror('提示', '兑换此券需要'+event.currentTarget.dataset.money+'积分，您确定要兑换吗？', true, function (ret) {
      if (ret.confirm) {
        self.goToGet(event);
      }
    })
  },
  save_coupon:function(id,code){
    var self = this;
    var action = 'c=Coupon&a=save_coupon';
    var param = new Object;
    param.id = id;
    param.code = code;
    app.ajaxRequest(action, 'POST', param, function (ret) {
      console.log(ret);
    }, true);
  },
  onShareAppMessage: function () {
    return {
      title: '卡券中心',
      desc: '快来领取优惠券吧!',
      path: '/' + $.getCurrentPageUrl() + '?invite_key=' + app.globalData.init_invite_key
    }
  },
  onPullDownRefresh: function () {
    this.setData({
      list: [],
      page: 1,
      param: {},
      hasMore: false,
      dataNull: true
    });
    this.getData();
  },
  //跳转优惠券详情
  goToDetaile: function (event) {
    app.HCnavigateTo('../cont/cont?id=' + event.currentTarget.dataset.gid);
  },
  // 按钮切换
  showMenuTap:function(event){
    var menuType = event.currentTarget.dataset.type;
    if(menuType==1){
      this.setData({
        isshow_discount:1,
        is_show_popmask:1,
        isshow_receive:0,
      });
    }else{
      this.setData({
        isshow_receive:1,
        is_show_popmask:1,
        isshow_discount:0,
      });
    }
  },
  // 关闭遮罩层
  close_mask:function(){
    this.setData({
      isshow_discount:0,
      is_show_popmask:0,
      isshow_receive:0,
    });
  },
  // 切换优惠方式
  get_discount:function(event){
    var self = this;
    var menuType = event.currentTarget.dataset.type;
    var search_type = self.data.param;
    search_type.discount_num = menuType;
    self.setData({
      param: search_type,
    });
    self.getData();
  },

  // 切换优惠方式
  get_receive:function(event){
    var self = this;
    var menuType = event.currentTarget.dataset.type;
    var search_type = self.data.param;
    search_type.receive_num = menuType;
    self.setData({
      param: search_type,
    });
    self.getData();
  },
})