﻿<template>
	<div class="mesMaterialTestTaskDetial-container">
		<el-dialog v-model="state.isShowDialog" v-if="state.isShowDialog" :width="800" draggable="" :close-on-click-modal="false" :append-to-body="true">
			<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="检验任务id" prop="qiInspJobId">
							<el-input v-model="state.ruleForm.qiInspJobId" placeholder="请输入检验任务id" maxlength="0" 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="inspJobNo">
							<el-input v-model="state.ruleForm.inspJobNo" disabled="true" placeholder="请输入检验单号" maxlength="50" 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="qiItemCode">
							<el-input v-model="state.ruleForm.qiItemCode" disabled="true" 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="qiItemName">
							<el-input v-model="state.ruleForm.qiItemName" disabled="true" 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="inspType">
							<el-input v-model="state.ruleForm.inspType" placeholder="请输入检验方法" maxlength="150" 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="itemProps">
							<el-input v-model="state.ruleForm.itemProps" placeholder="请输入检验项目属性" 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="testRequire">
							<el-input v-model="state.ruleForm.testRequire" placeholder="请输入检验要求" disabled="true" maxlength="255" 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="standVal">
							<el-input v-model="state.ruleForm.standVal" disabled="true" placeholder="请输入标准值" maxlength="18" 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="maxVal">
							<el-input v-model="state.ruleForm.maxVal" disabled="true" placeholder="请输入上限值" maxlength="18" 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="minVal">
							<el-input v-model="state.ruleForm.minVal" disabled="true" placeholder="请输入下限值" maxlength="18" 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="samplingNum">
							<el-input v-model="state.ruleForm.samplingNum" disabled="true" placeholder="请输入取样数" maxlength="18" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" v-if="state.ruleForm.samplingNum>=1==true">
						<el-form-item label="检验结果值1" prop="resultVal1" >
							<el-input v-model="state.ruleForm.resultVal1" placeholder="请输入检验结果值" maxlength="18" @input="handleInput1" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" v-if="state.ruleForm.samplingNum>=2==true">
						<el-form-item label="检验结果值2" prop="resultVal2" >
							<el-input v-model="state.ruleForm.resultVal2" placeholder="请输入检验结果值" maxlength="18" @input="handleInput2" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" v-if="state.ruleForm.samplingNum>=3==true">
						<el-form-item label="检验结果值3" prop="resultVal3">
							<el-input v-model="state.ruleForm.resultVal3" placeholder="请输入检验结果值" maxlength="18" @input="handleInput3" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" v-if="state.ruleForm.samplingNum>=4==true">
						<el-form-item label="检验结果值4" prop="resultVal4">
							<el-input v-model="state.ruleForm.resultVal4" placeholder="请输入检验结果值" maxlength="18" @input="handleInput4" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" v-if="state.ruleForm.samplingNum>=5==true"> 
						<el-form-item label="检验结果值5" prop="resultVal5">
							<el-input v-model="state.ruleForm.resultVal5" placeholder="请输入检验结果值" maxlength="18" @input="handleInput5" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" v-if="state.ruleForm.samplingNum<1==true">
						<el-form-item label="结论" prop="inspResult">
							<el-select clearable v-model="state.ruleForm.inspResult" placeholder="请选择结论" >
								<el-option v-for="(item,index) in dl('materialstate')"  :key="index" :value="item.code"  :label="`[${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="sortCode">
							<el-input-number v-model="state.ruleForm.sortCode" placeholder="请输入排序" 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="remark">
							<el-input v-model="state.ruleForm.remark" placeholder="请输入描述" maxlength="255" 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>
		</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 { ElMessage } from "element-plus";
	import type { FormRules } from "element-plus";
	import { addMesMaterialTestTaskDetial, updateMesMaterialTestTaskDetial, detailMesMaterialTestTaskDetial } from "/@/api/simple/mesMaterialTestTaskDetial";
	import { pageMesMaterialTestTaskDetial, deleteMesMaterialTestTaskDetial } from '/@/api/simple/mesMaterialTestTaskDetial';
	import { getDictDataItem as di, getDictDataList as dl } from '/@/utils/dict-utils';
  	import { formatDate } from '/@/utils/formatTime';
	  import { nextTick } from "vue";
	//父级传递来的参数
	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>({
	});

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

	});

	// 打开弹窗
	const openDialog = async (row: any) => {
		// state.ruleForm = JSON.parse(JSON.stringify(row));
		// 改用detail获取最新数据来编辑
		let rowData = JSON.parse(JSON.stringify(row));
		if (rowData.id)
			state.ruleForm = (await detailMesMaterialTestTaskDetial(rowData.id)).data.result;
		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(state.ruleForm.qiItemName=="芯板")
			    {
			    	return ElMessage({
                     message: `芯板不需要进行检验！`,
                     type: "error",
                    });
			    }
				state.ruleForm.resultVal1= ToCDB(state.ruleForm.resultVal1);
				state.ruleForm.resultVal2= ToCDB(state.ruleForm.resultVal2);
				state.ruleForm.resultVal3= ToCDB(state.ruleForm.resultVal3);
				state.ruleForm.resultVal4= ToCDB(state.ruleForm.resultVal4);
				state.ruleForm.resultVal5= ToCDB(state.ruleForm.resultVal5);
				console.log(state.ruleForm.resultVal1);
				console.log(state.ruleForm.resultVal2);
				console.log(state.ruleForm.resultVal3);
				console.log(state.ruleForm.resultVal4);
				console.log(state.ruleForm.resultVal5);
				if(state.ruleForm.resultVal1==undefined)
				{
					state.ruleForm.resultVal1=0;
				}
				if(state.ruleForm.resultVal2==undefined)
				{
					state.ruleForm.resultVal2=0;
				}
				if(state.ruleForm.resultVal3==undefined)
				{
					state.ruleForm.resultVal3=0;
				}
				if(state.ruleForm.resultVal4==undefined)
				{
					state.ruleForm.resultVal4=0;
				}
				if(state.ruleForm.resultVal5==undefined)
				{
					state.ruleForm.resultVal5=0;
				}
			if (isValid) {
			 	let values = state.ruleForm;
			 	if (state.ruleForm.id == undefined || state.ruleForm.id == null || state.ruleForm.id == "" || state.ruleForm.id == 0) {
			 		await addMesMaterialTestTaskDetial(values);closeDialog();
			 	} else {
			 		//await updateMesMaterialTestTaskDetial(values);
			 		var result=(await updateMesMaterialTestTaskDetial(values)).data.result;
			 		if(result=="修改成功")
			 		 closeDialog();
			 		else
			 		{
			 		  ElMessage({
			 		    message: `${result}`,
			 		    type: "error",
			 	       });
			 		}
			 	}
			
			 } else {
			 	ElMessage({
			 		message: `表单有${Object.keys(fields).length}处验证失败，请修改后再提交`,
			 		type: "error",
			 	});
			 }
		});
	};
	//值1
	const handleInput1=(value:any) =>{
      // 允许输入的字符串
	  const allowedValues = ['o','O','k','K','N','G','n','g','ｏ','Ｏ','ｋ','Ｋ','Ｎ','Ｇ','ｎ','ｇ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9','.','-','－','．', '０', '１', '２', '３', '４', '５', '６', '７', '８', '９'];
      // 当前输入的字符
      const currentChar = value.slice(-1);
      // 判断当前字符是否在允许的字符串内
      if (allowedValues.includes(currentChar)) {
        // 允许输入
        state.ruleForm.resultVal1 = value.toUpperCase();
      } else {
        // 不允许输入，将输入值回退到上一个合法状态
        nextTick(() => {
			state.ruleForm.resultVal1 = state.ruleForm.resultVal1.slice(0, -1);
        });
      }
    }

