<template>
	<view class="page">
		<view class="example-b">
			<view class="item">
				<view class="itemname fz36">报工单号</view>
				<view class="itemtext">
					<input class="iteminput" disabled v-model="code" />
				</view>
			</view>
			<view class="item">
				<view class="itemname">工序名</view>
				<view class="itemtext">
					<input class="iteminput" disabled  v-model="processName"/>
				</view>
			</view>
			<view class="item">
				<view class="itemname">检验方案</view>
				<view class="itemtext">
					<input class="iteminput" disabled v-model="checkPlanName" />
				</view>
			</view>
			<view class="item">
				<view class="itemname">产品</view>
				<view class="itemtext">
					<input class="iteminput" disabled v-model="materielName" />
				</view>
			</view>
			<view class="item">
				<view class="itemname">供应商</view>
				<view class="itemtext">
					<input class="iteminput" disabled v-model="supplierName" />
				</view>
			</view>
			<view class="item">
				<view class="itemname">主计量单位</view>
				<view class="itemtext">
					<input class="iteminput" disabled v-model="unitUomlName" />
				</view>
			</view>
			<view class="item">
				<view class="itemname">辅助单位</view>
				<view class="itemtext">
					<input class="iteminput" disabled v-model="supportUomName" />
				</view>
			</view>
			<view class="item">
				<view class="itemname">换算系数</view>
				<view class="itemtext">
					<input class="iteminput" disabled v-model="convCoefficient" />
				</view>
			</view>
			<view class="item">
				<view class="itemname">报检数量(主单位)</view>
				<view class="itemtext">
					<input class="iteminput" disabled v-model="checkCount" />
				</view>
			</view>
			<view class="item redMi">
				<view class="itemname">报检数量(辅单位)</view>
				<view class="itemtext">
					<input type="number" class="iteminput" v-model="checkCountSupport" placeholder="请输入报检数量(辅单位)" @input='toFixed1'/>
				</view>
			</view>
			
			<view class="item">
				<view class="itemname">合格数量(主单位)</view>
				<view class="itemtext">
					<input class="iteminput" disabled v-model="conformityCount" />
				</view>
			</view>
			<view class="item">
				<view class="itemname">返工数量(主单位)</view>
				<view class="itemtext">
					<input class="iteminput" disabled v-model="reworkCount" />
				</view>
			</view>
			<view class="item">
				<view class="itemname">报废数量(主单位)</view>
				<view class="itemtext">
					<input class="iteminput" disabled v-model="scrapCount" />
				</view>
			</view>
			<view class="item redMi">
				<view class="itemname">合格数量(辅单位)</view>
				<view class="itemtext">
					<input type="number" class="iteminput" v-model="conformityCountSupport" placeholder="请输入合格数量(辅单位)" @input='toFixed2'/>
				</view>
			</view>
			<view class="item redMi">
				<view class="itemname">返工数量(辅单位)</view>
				<view class="itemtext">
					<input type="number" class="iteminput" v-model="reworkCountSupport" placeholder="请输入退工数量(辅单位)" @input='toFixed3'/>
				</view>
			</view>
			<view class="item redMi">
				<view class="itemname">报废数量(辅单位)</view>
				<view class="itemtext">
					<input type="number" class="iteminput" v-model="scrapCountSupport" placeholder="请输入报废数量(辅单位)" @input='toFixed4'/>
				</view>
			</view>
		</view>
		
		<view class="fixMargin"></view>
		<view class="fixBottom">
			<view class="uni-flex uni-row">
				<view style="-webkit-flex:1;flex: 1;">
					<button type="primary" plain="true"  style="width: 90%; background: #fff;" @click="gotoDetail">编辑检验明细</button>
				</view>
				<view style="-webkit-flex:1;flex: 1;">
					<button type="primary"  style="width: 90%;" @click="gotoMain">取消</button>
				</view>
			</view>
		</view>
		<mpvue-picker themeColor="#007AFF" ref="mpvuePicker" mode="selector" :deepLength="deepLength"
		 @onConfirm="pickerConfirm" :pickerValueArray="check_type"></mpvue-picker>
		 <mpvue-picker themeColor="#007AFF" ref="mpvuePicker2" mode="selector" :deepLength="deepLength"
		  @onConfirm="pickerConfirmType" :pickerValueArray="process_type"></mpvue-picker>
		<view class="goHome" @click='goMain()'>
			<img src="../../static/img/gohome.png">
		</view>
	</view>
</template>

