// pages/catelist1/catelist1.js
var app = getApp();
var n = 1;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title: '搜索',
    inputs: '',
    amout: false,
    loading: false,
    allLoaded: true,
    topStatus: '',
    bottomStatus: '',
    wrapperHeight: 0,
    order_by: '',
    order_field: '',
    datas: [],
    totalGoodsNum: 0,
    pushData: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.coupon) {
      this.setData({
        coupon: options.coupon
      });
    }
    if (options.id) {
      this.setData({
        id: options.id
      });
    }
    if (options.shop_id) {
      this.setData({
        shop_id: options.shop_id
      });
    }
    this.gotoSearch(n);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.current_page >= this.data.last_page) {
      return;
    } else {
      console.log(this.data.page);
      let pages;
      if (this.data.page) {
        pages = Number(this.data.page) + 1;
      } else {
        pages = 2;
        this.setData({
          page: pages
        });
      }
      this.gotoSearch(pages);
    }
  },

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

  },
  gotoSearch(page = 1, j = true) {
    let that = this;
    let urlStr = app.getNetAddresss('plugin.micro.frontend.controllers.MicroShop.goods.search-goods');
    urlStr += "&search[keyword]=" + this.data.inputs;
    urlStr += "&page=" + page;
    urlStr += "&search[category]=" + this.data.id;
    urlStr += "&order_field=" + this.data.order_field;
    urlStr += "&order_by=" + this.data.order_by;
    urlStr += "&search[couponid]=" + this.data.coupon;
    urlStr += "&shop_id=" + this.data.shop_id;
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            loading: false,
            allLoaded: false
          });
          // 搜索结果为空
          if (res.data.data.length <= 0) {
            that.setData({
              loading: true,
              allLoaded: true
            });
            return;
          }
          // 如果当前页数大于总页数 就不加载了
          if (res.data.current_page > res.data.last_page) {
            that.setData({
              loading: true,
              allLoaded: true
            });
            return;
          }
          let datas = that.data.datas;
          datas.push(...res.data.data);
          that.setData({
            datas: datas,
            current_page: res.data.current_page,
            last_page: res.data.last_page
          });
          if (res.data.data.length < 20) {
            that.setData({
              loading: true,
              allLoaded: true
            });
            return;
          }
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  changeTotal(e) {
    let va = e.detail;
    let num = 0;
    // console.log(e.detail,'va')
    this.setData({
      pushData: ''
    });
    va.map((item) => {
      if (item.value) {
        num++;
        this.setData({
          pushData: this.data.pushData + item.id + ","
        });
      }
    });
    this.setData({
      'totalGoodsNum': num
    });
  },
  search() {
    n = 1;
    this.setData({
      datas: [],
      page: 1
    });
    this.gotoSearch(n, false);
  },
  inpbtn(e) {
    let val = e.detail.value;
    this.setData({
      inputs: val
    });
  },
  pushFn() {
    let that = this;
    if (app._isTextEmpty(this.data.pushData) || this.data.pushData.length == 0) {
      wx.showToast({
        icon: 'none',
        title: '请选择商品',
        duration: 1500
      });
      return;
    }
    let urlStr = app.getNetAddresss('plugin.micro.frontend.controllers.MicroShop.set-goods');
    urlStr += '&goods_ids=' + this.data.pushData;
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          wx.reLaunch({
            url: '/packageC/microshop/microShop_home/microShop_home?shop_id=' + that.data.shop_id
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
});