// pages/newAddPage/safetyManagement/index.js

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 1,
    deptId: '',
    projectName: '',
    projectId: '',
    loginName: '',
    userName: '',
    initData: {},
    projectData: {},

    //教育培训
    videoSurvey: [
    ],
    //基坑检测
    safetyDeepData: [
      {
        name: '沉降',
        total: 0,
        notMonitor: 0,
        yesMonitor: 0,
        unit: '个'
      },
      {
        name: '倾斜',
        total: 0,
        notMonitor: 0,
        yesMonitor: 0,
        unit: '个'
      },
      {
        name: '应力',
        total: 0,
        notMonitor: 0,
        yesMonitor: 0,
        unit: '个'
      },
    ],
    //爬架检测
    safetyFrameData: [
      {
        name: '倾斜',
        total: 0,
        notMonitor: 0,
        yesMonitor: 0,
        unit: '个'
      },
      {
        name: '高度',
        total: 0,
        notMonitor: 0,
        yesMonitor: 0,
        unit: '个'
      },
      {
        name: '载荷',
        total: 0,
        notMonitor: 0,
        yesMonitor: 0,
        unit: '个'
      },
    ],
    securityCheck: {
      routineRectificationRate: 0, //常规整改率
      routineProblemTotal: 0,//常规问题总数
      routineCheckNumber: 0,//常规检查次数
      specialRectificationRate: 0, //专项整改率
      specialProblemTotal: 0,//专项问题总数
      specialCheckNumber: 0,//专项检查次数
    },
    pitCount: {//基坑统计数据
      onlineNumber: 0,
      offlineNumber: 0,
      total: 0,
    },
    climbCount: {//爬架统计数据
      onlineNumber: 0,
      offlineNumber: 0,
      total: 0,
    },
    power: {//配电箱信息
      t1: 0,
      t2: 0,
      t3: 0,
      t4: 0,
      c1: 0,
      c2: 0,
      c3: 0,
      leak: 0
    },
    highlightPictures: []
  },
  showPopup() {
    this.setData({ show: true });
  },

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

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

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

  //项目切换 返回值
  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()
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this
    wx.getStorage({
      key: 'userinfo',
      success: function (res) {
        console.log(res.data);
        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.getProjectCorrespondence(app.globalData.projectId);
      }
    })
  },

  /**
   * 项目对应关系
   */
  getProjectCorrespondence(projectId) {
    var that = this;
    wx.request({
      url: app.globalData.reqUrl + '/weixin/training/getProjectCorrespondence',
      data: {
        "projectId": projectId,
      },
      method: "get",
      success: function (res) {
        console.log(res.data);
        that.setData({
          projectData: res.data
        })
        that.getSecurityCheck()
        that.getPitCount()
        that.getClimbCount()
        that.getDistributionBoxData()
        that.getHighlightPictures()
        that.getTraining()
      }
    })
  },
  /**
   * 教育培训
   */
  getTraining() {
    let that = this
    wx.request({
      url: app.globalData.szhUrl + "/api/building/listCultivate?project_id=" + this.data.projectData.szh + "&cultivate_type=4",
      method: "post",
      success: res => {
        wx.request({
          url: app.globalData.szhUrl + "/api/building/listCultivate?project_id=" + this.data.projectData.szh + "&cultivate_type=6",
          method: "post",
          success: res1 => {
            wx.request({
              url: app.globalData.szhUrl + "/api/building/listCultivate?project_id=" + this.data.projectData.szh + "&cultivate_type=3",
              method: "post",
              success: res2 => {
                let total = res.data.data.length
                  + res1.data.data.length
                  + res2.data.data.length
                that.setData({
                  videoSurvey: [
                    { name: '三级安全教育', value: res.data.data.length, prop: (res.data.data.length / total * 100).toFixed(2) },
                    // { name: '班前讲话', value: '0', prop: 0 },
                    { name: '应急演练', value: res2.data.data.length, prop: (res2.data.data.length / total * 100).toFixed(2) },
                    { name: '专项培训', value: res1.data.data.length, prop: (res1.data.data.length / total * 100).toFixed(2) }
                  ]
                })
                // wx.request({
                //   url: app.globalData.szhUrl + "/api/building/listCultivate?project_id=" + this.data.projectData.szh + "&cultivate_type=4",
                //   method: "post",
                //   success: res3 => {

                //   }
                // })
              }
            })
          }
        })
      }
    })
  },
  /**
   * 获取安全检查数据
   */
  getSecurityCheck() {
    wx.request({
      url: app.globalData.szhUrl + '/api/check/getCheckStatistics?project_id=' + this.data.projectData.szh + "&large_type=1",
      success: res1 => {
        wx.request({
          url: app.globalData.szhUrl + '/api/check/getCheckStatistics?project_id=' + this.data.projectData.szh + "&large_type=2",
          success: res2 => {
            this.setData({
              "securityCheck.routineRectificationRate": res1.data.data.rate,
              "securityCheck.routineProblemTotal": res1.data.data.problemTotal,
              "securityCheck.routineCheckNumber": res1.data.data.total,
              "securityCheck.specialRectificationRate": res2.data.data.rate,
              "securityCheck.specialProblemTotal": res2.data.data.problemTotal,
              "securityCheck.specialCheckNumber": res2.data.data.total,
            })
          }
        })
      }
    })
  },
  /**
   * 获取基坑监测统计
   */
  getPitCount() {
    wx.request({
      url: app.globalData.aqzgUrl + '/weixin/structProjectSensorInfo/select/sensorInfo?projectId=' + this.data.projectData.aqzg + "&sensorType=4",
      method: "post",
      success: res => {
        console.log(res)
        let zxDeviceTotal = 0
        let cj = 0
        let cj_o = 0
        let qx = 0
        let qx_o = 0
        let yl = 0
        let yl_o = 0
        res.data.data.map(x => {
          let a = 0
          if (x.status != 2) {
            zxDeviceTotal++;
            a = 1
          }
          switch (x.sensorType) {
            case "CollWeiyiPoint":
              cj++
              cj_o += a
              break
            case "CollQingxiePoint":
              qx++
              qx_o += a
              break
            case "YingLi":
              yl++
              yl_o += a
              bre
          }
        })
        this.setData({
          "pitCount.onlineNumber": zxDeviceTotal,
          "pitCount.offlineNumber": res.data.data.length - zxDeviceTotal,
          "pitCount.total": res.data.data.length,
          safetyDeepData: [
            {
              name: '沉降',
              total: cj,
              notMonitor: cj - cj_o,
              yesMonitor: cj_o,
              unit: '个'
            },
            {
              name: '倾斜',
              total: qx,
              notMonitor: qx - qx_o,
              yesMonitor: qx_o,
              unit: '个'
            },
            {
              name: '应力',
              total: yl,
              notMonitor: yl - yl_o,
              yesMonitor: yl_o,
              unit: '个'
            },
          ],
        })
      }
    })
  },
  /**
   * 获取爬架监测统计
   */
  getClimbCount() {
    wx.request({
      url: app.globalData.szhUrl + '/screen/api/getDeviceBaseInfo?projectId=' + this.data.projectData.aqzg + '&drawId=7053550232834936832',
      method: "post",
      success: res => {
        let zxDeviceTotal = 0
        let cj = 0
        let cj_o = 0
        let qx = 0
        let qx_o = 0
        let yl = 0
        let yl_o = 0

        res.data.data.map(x => {
          let a = 0
          if (x.status != 2) {
            zxDeviceTotal++;
            a = 1
          }
          switch (x.sensorType) {
            case "CollWeiyiPoint":
              cj++
              cj_o += a
              break
            case "CollQingxiePoint":
              qx++
              qx_o += a
              break
            case "YingLi":
              yl++
              yl_o += a
              bre
          }
        })
        this.setData({
          "climbCount.onlineNumber": zxDeviceTotal,
          "climbCount.offlineNumber": res.data.data.length - zxDeviceTotal,
          "climbCount.total": res.data.data.length,
          safetyFrameData: [
            {
              name: '倾斜',
              total: qx,
              notMonitor: qx - qx_o,
              yesMonitor: qx_o,
              unit: '个'
            },
            {
              name: '高度',
              total: 0,
              notMonitor: 0,
              yesMonitor: 0,
              unit: '个'
            },
            {
              name: '载荷',
              total: 0,
              notMonitor: 0,
              yesMonitor: 0,
              unit: '个'
            },
          ],
        })
      }
    })
  },
  /**
   * 获取塔机监测统计
   */
  getTowerCraneCount() {
    wx.request({
      url: app.globalData.aqzgUrl + '/api/statistics/getDeviceSourceCensus',
      method: "post",
      success: res => {
        let zxDeviceTotal = 0
        res.data.data.map(x => {
          if (x.status != 2) {
            zxDeviceTotal++;
          }
        })
        this.setData({
          "towerCraneCount.onlineNumber": zxDeviceTotal,
          "towerCraneCount.offlineNumber": res.data.data.length - zxDeviceTotal,
          "towerCraneCount.total": res.data.data.length
        })
      }
    })
  },
  /**
   * 获取配电箱数据
   */
  getDistributionBoxData() {
    wx.request({
      url: app.globalData.szhUrl + '/api/iot/power/latestList',
      data: {
        projectId: this.data.projectData.szh,
        uid: 'PWR.02731XTJ'
      },
      success: res => {
        this.setData({
          power: res.data.rows[0] ? res.data.rows[0] : {
            t1: 0,
            t2: 0,
            t3: 0,
            t4: 0,
            c1: 0,
            c2: 0,
            c3: 0,
            leak: 0
          }
        })
      }
    })
  },
  /**
   * 亮点照片
   */
  getHighlightPictures() {
    wx.request({
      url: app.globalData.szhUrl + '/api/building/photoList?projectId=' + this.data.projectData.szh + '&typeMain=8&typeId=18',
      method: "post",
      success: res => {
        console.log(res)
        this.setData({
          highlightPictures: res.data.data
        })
      }
    })
  },

  goHighLightPage() {
    wx.navigateTo({
      url: '../../Highlight-photos/index',
    })
  },

  /**
   * 教育培训页面
   */
  goEducations() {
    wx.navigateTo({
      url: '../../../pageage/educations-list/index?jumpState=1',
    })
  },

  /**
   * 基坑页面
   */
  gofoundation(event) {
    let id = event.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../../deepExcavation/index?id='+id,
    })
  },

   /**
   * 跳转安全教育
   */
  goCheckView(){
    wx.navigateTo({
      url: '../../../pageage/safetyManagement/securityCheck/index',
    })
  },
/**
 * 配电箱监测
 */
  goPDJCDetail() {
    wx.navigateTo({
      url: '../../Security-control-echarts/index',
    })
  },


  /****************************底部导航********************************/
  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'
    })
  },

   //跳转到安全管控
   XMSP:function(){
    wx.redirectTo({
      //url: '../deepExcavation/index'
      url:'../../newAddPage/safetyManagement/index'
    }) 
  },
  
   //跳转到质量管理
   ZLGL2:function(){
    wx.redirectTo({
      url:'../../Quality-Assurance/index'
    }) 
  },
   //跳转到进度管理
   JDGL2:function(){
    wx.redirectTo({
      //url: '../deepExcavation/index'
      url:'../../../pageage/Progress-management/index'
    }) 
  },

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

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


})
