﻿<template>
	<div class="mesknifestorerecord-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="部门编号" prop="departmentCode">
							<el-input v-model="state.ruleForm.departmentCode" placeholder="请输入部门编号" 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="departmentName">
							<el-input v-model="state.ruleForm.departmentName" placeholder="请输入部门名称" 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="knifeCode">
							<el-input v-model="state.ruleForm.knifeCode" placeholder="请输入刀具编号" 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="knifeName">
							<el-input v-model="state.ruleForm.knifeName" placeholder="请输入刀具名称" 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="knifeModel">
							<el-input v-model="state.ruleForm.knifeModel" placeholder="请输入刀具型号" readonly="true"  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="workShopCode">
							<el-input v-model="state.ruleForm.workShopCode" placeholder="请输入车间编号" 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="workShopName">
							<el-input v-model="state.ruleForm.workShopName" placeholder="请输入车间名称" 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="neckPeopleCode">
							<el-input v-model="state.ruleForm.neckPeopleCode" 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="neckPeople">
							<el-input v-model="state.ruleForm.neckPeople" 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="manageName">
							<el-input v-model="state.ruleForm.manageName" @click="chooseProduct()" placeholder="请输入领刀人"
								maxlength="10" 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" 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="knfneckNumber">
							<el-input-number v-model="knfneckNumber"  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="num">
							<el-input-number v-model="state.ruleForm.num" placeholder="请输入领用数量" :precision="0" 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="请输入排序" 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="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>
		<materialDialog title="选择人员" ref="materialDialogRef" :isMore="false" @finish="onChooseFinish"></materialDialog>
	</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 { addMesknifestorerecord, updateMesknifestorerecord, detailMesknifestorerecord ,batchsaveMesknifestore,Scan} from "/@/api/simple/mesknifestorerecord";
	import { pageMesknifestorerecord, deleteMesknifestorerecord } from '/@/api/simple/mesknifestorerecord';
	import materialDialog from '/@/views/simple/mesknifestorerecord/component/chooseDialog.vue';
	//import materialDialog from "/@/businessComponents/chooseUser.vue";
	import {Scans } from "/@/api/simple/mesknifestorerecord";
  	import { formatDate } from '/@/utils/formatTime';
	  const materialDialogRef = ref();
	  const knfneckNumber=ref(0);
	//父级传递来的参数
	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>({
		 manageName: [{required: true, message: '请选择领刀人！', trigger: 'blur',},],
		 num: [{required: true, message: '请输入领用数量！', trigger: 'blur',},],
		 typecode: [{required: true, message: '请选择领用类型！', trigger: 'blur',},],
	});

	const chooseProduct = async(row: any) => {
	// currentRow = row;
	let  res = await Scans({
        packCode:state.ruleForm.workShopName,
      })
    let data = res.data.result || {};
	console.log(data.id);
	materialDialogRef.value.openDialog({ codes: data.id });
}



// 产品选择完成
const onChooseFinish = async(e: any) => {
	//if (e.rows.length == 1) {
		const data = e;//e.rows[0];
		//console.log(data);
		state.ruleForm.manageName = data.nickName;
		state.ruleForm.neckPeople = data.nickName;
		state.ruleForm.neckPeopleCode = data.account;
		let  res = await Scan({
			userCode: data.account,
			knfineCode: state.ruleForm.knifeCode,
			workShopCode: state.ruleForm.workShopCode,
      });
      let date = res.data.result || {}; 
	  if(date.neckPeopleCode==undefined)
	  {
		knfneckNumber.value=0;
	  }
	  else{
		knfneckNumber.value=date.neckNumber;
	  }
	//}
	
}
	// 页面加载时
	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 detailMesknifestorerecord(rowData.id)).data.result;
		else
			state.ruleForm = rowData;

			knfneckNumber.value=0;
		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.manageName==undefined)
	        {
	    	  return ElMessage({
              message: `请选择领刀人！`,
              type: "error",
            });
	        }
			if(state.ruleForm.num==undefined)
	        {
	    	  return ElMessage({
              message: `请输入领用数量！`,
              type: "error",
            });
	        }
			if(state.ruleForm.typecode==undefined)
	        {
	    	  return ElMessage({
              message: `请选择领用类型！`,
              type: "error",
            });
	        }
			if(state.ruleForm.num==0)
	        {
	    	  return ElMessage({
              message: `领用数量不可以为0！`,
              type: "error",
            });
		    }
			if(state.ruleForm.num<0)
	        {
				if(state.ruleForm.num+knfneckNumber.value<0)
			    {
			    	return ElMessage({
                       message: `退刀数量不可以大于本人手中刀数！`,
                       type: "error",
                    });
			    }
		    }
			if(state.ruleForm.typecode=="换刀")
			{
				if(parseFloat(state.ruleForm.num)>knfneckNumber.value)
			    {
			    	return ElMessage({
                       message: `换刀数量不可以大于本人手中刀数！`,
                       type: "error",
                    });
			    }
			}
			if (isValid) {
				let values = state.ruleForm;
				if(state.ruleForm.num*1>state.ruleForm.neckNumber*1)
			    {
				     ElMessage({
					            message: `领用数量不可以大于现存数量`,
					            type: "error",
				    });
			    }
			    else{
			         //if (state.ruleForm.id == undefined || state.ruleForm.id == null || state.ruleForm.id == "" || state.ruleForm.id == 0) {
					     //await addMesknifestorerecord(values);
				     //} else {
					    //await updateMesknifestorerecord(values);
				     //}
					//  await batchsaveMesknifestore(values);
				    //  closeDialog();
					var result=(await batchsaveMesknifestore(values)).data.result;
					if(result=="添加成功")
					 closeDialog();
					else
					{
						ElMessage({
					    message: `${result}`,
					    type: "error",
				        });
					}
			       }
			} else {
				ElMessage({
					message: `表单有${Object.keys(fields).length}处验证失败，请修改后再提交`,
					type: "error",
				});
			}
		});
	};






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




