var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');



var app = getApp();

Page({
  data: {
    couponList: [],
    couponNumber: '',
    tabList: ['未使用', '已使用', '已过期'],
    discStatus: '',
    tabIndex: 0,
    ticketDiscountList:[]
  },
  toggleTab(e) {
    this.setData({
      tabIndex: e.currentTarget.dataset.index,
      couponList: [],
      page: 1,
    });
    this.switchDiscStatus(e.currentTarget.dataset.index);
  },
  /**
   * 根据使用类型查询用户已领取的优惠券列表
   */
  switchDiscStatus(tabIndex) {
    let that = this;
    if (tabIndex == 0) {
      that.setData({
        discStatus: 0
      });
    } else if (tabIndex == 1) {
      that.setData({
        discStatus: 1
      });
    } else if (tabIndex == 2) {
      that.setData({
        discStatus: 2
      });
    }
    that.getCouponList();
    this.getTicketDiscountList();
  },
  /**
   * 暂时无用，兑换码触发
   */
  bindCouponNumberInput: function (e) {
    this.setData({
      couponNumber: e.detail.value
    });
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    this.setData({
      discStatus: 0
    });
  },
  onReady: function () {
  },
  onShow: function () {
    let userInfo = wx.getStorageSync('userInfo');
    let token = wx.getStorageSync('token');
    if (userInfo && token) {
      this.getCouponList();
      this.getTicketDiscountList();
    } else {
      wx.navigateTo({
        url: '/pages/ucenter/userLogin/userLogin'
      })
    }
  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭
  },
  /**
   * 查看用户已领取的优惠券
   */
  getCouponList() {
    wx.showLoading({
      title: '加载中...',
    });
    let that = this;
    util.request(api.CouponList, { coupon_number: that.data.couponNumber, discStatus: that.data.discStatus}).then(function (res) {
      if (res.errno === 0) {
        that.setData({
          couponList: res.data
        });
        wx.hideLoading();
      }
    });
  },
  /**
   * 查看优惠券列表
   */
  getTicketDiscountList() {
    let that = this;
    util.request(api.TicketDiscountList, { storeId: wx.getStorageSync('storeId')}).then(function (res) {
      if (res.errno === 0) {
        // console.log(res.data);
        that.setData({
          ticketDiscountList: res.data
        });
      }
    });
  }, 
  /**
   * 兑换优惠券，暂时无用
   */
  exchangeCoupon() {
    var that = this;

    if (that.data.couponNumber.length == 0
      && that.data.couponNumber == '0') {
      wx.showModal({
        title: '错误信息',
        content: '优惠码不能为空',
        showCancel: false
      });
      return false;
    }

    util.request(api.CouponExchange, {
      coupon_number: that.data.couponNumber
    }, 'POST').then(function (res) {
      if (res.errno === 0) {
        wx.showToast({
          title: '兑换成功'
        });
        that.getCouponList();
      } else if (res.errno === 1) {
        util.showErrorToast(res.errmsg);
      }
    });
  },
  /**
   * 优惠券可使用商品
   */
  linkCoupon(event){
    // let url = event.currentTarget.dataset.couponUrl;
    let tickDiscId = event.currentTarget.dataset.tickId;
    let storeTopicId = event.currentTarget.dataset.storeTopicId;
    let storeId = wx.getStorageSync('storeId');

    wx.navigateTo({
      url: '../ticketDiscountGoods/ticketDiscountGoods?tickDiscId=' + tickDiscId + '&&storeId=' + storeId + '&&storeTopicId=' + storeTopicId
    })
  },
  /**
   * 领取优惠券
   */
  getUserCoupon(event) {
    var that = this;
    let tickDiscId = event.currentTarget.dataset.couponId;
    let storeTopicId = event.currentTarget.dataset.storeTopicId;
    let storeId = wx.getStorageSync('storeId');
    util.request(api.getUserCoupon, {
      tickDiscId: tickDiscId,
      storeTopicId: storeTopicId,
      storeId: storeId
    }, 'POST').then(function (res) {
      if (res.errno === 0) {
        wx.showToast({
          title: '领取成功'
        });
        that.setData({
          discStatus: 0,
          tabIndex: 0
        });
        that.getCouponList();
        that.getTicketDiscountList();
      } else {
        wx.showToast({
          title: res.errmsg,
          icon: 'none'
        })
      }
    }); 
  },
  clearData: function () {
    let that = this;
    that.setData({
      couponNumber: ''
    });
  },
  /**
   * 去逛逛
   */
  toIndexPage: function () {
    wx.switchTab({
      url: "/pages/index/index"
    });
  },
  getCouponDetail(event) {
    // let url = event.currentTarget.dataset.couponUrl;
    let couponId = event.currentTarget.dataset.couponId;
    let storeTopicId = event.currentTarget.dataset.storeTopicId;
    let storeId = wx.getStorageSync('storeId');

    if (this.data.discStatus == 0) {
      wx.navigateTo({
        url: '../couponDetail/couponDetail?couponId=' + couponId + '&&storeId=' + storeId + '&&storeTopicId=' + storeTopicId + "&&isShare=2"
      })
    }else{
      if(this.data.discStatus == 1){
        wx.showToast({
          title: '该优惠券已使用',
          icon: 'none'
        })
      }else{
        wx.showToast({
          title: '该优惠券已过期',
          icon: 'none'
        })
      }
    }

  },
  getCouponDetail2(event) {
    // let url = event.currentTarget.dataset.couponUrl;
    let discId = event.currentTarget.dataset.discId;
    let storeTopicId = event.currentTarget.dataset.storeTopicId;
    let storeId = wx.getStorageSync('storeId');

    wx.navigateTo({
      url: '../couponDetail/couponDetail?discId=' + discId + '&&storeId=' + storeId + '&&storeTopicId=' + storeTopicId + "&&isShare=0"
    })

  },
  
})