import $ from './../../utils/Tool'
import router from './../../utils/router'
import {formatMsgTime} from './../../utils/util'
import {getFeedbackList} from '../../requests/api'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataList:[],
    page:0,
    tempType:{
      1: "有点冷",
      3: "有点热",
      2: "太冷了",
      4: "太热了",
      5:"风太大",
      10: "关闭设备",
      11:"赞",
      12:"开启设备"
    },
    type:null,
    allCount:0
  },
  goBack(){
    router.pop()
  },
  goHome(){
    $.store.set('goHome',true);
    router.toHome()
  },
  initDate(value){
    let date = new Date(value);
    let month = date.getMonth() + 1
    let day = date.getDate()
    let hour = date.getHours()
    let minute = date.getMinutes()
    hour = hour<10?`0${hour}`:hour;
    minute = minute<10?`0${minute}`:minute;

    return `${month}.${day} ${hour}:${minute}`
  },
  getDate(){
    this.setData({page:this.data.page+1},async ()=>{
      let data={
          "criteria": {
              "projectId": $.store.get('projectId'),
              "userId":$.store.get('userId')
          },
          page: this.data.page,
          size:30,
          "orders": [
              {
                  "column": "createTime",
                  "asc": false
              }
          ]
      }
      if(this.data.type==="space"){
        data.orders.push(
          {
            "column": "objectId",
            "asc": true
        })
      }

      wx.showLoading({
        title:"加载中"
      })
      await getFeedbackList(data).then(res=>{
        if(res.count){
          res.content.forEach(item=>{
            item.text=this.data.tempType[item.itemId] || '';
            if(!item.text){
              item.text = item.valueType==3?'设置全天温度计划':`调温至${item.value}°C`;
            }
            item.createTime = formatMsgTime(new Date(item.createTime)); //this.initDate
          })
        }
        res.content&&this.setData({dataList:this.data.dataList.concat(res.content)});
        // console.log(res);
      })
      wx.hideLoading()
    });

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let {spaceFeedbackNum,type} = options;
    this.setData({allCount:Number(spaceFeedbackNum),type:type},()=>{
      this.getDate();
    })
  },

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

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
   onPullDownRefresh() {
    this.setData({page:0,dataList:[]},async ()=>{
      await this.getDate();
      wx.stopPullDownRefresh();
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.dataList&&(this.data.dataList.length<this.data.allCount)){
      this.getDate();
    }
  },

})