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

    /**
     * 页面的初始数据
     */
    data: {
        loginName:"",
        userName:"",
        projectId:"",
        projectData:{},
        projectNameArgs:"",
        initData: {},

        stateNav:1,
        statePosit:1,

        listData:[],
        listDataed:[],
        //常规检查总数
        conventionNumber:0,
        //转向检查总数
        specialNumber:0,
        //待整改总数
        unRectifyNumber:0,
        //已整改整数
        rectifyNumber:0
    },

    // 常规检查   专项检查  选择
    onStateSelect(e){
        let state = e.currentTarget.dataset.state;
        this.setData({
            stateNav:state,
        });
        this.getProjectCorrespondence(app.globalData.projectId)
    },

    //待整改 已整改 选择
    onStatePosit(e){
        let state = e.currentTarget.dataset.state;
        this.setData({
            statePosit:state,
        });
    },

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

    skipCheckView(){
        let {szh} = this.data.projectData
        wx.navigateTo({
            url: `../addSafetyInspect/index?projectId=${szh}`,
        })
    },

    getSaftCheckDetialsInfo(e){
        let {id,projectId} = e.currentTarget.dataset.set
        wx.navigateTo({
          url: `../problemRectificationGR/index?id=${id}&projectId=${projectId}`,
        })
    },



    /**
     * 生命周期函数--监听页面加载
     */
    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)
        }
      })
    },


      /**
   * 项目对应关系
   */
  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.getSaftCheckDataInfo()
      }
    })
    },

    /**
     *  获取检查数据
     */
    getSaftCheckDataInfo(){
        let that = this
        let {szh} = that.data.projectData
        
        wx.request({
            url: app.globalData.szhUrl+'/system/saftcheckissue/selectSaftCheckAllIssueDataList',
            method:"GET",
            data:{
                projectId:szh,
            },
            header:{
                'content-type': 'application/x-www-form-urlencoded'  
            },
            success(res){
            
                res = res.data
                if(res.code == 200 && res.data.length > 0){
             
                    let listDataTemp = []
                    let listDataTempEd = []
                    res.data.forEach((item,index)=>{
                        let questions_number = item.saftCheckIssueItemList.length
                        let rectification_number   = item.saftCheckIssueItemList.filter(item=>item.status == '1').length   
                        if(item.status == '0'){
                           let state = 2
                           for(let i = 0;i < item.saftCheckIssueItemList.length;i++){
                                if(item.saftCheckIssueItemList[i].checkLoginName == that.data.loginName){
                                    state = 1
                                    if(item.saftCheckIssueItemList[i].status =='0'){
                                        state = 0
                                        break;
                                    }    
                                }
                           }
                            listDataTemp.push({
                                id:item.id,
                                state,
                                status:item.status,
                                projectId:item.projectId,
                                checkType:item.checkType,
                                title:item.checkTitleName+' '+item.checkTime,
                                photo:item.saftCheckIssueItemList[0].beforeCheckUrl.split(",")[0],
                                questions_number,
                                rectification_number,
                                rectification_prop: (((rectification_number / questions_number) * 100).toFixed(1) ),
                                checkpoint:item.checkSiteName,
                                my_receive_rectification:item.saftCheckIssueItemList.filter(item=>item.checkLoginName == that.data.loginName).length,
                                my_already_rectification:item.saftCheckIssueItemList.filter(item=>item.checkLoginName == that.data.loginName && item.status == '1').length,
                            }) 
                            
                        }else{
                            //整改时间，取最后完成时间
                            let rectify_time   = item.saftCheckIssueItemList.filter(item=>item.status == '1').sort((prev,next)=>new Date(next.finishTime).getTime() - new Date(prev.finishTime).getTime())[0].finishTime 
                            listDataTempEd.push({
                                id:item.id,
                                status:item.status,
                                checkType:item.checkType,
                                title:item.checkTitleName+' '+item.checkTime,
                                rectify_front_img:item.saftCheckIssueItemList[0].beforeCheckUrl.split(",")[0],
                                rectify_after_img:item.saftCheckIssueItemList[0].afterCheckUrl.split(",")[0],
                                questions_number,
                                rectification_number,
                                rectification_prop:(((rectification_number / questions_number) * 100).toFixed(1) ),
                                rectify_time,
            
                            }) 
                        }

                    })
                    that.setData({
                        listData:listDataTemp.filter(item=>item.checkType == that.data.stateNav),
                        listDataed:listDataTempEd.filter(item=>item.checkType == that.data.stateNav),
                         //常规检查总数
                        conventionNumber:(listDataTemp.filter(item=>item.checkType == '1').length) + (listDataTempEd.filter(item=>item.checkType == '1').length),
                        //转向检查总数
                        specialNumber:(listDataTemp.filter(item=>item.checkType == '2').length) + (listDataTempEd.filter(item=>item.checkType == '2').length),
                        //待整改总数
                        unRectifyNumber:listDataTemp.filter(item=>item.checkType == that.data.stateNav).length,
                        //已整改整数
                        rectifyNumber:listDataTempEd.filter(item=>item.checkType == that.data.stateNav).length
                    })

                   console.log(that.data.listData) 
                   console.log(that.data.listDataed) 
                }
            }
        })
    },
    

    

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

    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    }
})