﻿<template>
	<div class="mesQiInspJob-container">
		<el-dialog v-model="state.isShowDialog" v-if="state.isShowDialog" :width="850" draggable="" :close-on-click-modal="false">
			<template #header>
				<div style="color: #fff">
					<!--<el-icon size="16" style="margin-right: 3px; display: inline; vertical-align: middle"> <ele-Edit /> </el-icon>-->
					<span>{{ props.title }}</span>
				</div>
			</template>
			<el-form :model="state.ruleForm" ref="ruleFormRef" label-width="auto" :rules="rules">
				<el-row :gutter="35">
					<el-form-item v-show="false">
						<el-input v-model="state.ruleForm.id" />
					</el-form-item>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="报检单号" prop="inspJobNo">
							<el-input v-model="state.ruleForm.inspJobNo" placeholder="请输入报检单号" readonly="true" maxlength="100" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="检验类型" prop="inspType">
							<el-select  v-model="state.ruleForm.inspType" placeholder="请选择检验类型" disabled="true">
								<el-option v-for="(item,index) in dl('checkTypes')"  :key="index" :value="Number(item.value)" :label="`${item.name} [${item.value}]`"></el-option>
								
							</el-select>
							
						</el-form-item>
						
					</el-col>
					
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="工单号" prop="produceWorkOrderNo">
							<el-input v-model="state.ruleForm.produceWorkOrderNo" placeholder="请输入工单号" readonly="true" maxlength="100" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="批次号" prop="batchNo">
							<el-input v-model="state.ruleForm.batchNo" placeholder="请输入批次号" readonly="true" maxlength="100" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					<!-- <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="生产订单号" prop="produceOrderNo">
							<el-input v-model="state.ruleForm.produceOrderNo" placeholder="请输入生产订单号" maxlength="100" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col> -->
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="产品编码" prop="productCode">
							<el-input v-model="state.ruleForm.productCode" placeholder="请输入产品编码" readonly="true" maxlength="100" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="产品名称" prop="productName">
							<el-input v-model="state.ruleForm.productName" placeholder="请输入产品名称" readonly="true" maxlength="100" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="零件编码" prop="partCode">
							<el-input v-model="state.ruleForm.partCode" placeholder="请输入零件编码" readonly="true" maxlength="100" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="零件名称" prop="partName">
							<el-input v-model="state.ruleForm.partName" placeholder="请输入零件名称" readonly="true" maxlength="100" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="工序编码" prop="processCode">
							<el-input v-model="state.ruleForm.processCode" placeholder="请输入工序编码" readonly="true" maxlength="100" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="工序名称" prop="processName">
							<el-input v-model="state.ruleForm.processName" placeholder="请输入工序名称" readonly="true" maxlength="100" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="设备编码" prop="equipmentCode">
							<el-input v-model="state.ruleForm.equipmentCode" placeholder="请输入设备编码" readonly="true" maxlength="100" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="设备名称" prop="equipmentName">
							<el-input v-model="state.ruleForm.equipmentName" placeholder="请输入设备名称" readonly="true" maxlength="32" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="送检数量" prop="sendNum">
							<el-input-number v-model="state.ruleForm.sendNum" placeholder="请输入送检数量" readonly="true" clearable />
							
						</el-form-item>
						
					</el-col>
					<!-- <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="检验数量" prop="inspNum">
							<el-input-number v-model="state.ruleForm.inspNum" placeholder="请输入检验数量" readonly="true" clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="合格数量" prop="passNum">
							<el-input-number v-model="state.ruleForm.passNum" placeholder="请输入合格数量"  readonly="true" clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="不合格数量" prop="inPassNum">
							<el-input-number v-model="state.ruleForm.inPassNum" placeholder="请输入不合格数量" readonly="true" clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="结论" prop="inspResult">
							<el-input v-model="state.ruleForm.inspResult" placeholder="请输入结论" readonly="true" maxlength="100" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col> -->
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="不合格处置" prop="inPassHandle">
							<el-input v-model="state.ruleForm.inPassHandle" placeholder="请输入不合格处置" maxlength="100" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="cancel">取 消</el-button>
					<el-button type="primary" @click="submit">确 定</el-button>
				</span>
			</template>
			<template v-if="props.title=='编辑检验任务'">
                <el-tabs v-model="tabActive">
                <el-tab-pane label="检验项目及标准" name="item"></el-tab-pane>
                </el-tabs>
                <el-table :data="detailList" >
				<el-table-column type="index" label="序号" width="55" align="center" />
                <el-table-column prop="qiItemName" label="检验项名称" width="120" align="center"   show-overflow-tooltip="" />
				<el-table-column prop="inspStand" label="检验标准" width="120" align="center"   show-overflow-tooltip="" />
				<el-table-column prop="standVal" label="标准值" width="80" align="center"   show-overflow-tooltip="" />
				<el-table-column prop="maxVal" label="上限值" width="80" align="center"  show-overflow-tooltip="" />
				<el-table-column prop="minVal" label="下限值" width="80" align="center"  show-overflow-tooltip="" />
				<el-table-column prop="itemPropsName" label="检验项属性" width="80" align="center"  show-overflow-tooltip="" />
				<el-table-column prop="resultVal" label="检验结果值" align="center"  show-overflow-tooltip="" >
                  <template  #default="scope">
                    <el-input v-model="scope.row.resultVal"  @input="handleInput1(scope.row)" :disabled="isDisableds(scope.row.itemPropsName)==true" />
                  </template>
                </el-table-column>
				<!-- <el-table-column prop="inspResult" label="结论"   show-overflow-tooltip="" >
                </el-table-column> -->
				<el-table-column prop="inspResult" label="结论" align="center" show-overflow-tooltip="">
                  <template  #default="scope">
                    <el-select  v-model="scope.row.inspResult"  :disabled="isDisabled(scope.row.itemPropsName)==true"><!-- @input="handleInput(scope.row)"-->
								<el-option value="合格">合格</el-option>
								<el-option value="不合格">不合格</el-option>
                     </el-select>
                  </template>
                </el-table-column>
				<el-table-column prop="remark" label="描述" align="center" show-overflow-tooltip="" >
                  <template  #default="scope">
                    <el-input type="text" v-model="scope.row.remark" ></el-input>
                  </template>
                </el-table-column>
            </el-table>
            </template>
		</el-dialog>
	</div>
