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

    /**
     * 页面的初始数据
     */
    data: {
        id:"",
        projectId:"",
        loginName:"",
        userName:"",
        //检查类型名称
        checkTypeName:"",
        //检查部位名称
        checkSiteName:"",
        //检查时间
        checkTime:"",
        //检查限制时间
        checkImposeTime:"",
        rectifySituation:"",
        
         // state    0  待我整改   1  我已整改   2 无需整改 
        problemData:[],    
        loadShow:false

    },

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


    /**
     * 获取安全检查详情信息
     * 
     */
    getSafetCheckDetailsInfo(){
        let {projectId,id} = this.data
        let that = this
        wx.request({
          url: app.globalData.szhUrl+'/system/saftcheckissue/getIssureDetails',
          method:"POST",
          data:{
              projectId,
              id
          },
          header:{
            'content-type': 'application/x-www-form-urlencoded'  
          },
          success(res){
            res = res.data
            let {id,projectId,checkImposeTime,checkSiteName,checkTime,checkTypeName,result} = res.data.saftCheckIssue
            if(res.code == 200){
               let  problemDataTemp = []
          
               result.forEach((item,index)=>{
                let  state = '2'
                let  rectify_front = []
                let  rectify_photo = []
                if(item.checkLoginName == that.data.loginName){
                    state = '0'
                    if(item.status == '1'){
                        state = '1'        
                    }
                }
                item.beforeCheckUrl.split(",").forEach(item=>{
                    rectify_front.push({url:item})
                })  
                if(item.finishList.length > 0){
                    item.finishList[0].afterCheckUrl.split(",").forEach(item=>{
                        rectify_photo.push({url:item})
                    }) 
                }
               
                problemDataTemp.push({    
                    title:'问题'+(index+1),
                    overall_state:false,
                    state,
                    existing_problems:item.issueContext,
                    rectify_person:item.checkUserName,
                    rectifyuserId:item.checkUserId,
                    rectify_front,
                    rectifyloginName:item.checkLoginName,
                    problem_btn: state == '0'?false:true,
                    rectify_situation:item.finishList.length > 0 ? item.finishList[0].finishContext:"",
                    rectify_time:item.finishList.length > 0 ? item.finishList[0].finishTime:"",
                    rectify_photo,
                    projectId,
                    issueId:id,
                    issueItemId:item.id
                })
               
               })   
               that.setData({
                    //检查类型名称
                    checkTypeName,
                    //检查部位名称
                    checkSiteName,
                    //检查时间
                    checkTime,
                    //检查限制时间
                    checkImposeTime,
                    //问题列表
                    problemData:problemDataTemp
               })  
                that.setData({
                    loadShow:false
                })
            }
          }
        })
    },

    //上传图片返回值
    onImagesArr(e){
        var index = e.currentTarget.dataset.index
        var data = this.data.problemData
        data[index].rectify_photo = e.detail
        this.setData({
            problemData:data
        })
    },

    //问题展开
    onExpandDetails(e){
        var index = e.currentTarget.dataset.index
        var data = this.data.problemData

        if(data[index].overall_state == false){
            data[index].overall_state = true
        }else{
            data[index].overall_state = false
        }
        
        this.setData({
            problemData:data
        })
    },

    //list 问题整改按钮
    onProblemRectification(e){
        var index = e.currentTarget.dataset.index
        var data = this.data.problemData
        data[index].problem_btn = true
        this.setData({
            problemData:data
        })
    },

    //list 整改情况 填写
    onRectificationSituation(e){
        var index = e.currentTarget.dataset.index
        var data = this.data.problemData
        data[index].rectify_situation = e.detail.value
        this.setData({
            problemData:data
        })
    },

    //list 取消按钮
    onCancellation(e){
        var index = e.currentTarget.dataset.index
        var data = this.data.problemData
        data[index].problem_btn = false
        this.setData({
            problemData:data
        })
    },
    //list 确认按钮
    onConfirm(e){
        let that = this
        this.setData({
            loadShow:true
        })
        var index = e.currentTarget.dataset.index
        var data = this.data.problemData
        let afterCheckUrl = []
        let {issueId,issueItemId,projectId,rectifyuserId,rectifyloginName,rectify_situation,rectify_photo} = data[index]
        if(rectify_photo.length <= 0){
            app.toast("请上传整改图片")
            this.setData({
                loadShow:false
            })
            return;
        }
        if(rectify_situation.length <= 0){
            app.toast("整改情况不能为空")
            this.setData({
                loadShow:false
            })
            return;
        }
        rectify_photo.forEach(item=>{
            wx.uploadFile({
                //图片上传地址
                url: app.globalData.reqUrl+'/weixin/security/fileUpload',
                filePath: item,
                name: 'file',
                header: {
                  "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
                },
                formData: { user: 'test' },
                success:resTemp => {
                    let dataFile = JSON.parse(resTemp.data);
                    afterCheckUrl.push(dataFile.url) 
                    if(afterCheckUrl.length >= rectify_photo.length){
                        //执行保存方法
                        wx.request({
                            url: app.globalData.szhUrl+'/system/saftissuefinish/add',
                            method:"POST",
                            data:{
                                projectId,
                                issueId,
                                issueItemId,
                                finishContext:rectify_situation,
                                afterCheckUrl:afterCheckUrl.join(","),
                                finishUserId:rectifyuserId,
                                finishCheckLoginName:rectifyloginName
                            },
                            header:{
                              'content-type': 'application/x-www-form-urlencoded'  
                            },
                            success(res){
                                app.toast("整改成功")
                                setTimeout(()=>{
                                    wx.navigateTo({
                                      url: '../securityCheckGR/index',
                                    },2000)
                                })
                                
                            }
                        })


                    }
                }
            })
        })    
        
    },


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

    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    }
})