// packageF/timeAppointment/timeAppointment_search/timeAppointment_search.js
var behavior = require('../common.js');
var app = getApp();
let search_text="";
Page({
  behaviors: [behavior],
  /**
   * 页面的初始数据
   */
  data: {
    tabsIndex:1,

    page: 1, //分页数，当前页数
    isLoadMore: true, //判断是否要加载更多的标志
    total_page: 0, //总页数
    listData: [],
    networkLoading: false,

    sort_order_field:'',
    sort_order_by:'',


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.status){
      this.setData({
        tabsIndex:options.status
      });
    }
    search_text="";
    this.getGoodsList();
  },

  setTabsIndex(evt){
    let index = evt.currentTarget.dataset.index;
    this.setData({
      tabsIndex:index
    });
    this.getGoodsList();
  },

  confirmSearch(evt){
    console.log(evt);
    if(evt.detail.value == search_text) return;
    search_text = evt.detail.value;
    this.getGoodsList();
  },

  setSort(evt){
    this.setData({
      sort_order_field:evt.detail.order_field,
      sort_order_by:evt.detail.order_by
    });
    this.getGoodsList();
  },

  gotoUserDe(evt){
    let item = evt.currentTarget.dataset.item;
    wx.navigateTo({
      url: '/packageF/timeAppointment/AppointmentUserDe/AppointmentUserDe?id='+item.id,
    });
  },

  initData() {
    this.setData({
      page: 1,
      total_page: 0,
      isLoadMore: true,
      listData:[],
      networkLoading: false,
    });
  },
  getGoodsList(){
    this.initData();
    let urlStr = app.getNetAddresss("plugin.reserve-simple.frontend.goods.search-goods");
    let json={};
    if(this.data.tabsIndex==1){
      if(this.data.sort_order_field!=''){
        json.order_field = this.data.sort_order_field;
      }
      if(this.data.sort_order_by!=''){
        json.order_by = this.data.sort_order_by;
      }
      if(search_text){
        json["search[keyword]"] = search_text;
      }
    }else if(this.data.tabsIndex==2){
      urlStr = app.getNetAddresss("plugin.reserve-simple.frontend.obj.search-objs");
      if(search_text){
        json.keyword = search_text;
      }
    }
   
    app._getNetWork({
      url: urlStr,
      data:json,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result != 1) return app.tips(res.msg);
        console.log(res);
        this.data.total_page = res.data.last_page;
        if (!this.data.total_page) {
          this.data.total_page = 0;
        }
        this.setData({
          listData:res.data.data,
          networkLoading:true
        });
      }
    });
  },

  //加载更多数据
  _getMoreData() {
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      // that.loading = true;
      return;
    } else {
      this.data.page += 1;
      let json={ page: this.data.page };
      let urlStr = app.getNetAddresss("plugin.reserve-simple.frontend.goods.search-goods");
      if(this.data.tabsIndex==1){
        if(this.data.sort_order_field!=''){
          json.order_field = this.data.sort_order_field;
        }
        if(this.data.sort_order_by!=''){
          json.order_by = this.data.sort_order_by;
        }
        if(search_text){
          json["search[keyword]"] = search_text;
        }
      }else if(this.data.tabsIndex==2){
        urlStr = app.getNetAddresss("plugin.reserve-simple.frontend.obj.search-objs");
        if(search_text){
          json.keyword = search_text;
        }
      }
     
      app._postNetWork({
        url: urlStr,
        data: json, 
        success: (resdata) => {
          let res = resdata.data;
          this.data.isLoadMore = true;
          if (res.result === 1) {
            let listData = this.data.listData.concat(res.data.data);
            this.setData({
              listData
            });
          } else {
            this.data.page = this.data.page - 1;
            this.data.isLoadMore = false;
          }
        },
      });
    }
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

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

  }
});