const util = require('../../../utils/util.js');
const app = getApp();
Page({
  data: {
    list:[],
    data:{},
    selectId:0,
    isLoadMore: true,
    pageIndex : 1,
    pageSize: 10,
    maskShow:false,
    shareInfo:{}
    
  },
  onLoad: function (options) {
    if(!this.isOptions(options)){
      return false;
    }
    if (options.packIds && options.aid && options.sid){
      this.data.data['meals'] = options.packIds;
      this.data.data['aid'] = options.aid;
      this.data.data['sid'] = options.sid;
      this.data.data['cid'] = options.cid;
      this.data.data['vid'] = options.vid != -1 ? options.vid : "";
      this.setData({ data: this.data.data });
    }
    this.getlist(true);
  },
  isOptions(options){
    var status = true;
    if(!util.isObjEmpty(options)){
      if (util.isEmpty(options.packIds)) {
        util.showTips('缺少场次和购买数量参数。');
        status = false;
      }
      if (util.isEmpty(options.aid)) {
        util.showTips('缺少活动Id参数。');
        status = false;
      }
      if (util.isEmpty(options.sid)) {
        util.showTips('缺少套餐的参数。');
        status = false;
      }
    }
    return status;
  },
  getlist:function(load){
    var self = this,
        url = "user/vouchers_list",
        data = { "pageIndex": self.data.pageIndex, "pageSize": self.data.pageSize };
    if (self.data.data.meals){
      url = "voucher/pay_vouchers";
      data = self.data.data;
      data.pageIndex = self.data.pageIndex;
      data.pageSize = self.data.pageSize;
    }
    util.ajax({
      func: url,
      data: data,
      load: load
    },function(res){
      if(res.code == 0){
        let isLoadMore = true;
        let newData = self.data.data.meals ? res.data.list : res.data.collects;
        let oldData = !load ? newData : [...self.data.list, ...newData];
        if (newData.length < self.data.pageSize && oldData.length > self.data.pageSize){
          isLoadMore = false;
        }
        for (let i = 0; i < oldData.length; i++){
          oldData[i].description = oldData[i].description.replace("<b>", "<b class='b'>");
        }
        self.setData({ list: oldData, isLoadMore: isLoadMore, selectId: data.vid || ''});
      }else
        util.showTips(res.reason);
    });
  },
  radioChange:function(e){
    if (!this.data.data.meals) return false;
    wx.setStorageSync("vids", e.detail.value);
    wx.navigateBack();
  },
  goUse(e) {
    let type = e.currentTarget.dataset.type;
    console.log(e,type)
    if (type==1) {
      wx.navigateTo({
        url: '/pages/product/goods/index'
      });
    } else {
      wx.navigateTo({
        url: '/pages/growthCard/activity?vid=' + e.currentTarget.dataset.vid
      });
    }
  },
  share(e){
    let that = this;
    util.ajax({
      func: "v2/voucher/share",
      data: { "vid":e.currentTarget.dataset.vid},
      load: false
    }, function (res) {
      if (res.code == 0) {
        that.setData({ shareInfo: res.data, maskShow: true});
      } else {
        util.showTips(res.reason);
      }
    });
  },
  close(){
    this.setData({maskShow:false})
  },
  onPullDownRefresh: function () {
    var self = this;
    wx.showNavigationBarLoading();
    setTimeout(function () {
      self.setData({ pageIndex: 1 });
      self.getlist(false);
      wx.hideNavigationBarLoading();
      wx.stopPullDownRefresh();
    }, 1000);
  },
  onReachBottom() {
    var self = this;
    if (self.data.isLoadMore == false) return false;
    setTimeout(function () {
      self.setData({ pageIndex: self.data.pageIndex + 1 });
      self.getlist(true);
    }, 1000);
  },
  onShareAppMessage: function (options) {
    let shareInfo = this.data.shareInfo;
      if (options.from == "button") {
        if (!util.isEmpty(shareInfo.id)) {
          return {
            title: `送你一张价值${shareInfo.price}元现金券，${shareInfo.title}`,
            path: '/pages/account/safe/give?id=' + shareInfo.id,
            imageUrl: '../../order/images/share.png'
          }
        } else {
          util.showTips('转赠失败！');
        }
      } else {
        return {
          title: '宝贝走天下，安全就出发！',
          path: '/pages/home/index',
          imageUrl: '../../order/images/share.png'
        }
      }
  }
});