import $ from './../../utils/Tool'
import router from './../../utils/router'
import {queryAllspace} from '../../requests/api'
let timer = 0;
Page({
  data: {
    spaceName:'',
    spaceId:'',
    seachValue:'',
    alllistContent:[],
    currentList:[],
    allListnum:0,
    userId:$.store.get('userId'),
    tenantId:$.store.get('tenantId'),
    projectId:$.store.get('projectId'),
    currtPage:0,
  },
  /**
   * 搜索
   */
  spaceSearch(e){
    let value = e.detail
    if(timer){
      clearTimeout(timer)
    }
    timer = setTimeout(()=>{
        if(value){
          this.updateList(value);
          // this.setData({seachValue:value,currtPage:0,alllistContent:[]},()=>{
          //   this.getallList(value);
          // });
        }else{
          this.setData({currentList:alllistContent})
        }
    },500)

  },
  updateList(value){
    let allList =JSON.parse(JSON.stringify(this.data.alllistContent));
    let currentList=[];
    allList.forEach(item=>{
      if(item.localName.includes(value)){
        currentList.push(item)
      }
    })
    this.setData({currentList:currentList})
  },
  goBack(){
    router.pop();
  },
  async getallList(){
    let projectId = $.store.get('projectId') ||$.storage.get('projectId');
    let tenantId = $.store.get('tenantId')||$.storage.get('tenantId');
    let userId = $.store.get('userId') || $.storage.get('userId');
    const data={
      "criteria": {
          "userId":userId,
          "projectId": projectId,
          "tenantId": tenantId
      },
      // page:this.data.currtPage + 1,
      // size:20,
      orders:[
        {
          column:"localName",
          asc:"true"
        }
      ]
    }
    // if(this.data.seachValue){
    //   data.criteria.localName={$like: `%${this.data.seachValue}%`}
    // }
    wx.showLoading()
    let res =await queryAllspace(data);
    wx.hideLoading()
    this.setData({allListnum:res.count});
    if(res.count&&res.content){
      this.setData({alllistContent:this.data.alllistContent.concat(res.content)},()=>{
        // res.count>30?this.setData({currentList:this.getPage(1)}):this.setData({currentList:this.data.alllistContent});
        this.setData({currentList:this.data.alllistContent})
      });
    }
  },
  // 前端分页
  getPage(page){
    let start = (page-1)*30;
    let end = ((page-1)*30 + 30)>this.data.allListnum?this.data.allListnum:((page-1)*30 + 30)
    return this.data.alllistContent.slice(start,end)
  },
  spaceClick(e){
    console.log(e.currentTarget.dataset.spaceitem)
    let {id,localName} = e.currentTarget.dataset.spaceitem;
    let pages = getCurrentPages();
    let prevPage = pages[ pages.length - 2 ];
    prevPage.setData({  // 将我们想要传递的参数在这里直接setData。上个页面就会执行这里的操作。
      spaceId:id,
      spaceName:localName,
      formSearch:true,
    },()=>{
      router.pop()
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getallList();
  },

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