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

    /**
     * 页面的初始数据
     */
    data: {
        loadShow:false,
        radio:'',
        id:'',
        projectId:'',
        retesData:{},
		ImageArr:[],
		//详情标识
		detailFlag:"",
		state:"",
		result:"",
		photoUrl:[]
    },

    onChange(event) {
        this.setData({
            radio: event.detail,
        });
    },

 
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
		let {id,projectId,detailFlag,state,result} = options
        this.setData({
            id,
			projectId,
			detailFlag,
			state,
			result
        })
		this.getSamplingRetesDataInfoById()
		//回显数据
		let flag = ""
		if(detailFlag == 1 && state == '3' && result =='1'){
			flag = '1'
		}else{
			flag = '2'
		}
		this.setData({
			radio:flag
		})
    },
    //跳转页面
    add_btn_qx(){
        wx.navigateTo({
          url: '/pageage/samplingRetesting/index',
        })
    },
    //凭证上传
    onImagesArr(event){
        let ImageArr = event.detail
        this.setData({
            ImageArr
        })
    },


     /**
     * 这里考虑上传图片异步问题，封装为同步
     */

    syncUploadImage(url,uploadFile,name) {
        return new Promise((resolve, reject) => {
            wx.uploadFile({
            url, // 上传的服务器接口地址
            filePath: uploadFile,
            header: {
                "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"      
            },
            name, //上传的所需字段，后端提供
            formData: { user: 'test' },
            success: (res) => {
                // 上传完成操作
                const data = JSON.parse(res.data)
                resolve({
                    data: data
                })
            },
            fail: (err) => {
                //上传失败：修改pedding为reject
                console.log("访问接口失败", err);
                wx.showToast({
                title: "网络出错,上传失败",
                icon: 'none',
                duration: 1000
                });
                reject(err)
            }
            });
        })
          
    
    
    },



    //提交数据
    add_btn_bc(){
        this.setData({
            loadShow:true
        })
        let that = this
        let {radio,ImageArr} = this.data
        let {typeFlag,id} = this.data.retesData
        if(!radio || ImageArr.length <= 0){
            
            this.setData({
                loadShow:false
            })
            app.toast("提交数据不合法")
            return;
        }
        let resultStr = ""
        if((typeFlag == '1' || typeFlag == '3' || typeFlag =='2')){
            if(radio == '1'){
                resultStr  = "已试拉合格"
            }else{
                resultStr  = "已试拉不合格"
            }
            
        }else{
            if(radio == '1'){
                resultStr  = "已试压合格"
            }else{
                resultStr  = "已试压不合格"
            }
        }
        let imageArrs =  []
        ImageArr.forEach(async (item)=>{
            let requestUrl = app.globalData.reqUrl+'/weixin/security/fileUpload'
            let name = "file" 
            let photoUrlObj = await that.syncUploadImage(requestUrl,item,name)  
            imageArrs.push(photoUrlObj.data.url)
            //多张图片上传完成
            if(imageArrs.length >= ImageArr.length){
                wx.request({
                    url: app.globalData.szhUrl + '/system/retestinfo/updateRetestStatus',
                    method:"POST",
                    data:{
                      mainId:id,
                      photoType:2,
                      photoUrl:imageArrs.join(","),
                      resultStr
                    },
                    header:{
                      'content-type': 'application/x-www-form-urlencoded'  
                    },
                    success(res){
                        that.setData({
                            loadShow:false
                        })
                        res = res.data
                        if(res.code == "200"){
                            app.toast("提交成功")
                            setTimeout(()=>{
                                wx.navigateTo({
                                 url: '/pageage/samplingRetesting/index',
                                })
                            },1200)    
                        }    
                    }
                })




            }
        })
            
    },



    getSamplingRetesDataInfoById(){
        let that = this
        let {id,projectId} = this.data
        wx.request({
            url: app.globalData.szhUrl + '/system/retestinfo/getRetestInfoById',
            method:"Get",
            data:{
              projectId,
              id
            },
            header:{
              'content-type': 'application/x-www-form-urlencoded'  
            },
            success(res){
                res = res.data
				res.data.typeFlagName = that.convertRetesingName(res.data)
                that.setData({
					retesData:res.data,
					photoUrl:res.data.photoUrl == null ? [] : res.data.photoUrl.split(',')
					
                })
            }
        })
    },

   /**
     * 置换名称
     */
    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;
	},
	
	showImg(e){
		var that=this;
		wx.previewImage({
			urls: that.data.photoUrl,
			current: that.data.photoUrl[e.currentTarget.dataset.index]
		})
	},

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

    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    }
})