// pageage/safetyManagement/securityCheckGR/index.js
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        deptId:"",
        loginName:"",
        userName:"",
        projectId:"",
        projectData:{},
        projectNameArgs:"",
        initData: {},
        show:false,
        listData:[],
        activeState:"zg",
        dzgCount:0,
        dfjCount:0,
        yclCount:0,
        zgcsCOunt:0,
        request:app.globalData.reqUrl,
        pageNum:1,
        pageSize:10,
        lastDataSize:10,
        minRoleId:"",
        list:[]
    },

    skipAdd(){
        wx.redirectTo({
            url: `../add/index?projectId=${this.data.initData.id}&projectName=`+this.data.initData.text,
        })
    },

    getInfo(e){
        let {id,checkState,lordSentUser,recheckSendUser} = e.currentTarget.dataset.set
        if((checkState==0 || checkState==3) && lordSentUser==this.data.loginName){
          //整改页面（状态时待整改&&整改人是当前登录人）
          wx.redirectTo({
            url: `../modify/index?id=${id}`,
          })
        }else if(checkState==1 && recheckSendUser==this.data.loginName){
          //复检页面 (状态时待复检&&复检人是当前登录人)
          wx.redirectTo({
            url: `../check/index?id=${id}`,
          })
        }else{
          wx.redirectTo({
            url: `../info/index?id=${id}`,
          })
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
     //获取缓存数据
      wx.getStorage({
        key: 'userinfo',
        success:res=>{
          this.setData({
            deptId:res.data.deptId,
            loginName:res.data.loginName,
            userName:res.data.userName,
            minRoleId:res.data.minRoleId,
            projectId:app.globalData.projectId,
            projectNameArgs:app.globalData.projectName,
            initData:{text:app.globalData.projectName,id:app.globalData.projectId},
            pageNum:1,
            pageSize:10,
            lastDataSize:10,
            listData:[]
          })  
          this.getListData();
        }
      })
    },

  /**
    * 查询项目举牌验收数据
    */
   getListData() {
    //进入这里说明数据加载完毕
    if(this.data.lastDataSize<this.data.pageSize){
      //app.toast("已经到底了，暂无可继续加载数据!")
      return;
    }
    let nowRole = "99";
    //判断角色，
    if(this.data.minRoleId==2||this.data.minRoleId==3||this.data.minRoleId==4){
      nowRole="-1";
    }
    let param = {
        "infoType":"0",//1质量类，0安全类
        "projectId": this.data.projectId,
        "nowRole":nowRole,//默认传普通角色，真实角色查看的数据量太大，这里只查看自己相关的数据
        "nowUser":this.data.loginName,
        "activeName":this.data.activeState
    }
    var that = this;
    that.queryCount(param);
    param.pageNum=that.data.pageNum;
    param.pageSize=that.data.pageSize;
    wx.request({
      url: app.globalData.reqUrl + '/wechat/projectProblemmodify/list',
      data:param,
      method: "get",
      success: function (res) {
        res = res.data;
        if(res.code=="200"){
          //这里处理this.data.lastDataSize=this.data.pageSize
          if(that.data.list.length>0 && res.data.length>0 && that.data.list[0].id==res.data[0].id){
            that.setData({
              lastDataSize:0,
            })
          }else{
            that.setData({
              pageNum:that.data.pageNum+1,
              lastDataSize:res.data.length,
              list:res.data,
              listData:that.data.listData.concat(res.data)
            })
          }
        }
      }
    });
    },

   queryCount(param){
      //查询统计数量
    let that = this;  
    wx.request({
      url: app.globalData.reqUrl + '/wechat/projectProblemmodify/queryCount',
      data: param,
      method: "get",
      success: function (res) {
        let sum1=0;
        let sumfj=0;
        if(res.data.data.check0){
          sum1+=res.data.data.check0;
        }
        if(res.data.data.check1){
          sumfj+=res.data.data.check1;
        }
        if(res.data.data.check3){
          sum1+=res.data.data.check3;
        }
        let sum4=0;
        let sum99=0;
        if(res.data.data.check4){
          sum4+=res.data.data.check4;
        }
        if(res.data.data.check99){
          sum99+=res.data.data.check99;
        }
        that.setData({
          dzgCount:sum1,
          dfjCount:sumfj,
          yclCount:sum4,
          zgcsCount:sum99
        });
      }
    });
   }, 

     /**
   * 标签切换
   */
  trainingTypeJump(e){
    let index = e.currentTarget.dataset.index;
    let nav = "";
    if(index == 1){
      nav = 'zg';
    }if(index == 2){
      nav = 'fj';
    }else if(index == 3){
      nav = 'ycl';
    }else if(index == 4){
      nav = 'zgcs';
    }
    this.setData({
      activeState:nav,
      pageNum:1,
      pageSize:10,
      lastDataSize:10,
      listData:[],
      list:[]
    });
    this.getListData();
  },

  returnToPage: function () {
    if(wx.getStorageSync('nav-menu')=="gd"){
      wx.redirectTo({
        url: '../../../../pages/gengduogongneng/gengduogongneng',
      })
    }else{
      wx.redirectTo({
        url: '../../../../pages/safety_manage/index',
      })
    }
  },

  skipAddDarft(){
    wx.setStorageSync('nav-menu', "list");
    wx.redirectTo({
      url: `../draft/index`,
    })
  },

    //项目切换 返回值
    onProjectSelect(e){
      let projectId = e.detail.id;
      let projectName = e.detail.text;
      app.globalData.projectId = projectId;
      app.globalData.projectName = projectName;  
      this.onLoad();
    },

    onScrollToLower(){
      console.log("滚动条到底了，开始加载新数据");
      this.getListData();
    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    }
})