// subPackage/page/pages/myValueCard/myValueCard.js
const wxService = require('../../../../utils/wxService')
const utils = require('../../../../utils/util')
var wxbarcode = require('../../../../codeUtils/index.js');
wxService.page({

  /**
   * 页面的初始数据
   */
  data: {
    no : '',
    pageNum : 1,
    pageSize  : 10,
    totalPage : 0,
    isNoData :false,
    myCardsList : []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideShareMenu();
    this.getMyValueCardList();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  //barcode 失败回调
  canvasIdErrorCallback(e){

  },
  //跳转到购买记录
  onTapToBuyRecords(){
    wx.navigateTo({
      url: '/subPackage/page/pages/valueCardRecords/valueCardRecords',
    });
  },
  //跳转到详情
  onTapToInfoPage(e){
    let item = e.currentTarget.dataset.item;
    wx.navigateTo({
      url: '/subPackage/page/pages/myValueCardInfo/myValueCardInfo?id=' + item.id,
    });
  },

  //刷新no
  onTapRefreshNo(e){
    let supValCardId = e.currentTarget.dataset.id;
    let index = e.currentTarget.dataset.index;
    let url = `/merchant/superValueCard/miniProgram/getChangeCode?supValCardId=${supValCardId}`;
    wxService.post(url).then(res => {
      if (res) {
        if (res.data.data){
          let no = res.data.data;
          this.data.no = no;
          let canvasId = 'barcode' + index;
          this.setData({
            no: this.data.no
          }, () => {
            this.generatorBarCode(canvasId);
            wx.showToast({
              title: '刷新成功!',
            });
          });
        }
        else{
          wx.showToast({
            title: '刷新失败，请重试',
            icon : ' none'
          })
        }
      }
    });
  },

  //获取我的超值卡列表
  getMyValueCardList(refresh){
    const { member } = wx.getStorageSync('_baseUserInfo');
    if (!member) {
      wx.showToast({
        title: '请先去登录',
        icon: 'none'
      })
      return;
    }

    wx.showLoading({
      title: '加载中..',
    });

    let memeberId = member.id,
    pageNum = this.data.pageNum,
    pageSize = this.data.pageSize;
    let url = `/merchant/superValueCard/miniProgram/getPageList?memberId=${memeberId}&pageNum=${pageNum}&pageSize=${pageSize}`;
    wxService.post(url).then(res => {
      if(res){
        let data = res.data.data ? res.data.data : [];
        if(data.length == 0 && this.data.pageNum > 1){
          this.data.isNoData = true;
        }
        else{
          this.data.isNoData = false;
        }
        //buyAwayType 1 现金 2 积分
        data.forEach(item => {
          item.leftTimes = item.totalTimes - item.usedTimes;
          item.isOpen = false;
          item.no = item.supValCardCode ? item.supValCardCode : item.supValCardId;
          item.id = item.supValCardId;
          item.startTime = item.startTime.substring(0, 10);
          item.endTime = item.endTime.substring(0,10);
          item.useResidueDays = item.useResidueDays ? item.useResidueDays : 0;
        });

        this.data.myCardsList = this.data.myCardsList.concat(data);
        this.setData({
          myCardsList: this.data.myCardsList,
          isNoData: this.data.isNoData
        })
      }

      if(refresh){
        wx.showToast({
          title: '刷新成功',
        });
        wx.stopPullDownRefresh();
      }
      wx.hideLoading();
    });
    
  },
  //生成条形码
  generatorBarCode(canvasId){
    if (this.data.no){
      wxbarcode.barcode(canvasId, this.data.no, 500, 100);
      setTimeout(() => {
        wxbarcode.barcode(canvasId, this.data.no, 500, 100);
      }, 200);
    }
  },
  //展开
  onTapShowMoreInfo(e){
    let item = e.currentTarget.dataset.item;
    let index = e.currentTarget.dataset.index;
    if (item.isOpen){
      return ;
    }

    this.data.myCardsList.forEach(item => {
      item.isOpen = false;
    });

    this.data.myCardsList[index].isOpen = true;
    this.data.no = this.data.myCardsList[index].no;
    let canvasId = 'barcode' + index;
    this.setData({
      myCardsList: this.data.myCardsList,
      no: this.data.no
    }, () => {
      this.generatorBarCode(canvasId);
    });
    
  },

  //关闭
  onTapCloseExtro(e){
    let index = e.currentTarget.dataset.index;
    this.data.myCardsList[index].isOpen = false;
    this.data.no = '';
    this.setData({
      myCardsList: this.data.myCardsList,
      no: this.data.no
    });
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.data.pageNum = 1;
    this.data.myCardsList = [];
    this.data.isNoData = false;
    this.getMyValueCardList(true);
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(!this.data.isNoData){
      this.data.pageNum++;
      this.getMyValueCardList();
    }
  },

})