</template>
<style lang="scss" scoped>
:deep(.el-select),
:deep(.el-input-number) {
	width: 100%;
}
</style>
<script lang="ts" setup>
    import { ref,onMounted, reactive } from "vue";
	import { ElMessageBox,ElMessage } from "element-plus";
	import type { FormRules } from "element-plus";
	import { addMesQiInspJob, updateMesQiInspJob, detailMesQiInspJob } from "/@/api/mes/mesQiInspJob";
	//import { pageMesQiInspJob, deleteMesQiInspJob } from '/@/api/mes/mesQiInspJob';
	import { getInspectionList,updateMesQimentTestTaskHead} from "/@/api/simple/mesMaterialTestTaskHead";
	import { getDictDataItem as di, getDictDataList as dl } from '/@/utils/dict-utils';
  	import { formatDate } from '/@/utils/formatTime';
	  import { nextTick } from "vue";
    //mport { tr } from "element-plus/es/locale";
 // 子表
 const detailList = ref([]);
 const tabActive = ref('item');
 const isEditable=ref(true);
	//父级传递来的参数
	var props = defineProps({
		title: {
		type: String,
		default: "",
	},
	});
	//父级传递来的函数，用于回调
	const emit = defineEmits(["reloadTable"]);
	const ruleFormRef = ref();
    const state = reactive({
    isShowDialog: false,
    ruleForm: {} as any,
});

	//自行添加其他规则
	const rules = ref<FormRules>({
		inspJobNo: [{required: true, message: '请输入报检单号！', trigger: 'blur',},],
		inspType: [{required: true, message: '请选择检验类型！', trigger: 'change',},],
		produceWorkOrderNo: [{required: true, message: '请输入工单号！', trigger: 'blur',},],
		producePlanNo: [{required: true, message: '请输入生产计划号！', trigger: 'blur',},],
		produceOrderNo: [{required: true, message: '请输入生产订单号！', trigger: 'blur',},],
		productCode: [{required: true, message: '请输入产品编码！', trigger: 'blur',},],
		processCode: [{required: true, message: '请输入工序编码！', trigger: 'blur',},],
		equipmentCode: [{required: true, message: '请输入设备编码！', trigger: 'blur',},],
		sendNum: [{required: true, message: '请输入送检数量！', trigger: 'blur',},],
		inspNum: [{required: true, message: '请输入检验数量！', trigger: 'blur',},],
		passNum: [{required: true, message: '请输入合格数量！', trigger: 'blur',},],
		inPassNum: [{required: true, message: '请输入不合格数量！', trigger: 'blur',},],
	});

	// 页面加载时
	onMounted(() => {

	});

	// 打开弹窗
	const openDialog = async (row: any) => {
		// state.ruleForm = JSON.parse(JSON.stringify(row));
		// 改用detail获取最新数据来编辑
		detailList.value=[];
		let rowData = JSON.parse(JSON.stringify(row));
		let list = await getInspectionList({
          id:rowData.id,
         });
		 let data = list.data.result ?? [];
		 detailList.value.push(...data);
		if (rowData.id)
		{
			state.ruleForm = (await detailMesQiInspJob(rowData.id)).data.result;
			state.ruleForm.batchNo=rowData.batchNo;
			state.ruleForm.partCode=rowData.partCode;
			state.ruleForm.partName=rowData.partName;
		}
		else
			state.ruleForm = rowData;
		state.isShowDialog = true;
	};

	// 关闭弹窗
	const closeDialog = () => {
		emit("reloadTable");
		state.isShowDialog = false;
	};

	// 取消
	const cancel = () => {
		state.isShowDialog = false;
	};

	// 提交
	const submit = async () => {
		ruleFormRef.value.validate(async (isValid: boolean, fields?: any) => {
			if (isValid) {
				debugger
				let values = state.ruleForm;
				values.data=detailList.value;
			    const result=ref(true);
				for(let index=0;index<values.data.length;index++) {
				if(values.data[index].inspResult=="不合格")
				{
					result.value=false;
				}
				if(values.data[index].itemPropsName=="定性")
				{
                    if(values.data[index].inspResult!="合格" && values.data[index].inspResult!="不合格")
					{
						return ElMessage({
                    message: `当前检验中第${index+1}行中存在未选择的检验项`,
                    type: "error",
                    });
					}
				}
				else{
					
					if(values.data[index].resultVal ==undefined && values.data[index].resultVal==null){
                    return ElMessage({
                    message: `当前检验中第${index+1}行中存在未输入的检验值`,
                    type: "error",
                    });
                    }
	              
				}
				}
				if(result.value==false)
				{
					ElMessageBox.confirm(`当前检验信息存在不合格是否确认提交?`, '提示', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning',
					})
					.then(async () => {
						await updateMesQimentTestTaskHead(values);
						closeDialog();
					})
					.catch(() => {});
					
				}
				else{
					await updateMesQimentTestTaskHead(values);
					closeDialog();
				}
			} else {
				ElMessage({
					message: `表单有${Object.keys(fields).length}处验证失败，请修改后再提交`,
					type: "error",
				});
			}
		});
	};
	
	const handleInput1=(value:any) =>{
		debugger
	const allowedValues = [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9','.','-','－','．', '０', '１', '２', '３', '４', '５', '６', '７', '８', '９'];
	const currentChar = value.resultVal.slice(-1);
	if (allowedValues.includes(currentChar))
	{
		if( /[-+]?\d*\.?\d*$/.test(value.resultVal))
	 {
		value.resultVal = ToCDB(value.resultVal.toUpperCase());
		if(value.standVal==null || value.standVal=="")
	    {
		value.standVal=undefined;
	    }
		if(value.maxVal==null || value.maxVal=="")
	    {
		value.maxVal=undefined;
	    }
		if(value.minVal==null || value.minVal=="")
	    {
		value.minVal=undefined;
	    }
		if(value.standVal!=undefined )
	   {
		  if(value.maxVal==undefined && value.maxVal==null && value.maxVal=="")
		  {
			value.maxVal=0;
		  }
		  if(value.minVal==undefined )
		  {
			value.minVal=0;
		  }
		  if(parseFloat(value.standVal)+parseFloat(value.maxVal)>=value.resultVal && parseFloat(value.standVal)-parseFloat(value.minVal)<=value.resultVal)
		  {
			value.inspResult="合格";
		  }else
		   value.inspResult="不合格";
	   }
	   //判断只存在最小值的
         else if(value.minVal!=undefined && value.standVal==undefined &&  value.maxVal==undefined)
	   {
		if(parseFloat(value.minVal)>parseFloat(value.resultVal) )
		  {
			value.inspResult="不合格";
		  }else
		  value.inspResult="合格";
	   }
	   //判断最大值最小值都不为0的
	   else if(value.minVal!=undefined && value.standVal==undefined &&  value.maxVal!=undefined)
	   {
		if(parseFloat(value.minVal)<=parseFloat(value.resultVal) &&  parseFloat(value.maxVal)>=parseFloat(value.resultVal))
		  {
			value.inspResult="合格";
		  }else
		  value.inspResult="不合格";
	   }
	   else{
		value.inspResult="合格";
	   }
	}
		
	}
	else{
		nextTick(() => {
			value.resultVal = undefined;
			value.inspResult = undefined;
        });
	}
    }
		//值
	// const handleInput1=(value:any) =>{
    //   // 允许输入的字符串
	//   const allowedValues = [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9','.','-','－','．', '０', '１', '２', '３', '４', '５', '６', '７', '８', '９'];
    //   // 当前输入的字符
    //   const currentChar = value.resultVal.slice(-1);
	//   if(value.itemProps==2)
	//   {
	// 	nextTick(() => {
	// 		//value.resultVal = value.resultVal.slice(0, -1);
	// 		value.resultVal=undefined;
    //     });
		
	//   }
	//   else{
		
    //   // 判断当前字符是否在允许的字符串内
    //   if (allowedValues.includes(currentChar)) {
	// 	value.inspResult=undefined;
    //          // 允许输入
    //          value.resultVal1 = ToCDB(value.resultVal1);
    //        } else {
    //          // 不允许输入，将输入值回退到上一个合法状态
    //          nextTick(() => {
    //  			//value.resultVal = value.resultVal.slice(0, -1);
	// 			 value.resultVal=undefined;
    //          });
    //   }
	//   }
    // }
const isDisabled=(value:any)=>{
    if(value=="定量")
	{
		return value === "定量";
	}
}
const isDisableds=(value:any)=>{
    if(value=="定性")
	{
		return value === "定性";
	}
}	
	

//全角转半角
const ToCDB=(str:any) =>{
        // 全角转半角
        var tmp = void 0;
        if (str) {
            tmp = '';
            for (var i = 0; i < str.length; i++) {
                if (str.charCodeAt(i) == 12288) {
                    tmp += String.fromCharCode(32);
                } else if (str.charCodeAt(i) == 160) {
                    tmp += String.fromCharCode(32); // 处理下Unicode为160的特殊空格
                } else if (str.charCodeAt(i) > 65248 && str.charCodeAt(i) < 65375) {
                    tmp += String.fromCharCode(str.charCodeAt(i) - 65248);
                } else {
                    tmp += str[i];
                }
            }
        } else {
            tmp = str;
        }
        return tmp;
      }


	//将属性或者函数暴露给父组件
	defineExpose({ openDialog });
</script>