<script>
	import uniIcons from '@/components/uni-icons/uni-icons.vue';
	import mpvuePicker from '@/components/mpvue-picker/mpvuePicker.vue';
	import mpvuePicker1 from '@/components/mpvue-picker/mpvuePicker.vue';
	export default {
		components: {
			uniIcons,mpvuePicker
		},
		data() {
			return {
				showPicker: false,
				deepLength:1,
				check_type: [],
				process_type: [],
				reportId: '',
				code: '',
				processName: '',
				checkPlanName: '',
				checkPlanId: '',
				supplierName: '',
				supplierId: '',
				materielName: '',
				materielId: '',
				checkCount: '',
				conformityCount: '',
				reworkCount: '',
				scrapCount: '',

				unitUomName:'',
				checkCountSupport:'',
				conformityCountSupport:'',
				reworkCountSupport:"",
				scrapCountSupport:"",
				supportUom:"",
				supportUomName:"",
				convCoefficient:"",
				
				quantityAccuracy:'',
				info: {
				},
				itemList: [],
				deletDetailIds:[],
				currentIndex: 0,
				processDetail: [],
			}
		},
		onLoad(option) {
			var processCheckoutInfo =  uni.getStorageSync('processCheckoutInfo');
			if(processCheckoutInfo!=''&&processCheckoutInfo!=undefined){
				this.itemList=processCheckoutInfo;
			}
			this.reportId = option.id;
			this.getReport();
			//初始化获取 检验结果
			var _this = this;
			this.$http.request({
			  url: '/apis/dict/getDictsByType',
			  params: {
			    type: 'check_result'
			  },
			}).then(res=>{
				if(res.statusCode == '200'){
					var useType = res.data.datas;
					for (let i = 0; i < useType.length; i++) {
					    var myVal=
					    	{
					    		'label': useType[i].name,
					    		'value': useType[i].id
					    	}
					    _this.check_type.push(myVal);
					}
				}
			}).catch(err=>{
				console.log(err);
			});
			//初始化获取 处理方式
			var _this = this;
			this.$http.request({
			  url: '/apis/dict/getDictsByType',
			  params: {
			    type: 'process_mode'
			  },
			}).then(res=>{
				if(res.statusCode == '200'){
					var useType = res.data.datas;
					for (let i = 0; i < useType.length; i++) {
					    var myVal=
					    	{
					    		'label': useType[i].name,
					    		'value': useType[i].id
					    	}
					    _this.process_type.push(myVal);
					}
				}
			}).catch(err=>{
				console.log(err);
			});
		},
		methods: {
			//报检数量
			toFixed1() {
				if (!isNaN(this.convCoefficient) && !isNaN(this.checkCountSupport)){
					this.checkCountSupport = Number(this.checkCountSupport).toFixed(this.quantityAccuracy)
					this.checkCount = Number(this.convCoefficient)*Number(this.checkCountSupport).toFixed(this.quantityAccuracy)
				}
			},
			//合格数量
			toFixed2() {
				if (!isNaN(this.convCoefficient) && !isNaN(this.conformityCountSupport)){
					this.conformityCountSupport = Number(this.conformityCountSupport).toFixed(this.quantityAccuracy)
					this.conformityCount = Number(this.convCoefficient)*Number(this.conformityCountSupport).toFixed(this.quantityAccuracy)
				}
			},
			//返工数量
			toFixed3() {
				if (!isNaN(this.convCoefficient) && !isNaN(this.reworkCountSupport)){
					this.reworkCountSupport = Number(this.reworkCountSupport).toFixed(this.quantityAccuracy)
					this.reworkCount = Number(this.convCoefficient)*Number(this.reworkCountSupport).toFixed(this.quantityAccuracy)
				}
			},
			//返工数量
			toFixed4() {
				if (!isNaN(this.convCoefficient) && !isNaN(this.scrapCountSupport)){
					this.scrapCountSupport = Number(this.scrapCountSupport).toFixed(this.quantityAccuracy)
					this.scrapCount = Number(this.convCoefficient)*Number(this.scrapCountSupport).toFixed(this.quantityAccuracy)
				}
			},

			//是否检验
			checkboxChange(e,index){
			if(e.detail.value.length==0){
				this.$set(this.itemList[index],'whetherCheck',0)
			}else{
				this.$set(this.itemList[index],'whetherCheck',1)
			}
			
			},
			getReport() {
				var filterRes = {};	
				filterRes.reportId = this.reportId
				this.$http.request({
					url: '/apis/mes/reportCheck/buttonOfCheck',
					params: filterRes,
				})
				.then(res => {
					if(res.data.code == 0) {
						this.code = res.data.HeadDetail.code?res.data.HeadDetail.code:''
						this.processName = res.data.HeadDetail.processName?res.data.HeadDetail.processName:''
						this.checkPlanName = res.data.HeadDetail.checkPlanName?res.data.HeadDetail.checkPlanName:''
						this.checkPlanId = res.data.HeadDetail.checkPlanId?res.data.HeadDetail.checkPlanId:''
						this.supplierName = res.data.HeadDetail.supplierName?res.data.HeadDetail.supplierName:''
						this.supplierId = res.data.HeadDetail.supplierId? res.data.HeadDetail.supplierId:''
						this.materielName = res.data.HeadDetail.materielName?res.data.HeadDetail.materielName:''
						this.materielId = res.data.HeadDetail.materielId?res.data.HeadDetail.materielId:''
						
						this.checkCount = 0
						this.conformityCount =0
						this.reworkCount = 0
						this.scrapCount =0

						this.unitUomlName = res.data.HeadDetail.unitUomlName?res.data.HeadDetail.unitUomlName:''
						this.checkCountSupport = 0
						this.conformityCountSupport = res.data.HeadDetail.conformityCountSupport?res.data.HeadDetail.conformityCountSupport:''
						this.reworkCountSupport = res.data.HeadDetail.reworkCountSupport?res.data.HeadDetail.reworkCountSupport:''
						this.scrapCountSupport = res.data.HeadDetail.scrapCountSupport?res.data.HeadDetail.scrapCountSupport:''
						this.supportUom = res.data.HeadDetail.supportUom?res.data.HeadDetail.supportUom:''
						this.supportUomName = res.data.HeadDetail.supportUomName?res.data.HeadDetail.supportUomName:''
						this.convCoefficient = res.data.HeadDetail.convCoefficient?res.data.HeadDetail.convCoefficient:''
						this.quantityAccuracy = res.data.HeadDetail.quantityAccuracy
						if(!isNaN(this.convCoefficient)){
							this.checkCountSupport = res.data.HeadDetail.checkCountSupport?res.data.HeadDetail.checkCountSupport:''
							this.checkCount = Number(this.checkCountSupport)*Number(this.convCoefficient).toFixed(this.quantityAccuracy)
						}
						res.data.BadyDetail.forEach(item=>{
							this.$set(item,'old',true);
						})
						this.itemList =this.itemList.length==0?res.data.BadyDetail:this.itemList
					}
				})
			},
			
			gotoDetail() {
				if(this.conformityCountSupport ==''){
					uni.showToast({
						title: '请输入合格数量(辅单位)',
						duration: 	2000,
						icon:"none"
					});
					return
				} else {
					let a=this.conformityCount?this.conformityCount:0;
					let b=this.reworkCount?this.reworkCount:0;
					let c=this.scrapCount?this.scrapCount:0;
					let d=parseFloat(a)+parseFloat(b)+parseFloat(c);
					if(d!=this.checkCount){
						uni.showToast({
							title: '合格数量、返工数量、报废数量的总和必须等于报检数量',
							icon: "none",
							duration: 2000
						});
						return
					}
					
					this.reworkCount = this.reworkCount?this.reworkCount:0
					this.scrapCount = this.scrapCount?this.scrapCount:0
					uni.navigateTo({
					    url: 'checkDetail?reportId='+this.reportId+"&reworkCount="+this.reworkCount+"&scrapCount="+this.scrapCount+"&conformityCount="+this.conformityCount+"&conformityCountSupport="+this.conformityCountSupport+"&reworkCountSupport="+this.reworkCountSupport+"&scrapCountSupport="+this.scrapCountSupport+"&checkCountSupport="+this.checkCountSupport 
					});
				}
			},
			gotoMain() {
				uni.switchTab({
					url: "../main/main"
				});
			},
		},
		onBackPress(options) { //取消默认的返回事件.用来返回上一页时重新加载页面，删除本地缓存的巡检操作
			if (options.from === 'backButton' || options.from === 'backbutton' || options.from === 'navigateBack') {
				uni.redirectTo({
					url: 'reportList'
				});
				return true;	
			}		
		},
	}
</script>

<style scoped>
	.fz36 {
		font-size: 32upx !important;
		font-weight: 700;
	}
	.mt {
		margin-top: 20upx !important;
	}
	
</style>
