﻿<template>
	<div class="mesknifestore-container">
		<el-dialog v-model="state.isShowDialog" v-if="state.isShowDialog" :width="800" 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="U8单据号" prop="cCode">
							<el-input v-model="state.ruleForm.cCode" placeholder="请输入u8单据号" readonly="true" 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="U8单据日期" prop="dDate">
							<el-date-picker v-model="state.ruleForm.dDate" readonly="true" type="datetime" placeholder="U8单据日期" />
							
						</el-form-item>
						
					</el-col>
					<!-- <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="当前入库人编号" prop="peopleCode">
							<el-input v-model="state.ruleForm.peopleCode" readonly="true"  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="peopleName">
							<el-input v-model="state.ruleForm.peopleName" readonly="true"  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="departmentCode">
							<el-input v-model="state.ruleForm.departmentCode"   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="departmentName">
							<el-input v-model="state.ruleForm.departmentName"  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="knifeCode">
							<el-input v-model="state.ruleForm.knifeCode" readonly="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="knifeName">
							<el-input v-model="state.ruleForm.knifeName" readonly="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="knifeModel">
							<el-input v-model="state.ruleForm.knifeModel" readonly="true" 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="number">
							<el-input-number v-model="state.ruleForm.number" readonly="true" placeholder="请输入数量"  clearable /><!--:precision="0" min="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="workShopCode">
							<el-input v-model="state.ruleForm.workShopCode" 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="workShopCode">
							<el-select clearable filterable v-model="state.ruleForm.workShopCode" placeholder="请选择车间" >
								<el-option v-for="(item,index) in mesQiItemQiItemCodeDropdownList" :key="index" :value="item.workShopCode" :label="item.workShopName" />

							</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="neckpeoplecode">
							<el-input v-model="state.ruleForm.neckpeoplecode"   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="neckpeople">
							<el-input v-model="state.ruleForm.neckpeople"   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="necknumber">
							<el-input-number v-model="state.ruleForm.necknumber"   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="typecode">
							<el-input v-model="state.ruleForm.typecode"   maxlength="50" show-word-limit clearable />
							<el-select v-model="state.ruleForm.typecode" placeholder="请选择" maxlength="50" show-word-limit clearable >
								<el-option value="领刀">领刀</el-option>
								<el-option 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"  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 { addMesknifestore, updateMesknifestore, detailMesknifestore,batchsaveMesknifestore } from "/@/api/simple/mesknifestore";
	import { pageMesknifestore, deleteMesknifestore } from '/@/api/simple/mesknifestore';
	import { getMesQiItemQiItemCodeDropdown } from '/@/api/simple/mesknifestore';
  	import { formatDate } from '/@/utils/formatTime';


	
	//父级传递来的参数
	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>({
		workShopCode: [{required: true, message: '请选择车间', trigger: 'blur',},],
		// cCode: [{required: true, message: '请输入u8单据号', trigger: 'blur',},],
		// dDate: [{required: true, message: '请输入u8单据日期！', trigger: 'blur',},],
		// knifeCode: [{required: true, message: '请输入刀具编号！', trigger: 'blur',},],
		// knifeName: [{required: true, message: '请输入刀具名称', trigger: 'blur',},],
		// knifeModel: [{required: true, message: '请输入刀具型号！', trigger: 'blur',},],
		// number: [{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 detailMesknifestore(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.cCode==undefined)
	        {
	    	  return ElMessage({
              message: `请输入u8单据号`,
              type: "error",
            });
	        }
			if(state.ruleForm.dDate==undefined)
	        {
	    	  return ElMessage({
              message: `请输入u8单据日期！`,
              type: "error",
            });
	        }
			if(state.ruleForm.knifeCode==undefined)
	        {
	    	  return ElMessage({
              message: `请输入刀具编号！`,
              type: "error",
            });
	        }
			if(state.ruleForm.knifeName==undefined)
	        {
	    	  return ElMessage({
              message: `请输入刀具名称`,
              type: "error",
            });
	        }
			if(state.ruleForm.knifeModel==undefined)
	        {
	    	  return ElMessage({
              message: `请输入刀具型号！`,
              type: "error",
            });
	        }
			if(state.ruleForm.number==undefined)
	        {
	    	  return ElMessage({
              message: `请输入数量！`,
              type: "error",
            });
	        }
			if(mesQiItemQiItemCodeDropdownList.value.find((u:any)=> { return u.workShopCode == state.ruleForm.workShopCode })){
                    const values=mesQiItemQiItemCodeDropdownList.value.find((u:any)=> { return u.workShopCode== state.ruleForm.workShopCode });
					state.ruleForm.workShopName=values.workShopName;
            }
				console.log(state.ruleForm.workShopCode);
				console.log(state.ruleForm.workShopName);
			if(state.ruleForm.workShopName==undefined)
	        {
	    	  return ElMessage({
              message: `请输入车间！`,
              type: "error",
            });
	        }
			if(state.ruleForm.number<=0)
	        {
	    	  return ElMessage({
              message: `数量不可以为0！`,
              type: "error",
            });
	        }
			if (isValid) {
				let values = state.ruleForm;
			   if (state.ruleForm.id == undefined || state.ruleForm.id == null || state.ruleForm.id == "" || state.ruleForm.id == 0) {
				 	await addMesknifestore(values);closeDialog();
				 } else {
				 	//await updateMesknifestore(values);
					 //await batchsaveMesknifestore(values);
					 var result=(await batchsaveMesknifestore(values)).data.result;
					if(result=="分配成功")
					 closeDialog();
					else
					{
						ElMessage({
					message: `${result}`,
					type: "error",
				});
					}
				 }
				//await batchsaveMesknifestore(values);
				
			} else {
				ElMessage({
					message: `表单有${Object.keys(fields).length}处验证失败，请修改后再提交`,
					type: "error",
				});
			}
		});
	};

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





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




