// packageI/allowanceIndex/allowanceReward/allowanceReward.js
const app = getApp();
var control = true;
Page({

  /**
     * 页面的初始数据
     */
  data: {
    active: null,
    years: '',
    title: "极差奖励",
    showPicker: false,
    // 年份数组
    list: [],
    // 年月下的订单详情
    info: [],
    nowYears: new Date().getFullYear(),
    getUrl: "",
    getDetailUrl: "",
    tapind: ""
  },

  /**
     * 生命周期函数--监听页面加载
     */
  onLoad: function (options) {
    let title = '';
    let getUrl, getDetailUrl;
    if (options.ind == 0) {
      title = '极差奖励';
      getUrl = "plugin.agency-subsidy.frontend.controllers.records.order.index";
      getDetailUrl = "plugin.agency-subsidy.frontend.controllers.records.order.detail";
    } else if (options.ind == 1) {
      title = '层级奖励';
      getUrl = "plugin.agency-subsidy.frontend.controllers.records.level.index";
      getDetailUrl = "plugin.agency-subsidy.frontend.controllers.records.level.detail";
    } else if (options.ind == 2) {
      title = '补贴奖励';
      getUrl = "plugin.agency-subsidy.frontend.controllers.records.subsidy.index";
      getDetailUrl = "plugin.agency-subsidy.frontend.controllers.records.subsidy.detail";
    } else if (options.ind == 3) {
      title = '业绩奖励';
      getUrl = "plugin.agency-subsidy.frontend.controllers.records.performance.index";
      getDetailUrl = "plugin.agency-subsidy.frontend.controllers.records.performance.detail";
    }
    wx.setNavigationBarTitle({
      title,
    });
    this.setData({
      active: options.ind,
      getUrl,
      getDetailUrl,
      tapind: options.ind
    });
    this.getData();
  },
  /**
     * 生命周期函数--监听页面初次渲染完成
     */
  onReady: function () {

  },
  pickChange(e) {
    let val = e.detail.value;
    this.setData({
      nowYears: val,
      list: []
    });
    this.getData();
  },
  tabChange(e) {
    let ind = e.detail.index;
    console.log(ind);
    this.setData({
      active: ind,
      nowYears: new Date().getFullYear(),
      list: []
    });
    this.getData();
  },
  moreShowTap(e) {
    console.log(e);
    let item = e.currentTarget.dataset.item;
    let ind = e.currentTarget.dataset.ind;
    let index = e.currentTarget.dataset.index;
    if (!item.info[ind].moreShow) {
      this.setData({
        ['list[' + index + '].info[' + ind + '].moreShow']: true
      });
    }
  },
  showGoods(e) {
    let ind = e.currentTarget.dataset.ind;
    let index = e.currentTarget.dataset.index;
    let item = e.currentTarget.dataset.itemk;
    this.setData({
      ['list[' + index + '].info[' + ind + '].show']: item.show ? false : true
    });
  },
  tapGetList(e) {
    let item = e.currentTarget.dataset.item;
    let index = e.currentTarget.dataset.index;
    if (item.show) {
      this.setData({
        ['list[' + index + '].show']: item.show ? false : true
      });
    } else {
      this.setData({
        ['list[' + index + '].show']: item.show ? false : true,
        ['list[' + index + '].page']: 1,
        ['list[' + index + '].last_page']: 1
      });
      this.getDetail(item, index);
    }
  },
  getDetail(item, index) {
    let urlStr = app.getNetAddresss(this.data.getDetailUrl);
    let json = {
      year: item.year,
      month: item.month,
      award_status: this.data.active == 2?'-1':this.data.active,
      page: item.page
    };
    app._getNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result != 1) return app.tips(res.msg);
        if (this.data.tapind == 0 || this.data.tapind == 1) {
          for (let i = 0; i < res.data.data.length; i++) {
            if (res.data.data[i].order_goods.length > 0) {
              res.data.data[i].show = false;
            }
          }
        }
        this.setData({
          ['list[' + index + '].info']: res.data.data,
          ['list[' + index + '].last_page']: res.data.last_page
        });
        console.log(this.data.list);
      }
    });
  },
  getMore(e) {
    let item = e.currentTarget.dataset.item;
    let index = e.currentTarget.dataset.index;
    let json = {
      year: item.year,
      month: item.month,
      award_status: this.data.active == 2?'-1':this.data.active,
      page: item.page + 1
    };
    let urlStr = app.getNetAddresss(this.data.getDetailUrl);
    if (item.page >= item.last_page) {
      return;
    }
    if (!control) {
      console.log('请求中');
      return;
    }
    control = false;
    app._getNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        control = true;
        if (resdata.result == 0) {
          app.tips(res.msg);
        }
        var res = resdata.data;
        let nextData = res.data.data;
        this.data.list[index].info = this.data.list[index].info.concat(nextData);
        this.setData({
          ['list[' + index + '].info']: this.data.list[index].info,
          ['list[' + index + '].last_page']: res.data.last_page,
          ['list[' + index + '].page']: res.data.current_page
        });
      },
      fail: (err) => {
        control = true;
      }
    });
  },
  getData() {
    let status = "";
    if (this.data.active == 2) {
      status = "-1";
    } else {
      status = this.data.active;
    }
    let json = {
      year: this.data.nowYears,
      award_status: status
    };
    let urlStr = app.getNetAddresss(this.data.getUrl);
    app._getNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result != 1) return app.tips(res.msg);
        for (let i = 0; i < res.data.length; i++) {
          if (res.data[i].amounts > 0) {
            res.data[i].show = false;
          }
        }
        this.setData({
          list: res.data
        });
      }
    });
  },
  /**
     * 生命周期函数--监听页面显示
     */
  onShow: function () {

  },

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

  },

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

  },

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

  },

  /**
     * 页面上拉触底事件的处理函数
     */
  onReachBottom: function () {

  },

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

  }
});