<template>
	<view class="page">
		<view class="example-b">
			<view class="item">
				<view class="itemname">工单号</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="dataList.reportName" />
				</view>
				<!-- <view class="itemtext" @click="selectReporter()">
					<input class="iteminput InputRightMargin" disabled placeholder="请选择" v-model="dataList.reportName" />
					<img src="../../static/img/personlistIcon.png" class="dateIcon" />
				</view> -->
			</view>
			<view class="item" v-if="isOutsource==1">
				<view class="itemname">供应商</view>
				<view class="itemtext" @click='selectSupplier()'>
					<input class="iteminput" placeholder="请选择" disabled v-model="dataList.supplierName" />
				</view>
			</view>
			<view class="item" v-if="isOutsource==0">
				<view class="itemname">设备</view>
				<view class="itemtext" @click='selectDevice()'>
					<input class="iteminput" placeholder="请选择" disabled v-model="dataList.deviceName" />
				</view>
			</view>
			<view class="item">
				<view class="itemname">是否检验</view>
				<view class="itemtext">
					<input class="iteminput" disabled v-model="isExamineName" />
				</view>
			</view>
			<view class="item">
				<view class="itemname">主计量单位</view>
				<view class="itemtext">
					<input class="iteminput" disabled v-model="unitUomName" />
				</view>
			</view>
			<view class="item redMi">
				<view class="itemname">辅助单位</view>
				<view class="itemtext">
					<picker @change="bindPickerChange" :range="arrayName" >
							<label class="">{{dataList.supportUomName?dataList.supportUomName:"请选择辅单位"}}</label>
					</picker>
				</view>
			</view>
			<view class="item redMi">
				<view class="itemname">换算系数</view>
				<view class="itemtext">
					<input type="number" class="iteminput" placeholder="请输入换算系数" v-model="dataList.convCoefficient" @input='toFixedOfConv'/>
				</view>
			</view>

			<view class="item">
				<view class="itemname">计划数量(主单位)</view>
				<view class="itemtext">
					<input type="number" class="iteminput" disabled v-model="planCount" />
				</view>
			</view>
			<view class="item">
				<view class="itemname">已完工数量(主单位)</view>
				<view class="itemtext">
					<input type="number" class="iteminput" disabled v-model="hascompletionCount" />
				</view>
			</view>
			<view class="item">
				<view class="itemname">完工数量(主单位)</view>
				<view class="itemtext">
					<input type="number" class="iteminput" disabled v-model="dataList.completionCount" />
				</view>
			</view>
      <view class="item" v-if="isExamine==0">
        <view class="itemname">合格数量(主单位)</view>
        <view class="itemtext">
          <input type="number" class="iteminput" disabled v-model="dataList.conformityCount" />
        </view>
      </view>
      <view class="item" v-if="isExamine==0">
        <view class="itemname">返工数量(主单位)</view>
        <view class="itemtext">
          <input type="number" class="iteminput" disabled v-model="dataList.reworkCount" />
        </view>
      </view>
      <view class="item" v-if="isExamine==0">
        <view class="itemname">报废数量(主单位)</view>
        <view class="itemtext">
          <input type="number" class="iteminput" disabled v-model="dataList.scrapCount" />
        </view>
      </view>
			<view class="item  redMi">
				<view class="itemname">完工数量(辅单位)</view>
				<view class="itemtext">
					<input type="number" class="iteminput" placeholder="请输入完工数量(辅单位)" v-model="dataList.completionCountSupport" @input='toFixed1'/>
				</view>
			</view>

			<view class="item redMi" v-if="isExamine==0">
				<view class="itemname">合格数量(辅单位)</view>
				<view class="itemtext">
					<input type="number" class="iteminput" placeholder="请输入合格数量(辅单位)" v-model="dataList.conformityCountSupport" @input='toFixed2'/>
				</view>
			</view>

			<view class="item redMi" v-if="isExamine==0">
				<view class="itemname">返工数量(辅单位)</view>
				<view class="itemtext">
					<input type="number" class="iteminput" placeholder="请输入返工数量(辅单位)" v-model="dataList.reworkCountSupport" @input='toFixed3'/>
				</view>
			</view>
			<view class="item redMi" v-if="isExamine==0">
				<view class="itemname">报废数量(辅单位)</view>
				<view class="itemtext">
					<input type="number" class="iteminput" placeholder="请输入报废数量(辅单位)" v-model="dataList.scrapCountSupport" @input='toFixed4'/>
				</view>
			</view>
			<view class="item redMi" >
				<view class="itemname">批号</view>
				<view class="itemtext">
					<input class="iteminput" placeholder="请输入批号信息" v-model="dataList.batch"/>
				</view>
			</view>

			<view class="item">
				<view class="itemname">备注</view>
				<view class="itemtext">
					<input class="iteminput" placeholder="请输入备注信息" v-model="dataList.remark"/>
				</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" style="width: 90%;" @click="save">提交</button>
					</view>
				</view>
			</view>
		</view>
		<view class="goHome" @click='goMain("reimInfo")'>
			<img src="../../static/img/gohome.png">
		</view>
		<view class="mysearchlist" style="z-index: 10000;">
			<chooseUserSingle ref="mysearchlist"
				:showsearch.sync="show_search_user"
				:itemInfo_.sync="search_value_user">
			</chooseUserSingle>
		</view>
		<view class="mysearchlist" style="z-index: 10000;">
			<chooseSupplierSingle ref="mysearchlist"
				:showsearch.sync="show_search_user2"
				:itemInfo_.sync="search_value_user2">
			</chooseSupplierSingle>
		</view>
		<view class="mysearchlist" style="z-index: 10000;">
			<chooseEquipmentSingle ref="mysearchlist"
				:showsearch.sync="show_search_user3"
				:device_type='device_type'
				:itemInfo_.sync="search_value_user3">
			</chooseEquipmentSingle>
		</view>
	</view>
