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

    /**
     * 页面的初始数据
     */
    data: {
        statePosit:1,

        //state  1 待送检   2  检验中   3  已完成
        //result 0 未完成 1 合格   2 不合格
        listData:[
           
        ],
        //待送检数组
        prepareDataList:[],
        //检验中数组
        underwayDataList:[],
        //已完成数组
        finishDataList:[],
        //项目id
        projectId:"",
        //项目名称
        projectName:"",
        initData:{},
        userName:"",
        loginName:"",
        //加载状态
		loadShow:false,
		
    },


    //待送检 检验中 已完成  选择
    onStatePosit(e){
        let {prepareDataList,underwayDataList,finishDataList} = this.data
        let state = e.currentTarget.dataset.state;
        this.setData({
            statePosit:state,
            listData: state == '1' ? prepareDataList : state == '2' ? underwayDataList : finishDataList
        });
    },

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

    //去送检
    onGoInspection(e){
        let {id} = e.currentTarget.dataset.set
        let {szh} = this.data.projectData
        wx.navigateTo({
            url: `../../pageage/samplingRetestingDeliver/index?id=${id}&projectId=${szh}`
        })
    },

    //凭证上传
    onUpload(e){
        let {id} = e.currentTarget.dataset.set
        let {szh} = this.data.projectData
        wx.navigateTo({
            url: `../../pageage/samplingRetestingUpload/index?id=${id}&projectId=${szh}&detailFlag=0`
        })
    },

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

    getSamplingRetesDataInfo(){
        let that = this
        that.setData({
          loadShow:true
        })
        let {szh} = that.data.projectData
        wx.request({
          url: app.globalData.szhUrl + '/system/retestinfo/list',
          method:"POST",
          data:{
            projectId:szh,
            pageSize:99999
          },
          header:{
            'content-type': 'application/x-www-form-urlencoded'  
          },
          success(res){
            that.setData({
              loadShow:false
            })
            res = res.data
            let  prepareDataList = []
            let  underwayDataList = []
            let  finishDataList = []
            res.rows.forEach(item=>{
                if(item.resultStr.indexOf('待检') >= 0 ){
                    prepareDataList.push({
                        id:item.id,
                        title:that.convertRetesingName(item),
                        material_name:item.materialName == null ? "":item.materialName,
                        received_quantity:item.approach_number == null ? 0:item.approach_number,
                        received_time:item.approach_time == null ? "":item.approach_time,
                        state:1,
                        result:1
                    })
                }else if(item.resultStr.indexOf('已送样') >= 0 || item.resultStr.indexOf('标养中') >= 0){
                    underwayDataList.push({
                        id:item.id,
                        title:that.convertRetesingName(item),
                        material_name:item.materialName == null ? "":item.materialName,
                        received_quantity:item.approach_number == null ? 0:item.approach_number,
                        received_time:item.approach_time == null ? "":item.approach_time,
                        state:2,
                        result:1
                    })
                }else{
                    finishDataList.push({
                        id:item.id,
                        title:that.convertRetesingName(item),
                        material_name:item.materialName == null ? "":item.materialName,
                        received_quantity:item.approach_number == null ? 0:item.approach_number,
                        received_time:item.approach_time == null ? "":item.approach_time,
                        state:3,
                        result:1
                    })
                }
            })
            that.setData({
                 //待送检数
                 prepareDataList,
                //检验中数
                underwayDataList,
                //已完成数
                finishDataList,
                //数据
                listData:prepareDataList 
            })
          }
        })
    },

    /**
     * 置换名称
     */
    convertRetesingName(item){
        let typeName = ""
        let typeFlag = item.typeFlag
        switch(typeFlag){
            case 1:
                typeName = "钢筋原材料取样复试"
                break;
            case 2:
                typeName = "钢筋试拉件取样复试"
                break;
            case 3:
                typeName = "钢筋试拉件取样复试"
                break;
            case 4:
                typeName = "混凝土试件取样复试"
                break;
            case 5:
                typeName = "混凝土试件取样复试"
                break;
                
        }
        return typeName;
	},
	//跳转添加凭证管理页面
	skipCheckView(){
		let {szh} = this.data.projectData
		wx.navigateTo({
		  url: `../samplingAddRetestingDeliver/index?szhId=${szh}`,
		})
	},

	//跳转详情凭证页面
	skipDetialsFinishView(e){
		let statePosit = this.data.statePosit
		if(statePosit == 3){
			let {id} = e.currentTarget.dataset.set
			let {szh} = this.data.projectData
			wx.navigateTo({
				url: `../samplingRetestingDetail/index?id=${id}&projectId=${szh}`
			})
		}
	},




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

    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    }
})