const app = getApp() 
//导入日志信息请求发方法 
import { 
  Logsinfo 
} from '../../lib/RequestingCenter.js' 
var util = require('../../utils/util.js') 
// pages/logs/logs.js 
Page({ 
  /** 
   * 页面的初始数据 
   */ 
  data: { 
    // 查询类型 
    apptype:-1, 
    // 时间范围 4 今日 
    seltime:4, 
    // 起始时间 
    starttime:"", 
    // 结束时间 
    endtime:"", 
    //正加载 
    status:0, 
    page:1, 
    logsdata:[], 
    //总数 
    sum:0, 
    //过滤后 
    filtcount:0, 
    //每次加载数量 
    pagelength:10, 
    //swview高度 
    topheight: 0, 
    //是否下拉 
    refresher:false, 
    // 酒店 
    Hotels:[], 
    // 下拉框的酒店 
    Hotelslist:[], 
   // 下拉框的索引 
    index:0, 
    //名字搜素 
    name:'' 
  }, 
 
  /** 
   * 生命周期函数--监听页面加载 
   */ 
  onLoad:async function (options) { 
    //测试无需登录 
    if(app.globalData.userinfo==null){ 
      wx.reLaunch({ 
        url: "../autho/index", 
      }) 
      return; 
    } 
    let that = this; 
    //酒店信息 
    let Hotels =[]; 
    let Hotelslist =[] 
    Hotelslist.push("全部酒店"); 
    let autho = app.globalData.autho 
    autho.forEach((x)=>{ 
      x.Hotels.forEach(z=>{ 
        Hotels.push(z); 
    Hotelslist.push(z.HotelName); 
      }) 
    }) 
    that.setData({ 
      Hotels :Hotels, 
      Hotelslist:Hotelslist 
    }) 
    setTimeout(function () { 
      try { 
      // 1.使用wx.createSelectorQuery()查询到需要滚动到的元素位置 
      wx.createSelectorQuery().select('#scroll').boundingClientRect(res => { 
        // 2.使用wx.getSysTemInfo()获取设备及页面高度windowHeight（px） 
        wx.getSystemInfo({ 
          success(ress) { 
            that.setData({ 
              topheight: ress.windowHeight - res.top 
            }) 
          } 
        }) 
      }).exec(function (params) { 
        console.log(params) 
      }) 
    } catch (error) { 
        console.log(error) 
    } 
    }, 100) 
    that.init(); 
  }, 
  sreach:function(params) { 
    this.setData({ 
      page:1 
    }) 
    this.init(); 
  }, 
  /** 
   * 下拉选择酒店 
   */ 
  bindPickerChange:function (e) { 
    this.setData({ 
      index: e.detail.value, 
      page:1 
    }) 
    this.init(); 
  }, 
  bindTimeChange:function (e) { 
    if(e.target.dataset.type==1){ 
      this.setData({ 
        seltime:5, 
        starttime: e.detail.value, 
        page:1 
      }) 
    }else{ 
      this.setData({ 
        seltime:5, 
        endtime: e.detail.value, 
        page:1 
      }) 
    } 
    this.init(); 
  }, 
  // 下拉刷新 
  toupper:function() { 
    if(this.data.refresher){ 
      return; 
    } 
    this.setData({ 
      refresher: true, 
    }) 
    var that=this; 
      that.setData({ 
        page: 1, 
      }) 
      that.init(); 
  }, 
  // 上滑加载 
  MoreData:function (params) { 
    if(this.data.status==1 || this.data.status==3){ 
      return; 
    } 
    let s = this.data.page+1; 
    this.setData({ 
      status:1, 
      page:s 
    }) 
    this.init(); 
  }, 
  //加载数据 
init:async function () { 
  try { 
  if(this.data.page<1){ 
    this.setData({ 
      page:1 
    }) 
  } 
  let that = this; 
  let search = {name:that.data.name,endtime:that.data.endtime,starttime:that.data.starttime,hotelsid:''}; 
  let index = this.data.index -1; 
  if(index>=0){ 
    search.hotelsid= that.data.Hotels[index].HotelId 
  } 
  await Logsinfo({ 
    page:that.data.page, 
    search:search, 
    length:that.data.pagelength, 
    AppType:that.data.apptype 
  }).then(res => { 
    if (res.Status == 200) { 
      let newdata = res.Data.data 
      if(that.data.page > 1){ 
        newdata = that.data.logsdata.concat(newdata); 
      } 
      //判断是否有更多 
      let more = 0; 
      if((that.data.page * that.data.pagelength)>=res.Data.filtcount){ 
        more = 3; 
      } 
      if(res.Data.filtcount<=0){ 
        newdata = []; 
      } 
      that.setData({ 
        refresher: false, 
       logsdata: newdata, 
       status: more, 
       filtcount:res.Data.filtcount, 
       sum: res.Data.sum 
      }) 
    } else { 
      that.setData({ 
        status:2, 
        page:that.data.page-1 
      }) 
      app.toast(2, res.Message || "网络繁忙") 
    } 
  }, err => { 
    that.setData({ 
      status:2, 
      page:that.data.page-1 
    }) 
    app.toast(2 ,"网络繁忙") 
  }).catch(err => { 
    that.setData({ 
      status:2, 
      page:that.data.page-1 
    }) 
    console.log(err) 
    app.toast(2 ,"网络繁忙") 
  }); 
} catch (error) { 
  console.log(error) 
} 
}, 
selapptype:function (e) { 
  let sel =Number(e.target.dataset.val) ; 
  this.setData({ 
    apptype: sel, 
    page:1 
  }) 
  this.init(); 
}, 
// 时间选择 
seltime:function (e) { 
  let sel =Number(e.target.dataset.val) ; 
  let starttime = util.formatTime(new Date()).split(' ')[0]; 
  let endtime = util.formatTime(new Date()).split(' ')[0]; 
  switch (sel) { 
    case 2: 
      let time = this.getThisWeekData(); 
 
      starttime = time.start_day;//显示周一 
      endtime =  time.end_day;//显示周日 
      break; 
      case 3: 
        starttime = util.getCurrentMonthFirst(new Date()) 
        endtime = util.getCurrentMonthLast(new Date()) 
      break; 
      case 4: 
        starttime = "" 
        endtime = "" 
      break; 
  } 
  this.setData({ 
    seltime: sel, 
    starttime:starttime, 
    endtime:endtime, 
    page:1 
  }) 
  this.init(); 
 
}, 
getThisWeekData:function () {//获得本周周一~周日的年月日   
  var thisweek = {}; 
  var date = new Date(); 
  // 本周一的日期 
  date.setDate(date.getDate() - date.getDay() + 1); 
  thisweek.start_day = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate() ; 
 
  // 本周日的日期 
  date.setDate(date.getDate() + 6); 
  thisweek.end_day = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate(); 
  return thisweek; 
}, 
  /** 
   * 生命周期函数--监听页面初次渲染完成 
   */ 
  onReady: function () { 
 
  }, 
 
  /** 
   * 生命周期函数--监听页面显示 
   */ 
  onShow: function () { 
// this.init(); 
  }, 
 
  /** 
   * 生命周期函数--监听页面隐藏 
   */ 
  onHide: function () { 
 
  }, 
 
  /** 
   * 生命周期函数--监听页面卸载 
   */ 
  onUnload: function () { 
 
  }, 
 
  /** 
   * 页面相关事件处理函数--监听用户下拉动作 
   */ 
  onPullDownRefresh: function () { 
 
  }, 
 
  /** 
   * 页面上拉触底事件的处理函数 
   */ 
  onReachBottom: function () { 
 
  }, 
 
  /** 
   * 用户点击右上角分享 
   */ 
  onShareAppMessage: function () { 
 
  } 
})
