// packageI/warehouseFarm/warehouseFarmBuyGift/warehouseFarmBuyGift.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cate: [{
      id: 0,
      name: "全部"
    }],
    isResizeTabs: false, //是否开始渲染选项卡
    keyword: "",
    active: 0,
    loading: false,
    finished: false,
    refreshing: false,
    page: 1,
    total_page: 0,
    list: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.id) {
      this.setData({
        active: Number(options.id)
      });
    }
    this.getInit();
    this.getData();
  },

  getInit() {
    this.data.cate = [{
      id: 0,
      name: "全部"
    }];
    let urlStr = app.getNetAddresss("plugin.warehouse.frontend.controllers.purchasing.list.index");
    app._postNetWork({
      url: urlStr,
      data: {},
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          this.setData({
            cate: this.data.cate.concat(res.data.types),
            isResizeTabs: true
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1000,
          });
        }
      },
      fail: (res) => {
        console.log(res);
      },
    });
  },
  getData() {
    if (this.data.loading || this.data.finished) {
      return;
    }
    this.data.loading = true;
    let urlStr = app.getNetAddresss("plugin.warehouse.frontend.controllers.purchasing.list.get-goods");
    app._postNetWork({
      url: urlStr,
      data: {
        keyword: this.data.keyword,
        goods_type: this.data.active,
        page: this.data.page
      },
      success: (resdata) => {
        let res = resdata.data;
        this.data.refreshing = false;
        this.data.loading = false;
        if (res.result == 1) {
          let _list = this.data.list;
          this.setData({
            list: _list.concat(res.data.data),
            total_page: res.data.last_page
          });
          this.data.page = this.data.page + 1;
          if (res.data.current_page == res.data.last_page) {
            this.data.finished = true;
          }
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1000,
          });
        }
      },
      fail: (res) => {
        console.log(res);
      },
    });
  },
  onChangeSearch(e) {
    this.setData({
      keyword: e.detail
    });
  },
  onSearch() {
    this.initData();
    this.getData();
  },
  onClickCate(event) {
    this.setData({
      active: event.detail.name
    });
    this.initData();
    this.getData();
  },
  initData() {
    // 清空列表数据
    this.data.finished = false;
    this.data.loading = false;
    this.data.list = [];
    this.data.page = 1;
  },
  toUrl(e) {
    let _data = e.currentTarget.dataset || {};
    this.wxRouterLink(_data);
  },
  wxRouterLink(_data) {
    let _Param = '';
    const {
      url,
      ...otherParam
    } = _data;
    Object.keys(otherParam).forEach(function (key) {
      if (otherParam[key] !== '') _Param += key + '=' + otherParam[key] + '&';
    });
    if (url === '') return;
    wx.navigateTo({
      url: url + '?' + _Param.slice(0, -1)
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getData();
  },

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

  }
});