// pages/deepExcavation/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    DeviceGroupData: [],
    DeviceGroupNameData:[],
    stateNav:1,
    statePosit:1,
    qingxieData: {},
    weiyiData:{},
    active:3,
    show: false,
    loadShow:false,
    loginName:'',
    userName:'',
    deptId:'',
    projectName:'',
    projectId:'',
    //业务参数
    groupType:'',
    groupId:'',
    sensorArray:[],
    sensorObject:{},
    //位移
    shiftCount:0,
    //倾斜
    tiltCount:0,
    //应力
    stressCount:0,
    //报警
    callPoliceCount:0,
    //预警
    earlyWarningCount:0,
    //离线
    offlineCount:0,
    //正常
    normalCount:0,

    initData:{},
    sensorTypeName:'',
    sensorTypeIndex:''

  },

  //项目切换 返回值
  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,
      sensorArray:[]
    })
    this.getSensorTypeList('');
  },

  showPopup() {
    this.setData({ show: true });
  },

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

  onClickShow() {
    this.setData({ loadShow: true });
  },

  onClickHide() {
    this.setData({ loadShow: false });
  },

  //设备组  选择  
  onDeviceGroupSelect(e){
    this.onClickShow();
    this.setData({
      groupType:e.detail.id,
      sensorArray:[]
    })
    this.getSensorGroupList(e.detail.id);
  },

  //设备组名称  选择    
  onDeviceGroupNameSelect(e){
    this.onClickShow();
    this.setData({
      groupId:e.detail.id,
      sensorArray:[]
    })
    this.getSensorDataDay(this.data.groupType,e.detail.id);
  },

  //状态点击
  onStatePosit(e){
    this.onClickShow();
    let state = e.currentTarget.dataset.state;
    let sensorObject = this.data.sensorObject;
    let dateList = sensorObject.dateList;
    this.setData({statePosit:state,stateNav:1,displayState:true});
    if(state == 1){
      this.getShiftArray(sensorObject.shiftCallPoliceArray,dateList);
      this.setData({
        callPoliceCount:sensorObject.shiftCallPoliceArray.length,
        earlyWarningCount:sensorObject.shiftEarlyWarningArray.length,
        offlineCount:sensorObject.shiftOfflineArray.length,
        normalCount:sensorObject.shiftNormalArray.length
      })
    }else if(state == 2){
      this.getTiltArray(sensorObject.tiltCallPoliceArray,dateList);
      this.setData({
        callPoliceCount:sensorObject.tiltCallPoliceArray.length,
        earlyWarningCount:sensorObject.tiltEarlyWarningArray.length,
        offlineCount:sensorObject.tiltOfflineArray.length,
        normalCount:sensorObject.tiltNormalArray.length
      })
    }else if(state == 3){
      this.getStressArray(sensorObject.stressCallPoliceArray,dateList);
      this.setData({
        callPoliceCount:sensorObject.stressCallPoliceArray.length,
        earlyWarningCount:sensorObject.stressEarlyWarningArray.length,
        offlineCount:sensorObject.stressOfflineArray.length,
        normalCount:sensorObject.stressNormalArray.length
      })
    }
  },

  onStateSelect(e){
    this.onClickShow();
    let state = e.currentTarget.dataset.state;
    this.setData({stateNav:state,displayState:true});
    this.judgmentData(state);
  },

  /**
   *  返回值   // 1 为单次   2  为累计
  */
  onWeiyi1(e){
    let deviceId = e.currentTarget.dataset.deviceid
    let sensorArray = this.data.sensorArray;
    for(let i = 0;i<sensorArray.length;i++){
      if(sensorArray[i].deviceId == deviceId){
        sensorArray[i] = {"lineChart":sensorArray[i].lineChart,"lineChartSum":sensorArray[i].lineChartSum,"pointName":sensorArray[i].pointName,"type":sensorArray[i].type,"deviceId":sensorArray[i].deviceId,"displayState":e.detail == 1?true:false};
      }
    }
    this.setData({sensorArray:sensorArray});
  },

  onQingxie1(e){
    let deviceId = e.currentTarget.dataset.deviceid
    let sensorArray = this.data.sensorArray;
    for(let i = 0;i<sensorArray.length;i++){
      if(sensorArray[i].deviceId == deviceId){
        sensorArray[i] = {"lineChart":sensorArray[i].lineChart,"lineChartSum":sensorArray[i].lineChartSum,"pointName":sensorArray[i].pointName,"type":sensorArray[i].type,"deviceId":sensorArray[i].deviceId,"displayState":e.detail == 1?true:false};
      }
    }
    this.setData({sensorArray:sensorArray});
  },

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

  /**
   * 获取传感器类型
   */
  getSensorTypeList:function(id){
    wx.request({
      url: app.globalData.reqUrl+'/weixin/security/getSensorTypeList',
      method: 'get',
      data: {
        deptId:this.data.deptId,
        projectId:this.data.projectId
      },
      success: resData => {
        this.onClickHide();
        let DeviceGroupData = [];
        let sensorTypeName;
        let sensorTypeIndex;
        for(let i = 0;i<resData.data.length;i++){
          if(resData.data[i].group_type == id){
            sensorTypeName = resData.data[i].type_name;
            sensorTypeIndex = i;
          }

          let GroupData = {"id":resData.data[i].group_type,"text":resData.data[i].type_name};
          DeviceGroupData.push(GroupData);
        }
        this.setData({
          DeviceGroupData:DeviceGroupData,
          sensorTypeName:sensorTypeName,
          sensorTypeIndex:sensorTypeIndex
        })
        //初始化
        this.getSensorGroupList(id ==''?DeviceGroupData[0].id:id);
      }
    })
  },

  /**
   * 获取传感器分组
   */
  getSensorGroupList:function(groupType){
    wx.request({
      url: app.globalData.reqUrl+'/weixin/security/getSensorGroupList',
      method: 'get',
      data: {
        deptId:this.data.deptId,
        projectId:this.data.projectId,
        groupType:groupType
      },
      success: resData => {
        this.onClickHide();
        if(resData.length > 0){
          let DeviceGroupNameData = [];
          for(let i = 0;i<resData.data.length;i++){
            let GroupData = {"id":resData.data[i].group_id,"text":resData.data[i].group_name};
            DeviceGroupNameData.push(GroupData);
          }
          this.setData({
            DeviceGroupNameData:DeviceGroupNameData
          })
          //初始化
          this.getSensorDataDay(groupType,DeviceGroupNameDataDeviceGroupNameData[0].id);
        }
      }
    })
  },

  /**
   * 获取传感器日常数据
   */
  getSensorDataDay:function(groupType,groupId){
    wx.request({
      url: app.globalData.reqUrl+'/weixin/security/getSensorDataDay',
      method: 'get',
      data: {
        deptId:this.data.deptId,
        projectId:this.data.projectId,
        groupType:groupType,
        groupId:groupId
      },
      success: resData => {
        var that = this;
        that.onClickHide();
        //位移
        let shiftCallPoliceArray = resData.data.shiftCallPoliceArray;
        let shiftEarlyWarningArray = resData.data.shiftEarlyWarningArray;
        let shiftOfflineArray = resData.data.shiftOfflineArray;
        let shiftNormalArray = resData.data.shiftNormalArray;
        //倾斜
        let tiltCallPoliceArray = resData.data.tiltCallPoliceArray;
        let tiltEarlyWarningArray = resData.data.tiltEarlyWarningArray;
        let tiltOfflineArray = resData.data.tiltOfflineArray;
        let tiltNormalArray = resData.data.tiltNormalArray;
        //应力
        let stressCallPoliceArray = resData.data.stressCallPoliceArray;
        let stressEarlyWarningArray = resData.data.stressEarlyWarningArray;
        let stressOfflineArray = resData.data.stressOfflineArray;
        let stressNormalArray = resData.data.stressNormalArray;

        let shiftCount = shiftCallPoliceArray.length+ shiftEarlyWarningArray.length+shiftOfflineArray.length +shiftNormalArray.length;
        let tiltCount = tiltCallPoliceArray.length+ tiltEarlyWarningArray.length+ tiltOfflineArray.length+ tiltNormalArray.length;
        let stressCount = stressCallPoliceArray.length+ stressEarlyWarningArray.length+ stressOfflineArray.length+ stressNormalArray.length;

        //判断默认展示位移有数据的
        let stateNav = this.data.stateNav;
        if(shiftCallPoliceArray.length != 0){
          this.getShiftArray(shiftCallPoliceArray,resData.data.dateList);
          stateNav = 1;
        }else if(shiftEarlyWarningArray.length != 0){
          this.getShiftArray(shiftEarlyWarningArray,resData.data.dateList);
          stateNav = 2;
        }else if(shiftOfflineArray.length != 0){
          this.getShiftArray(shiftOfflineArray,resData.data.dateList);
          stateNav = 3;
        }else if(shiftNormalArray.length != 0){
          this.getShiftArray(shiftNormalArray,resData.data.dateList);
          stateNav = 4;
        }

        this.setData({
          sensorObject:resData.data,
          shiftCount:shiftCount,
          tiltCount:tiltCount,
          stressCount:stressCount,
          callPoliceCount:shiftCallPoliceArray.length,
          earlyWarningCount:shiftEarlyWarningArray.length,
          offlineCount:shiftOfflineArray.length,
          normalCount:shiftNormalArray.length,
          stateNav:stateNav
        })
      }
    })
  },

  /**
   * 位移
   */
  getShiftArray:function(array,dateList){
    let sensorArray = [];
    for(let i = 0;i<array.length;i++){
      let dataList = array[i].dataList;
      let baojingUp = [];
      let baojingDown = [];
      let yujingUp = [];
      let yujingDown = [];
      let celiang = [];

      let ljbaojingUp = [];
      let ljbaojingDown = [];
      let ljyujingUp = [];
      let ljyujingDown = [];
      let ljceliang = [];
      for(let j = 0;j<dataList.length;j++){
        baojingUp.push(dataList[j].singleTimeUpAlarmValue);
        baojingDown.push(dataList[j].singleTimeDownAlarmValue);
        yujingUp.push(dataList[j].singleTimeUpWarnValue);
        yujingDown.push(dataList[j].singleTimeDownWarnValue);
        celiang.push(dataList[j].singleTimeMesureValue);

        ljbaojingUp.push(dataList[j].cumulativeMesureUpAlarmValue);
        ljbaojingDown.push(dataList[j].cumulativeMesureDownAlarmValue);
        ljyujingUp.push(dataList[j].cumulativeMesureUpWarnValue);
        ljyujingDown.push(dataList[j].cumulativeMesureDownWarnValue);
        ljceliang.push(dataList[j].cumulativeMesureValue);
      }
      let lineChart = {
        unit : 'mm',
        Xdata:dateList,
        Ydata:celiang,
        warningMax :yujingUp,
        warningMin :yujingDown,
        policeMax : baojingUp,
        policeMin : baojingDown,
      };

      let lineChartSum = {
        unit : 'mm',
        Xdata:dateList,
        Ydata:ljceliang,
        warningMax :ljyujingUp,
        warningMin :ljyujingDown,
        policeMax : ljbaojingUp,
        policeMin : ljbaojingDown,
      };
      
      sensorArray.push({"lineChart":lineChart,"lineChartSum":lineChartSum,"deviceId":array[i].deviceId,"type":1,"pointName":"位移（"+array[i].pointName+"）","displayState":true});
    }
    this.setData({
      sensorArray:sensorArray
    })
    this.onClickHide();
  },

  /**
   * 倾斜
   */
  getTiltArray:function(array,dateList){
    let sensorArray = [];
    for(let i = 0;i<array.length;i++){
      let dataList = array[i].dataList;
      let xCeliang = [];
      let yCeliang = [];

      let ljxCeliang = [];
      let ljyCeliang = [];
     
      for(let j = 0;j<dataList.length;j++){
        xCeliang.push(dataList[j].singleTimeVerticalMesureValue);
        yCeliang.push(dataList[j].singleTimeHorizontalMesureValue);

        ljxCeliang.push(dataList[j].cumulativeVerticalMesureValue);
        ljyCeliang.push(dataList[j].cumulativeHorizontalMesureValue);
      }
      let lineChart = {
        unit : 'mm',
        legend:['X轴', 'Y轴'],
        color:['#2e6ed0','#fb9300'],
        Xdata :dateList,
        Ydata:[xCeliang,yCeliang]
      };
      let lineChartSum = {
        unit : 'mm',
        legend:['X轴', 'Y轴'],
        color:['#2e6ed0','#fb9300'],
        Xdata :dateList,
        Ydata:[ljxCeliang,ljyCeliang]
      };
      sensorArray.push({"lineChart":lineChart,"lineChartSum":lineChartSum,"deviceId":array[i].deviceId,"type":2,"pointName":"倾斜（"+array[i].pointName+"）","displayState":true});
    }
    this.setData({
      sensorArray:sensorArray
    })
    this.onClickHide();
  },

  /**
   * 应力
   */
  getStressArray:function(array,dateList){
    let sensorArray = [];
    for(let i = 0;i<array.length;i++){
      let dataList = array[i].dataList;
      let baojingUp = [];
      let baojingDown = [];
      let yujingUp = [];
      let yujingDown = [];
      let celiang = [];

      let ljbaojingUp = [];
      let ljbaojingDown = [];
      let ljyujingUp = [];
      let ljyujingDown = [];
      let ljceliang = [];
      for(let j = 0;j<dataList.length;j++){
        baojingUp.push(dataList[j].singleTimeUpAlarmValue);
        baojingDown.push(dataList[j].singleTimeDownAlarmValue);
        yujingUp.push(dataList[j].singleTimeUpWarnValue);
        yujingDown.push(dataList[j].singleTimeDownWarnValue);
        celiang.push(dataList[j].singleTimeMesureValue);

        ljbaojingUp.push(dataList[j].cumulativeMesureUpAlarmValue);
        ljbaojingDown.push(dataList[j].cumulativeMesureDownAlarmValue);
        ljyujingUp.push(dataList[j].cumulativeMesureUpWarnValue);
        ljyujingDown.push(dataList[j].cumulativeMesureDownWarnValue);
        ljceliang.push(dataList[j].cumulativeMesureValue);
      }
      let lineChart = {
        unit : 'KN',
        Xdata:dateList,
        Ydata:celiang,
        warningMax :yujingUp,
        warningMin :yujingDown,
        policeMax : baojingUp,
        policeMin : baojingDown,
      };

      let lineChartSum = {
        unit : 'KN',
        Xdata:dateList,
        Ydata:ljceliang,
        warningMax :ljyujingUp,
        warningMin :ljyujingDown,
        policeMax : ljbaojingUp,
        policeMin : ljbaojingDown,
      };
      sensorArray.push({"lineChart":lineChart,"lineChartSum":lineChartSum,"deviceId":array[i].deviceId,"type":1,"pointName":"应力（"+array[i].pointName+"）","displayState":true});
    }
    this.setData({
      sensorArray:sensorArray
    })
    this.onClickHide();
  },
  
  /**
   * 展示数据逻辑判断
   *    type  1 单次
   *          2 累计
   */
  judgmentData:function(state){
    let statePosit = this.data.statePosit;
    let sensorObject = this.data.sensorObject;
    let sensorArray = [];
    let dateList = sensorObject.dateList;
    //位移
    if(statePosit == 1){
      if(state == 1){
        sensorArray = sensorObject.shiftCallPoliceArray;
      }else if(state == 2){
        sensorArray = sensorObject.shiftEarlyWarningArray;
      }else if(state == 3){
        sensorArray = sensorObject.shiftOfflineArray;
      }else if(state == 4){
        sensorArray = sensorObject.shiftNormalArray;
      } 
      this.getShiftArray(sensorArray,dateList);
    }else if(statePosit == 2){//倾斜
      if(state == 1){
        sensorArray = sensorObject.tiltCallPoliceArray;
      }else if(state == 2){
        sensorArray = sensorObject.tiltEarlyWarningArray;
      }else if(state == 3){
        sensorArray = sensorObject.tiltOfflineArray;
      }else if(state == 4){
        sensorArray = sensorObject.tiltNormalArray;
      } 
      this.getTiltArray(sensorArray,dateList);
    }else if(statePosit == 3){//应力
      if(state == 1){
        sensorArray = sensorObject.stressCallPoliceArray;
      }else if(state == 2){
        sensorArray = sensorObject.stressEarlyWarningArray;
      }else if(state == 3){
        sensorArray = sensorObject.stressOfflineArray;
      }else if(state == 4){
        sensorArray = sensorObject.stressNormalArray;
      } 
      this.getStressArray(sensorArray,dateList);
    }
  },


  // 底部导航
   onChange(event) {
    // event.detail 的值为当前选中项的索引
    this.setData({ active: event.detail });
  },

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

  //跳转到设备管理
  SBGL:function(){
    wx.redirectTo({
      url: '../shebieguanli-jxsb/shajiangguan',
    })
  },

  //跳转到人员管理
  RYGL:function(){
      wx.redirectTo({
        url: '../renyuanguanli/renyuanguanli'
      })
  },

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

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

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

  //返回到安全管理页面
  goGCLB:function(){
    wx.redirectTo({
      //url: '../gongchengliebiao/gongchengliebiao'
      url:'../newAddPage/safetyManagement/index'
    })
  }

})