//值2
const handleInput2=(value:any) =>{
      // 允许输入的字符串
	  const allowedValues = ['o','O','k','K','N','G','n','g','ｏ','Ｏ','ｋ','Ｋ','Ｎ','Ｇ','ｎ','ｇ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9','.','-','－','．', '０', '１', '２', '３', '４', '５', '６', '７', '８', '９'];
      // 当前输入的字符
      const currentChar = value.slice(-1);
      // 判断当前字符是否在允许的字符串内
      if (allowedValues.includes(currentChar)) {
        // 允许输入
        state.ruleForm.resultVal2 = value.toUpperCase();
      } else {
        // 不允许输入，将输入值回退到上一个合法状态
        nextTick(() => {
			state.ruleForm.resultVal2 = state.ruleForm.resultVal2.slice(0, -1);
        });
      }
    }

	//值3
	const handleInput3=(value:any) =>{
      // 允许输入的字符串
	  const allowedValues = ['o','O','k','K','N','G','n','g','ｏ','Ｏ','ｋ','Ｋ','Ｎ','Ｇ','ｎ','ｇ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9','.','-','－','．', '０', '１', '２', '３', '４', '５', '６', '７', '８', '９'];
      // 当前输入的字符
      const currentChar = value.slice(-1);
      // 判断当前字符是否在允许的字符串内
      if (allowedValues.includes(currentChar)) {
        // 允许输入
        state.ruleForm.resultVal3 = value.toUpperCase();
      } else {
        // 不允许输入，将输入值回退到上一个合法状态
        nextTick(() => {
			state.ruleForm.resultVal3 = state.ruleForm.resultVal3.slice(0, -1);
        });
      }
    }

	//值4
	const handleInput4=(value:any) =>{
      // 允许输入的字符串
	  const allowedValues = ['o','O','k','K','N','G','n','g','ｏ','Ｏ','ｋ','Ｋ','Ｎ','Ｇ','ｎ','ｇ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9','.','-','－','．', '０', '１', '２', '３', '４', '５', '６', '７', '８', '９'];
      // 当前输入的字符
      const currentChar = value.slice(-1);
      // 判断当前字符是否在允许的字符串内
      if (allowedValues.includes(currentChar)) {
        // 允许输入
        state.ruleForm.resultVal4 = value.toUpperCase();
      } else {
        // 不允许输入，将输入值回退到上一个合法状态
        nextTick(() => {
			state.ruleForm.resultVal4 = state.ruleForm.resultVal4.slice(0, -1);
        });
      }
    }

	//值5
	const handleInput5=(value:any) =>{
      // 允许输入的字符串
	  const allowedValues = ['o','O','k','K','N','G','n','g','ｏ','Ｏ','ｋ','Ｋ','Ｎ','Ｇ','ｎ','ｇ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9','.','-','－','．', '０', '１', '２', '３', '４', '５', '６', '７', '８', '９'];
      // 当前输入的字符
      const currentChar = value.slice(-1);
      // 判断当前字符是否在允许的字符串内
      if (allowedValues.includes(currentChar)) {
        // 允许输入
        state.ruleForm.resultVal5 = value.toUpperCase();
      } else {
        // 不允许输入，将输入值回退到上一个合法状态
        nextTick(() => {
			state.ruleForm.resultVal5 = state.ruleForm.resultVal5.slice(0, -1);
        });
      }
    }

	//全角转半角
	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;
      }

	//   const ToDBC=(txtstring:any)=> {
    //     // 半角转全角
    //     if (txtstring == null || txtstring == '' || txtstring == ' ') {
    //         return '';
    //     }
    //     var tmp = '';
    //     for (var i = 0; i < txtstring.length; i++) {
    //         if (txtstring.charCodeAt(i) == 32) {
    //             tmp += String.fromCharCode(12288);
    //         } else if (txtstring.charCodeAt(i) == 160) {
    //             // 处理下Unicode为160的特殊空格
    //             tmp += String.fromCharCode(12288);
    //         } else if (txtstring.charCodeAt(i) < 127) {
    //             tmp += String.fromCharCode(txtstring.charCodeAt(i) + 65248);
    //         } else {
    //             tmp += String.fromCharCode(txtstring.charCodeAt(i));
    //         }
    //     }
    //     return tmp;
    //   }

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




