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

Page({
    /**
     * 页面的初始数据
     */
    data: {
        initData: {},
        projectData:{},
        projectId: '',
        projectName: '',
        

        gj1:0,
        gj1per:0,
        gj1_Y:0,
        gj1_N:0,
        
        gj2:0,
        gj2per:0,
        gj2_Y:0,
        gj2_N:0,

        gj3:0,
        gj3per:0,
        gj3_Y:0,
        gj3_N:0,
        
        gj4:0,
        gj4per:0,
        gj4_Y:0,
        gj4_N:0,

        gj5:0,
        gj5per:0,
        gj5_Y:0,
        gj5_N:0,

        jp1:0,
        jp1_Y:0,
        jp1_N:0,
        jp1per:0,

        bys1:0,
        bys2:0,

        tempOut:0,
        tempIn:0,
        strong:0,
        cooked:0,

      
        biaoyangshiList:[{text:'010840009F',id:0}],
        hntList:[],
        hntDevceNO:''

        
    },

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

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    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()
  },




  /**
    * 项目对应关系
    */
   getProjectCorrespondence(projectId) {
    var that = this;
    wx.request({
      url: app.globalData.reqUrl + '/weixin/training/getProjectCorrespondence',
      data: {
        "projectId": projectId,
      },
      method: "get",
      success: function (res) {
        that.setData({
          projectData: res.data
        })
        //获取取样复试数据
        that.getqyfsData();
        //获取举牌验收数据信息
        that.getjpysData();
        //标养室数据
        that.getbysData();
        //混凝设备列表获取
        that.gethntDevice();
        //混凝土数据获取
        that.gethntData();

      }
    })
    },



    /**
     * 获取取样复试数据
     */
    getqyfsData(){
      let that = this;
      let {szh} = that.data.projectData;
        wx.request({
          url: app.globalData.szhUrl+'/api/building/retestList?project_id='+szh+'&dept_id=131',
          method:"POST",
          data:{},
          success(res){
            let data =  res.data.data            
            let gj1=0,gj1_Y=0,gj1_N=0,gj2=0,gj2_Y=0,gj2_N=0,gj3=0,gj3_Y=0,gj3_N=0,gj4=0,gj4_Y=0,gj4_N=0,gj5=0,gj5_Y=0,gj5_N=0;            
            for(let d of data){
              switch (d.type_flag) {
                  case 1:
                    //钢筋原材料取样复试
                      gj1++
                      if(d.result_str.indexOf('不合格') == -1 && (d.result_str.indexOf('合格') != -1)){
                        gj1_Y++
                      }
                      if(d.result_str.indexOf('不合格') == -1 && (d.result_str.indexOf('合格') == -1)){
                        gj1_N++
                      }
                      break
                  case 2:
                    //钢筋试拉件取样复 -- 机械连接
                      gj2++
                      if(d.result_str.indexOf('不合格') == -1 && (d.result_str.indexOf('合格') != -1)){
                        gj2_Y++
                      }
                      if(d.result_str.indexOf('不合格') == -1 && (d.result_str.indexOf('合格') == -1)){
                        gj2_N++
                      }
                      break
                  case 3:
                    //钢筋试拉件取样复 -- 钢筋焊接
                      gj3++
                      if(d.result_str.indexOf('不合格') == -1 && (d.result_str.indexOf('合格') != -1)){
                          gj3_Y++
                      }
                      if(d.result_str.indexOf('不合格') == -1 && (d.result_str.indexOf('合格') == -1)){
                          gj3_N++
                      }
                      break
                  case 4:
                    // 混凝土试件取样复试 -- 楼层标养                
                      gj4++
                      if(d.result_str.indexOf('已送样') != -1){
                          
                      }
                      if(d.result_str){
                          if(d.result_str.indexOf('不合格') == -1 && (d.result_str.indexOf('合格') != -1)){
                            gj4_Y++
                          }
                          if(d.result_str.indexOf('标养中') != -1){
                            gj4_N++
                          }
                      }
                      break;
                  case 5:
                    // 混凝土试件取样复试 -- 楼桩标养   
                      gj5++
                      if(d.result_str.indexOf('已送样') != -1){
                          
                      }
                      if(d.result_str.indexOf('不合格') == -1 && (d.result_str.indexOf('合格') != -1)){
                         gj5_Y++
                      }
                      if(d.result_str.indexOf('标养中') != -1){
                          gj5_N++
                      }
                      break;
              }
          }
            that.setData({
              gj1:gj1,
              gj1per:(gj1_Y/gj1*100).toFixed(0),
              gj1_Y:gj1_Y,
              gj1_N:gj1_N,
              
              gj2:gj2,
              gj2per:(gj2_Y/gj2*100).toFixed(0),
              gj2_Y:gj2_Y,
              gj2_N:gj2_N,

              gj3:gj3,
              gj3per:(gj3_Y/gj3*100).toFixed(0),
              gj3_Y:gj3_Y,
              gj3_N:gj3_N,
              
              gj4:gj4,
              gj4per:(gj4_Y/gj4*100).toFixed(0),
              gj4_Y:gj4_Y,
              gj4_N:gj4_N,

              gj5:gj5,
              gj5per:(gj5_Y/gj5*100).toFixed(0),
              gj5_Y:gj5_Y,
              gj5_N:gj5_N,
              
            })
          }
        })
    },

    /**
     * 
     * 获取举牌验收的数据
     */
    getjpysData(){
      let that = this;
      wx.request({
        url: app.globalData.szhUrl+'/mkl/api/getScreenCheckAccept',
        method:"POST",
        data:{
          projectId:that.data.projectData.szh
        },
        success(res){
          let data =  res.data     
          let jp1 = 0;
          let jp1_Y = 0;
          let jp1_N = 0;
          let jp1per = 0;

          for(let d of data){
            jp1++;
            if(d.check_result == 0){
              jp1_Y++;
            } else {
              jp1_N++;
            }
          }

          that.setData({
            jp1: jp1,
            jp1per:(jp1==0?0:(jp1_Y/jp1*100)).toFixed(0),
            jp1_Y:jp1_Y,
            jp1_N:jp1_N,
          })

      }
    })

    },

    /**
     * 
     * 标养室检测
     * 
     */
    getbysData(){
      let that = this;
      let {szh} = that.data.projectData;
      wx.request({
        url: app.globalData.szhUrl+'/api/getProcessControlCount',
        method:"POST",
        data:{
          dept_id:'131',
          project_id:szh,
        },
        success(res){
          let data =  res.data.data.markingRoomCount
          that.setData({
            bys1:data.left_data,
            bys2:data.right_data,
          })


      }
    })
    },



    /**
     * 混凝土设备列表
     */
    gethntDevice(){
      let that = this;
      wx.request({
        url: app.globalData.szhUrl+'/api/iot/power/gainHntDeviceList',
        method:"POST",
        header:{
          'content-type': 'application/x-www-form-urlencoded'  
        },
        data:{
          projectId:'1649949529732075522',
        },
        success(res){
          var tempArray =[];
            for( let d of res.data.data){
              tempArray.push({text:d.deviceId,id:d.deviceId})
            }
            that.setData({
              hntList:tempArray,
              hntDevceNO:tempArray[0].text
            })
            that.gethntData();
        }
      })     
    },

    /**
     * 混凝土强度数据
     */
    gethntData(){
      let that = this;
      wx.request({
        url: app.globalData.szhUrl+'/api/iot/power/gainHntLatestList',
        method:"POST",
        header:{
          'content-type': 'application/x-www-form-urlencoded'  
        },
        data:{
          projectId:'1649949529732075522',
          deviceNo: that.data.hntDevceNO
        },
        success(res){
          let data = res.data.data
          if(data != undefined){
            that.setData({
              tempOut:data.tempOut,
              tempIn:data.tempIn,
              strong:data.strong,
              cooked:data.cooked
            })
          }
        }
      
    })
  },

  changeHntDevice(e){
    this.setData({
      hntDevceNO:e.detail.text
    })
   this.gethntData();
   
  },

  tobysDetail(){
    wx.navigateTo({
      url: '../Standard-maintenance-room-monitoring/index',
    })
  },
  tohntDetial(){
    wx.navigateTo({
      url: '../hnt-strong/index',
    })
  },
     /**
   * 返回到更多功能页面
   */
  goGCLB:function(){
    wx.redirectTo({
      url: '../../pages/gengduogongneng/gengduogongneng'
    })
  },
    
  showPopup() {
    this.setData({ show: true });
  },
  onClose() {
    this.setData({ show: false });
  },
  
})