// monitor/pages/deviceList/deviceList.js
const Api = require("../../../utils/util.js"),
  app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:[],
    pageNum: 1,
    pageSize: 10, 
    entryId: ''  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(options.id){
      this.setData({
        entryId: options.id
      })
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getList()
  },
  // 列表
  getList: function () {
    var that = this
    var data = {
      current: this.data.pageNum,
      size: this.data.pageSize,
      innerStationId: this.data.entryId,
    }
    wx.showLoading({
      title: '加载中...',
    })   
    Api.req('nh/device/list', data, 'get').then(res => {
      wx.stopPullDownRefresh()
      wx.hideLoading()
      if(res.code== 200){
        let resData = res.data.records;
        resData.forEach((item, index)=>{
          let indexs = index + 1
          item.codeIndex = indexs - Math.round(index/2)
          // if(indexs % 2 === 0 ){//双数
          //   console.log('双数', indexs, index, '双数从1开始减',indexs - Math.round(index/2))
          //   item.codeIndex = indexs - index
          // }else{//单数
          //   console.log('单数', indexs, index, '单数从0开始减',indexs - Math.round(index/2))
          //   item.codeIndex = indexs - index
          // }
        })
        this.data.loadEnd = resData.length < this.data.pageSize
        if (this.data.pageNum == 1) {
          this.data.records = resData
        } else {
          this.data.records = this.data.records.concat(resData)
        }
        if (!this.data.loadEnd) {
          this.data.pageNum++;
        }
        this.setData({
          list: this.data.records,
          loadEnd: this.data.loadEnd,
        })
      }else{
        wx.showToast({
          title: res.errMsg,
          icon: 'none'
        })
      }
    })
  },
  //搜索
  keyWordsSearch: function (e) {
    this.setData({
      keyWords: e.detail,
      pageNum: 1,
      list: []
    })
    this.getList()
  },

  /** 联系租户 **/
  phone(e) {
    const phone = e.currentTarget.dataset.phone;
    if (!phone) {
      wx.showToast({
        title: '暂无租户联系方式',
        icon: 'none'
      })
      return
    }
    wx.makePhoneCall({
      phoneNumber: phone,
    })
  },
//跳转详情
  goDetail(e){
    var type = e.currentTarget.dataset.type 
    var id = e.currentTarget.dataset.id
    //只有逆变器可以跳转
    if(type == '2'){ return}
    wx.navigateTo({
      url: '../deviceDetail/deviceDetail?id=' + id,
    })
  },

  /** 下拉刷新 **/
  onPullDownRefresh(e) {
    this.setData({
      pageNum: 1,
      pageSize: 10,
      loadEnd: false,
    })
    this.getOrder() 
    this.getNum();
  },
  /** 页面触底上拉刷新 **/
  onReachBottom(e) {
    if (!this.data.loadEnd) {       
      this.getOrder()
    } else {
      wx.showToast({
        title: '暂无更多数据',
        icon: 'none',
        duration: 500
      })
    }
  },
})