// pages/shebieguanli-taji/taji.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active:1,  
    leftShow:false,
    show: false,
    loadShow:false,
    value:'',
    actions: [],
    //基础参数
    projectId:'',
    projectName:'' ,
    loginName:'',
    userName:'',
    //塔基数据
    deviceName:'',
    deviceID:'',
    chargePerson:'',
    towerBasicData:{},
    towerWarnData:{},

    initData:{}
  },

   //项目切换 返回值
   onProjectSelect(e){
    this.onClickShow();
    let projectId = e.detail.id;
    let projectName = e.detail.text;
    app.globalData.projectId = projectId;
    app.globalData.projectName = projectName;
    this.setData({
      projectId:projectId,
      projectName:projectName
    })
    
    this.onLoad();
  },


  onClose() {
    this.setData({ show: false });
  },

  onclick(){
    this.setData({ show: true });
  },
  onSelect(event) {
    this.onClickShow();
    this.setData({
      value: event.detail.name,
      deviceName:event.detail.name,
      deviceID:event.detail.deviceSn,
      chargePerson:event.detail.chargePerson,
    });
    this.getTowerBasicData(event.detail.deviceID);
    this.selectTowerCraneDetails(event.detail.deviceID);
  },

  showPopup() {
    this.setData({ leftShow: true });
  },
  onLeftClose(){
    this.setData({ leftShow: false });
  },
  onClickShow() {
    this.setData({ loadShow: true });
  },
  onClickHide() {
    this.setData({ loadShow: false });
  },
  // 底部导航
  onChange(event) {
    // event.detail 的值为当前选中项的索引
    this.setData({ active: event.detail });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //this.onClickShow();
    var that = this;
      //获取缓存数据
      wx.getStorage({
        key: 'userinfo',
        success:function(res){
          that.setData({
            loginName:res.data.loginName,
            userName:res.data.userName,
            projectId:app.globalData.projectId,
            projectName:app.globalData.projectName,
            initData:{text:app.globalData.projectName,id:app.globalData.projectId}
          })  
        }
      })
      this.selectTowerCraneList(app.globalData.projectId);
  },

  //获取塔吊列表
  selectTowerCraneList:function(event){
      var that = this;
      wx.request({
        url: app.globalData.reqUrl+'/weixin/machicnery/selectTowerCraneList',
        data:{
          "projectId":event
        },
        method:"GET",
        success:function(res){
          that.onClickHide();
          if(res.data.code ==0 && res.data.data.length>0){     
              let actions = [];
              for(let i = 0;i<res.data.data.length;i++){
                actions.push({"name":res.data.data[i].towerName,"deviceID":res.data.data[i].id,"deviceSn":res.data.data[i].deviceSn,"chargePerson":res.data.data[i].principalPhone1});
              }
              that.setData({
                value:res.data.data[0].towerName,
                deviceName:res.data.data[0].towerName,
                deviceID:res.data.data[0].deviceSn,
                chargePerson:res.data.data[0].principalPhone1,
                actions:actions
              })
              that.getTowerBasicData(res.data.data[0].id);
              that.selectTowerCraneDetails(res.data.data[0].id);
          }else{
            that.setData({
              value:'',
              deviceName:'',
              deviceID:'',
              chargePerson:'',
              actions:[],
              towerBasicData:{},
              towerWarnData:{}
            })
          }     
        }
      })
  },

  /**
   * 塔机基础信息
   * @param {*} deviceId 
   */
  getTowerBasicData:function(deviceId){
    var that = this;
    wx.request({
      url: app.globalData.reqUrl+'/weixin/machicnery/getTowerBasicData',
      data:{
        "projectId":this.data.projectId,
        "deviceId":deviceId
      },
      method:"GET",
      success:function(res){
        that.onClickHide();
        that.setData({
          towerBasicData:res.data
        })
      }
    })
  },

  /**
   * 塔吊预警阈值
   * @param {*} deviceId 
   */
  selectTowerCraneDetails:function(deviceId){
    var that = this;
    wx.request({
      url: app.globalData.reqUrl+'/weixin/machicnery/selectTowerCraneDetails',
      data:{
        "projectId":this.data.projectId,
        "deviceId":deviceId
      },
      method:"GET",
      success:function(res){
        that.onClickHide();
        if(res.data.code == 0){
          that.setData({
            towerWarnData:res.data.data
          })
        }
      }
    })
  },

  //跳转到项目概况页面
  XMGK:function(){
    wx.redirectTo({
      url: '../xiangmugaikuang/xiangmugaikuang'
    })
  },

  //跳转到人员管理
  RYGL:function(){
    wx.redirectTo({
      url: '../renyuanguanli/renyuanguanli'
    })
  },
 
  //跳转到安全管控
  XMSP:function(){
    wx.redirectTo({
      //url: '../deepExcavation/index'
      url:'../newAddPage/safetyManagement/index'
    }) 
  },

  //跳转到更多更能
  GDGN:function(event){
    wx.redirectTo({
      url: '../gengduogongneng/gengduogongneng',
    })
  },

  //跳转到机械设备
  gojxsb:function(event){
    wx.redirectTo({
      url: '../shebieguanli-jxsb/shajiangguan'
    })
  },

  //退出登录
  TCDL:function(){
    wx.clearStorageSync();
    wx.setStorageSync('isReload', "1")
    wx.redirectTo({
      url: '../login/login',
    })
  },

  //返回到工程列表页面
  goGCLB:function(){
    wx.redirectTo({
      url: '../gongchengliebiao/gongchengliebiao'
    })
  },

  //返回到地图页面
  GOMAP:function(){
    wx.redirectTo({
      url: '../map/map',
    })
  },  
})