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



var app = getApp();

Page({
  data: {
    discStatus: '',
    couponId: '',
    storeId: '',
    storeTopicId: '',
    tickDiscount: {},
    isShare: '',
    referrer: 0,
    discId: ''
  },
  onLoad: function(options) {
    // 页面初始化 options为页面跳转所带来的参数
    // options.scene = "4&8&0";
    if (options.scene) {
      var scene = decodeURIComponent(options.scene);
      console.log("scene is ", scene);
      var scanArray = scene.split('&');
      var discId = scanArray[0];
      var storeId = scanArray[1];
      var isShare = scanArray[2];
      // console.log("discId is ", discId);
      // console.log("storeId is ", storeId);
      // console.log("isShare is ", isShare);
      this.setData({
        discId: discId,
        storeId: storeId,
        isShare: isShare
      });
      if (storeId) {
        wx.setStorageSync('storeId', storeId);
      }
    } else {
      if (options.couponId) {
        this.setData({
          couponId: options.couponId,
          storeId: options.storeId,
          isShare: options.isShare
        });
      }
      if (options.discId) {
        this.setData({
          discId: options.discId,
          storeId: options.storeId,
          isShare: options.isShare
        });
      }
    }
    console.log("discId is ", options.discId);
    console.log("storeId is ", options.storeId);
    console.log("isShare is ", options.isShare);
    this.getTickDiscountDetail();
  },
  onReady: function() {},
  onShow: function() {},
  onHide: function() {
    // 页面隐藏

  },
  onUnload: function() {
    // 页面关闭
  },
  getTickDiscountDetail() {
    var that = this;
    util.request(api.DiscountDetail, {
        couponId: that.data.couponId,
        storeId: that.data.storeId,
        discId: that.data.discId
      })
      .then(function(res) {
        if (res.errno === 0) {
          console.log(res.data);
          if (res.data) {
            that.setData({
              tickDiscount: res.data
            });
          }
        }
      });
  },
  linkCouponInfo(event) {
    // let url = event.currentTarget.dataset.couponUrl;
    let discId = event.currentTarget.dataset.discId;
    let storeId = wx.getStorageSync('storeId');
    let couponId = event.currentTarget.dataset.couponId;

    console.log('couponId' + couponId)
    console.log('discId' + discId)
    if (couponId) {
      console.log('couponId' + couponId)
      wx.navigateTo({
        url: '../couponTickInfo/couponTickInfo?couponId=' + couponId + '&&storeId=' + storeId
      })
    }
    if (discId) {
      console.log('discId' + discId)
      wx.navigateTo({
        url: '../couponTickInfo/couponTickInfo?discId=' + discId + '&&storeId=' + storeId
      })
    }
  },
  linkMyCoupon() {
    wx.navigateTo({
      url: '../coupon/coupon'
    })
  },
  /**
   * 领取优惠券
   */
  getUserCoupon(event) {
    var that = this;
    let tickDiscId = event.currentTarget.dataset.tickId;
    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
        });
        that.getTickDiscountDetail();
      } else {
        wx.showToast({
          title: res.errmsg,
          icon: 'none'
        })
      }
    });
  },
  /**
   * 优惠券可使用商品
   */
  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
    })
  },
})