﻿<template>
	<div class="mesmaterialtesttemplatedetial-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="原材料检验模板主键" prop="headId">
							<el-input v-model="state.ruleForm.headId" placeholder="请输入原材料检验模板主键" 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="qiItemCode">
							<el-input v-model="state.ruleForm.qiItemCode" 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="qiItemCode">
							<el-select clearable filterable v-model="state.ruleForm.qiItemCode" placeholder="请选择检验项" @change="onItemCodeChange">
								<el-option v-for="(item,index) in mesQiItemQiItemCodeDropdownList" :key="index" :value="item.value" :label="item.label" />

							</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="qiItemName">
                           <el-input v-model="state.ruleForm.qiItemName" placeholder="请选择检验项" readonly="true" maxlength="100" show-word-limit clearable >
                               <template #append v-if="props.title=='编辑原材料检验模板主表'===false">
                                  <el-button @click="openChoose">选择</el-button>
                               </template>
                            </el-input>
						</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" 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="samplingNum">
							<el-input-number v-model="state.ruleForm.samplingNum" :precision="0"  min="0" max="5"  placeholder="请输入抽样数" clearable />
							
						</el-form-item>
						
					</el-col>

					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" v-if="itemProps!='2'">
						<el-form-item label="标准值" prop="standVal">
							<el-input-number v-model="state.ruleForm.standVal" placeholder="请输入标准值" clearable />

						</el-form-item>

					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" v-if="itemProps!='2'">
						<el-form-item label="上限值" prop="maxVal">
							<el-input-number v-model="state.ruleForm.maxVal" placeholder="请输入上限值" clearable />

						</el-form-item>

					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" v-if="itemProps!='2'">
						<el-form-item label="下限值" prop="minVal">
							<el-input-number v-model="state.ruleForm.minVal" 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="isShemistry">
							<el-switch v-model="state.ruleForm.isShemistry"  active-text="是" inactive-text="否" />
							
						</el-form-item>
						
					</el-col>
					<!-- <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="必填" prop="isMust">
							<el-switch v-model="state.ruleForm.isMust" active-text="是" inactive-text="否" :active-value="1" :inactive-value="0"  />

						</el-form-item>

					</el-col> -->
					<!-- <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="默认结论填充项" prop="defaultResult">
							<el-input v-model="state.ruleForm.defaultResult" 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="sortCode">
							<el-input v-model="state.ruleForm.sortCode" 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
							type="textarea" :rows="4"
							v-model="state.ruleForm.testRequire" placeholder="请输入检验要求" maxlength="500" 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="remark">
							<el-input type="textarea" :rows="4" v-model="state.ruleForm.remark" maxlength="500" />
							
						</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>
		<!--  选择检验项  -->
		<choose-qiItem title="选择原材料" ref="chooseMaterialRef"  :is-any="false" @finish="onChooseMaterialFinish"></choose-qiItem>
	</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 { addMesmaterialtesttemplatedetial, updateMesmaterialtesttemplatedetial, detailMesmaterialtesttemplatedetial } from "/@/api/simple/mesmaterialtesttemplatedetial";
	import { pageMesmaterialtesttemplatedetial, deleteMesmaterialtesttemplatedetial } from '/@/api/simple/mesmaterialtesttemplatedetial';
	import { getMesQiItemQiItemCodeDropdown } from '/@/api/mes/mesQiInspTempItem';
	import ChooseQiItem from "/@/views/simple/mesmaterialtesttemplatedetial/component/chooseQuinitem.vue";
  	import { formatDate } from '/@/utils/formatTime';
    import { stat } from "fs";

	//父级传递来的参数
	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>({
		qiItemName: [{required: true, message: '请选择检验项目！', trigger: 'blur',},],
		testRequire: [{required: true, message: '检验要求不可以为空！', trigger: 'blur',},],
	});

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

	});

	// 打开弹窗
	const openDialog = async (row: any) => {
		// state.ruleForm = JSON.parse(JSON.stringify(row));
		// 改用detail获取最新数据来编辑
		getMesQiItemQiItemCodeDropdownList();
		let rowData = JSON.parse(JSON.stringify(row));
		if (rowData.id)
	    {
	    	state.ruleForm = (await detailMesmaterialtesttemplatedetial(rowData.id)).data.result;
			state.ruleForm.isMust=rowData.isMust
	    }
		else
		{
			state.ruleForm = rowData;
			state.ruleForm.samplingNum=3;
			state.ruleForm.isMust=true;
		}
		onItemCodeChange();
		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) {
				let values = state.ruleForm;
				
				if(mesQiItemQiItemCodeDropdownList.value.find((u:any)=> { return u.value== state.ruleForm.qiItemCode })){
                    const values=mesQiItemQiItemCodeDropdownList.value.find((u:any)=> { return u.value== state.ruleForm.qiItemCode });
					state.ruleForm.qiItemName=values.label;
                }
				if(state.ruleForm.qiItemName==undefined)
			        {
			    	   return ElMessage({
                         message: `检验项不可以为空`,
                         type: "error",
                        });
			        }
				if(state.ruleForm.qiItemName=="外观检查")
				{
					if(state.ruleForm.testRequire==undefined)
			        {
			    	   return ElMessage({
                         message: `检验要求不可以为空`,
                         type: "error",
                        });
			        }
				}
				else
				{
					if(state.ruleForm.testRequire==undefined)
			        {
			    	   return ElMessage({
                         message: `检验要求不可以为空`,
                         type: "error",
                        });
			        }
					if(state.ruleForm.samplingNum==undefined)
			        {
			    	   return ElMessage({
                         message: `抽样数不可以为空`,
                         type: "error",
                        });
			        }
					// if(state.ruleForm.standVal==undefined)
			        // {
			    	//    return ElMessage({
                    //      message: `标准值不可以为空`,
                    //      type: "error",
                    //     });
			        // }
					// if(state.ruleForm.maxVal==undefined)
			        // {
			    	//    return ElMessage({
                    //      message: `上限值不可以为空`,
                    //      type: "error",
                    //     });
			        // }
					// if(state.ruleForm.minVal==undefined)
			        // {
			    	//    return ElMessage({
                    //      message: `下限值不可以为空`,
                    //      type: "error",
                    //     });
			        // }
					if(state.ruleForm.minVal!=undefined && state.ruleForm.maxVal!=undefined)
					{
						if(parseFloat(state.ruleForm.minVal)>parseFloat(state.ruleForm.maxVal))
					   {
						return ElMessage({
							message: `下限值不可以大于上限值`,
							type: "error",
						});
					   }
					}
				}
			    if (state.ruleForm.id == undefined || state.ruleForm.id == null || state.ruleForm.id == "" || state.ruleForm.id == 0) {
			    	//await addMesmaterialtesttemplatedetial(values);
					var result=(await addMesmaterialtesttemplatedetial(values)).data.result;
					if(result=="添加成功")
					 closeDialog();
					else
					{
					  ElMessage({
					    message: `${result}`,
					    type: "error",
				       });
					}
			    } else {
			    	await updateMesmaterialtesttemplatedetial(values);
					closeDialog();
			    }
			    
			} else {
				ElMessage({
					message: `表单有${Object.keys(fields).length}处验证失败，请修改后再提交`,
					type: "error",
				});
			}
		});
	};

	const mesQiItemQiItemCodeDropdownList = ref<any>([]);
	const getMesQiItemQiItemCodeDropdownList = async () => {
		let list = await getMesQiItemQiItemCodeDropdown();
		mesQiItemQiItemCodeDropdownList.value = list.data.result ?? [];
	};

	const itemProps = ref('');
    const onItemCodeChange=()=>{
	var temp = mesQiItemQiItemCodeDropdownList.value.find((u:any)=>u.value==state.ruleForm.qiItemCode)
	if(temp){
		if(temp.itemProps=='1'){
			rules.value = {
				headId: [{required: false, message: '', trigger: 'blur',},],
				qiItemName: [{required: true, message: '请选择检验项目！', trigger: 'blur',},],
	        	testRequire: [{required: true, message: '检验要求不可以为空！', trigger: 'blur',},],
			};
		}else{
			rules.value = {
				headId: [{required: true, message: '请输入原材料检验项模板！', trigger: 'blur',},],
				qiItemName: [{required: true, message: '请选择检验项目！', trigger: 'blur',},],
	        	testRequire: [{required: true, message: '检验要求不可以为空！', trigger: 'blur',},],
			};
			state.ruleForm.samplingNum=undefined;
		}
		itemProps.value = temp.itemProps;
		if(props.title!="编辑原材料检验项目及标准")
		ruleFormRef.value.clearValidate();
	}
	
	if(state.ruleForm.id==undefined)
	{
		rules.value = {
				headId: [{required: true, message: '', trigger: 'blur',},],
				qiItemName: [{required: true, message: '请选择检验项目！', trigger: 'blur',},],
	        	testRequire: [{required: true, message: '检验要求不可以为空！', trigger: 'blur',},],
			};
	}
	
	
}

const chooseMaterialRef = ref();
    const openChoose = ()=>{
      chooseMaterialRef.value.openDialog({})
    }
    const onChooseMaterialFinish = (row)=>{
      state.ruleForm.qiItemName = row?.itemName;
      state.ruleForm.qiItemCode = row?.itemCode;
	  onItemCodeChange();
    }


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




