// pages/index/sw/index.js
let config = require("../../../config/config.js");
let Http = require("../../../utils/HttpBasics");
let app = getApp();
const imgurl = require("../../../utils/imgurl");
// 请求数据
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    loading: {
      type: Boolean,
      value: ''
    },
    loadingtext: {
      type: String,
      value: ''
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    actUrl: imgurl.act.url,
    loadingUrl: imgurl.loading.url,
    tabs: [],
    list: [],
    flag: "",
    loading: true, //"上拉加载"的变量，默认false，隐藏
    content: "",
    return_list: [],
    current: 0,
    current_scroll: 0,
    pageNum: 1, // 设置加载的第几次，默认是第一次
    pageSize: 10, //返回数据的个数
    searchLoadingComplete: false, //“没有数据”的变量，默认false，隐藏
    allow_load: true, // 是否允许继续加载标识  默认 true 允许，false 加载完成
  },
  /**
   * 组件的方法列表
   */
  methods: {
    handleChange({
      detail
    }) {
      this.setData({
        current: detail.key
      });
    },

    handleChangeScroll({
      detail
    }) {
      let that = this;
      // 切换tab时重新初始化下list 和 allow_load
      that.setData({
        current_scroll: detail.key,
        flag: "click",
        list: [],
        allow_load: true
      });
      this.getList(detail.key, 1);
      var val = detail.key;
      //通过这个传递数据
      var myEventDetail = {
        val: val,
        pageNum: 1
      };
      // detail对象，提供给事件监听函数
      this.triggerEvent("myevent", myEventDetail);
    },

    getList(key, pageNum, from) {
      var that = this;
      console.log("pageNum------>"+pageNum);
      /**
       * from==refresh
       * 表示是从下拉刷新进来的
       */
      app.couponListCallback = token => {
        Http.setToken(token);
        // 券list获取
        if (from == 'refresh') {
          that.setData({
            allow_load: true,
            list: [],
          })
          console.log(that.data.list);
        }
        if (that.data.allow_load) {
          that.setData({
            loading: true,
            content: '小主,我在玩命加载中...'
          })
          // 根据 key == 0 区分全部或其它tab，决定是否传参数 business
          if (key == 0) {
            var param = {
              pageNum: pageNum,
              pageSize: 6,
              targetAd: 1
            };
          } else {
            var param = {
              pageNum: pageNum,
              pageSize: 6,
              business: key,
              targetAd: 1
            };
          }
          // 请求接口
          Http.get({
            url: config.api.couponChannelList,
            data: param
          }).then(res => {
            /**
             * 加载完成
             */
            if (pageNum >= res.data.pages) {
              if (res.data.pages == 0 || res.data.pages == 1) {
                that.setData({
                  allow_load: true,
                  loading: false,
                  content: ""
                });
              }
              else {
                that.setData({
                  allow_load: false,
                  loading: true,
                  content: "——— 再拉裤子就掉了啦 ———",
                });
              }
            }
            if (pageNum == 1) {
              that.setData({
                list: [],
              })
            }

            var tmpArr = that.data.list;
            tmpArr.push.apply(tmpArr, res.data.list);
            that.setData({
              list: tmpArr
            })
          })
          .catch(err => {
            wx.showToast({
              title: err.errMsg,
              icon: 'none',
              duration: 2000,
              mask: false
            });
          })
          setTimeout(function () {
            that.setData({
              loading: false,
            })
          }, 1400);
        }
        else {
          that.setData({
            loading: true,
            content: "——— 再拉裤子就掉了啦 ———"
          })
          setTimeout(function () {
            that.setData({
              loading: false,
            })
          }, 1400)
        }
      };

      if (app.globalData.token && app.globalData.token != null) {
        app.couponListCallback(app.globalData.token);
      }
    }
  },
  ready() {
    let that = this;
    app.businessListCallback = token => {
      Http.setToken(token);
      // business获取
      Http.get({
        url: config.api.businessList,
        data:{
          filter:2
        }
      }).then(res => {
        that.setData({
          tabs: [{ id: 0, title: "全部", type: 1 }].concat(res.data)
        });
      })
        .catch(err => {
          wx.showToast({
            title: err.errMsg,
            icon: 'none',
            duration: 2000,
            mask: false
          }); 
        })
    };
    if (app.globalData.token && app.globalData.token != null) {
      app.businessListCallback(app.globalData.token);
    }
    that.setData({
      list: []
    })
    that.getList(0, 1);
  }
});
function compare(pro) {
  return function (obj2, obj1) {
    var val1 = obj1[pro];
    var val2 = obj2[pro];
    if (val1 < val2) {
      return 1;
    } else if (val1 > val2) {
      return -1;
    } else {
      return 0;
    }
  }
}