// pages/Quality-Assurance/index.js
const app = getApp()

Page({
    /**
     * 页面的初始数据
     */
    data: {
        active:2,
        initData: {},
        projectData:{},
        projectId: '',
        projectName: '',
        loginName:"",
        nickName:"",
        deptId:"",
        deptName:"",
        measureStatistics:[{ name: "其它", value: 0, prop: 0}],
        securityCheck: {
          routineRectificationRate: 0, //常规整改率
          routineProblemTotal: 0,//常规问题总数
          routineCheckNumber: 0,//常规检查次数
          specialRectificationRate: 0, //专项整改率
          specialProblemTotal: 0,//专项问题总数
          specialCheckNumber: 0,//专项检查次数
        },
        checkDetectionData:{
          type1Value:0,
          type1PassRate:100,
          type2Value:0,
          type2PassRate:100,
          type3Value:0,
          type3PassRate:100,
          type4Value:0,
          type4PassRate:100,
        },
        //举牌验收
        checkValue:0,
        checkPassRate:100,
        materialSealStatistics:[{ name: "其它", value: 0, prop: 0}],
        todoDb:0,
        approveDb:0,
        qyfsDb:0,
        scslDb:0,
        jpysDb:0,
        clfyDb:0,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let that = this;
        //获取缓存数据
        wx.getStorage({
          key: 'userinfo',
          success:res=>{
            that.setData({
              loginName:res.data.loginName,
              nickName:res.data.nickName,
              projectId:app.globalData.projectId,
              projectNameArgs:app.globalData.projectName,
              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);
          }
        })
    },

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

  //初始化页面  
  initPage(projectId){
    this.getMeasureData(projectId);
    this.getQualityCheck(projectId);
    this.getCheckDetectionData(projectId);
    this.getMaterialSealData(projectId);
    this.getCheckingData(projectId);
  },

  /**
   * 实测实量数据
   */
  getMeasureData(projectId) {
    wx.request({
      url: app.globalData.reqUrl + '/wechat/projectMeasure/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 measureStatistics=[];
          res.data.forEach((it,idx)=>{
            measureStatistics.push({ name: it.measureTypeName, value: it.total, prop: (it.total / sum * 100).toFixed(2)});
          });
          //数据绑定
          this.setData({
            measureStatistics
          })
        }
      }
    })
  },

   /**
   * 获取质量检查数据
   */
  getQualityCheck(projectId) {
    wx.request({
      url: app.globalData.reqUrl + '/wechat/projectProblemmodify/selectGroupCountByProjectId',
      data:{projectId:projectId,infoType:"1"},
      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,
              })
            }
          });
        }
      }
    })
  },

  //取样复试数据
  getCheckDetectionData(projectId){
    wx.request({
      url: app.globalData.reqUrl + '/wechat/projectDetection/selectGroupCountByProjectId',
      data:{projectId:projectId},
      method:"get",
      success: res => {
        res = res.data;
        if(res.code == 200 && res.data){
          //数据绑定
          res.data.forEach((it,idx)=>{
            if(it.checkType=="1"){
              this.setData({
                "checkDetectionData.type1Value": it.total,
                "checkDetectionData.type1PassRate": (it.passTotal / it.checkTotal * 100).toFixed(2)
              });
            }else if(it.checkType=="2"){
              this.setData({
                "checkDetectionData.type2Value": it.total,
                "checkDetectionData.type2PassRate": (it.passTotal / it.checkTotal * 100).toFixed(2)
              });
            }else if(it.checkType=="3"){
              this.setData({
                "checkDetectionData.type3Value": it.total,
                "checkDetectionData.type3PassRate": (it.passTotal / it.checkTotal * 100).toFixed(2)
              });
            }else{
              this.setData({
                "checkDetectionData.type4Value": it.total,
                "checkDetectionData.type4PassRate": (it.passTotal / it.checkTotal * 100).toFixed(2)
              });
            }
          });
        }
      }
    })
  },

  /**
   * 材料封样数据
   */
  getMaterialSealData(projectId) {
    wx.request({
      url: app.globalData.reqUrl + '/wechat/projectMaterialSeal/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 materialSealStatistics=[];
          res.data.forEach((it,idx)=>{
            if(flag){
              let typeName = it.type;
              if(typeName.length>6){
                //字符串截取
                typeName = typeName.substring(typeName.length-7,typeName.length);
              }
              materialSealStatistics.push({ name: typeName, value: it.total, prop: (it.total / sum * 100).toFixed(2)});
              if(idx>3 && res.data.length>6){
                //防止变形，这里统计剩余所有
                materialSealStatistics.push({ name: "其它部位", value: otherSum, prop: (otherSum / sum * 100).toFixed(2)});
                flag=false;
              }
            }
          });
          //数据绑定
          this.setData({
            materialSealStatistics
          })
        }
      }
    })
  },

  /**
   * 举牌验收数据
   */
  getCheckingData(projectId) {
    wx.request({
      url: app.globalData.reqUrl + '/wechat/projectchecking/findStatisticsByProjectId',
      data:{projectId:projectId},
      method:"get",
      success: res => {
        res = res.data;
        if(res.code == 200 && res.data){
          res.data.forEach((it,idx)=>{
            //数据绑定
            this.setData({
              checkValue:it.total,
              checkPassRate:(it.pass / it.total * 100).toFixed(2),
            })
          });
        }
      }
    })
  },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

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

    /**
     * 实测实量页面
     */
    goSCSL: function () {
      wx.setStorageSync('nav-menu', "zl");
      wx.redirectTo({
        url: '../../pageage/project_measure/list/index'
      })
    },

    /**
     * 质量隐患排查
     */
    goYHPC: function () {
      wx.setStorageSync('nav-menu', "zl");
      wx.redirectTo({
        url: '../../pageage/project_problemmodify/quality/list/index'
      })
    },

    /**
     * 取样复试
     */
    goQYFS: function () {
      wx.setStorageSync('nav-menu', "zl");
      wx.redirectTo({
        url: '../../pageage/project_checkDetection/list/index'
      })
    },

    /**
     * 材料封样
     */
    goCLFY: function () {
      wx.setStorageSync('nav-menu', "zl");
      wx.redirectTo({
        url: '../../pageage/project_materialSeal/list/index'
      })
    },

    /**
     * 举牌验收
     */
    goJPYS: function () {
      wx.setStorageSync('nav-menu', "zl");
      wx.redirectTo({
        url: '../../pageage/project_checking/list/index'
      })
    },

   // 底部导航
   onChange(event) {
    // event.detail 的值为当前选中项的索引
    this.setData({ active: event.detail });
    },
  
    /**
     * 项目概况页面
     */
    XMGK: function () {
      wx.redirectTo({
        url: '../xiangmugaikuang/xiangmugaikuang'
      })
    },
    //跳转到安全管理
    AQGL:function(){
      wx.redirectTo({
        url:'../safety_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', "zl");  
      wx.redirectTo({
        url: '../updatePassword/updatePassword'
        }) 
    },
    
    showPopup() {
      this.setData({ show: true });
    },
    onClosePopup() {
      this.setData({ show: false });
    },
    
      //查询当前登录人的代办任务
  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,
            qyfsDb:res.data.approveQYFS,
            scslDb:res.data.approveSCSL,
            jpysDb:res.data.approveJPYS,
            clfyDb:res.data.approveCLFY
          })
          console.log(that.data) 
        }
      }
    })
  },
})