</template>

<script>
	import chooseSupplierSingle from '@/components/xinsoft-search-list/chooseSupplierSingle.vue';
	import chooseEquipmentSingle from '@/components/xinsoft-search-list/chooseEquipmentSingle.vue';
	import chooseUserSingle from '@/components/xinsoft-search-list/chooseUserSingle.vue';
	export default {
		components:{
			chooseEquipmentSingle,chooseUserSingle,chooseSupplierSingle
		},
		data() {
				return {
					device_type:'',//设备类型
					id:'',
					processName: '',
					code: '',
					isExamine: '',
					isOutsource: '',
					isExamineName: '',
					planCount: '',
					unitUomName:'',
					hascompletionCount: '',
					dispatchItemId: '',
					tableInfo: {

					},
					show: false,
					dataList: {
						completionCount: '',
						conformityCount: '',
						reworkCount:'',
						scrapCount:'',
						isLot:'',
						completionCountSupport:'',
						conformityCountSupport:'',
						reworkCountSupport:"",
						scrapCountSupport:"",
						supportUom:"",
						supportUomName:"",
						convCoefficient:"",
						batch:""
					},
					quantityAccuracy:'',
					reportDetail: [],
					show_search_user:false,
					search_value_user:"",
					show_search_user2:false,
					search_value_user2:"",
					show_search_user3:false,
					search_value_user3:"",
					arrayName:[],
					array: []
				}
			},
			created() {
				this.getWorking();
				this.getSpupportUnomName();
			},
			onShow() {
			  // if(this.isExamine == 1) {
				 //  this.show = true
			  // } else {
				 //  this.show = false
			  // }
			  var processReportInfo=  uni.getStorageSync('processReportInfo');
			  if(processReportInfo!=''&&processReportInfo!=undefined){
			  	this.dataList=processReportInfo;
			  }
			},
			onLoad(option) {
				this.id = option.dispatchItemId
				//初始化当前 汇报人为默认当前登陆人。
				var userInfo  = uni.getStorageSync('userInfo');
				console.log(userInfo)
				this.dataList.reportName = userInfo.name
				this.dataList.reportId = userInfo.userId
				// if(this.tableInfo !='') {
				// 	this.code = this.tableInfo.processCode;
				// 	this.processName = this.tableInfo.processName ? this.tableInfo.processName : '';
				// 	this.isExamine = this.tableInfo.isExamine ? this.tableInfo.isExamine : 0;
				// 	this.planCount = this.tableInfo.planCount!=undefined  ? this.tableInfo.planCount : 0;

				// }
			},
			watch:{
				search_value_user(newv){
					this.$set(this.dataList,'reportId',newv.userId);
					this.$set(this.dataList,'reportName',newv.NAME);
					uni.setStorage({
						key: 'processReportInfo',
						data: this.dataList,
						success: function () {

						}
					})

				},
				search_value_user2(newv){
					console.log(newv)
					this.$set(this.dataList,'spplierId',newv.id);
					this.$set(this.dataList,'supplierName',newv.unit_name);
					uni.setStorage({
						key: 'processReportInfo',
						data: this.dataList,
						success: function () {

						}
					})

				},
				search_value_user3(newv){
					console.log(newv)
					this.$set(this.dataList,'deviceId',newv.id);
					this.$set(this.dataList,'deviceName',newv.name);
					uni.setStorage({
						key: 'processReportInfo',
						data: this.dataList,
						success: function () {

						}
					})

				}
			},
			methods: {
				getSpupportUnomName(){
					let type ='uom_type'
					this.$http.request({
						url: '/apis/dict/getDictsByType',
						params: {type:type}
					})
					.then(res=>{
						this.array=res.data.datas
						let arr = []
						this.array.forEach(item=>{
							arr.push(item.name)
						})
						this.arrayName = arr
					})
				},

				bindPickerChange:function(e){
					var indexNew=e.target.value
					let supportUomName = this.arrayName[indexNew]
					this.$set(this.dataList, "supportUomName", supportUomName)
					let supportUnomId =0
					this.array.forEach(aa =>{
						if(aa.name==supportUomName){
							supportUnomId=aa.id
						}
					})
					this.$set(this.dataList, "supportUom", supportUnomId)
				},
				//係數
				toFixedOfConv(){
					if(!isNaN(this.dataList.convCoefficient)){
						if(Number(0.00)==Number(this.dataList.convCoefficient)){
							this.$set(this.dataList,'convCoefficient', null)
							}else{
								this.$set(this.dataList,'convCoefficient',Number(this.dataList.convCoefficient))
							}
					}else{
						this.$set(this.dataList,'convCoefficient', 1)
					}
					
					if(this.isExamine==0){
						this.toFixed1();
						this.toFixed2();
						this.toFixed3();
						this.toFixed4();
					}else{
						this.toFixed1();
					}
				},
				//完工数量
				toFixed1() {
					if (!isNaN(this.dataList.convCoefficient) && !isNaN(this.dataList.completionCountSupport)) {
						if(Number(0.00)==Number(this.dataList.completionCountSupport)){
							this.$set(this.dataList,'completionCountSupport', null)
							this.$set(this.dataList,'completionCount', null)
						}else{
							this.$set(this.dataList,'completionCountSupport',Number(this.dataList.completionCountSupport))
							this.$set(this.dataList,'completionCount',(Number(this.dataList.completionCountSupport)*Number(this.dataList.convCoefficient)).toFixed(this.dataList.quantityAccuracy))
						}
					}else{
						this.$set(this.dataList,'completionCount', null)
					}
				},
				//合格数量
				toFixed2() {
					if (!isNaN(this.dataList.convCoefficient) && !isNaN(this.dataList.conformityCountSupport)) {						
						if(Number(0.00)==Number(this.dataList.conformityCountSupport)){
							this.$set(this.dataList,'conformityCountSupport', null)
							this.$set(this.dataList,'conformityCount', null)
						}else{
							this.$set(this.dataList,'conformityCountSupport',Number(this.dataList.conformityCountSupport))
							this.$set(this.dataList,'conformityCount',(Number(this.dataList.conformityCountSupport)*Number(this.dataList.convCoefficient)).toFixed(this.dataList.quantityAccuracy))
						}
					}else{
						this.$set(this.dataList,'conformityCount', null)
					}
				},
				//返工数量
				toFixed3() {
					if (!isNaN(this.dataList.convCoefficient) && !isNaN(this.dataList.reworkCountSupport)) {
						if(Number(0.00)==Number(this.dataList.reworkCountSupport)){
							this.$set(this.dataList,'reworkCountSupport', null)
						}else{
						this.$set(this.dataList,'reworkCountSupport',Number(this.dataList.reworkCountSupport))
						this.$set(this.dataList,'reworkCount',(Number(this.dataList.reworkCountSupport)*Number(this.dataList.convCoefficient)).toFixed(this.dataList.quantityAccuracy))
						}
					}else{
						this.$set(this.dataList,'reworkCount', null)
					}
				},
				toFixed4() {
					if (!isNaN(this.dataList.convCoefficient) && !isNaN(this.dataList.scrapCountSupport)) {
						if(Number(0.00)==Number(this.dataList.scrapCountSupport)){
							this.$set(this.dataList,'scrapCountSupport', null)
						}else{
							this.$set(this.dataList,'scrapCountSupport',Number(this.dataList.scrapCountSupport))
							this.$set(this.dataList,'scrapCount',(Number(this.dataList.scrapCountSupport)*Number(this.dataList.convCoefficient)).toFixed(this.dataList.quantityAccuracy))
						}
					}else{
						this.$set(this.dataList,'scrapCount', null)
					}
				},
				// toFixed5(){
				// 	this.$set(this.dataList,'batch',this.dataList.batch)
				// },

				//点击导航栏按钮
				onNavigationBarButtonTap(obj) {
						uni.navigateTo({
						    url: 'add'
						});
				},

				// 选择设备
				selectDevice(){
					if(this.dataList.deviceId) {
						this.show_search_user3=false;
					} else {
						this.show_search_user3=true;
						uni.setStorage({
							key: 'processReportInfo',
							data: this.dataList,
							success: function () {

							}
						})
					}
				},
				// 获取工单信息
				getWorking() {
					var filterRes = {id:this.id};
					this.$http.request({
						url: 'apis/mes/dispatch/getStartWorkingDetail',
						params: filterRes,
					})
					.then(res => {
						if(res.data) {
							this.isExamine = res.data.data.isExamine;
							this.isOutsource = res.data.data.isOutsource;
							this.isExamineName = res.data.data.isExamine==1?'是':'否'
							this.processName = res.data.data.processName;
							this.code = res.data.data.code
							this.planCount = res.data.data.planCount?res.data.data.planCount:0
							this.hascompletionCount =res.data.data.completionCount?res.data.data.completionCount:0
							this.dispatchItemId = res.data.data.dispatchItemId
							this.quantityAccuracy = res.data.data.quantityAccuracy?res.data.data.quantityAccuracy:2
							this.dataList.deviceName = res.data.data.deviceName?res.data.data.deviceName:''
							this.dataList.deviceId = res.data.data.deviceId?res.data.data.deviceId:''
							this.dataList.supplierName = res.data.data.supplierName?res.data.data.supplierName:''
							this.dataList.supplierId = res.data.data.supplierId?res.data.data.supplierId:''

							this.dataList.completionCount = 0;
							this.dataList.conformityCount =0;
							this.dataList.reworkCount = 0;
							this.dataList.scrapCount = 0;

							// this.dataList.completionCountSupport =  0;
							// this.dataList.conformityCountSupport =0;
							// this.dataList.reworkCountSupport = 0;
							// this.dataList.scrapCountSupport = 0;
							this.dataList.supportUom = res.data.data.supportUom;
							this.dataList.supportUomName = res.data.data.supportUomName;
							this.dataList.convCoefficient = res.data.data.convCoefficient ? res.data.data.convCoefficient : 1;
							this.unitUomName = res.data.data.unitUomName
							this.dataList.isLot= res.data.data.isLot;
						}
					})
				},
				// 选择汇报人
				selectReporter() {
					this.show_search_user=true;
					uni.setStorage({
						key: 'processReportInfo',
						data: this.dataList,
						success: function () {

						}
					})
					// uni.navigateTo({
					// 	url: 'selectReport'
					// });
				},
				// 选择供应商
				selectSupplier() {
					if(this.dataList.supplierId) {
						this.show_search_user2=false;
					} else {
						this.show_search_user2=true;
						uni.setStorage({
							key: 'processReportInfo',
							data: this.dataList,
							success: function () {

							}
						})
						// uni.navigateTo({
						// 	url: 'selectSupplier'
						// });
					}

				},
				// 提交
				save() {
					if(this.dataList.isLot==1&&(this.dataList.batch==null||this.dataList.batch=='')){
						uni.showToast({
							title: '请输入批号!',
							icon: "none",
							duration: 2000
						});
						return false
					}
					if(!this.dataList.completionCount){
						uni.showToast({
							    title: '请填写完工数量',
							    duration: 2000,
								icon:"none"
							});
						return false;
					}
					if(this.isExamine==1) {
						let detail = [];
						let dataList = this.dataList
							let obj = { dispatchItemId: this.dispatchItemId, createBy: dataList.reportId,
							supplierId: dataList.spplierId?dataList.spplierId:'',deviceId: dataList.deviceId?dataList.deviceId:'', completionCount: dataList.completionCount, conformityCount: 0,
							reworkCount: 0, scrapCount: 0,remark: dataList.remark,supportUom:dataList.supportUom,convCoefficient:dataList.convCoefficient,completionCountSupport:dataList.completionCountSupport};
							detail.push(obj)
							this.reportDetail = encodeURIComponent(JSON.stringify(detail))
					} else {

						let dataList = this.dataList
						let a=dataList.conformityCount?dataList.conformityCount:0;
						let b=dataList.reworkCount?dataList.reworkCount:0;
						let c=dataList.scrapCount?dataList.scrapCount:0;
						let d=parseFloat(a)+parseFloat(b)+parseFloat(c);
						if(d!=dataList.completionCount){
							uni.showToast({
								title: '合格数量、返工数量、报废数量的总和必须等于完工数量',
								icon: "none",
								duration: 2000
							});
							return
						}
						let detail = [];
							let obj = {
								dispatchItemId: this.dispatchItemId,
								createBy: dataList.reportId,
								deviceId: dataList.deviceId?dataList.deviceId:'',
								supplierId: dataList.spplierId?dataList.spplierId:'',
								completionCount: dataList.completionCount,
								conformityCount: dataList.conformityCount,
								reworkCount: dataList.reworkCount,
								scrapCount: dataList.scrapCount,
								remark: dataList.remark,
								completionCountSupport: dataList.completionCountSupport,
								conformityCountSupport: dataList.conformityCountSupport,
								reworkCountSupport: dataList.reworkCountSupport,
								scrapCountSupport: dataList.scrapCountSupport,
								supportUom: dataList.supportUom,
								supportUomName: dataList.supportUomName,
								convCoefficient: dataList.convCoefficient,
								batch: dataList.batch,
							};
							detail.push(obj)
							this.reportDetail = encodeURIComponent(JSON.stringify(detail))
					}
					this.$http.request({
						url: 'apis/mes/processReport/submitReport',
						params: {
							reportDetail: this.reportDetail,
						}
					}).then(res=>{
						if (res.data.code === 0) {
							uni.removeStorageSync('processReportInfo');
							uni.showToast({
								title: res.data.msg ? res.data.msg : '操作成功',
								duration: 1000,
								icon: "none"
							});
							var _this = this
							setTimeout( function(){
								console.log(_this.dispatchItemId)
								uni.navigateTo({
								    url: 'startWorking?+id='+_this.dispatchItemId,
								});
							},1000)
						} else {
							uni.showToast({
								title: res.data.msg ? res.data.msg : '操作失败',
								duration: 2000,
								icon: "none"
							});
						}

					}).catch(err=>{
						uni.showToast({
							title: '操作失败',
							duration: 2000,
							icon: "none"
						});
					})
				}
			}
	}
</script>

<style scoped>
	.example-b {
		margin-top: 20upx;
	}
</style>
