// pages/newAddPage/safetyManagement/index.js

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active:1,
    loginName:"",
    nickName:"",
    deptId:"",
    deptName:"",
    projectName: '',
    projectId: '',
    //教育培训
    videoSurvey: [
      { name: '应急演练', value: 0, prop: 0 },
      { name: '专项培训', value: 0, prop: 0 }
    ],
    trainList:[],
    trainTotal:0,
    emergencyDrillList:[],
    emergencyDrillTotal:0,
    //特种作业人员
    specialStatistics:[{name:'其它', value: 0, prop: 0 }],
    insuranceStatistics:[{name:'安责险', value: 0, prop: 0 },{name:'一切险', value: 0, prop: 0 }],
    deptWorksStatistics:[{name:'劳务人员', value: 0, prop: 0 },{name:'监理人员', value: 0, prop: 0 },{name:'总包人员', value: 0, prop: 0 }],
    securityCheck: {
      routineRectificationRate: 0, //常规整改率
      routineProblemTotal: 0,//常规问题总数
      routineCheckNumber: 0,//常规检查次数
      specialRectificationRate: 0, //专项整改率
      specialProblemTotal: 0,//专项问题总数
      specialCheckNumber: 0,//专项检查次数
    },
    menuList:[],
    request:app.globalData.reqUrl,
    todoDb:0,
    approveDb:0,
  },

  //项目切换 返回值
  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,
          nickName: res.data.nickName,
          deptId: res.data.deptId,
          deptName:res.data.deptName,
          projectName: app.globalData.projectName,
          projectId: app.globalData.projectId,
          initData: { text: app.globalData.projectName, id: app.globalData.projectId }
        })
        that.selectMenuList(res.data.loginName);
        that.initPage(app.globalData.projectId);
        that.awaitTask(res.data.minRoleId,res.data.deptId,res.data.loginName,res.data.userId);
      }
    })
  },

  /**
   * 初始化项目安全管理
   */
  initPage(projectId) {
    this.getTrainList(projectId,0);
    this.getSecurityCheck(projectId);
    this.getSpecial(projectId);
    this.getDeptWorks(projectId);
    this.getPojectInsurance(projectId);
    //this.getTraining(projectId);
    this.getTrainList(projectId,1);
  },

  selectMenuList:function(loginName){
    var that = this;
    wx.request({
      url: app.globalData.reqUrl+'/wechat/selectRoleMenuList',
      data:{
        username:loginName,
        type:"aq"
      },
      method:"get",
      success:function(res){
        res=res.data;
        if(res.code =='200'){
            that.setData({
              menuList:res.data
            })
        }
      }
    })
  },

  /**
   * 教育培训
   */
  getTraining(projectId) {
    wx.request({
      url: app.globalData.reqUrl + '/wechat/projectTrain/selectGroupCountByProjectId',
      data:{projectId:projectId},
      method:"get",
      success: res => {
        res = res.data;
        if(res.code == 200){
          let sum=0;
          res.data.forEach((it,idx)=>{
            sum+=it.total;
          });
          let videoSurvey=[];
          res.data.forEach((it,idx)=>{
            videoSurvey.push({ name: it.typeName, value: it.total, prop: (it.total / sum * 100).toFixed(2)});
          });
          //数据绑定
          this.setData({
            videoSurvey
          })
        }
      }
    })
  },

  /**
   * 特种人员
   */
  getSpecial(projectId) {
    wx.request({
      url: app.globalData.reqUrl + '/wechat/projectSpecial/selectGroupCountByProjectId',
      data:{projectId:projectId},
      method:"get",
      success: res => {
        res = res.data;
        if(res.code == 200 && res.data){
          let sum=0;
          let otherSum=0;
          let flag=true;
          res.data.forEach((it,idx)=>{
            sum+=it.total;
            if(idx>4 && res.data.length>6){
              otherSum+=it.total;
            }
          });
          let specialStatistics=[];
          res.data.forEach((it,idx)=>{
            if(flag){
              specialStatistics.push({ name: it.typeName, value: it.total, prop: (it.total / sum * 100).toFixed(2)});
              if(idx>3 && res.data.length>6){
                specialStatistics.push({ name: "其它工种", value: otherSum, prop: (otherSum / sum * 100).toFixed(2)});
                flag=false;
              }
            }
          });
          //数据绑定
          this.setData({
            specialStatistics
          })
        }
      }
    })
  },

  /**
   * 专项培训&&应急演练
   */
  getTrainList(projectId,type){
    let that = this;
    wx.request({
      url: app.globalData.reqUrl + '/wechat/projectTrain/list',
      data:{projectId:projectId,trainType:type,deptId:0},
      method:"get",
      success: res => {
        res = res.data;
        if(res.code == 200){
          if(type=="1"){
          //数据绑定
          this.setData({
            emergencyDrillList: res.data,
            emergencyDrillTotal: res.data.length
          })
          }else{
          //数据绑定
          this.setData({
            trainList: res.data,
            trainTotal: res.data.length
          })
          }
        }
      }
    })
  },

  /**
   * 项目人员
   */
  getDeptWorks(projectId) {
    wx.request({
      url: app.globalData.reqUrl + '/wechat/projectDeptWroks/findSumByProjectId',
      data:{projectId:projectId},
      method:"get",
      success: res => {
        res = res.data;
        if(res.code == 200){
          if(res.data){
            let sum=0;
            let deptWorksStatistics=[];
            if(res.data.contractorPersonnel){
              sum+=res.data.contractorPersonnel;
            }
            if(res.data.servicePersonnel){
              sum+=res.data.servicePersonnel;
            }
            if(res.data.supervisorPersonnel){
              sum+=res.data.supervisorPersonnel;
            }
            if(res.data.servicePersonnel){
              deptWorksStatistics.push({ name: "劳务人员", value: res.data.servicePersonnel, prop: (res.data.servicePersonnel / sum * 100).toFixed(2)});
            }else{
              deptWorksStatistics.push({ name: "劳务人员", value: 0, prop: (0 / sum * 100).toFixed(2)});
            }
            if(res.data.supervisorPersonnel){
              deptWorksStatistics.push({ name: "监理人员", value: res.data.supervisorPersonnel, prop: (res.data.supervisorPersonnel / sum * 100).toFixed(2)});
            }else{
              deptWorksStatistics.push({ name: "监理人员", value: 0, prop: (0 / sum * 100).toFixed(2)});
            }
            if(res.data.contractorPersonnel){
              deptWorksStatistics.push({ name: "总包人员", value: res.data.contractorPersonnel, prop: (res.data.contractorPersonnel / sum * 100).toFixed(2)});
            }else{
              deptWorksStatistics.push({ name: "总包人员", value: 0, prop: (0 / sum * 100).toFixed(2)});
            }
            //数据绑定
          this.setData({
            deptWorksStatistics
          })
          }
        }
      }
    })
  },

  /**
   * 获取项目保险数据
   */
  getPojectInsurance(projectId){
    wx.request({
      url: app.globalData.reqUrl + '/wechat/projectInsurance/selectGroupCountByProjectId',
      data:{projectId:projectId},
      method:"get",
      success: res => {
        res = res.data;
        if(res.code == 200 && res.data){
          let sum=0;
          res.data.forEach((it,idx)=>{
            sum+=it.total;
          });
          let insuranceStatistics=[];
          if(res.data.length==2){
            res.data.forEach((it,idx)=>{
              insuranceStatistics.push({ name: it.insuranceTypeName, value: it.total, prop: (it.total / sum * 100).toFixed(2)});
            });
          }else{
            res.data.forEach((it,idx)=>{
              if(it.insuranceType=="1"){
                insuranceStatistics.push({ name: it.insuranceTypeName, value: it.total, prop: (it.total / sum * 100).toFixed(2)});
                insuranceStatistics.push({ name: "安责险", value: 0, prop:0});
              }else{
                insuranceStatistics.push({ name: it.insuranceTypeName, value: it.total, prop: (it.total / sum * 100).toFixed(2)});
                insuranceStatistics.push({ name: "一切险", value: 0, prop: 0});
              }
            });
          }
          //数据绑定
          this.setData({
            insuranceStatistics
          })
        }
      }
    })
  },

  /**
   * 获取安全检查数据
   */
  getSecurityCheck(projectId) {
    wx.request({
      url: app.globalData.reqUrl + '/wechat/projectProblemmodify/selectGroupCountByProjectId',
      data:{projectId:projectId,infoType:"0"},
      method:"get",
      success: res => {
        res = res.data;
        if(res.code == 200){
          //数据绑定
          res.data.forEach((it,idx)=>{
            if(it.problemType=="1"){
              this.setData({
                "securityCheck.routineRectificationRate": (it.comTotal / it.total * 100).toFixed(2),
                "securityCheck.routineProblemTotal": it.comTotal,
                "securityCheck.routineCheckNumber": it.total
              })
            }else{
              this.setData({
                "securityCheck.specialRectificationRate": (it.comTotal / it.total * 100).toFixed(2),
                "securityCheck.specialProblemTotal": it.comTotal,
                "securityCheck.specialCheckNumber": it.total,
              })
            }
          });
        }
      }
    })
  },

  goZXPX() {
    wx.setStorageSync('nav-menu', "aq");
    wx.navigateTo({
      url: '../../pageage/project_train/list/index?trainType=0',
    })
  },

  /**
   * 安全隐患页面
   */
  goAQYH() {
    wx.setStorageSync('nav-menu', "aq");
    wx.redirectTo({
      url: '../../pageage/project_problemmodify/security/list/index',
    })
  },

  goTZRY() {
    wx.setStorageSync('nav-menu', "aq");
    wx.redirectTo({
      url: '../../pageage/project_special/list/index',
    })
  },

  goXMBX() {
    wx.setStorageSync('nav-menu', "aq");
    wx.redirectTo({
      url: '../../pageage/project_insurance/list/index',
    })
  },

  goZGRY() {
    wx.setStorageSync('nav-menu', "aq");
    wx.redirectTo({
      url: '../../pageage/project_deptWorks/list/index',
    })
  },

  goYJYL() {
    wx.setStorageSync('nav-menu', "aq");
    wx.navigateTo({
      url: '../../pageage/project_train/list/index?trainType=1',
    })
  },

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

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

  goMenu:function(event){
    wx.setStorageSync('nav-menu', "aq");
    wx.redirectTo({
      url: event.currentTarget.dataset.url
    })
  },

  /**
   * 项目概况页面
   */
  XMGK: function () {
    wx.redirectTo({
      url: '../xiangmugaikuang/xiangmugaikuang'
    })
  },
  //跳转到质量管理
  ZLGL:function(){
    wx.redirectTo({
      url:'../quality_manage/index'
    }) 
  },
  //跳转到进度管理
  JDGL:function(){
    app.toast("敬请期待！");
    //wx.redirectTo({
    //  url:'../../pageage/Progress-management/index'
    //}) 
  },
  /**
   * 更多功能
   */
  GDGN: function () {
    wx.redirectTo({
      url: '../gengduogongneng/gengduogongneng'
    })
  },
  //退出登录
  TCDL:function(){
    wx.clearStorageSync();
    wx.setStorageSync('isReload', "1")
    wx.redirectTo({
      url: '../login/login',
    })
  },
  //修改密码
  XGMM:function(){
  wx.setStorageSync('nav-menu', "aq");  
    wx.redirectTo({
      url: '../updatePassword/updatePassword'
      }) 
  },

    //查询当前登录人的代办任务
    awaitTask(minRoleId,deptId,loginName,userId) {
      let param = {
        //"businessKey":this.data.projectId,
        "nowRole":minRoleId,
        "nowDept":deptId,
        "nowUserName":loginName,
        "nowUser":userId,
        "activeName":"await"
      }
      var that = this;
      wx.request({
        url: app.globalData.reqUrl + '/wechat/flowTask/myAwaitFlowTaskListCount',
        data:param,
        method: "post",
        success: function (res) {
          res = res.data;
          if(res.code=="200"){
            that.setData({
              todoDb:res.data.todo,
              approveDb:res.data.approve
            }) 
          }
        }
      })
    },
})
