// packageA/member/extension/CustomQueue/CustomQueue.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    language: '',
    active: 0,
    rewardsData: {},
    recordsList: [],
    MemberData: {},
    url: '',
    amount_id: '',
    currentTabIndex: 1,
    //more
    isLoadMore: true,
    page: 1,
    total_page: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.checkrouter();
    this.initData();
    this.getIndex();
    this.getMember();
    this.handleClick(0);
  },
  checkrouter() {
    try {
      // 推广中心是否开启该功能，没开启跳转到指定路径
      let basic_info = wx.getStorageSync("yz_basic_info");
      let stop_info = basic_info.popularize_page.mini.vue_route;
      for (let i = 0; i < stop_info.length; i++) {
        if (stop_info[i] == 'ClockPunch') {
          console.log(basic_info.popularize_page.mini.mini_url + "跳转的路径");

          wx.showToast({
            title: '未开启推广权限',
            duration: 1000,
            icon: 'none',
            success: function () {
              setTimeout(() => {
                wx.redirectTo({
                  url: basic_info.popularize_page.mini.mini_url,
                });
              }, 1000);
            }
          });
          return;
        }
      }
    } catch (e) {
      console.log(e);
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let language = wx.getStorageSync('langIndex');
    this.setData({ 'language': language.en });
  },

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

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多数据');
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  initData() {
    this.setData({
      rewardsData: {},
      recordsList: [],
      active: 0,
      page: 1,
      currentTabIndex: 1,
      total_page: 0,
      amount_id: '',
      isLoadMore: true
    });
  },
  getIndex() {
    let that = this;
    let urlStr = app.getNetAddresss('plugin.diy-queue.frontend.diy-queue.get-statistics');
    app._getNetWork({
      url: urlStr,
      data: {},
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            rewardsData: res.data
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getMember() {
    let that = this;
    let urlStr = app.getNetAddresss('plugin.diy-queue.frontend.diy-queue.get-member');
    app._getNetWork({
      url: urlStr,
      data: {},
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            MemberData: res.data.member
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  handleClickbtn(e) {
    let index = e.detail.index;
    this.handleClick(index);
  },
  //tab 点击
  handleClick(index, title) {
    this.setData({
      amount_id: '',
      recordsList: []
    });
    if (this.data.currentTabIndex !== index) {
      this.setData({
        currentTabIndex: index
      });
      if (index === 1) {
        this.setData({
          url: 'plugin.diy-queue.frontend.diy-queue.get-logs'
        });
      } else {
        this.setData({
          url: 'plugin.diy-queue.frontend.diy-queue.get-queues'
        });
      }
      this.getData();
    }
  },
  //获取数据
  getData() {
    this.setData({
      isLoadMore: false,
      page: 1
    });
    let that = this;
    let urlStr = app.getNetAddresss(this.data.url);
    app._getNetWork({
      url: urlStr,
      data: {
        page: 1
      },
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            isLoadMore: true
          });
          if (that.data.currentTabIndex === 1) {
            that.setData({
              total_page: res.data.logs.last_page,
              recordsList: res.data.logs.data
            });
          } else {
            that.setData({
              total_page: res.data.queues.last_pag,
              recordsList: res.data.queues.data
            });
          }
          if (!that.data.total_page) {
            that.setData({
              total_page: 0
            });
          }
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  //获取更多数据
  getMoreData() {
    let that = this;
    this.setData({
      isLoadMore: false
    });
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.setData({
        page: this.data.page + 1
      });
      let urlStr = app.getNetAddresss(this.data.url);
      app._getNetWork({
        url: urlStr,
        data: {
          page: this.data.page
        },
        success: function (resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            if (that.data.currentTabIndex === 1) {
              myData = res.data.logs.data;
            } else {
              myData = res.data.queues.data;
            }
            that.setData({
              recordsList: that.data.recordsList.concat(myData)
            });
          } else {
            that.setData({
              page: that.data.page - 1,
              isLoadMore: false
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },
  showMore(e) {
    let index = e.currentTarget.dataset.index;
    let obj = e.currentTarget.dataset.item;
    if (obj.show && this.data.amount_id === obj.id) {
      this.setData({
        ['recordsList[' + index + '].show']: false
      });
      return;
    }
    this.setData({
      'amount_id': obj.id,
      ['recordsList[' + index + '].show']: true
    });
  },
});
