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

  /**
   * 页面的初始数据
   */
  data: {
    value: '',
    actionText: '',
    name: '',
    url: '',
    projectData: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var url = '';
    if (options.type == 'xunjian') {
      url = 'inspection/deviceList';
    } else if (options.type == 'paigong') {
      url = 'task/deviceList'
    } else if (options.type == 'xianxia') {
      url = 'repair/deviceList'
    } else if (options.type == 'gaojing') {
      url = 'alarm/deviceList'
    } else if (options.type == 'jiankong') {
      url = 'system/devicesLocation'
    } else if (options.type == 'shuizhi') {
      url = 'inspection/deviceList'
    }
    this.setData({
      url: url
    })
  },
  changeHandle(e) {
    const { value } = e.detail;
    this.setData({
      name: value
    });
    this.getProjectData();
  },
 //获取项目下所有站点位置
 getProjectData() {
    var that = this;
    var url = this.data.url;
    myRequest.myRequest(url, {
      projectId: wx.getStorageSync('projectId'),
      name: that.data.name,
    },'get').then(function(res){
      that.setData({
        projectData: res.data.data,
      })
    }).catch(function(res){
      console.log(res);
    })
  },
  toJump(e) {
    var code = e.currentTarget.dataset.code;
    var name = e.currentTarget.dataset.name;
    var id = e.currentTarget.dataset.id;
    var pages = getCurrentPages(); // 获取页面栈
    var currPage = pages[pages.length - 1]; // 当前页面
    var prevPage = pages[pages.length - 2]; // 上一个页面
    console.log(code)
    prevPage.setData({
      deviceCode: code,
      deviceName: name,
      deviceId: id
    })
    wx.navigateBack({ delta: 1})
  },
  focusHandle() {
    this.setData({
      actionText: '取消',
    });
  },

  blurHandle() {
    this.setData({
      actionText: '',
    });
  },

  actionHandle() {
    this.setData({
      name: '',
      actionText: '',
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getProjectData();
  },

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

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

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

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

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