// pages/t-alarm/t-alarm.js
var myRequest = require("../../utils/api.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    icons: [,
      'chevron-right',
    ],
    page: 1,
    size: 10,
    label: '请选择站点',
    singleSelect: {
      value: '',
      options: [],
    },
    keys: {
      value: 'code',
      label: 'name',
    },
    isFromSearch: false,
    projectId: '',
    alarmData: [],
    deviceCode: '',
    deviceName: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var projectId = wx.getStorageSync('projectId');
    this.setData({
      projectId: projectId
    })
    
    // this.getDevicesData();
  },
  
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      alarmData: [],
    })
    this.getAlarmData()
  },
  //上拉
  onReachBottom() {
    this.setData({
      page: that.data.page+1,
      isFromSearch: false
    });
    this.getAlarmData()
  },
  search() {
    wx.navigateTo({
      url: '../search-site/search-site?type=gaojing',
    })
  },
//获取站点
getDevicesData: function()
{
  var that = this;
  myRequest.myRequest('alarm/deviceList', {
    projectId: that.data.projectId,
  }, 'get').then(function(res){
    var devicesData = res.data.data;
    that.setData({
      'singleSelect.options': devicesData,
    });
    
  }).catch(function(res){
    console.log(res);
  })
},
  getAlarmData() {
    var that = this;
    var deviceCode = that.data.deviceCode;
    // var deviceCode = that.data.singleSelect.value;
    var url  = 'alarm/listPage?projectId=' + that.data.projectId + '&page=' + that.data.page + '&size=' + that.data.size;
    if (deviceCode != '') {
      url += '&deviceCode=' + deviceCode;
    }
    myRequest.myRequest(url, {}, 'get').then(function(res){
      var siteList = res.data.data.items;
      if (siteList.length != 0){
        var list = [];
        //如果isFromSearch是true从data中取出数据，否则先从原来的数据继续添加 
        that.data.isFromSearch ? list = siteList : list = that.data.alarmData.concat(siteList) 
        that.setData({ 
          alarmData: list, //获取数据数组 
        });
      }
      that.setData({ 
        searchLoadingComplete: true, //把“没有数据”设为true，显示 
        searchLoading: false //把"上拉加载"的变量设为false，隐藏 
       });
    }).catch(function(res){
      console.log(res);
    })
  },
  handleSingleSelect(e) {
    this.setData({
      'singleSelect.value': e.detail.value,
      alarmData: [],
    });
    this.setSelectLabel(e.detail.value)
    this.getAlarmData();
  },
  setSelectLabel(value) {
    var options = this.data.singleSelect.options;
    for (let i = 0; i < options.length; i++) {
      if(options[i].code == value) {
        this.setData({
          label: options[i].name
        })
      }
    }
    console.log(this.data.label)
  },
  toTask(e) {
    var id = e.currentTarget.dataset.id;
    var deviceCode = e.currentTarget.dataset.code;
    var deviceName = e.currentTarget.dataset.name;
    wx.navigateTo({
      url: '../u-task/u-task?alarmRecordId=' + id +'&deviceCode=' + deviceCode +'&deviceName=' + deviceName,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },


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

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

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

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

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