﻿<template>
	<div class="mesProduceOrderProduct-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="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="approveStatus">
							<el-input-number v-model="state.ruleForm.approveStatus" placeholder="请输入审批状态" clearable />
							
						</el-form-item>
						
					</el-col> -->
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20"></el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="物料编码" prop="materialCode">
							<el-input v-model="state.ruleForm.materialCode" 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="materialName">
							<el-input v-model="state.ruleForm.materialName" 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="customerCode">
							<el-input v-model="state.ruleForm.customerCode" 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="customerName">
							<el-input v-model="state.ruleForm.customerName" 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="materialNo">
							<el-input v-model="state.ruleForm.materialNo" 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="specs">
							<el-input v-model="state.ruleForm.specs" 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="num">
							<el-input-number v-model="state.ruleForm.num" 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="MRP净算量" prop="mrpNum">
							<el-input-number v-model="state.ruleForm.mrpNum" placeholder="请输入MRP净算量" 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="alreadyNum">
							<el-input-number v-model="state.ruleForm.alreadyNum" 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="finishNum">
							<el-input-number v-model="state.ruleForm.finishNum" 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="typeIntro">
							<el-input v-model="state.ruleForm.typeIntro" placeholder="请输入类别说明" maxlength="400" 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="workStartDate">
							<el-date-picker v-model="state.ruleForm.workStartDate" type="date" placeholder="开工日期" />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="完工日期" prop="workEndDate">
							<el-date-picker v-model="state.ruleForm.workEndDate" type="date" placeholder="完工日期" />
							
						</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 { addMesProduceOrderProduct, updateMesProduceOrderProduct, detailMesProduceOrderProduct } from "/@/api/mes/mesProduceOrderProduct";
	import { pageMesProduceOrderProduct, deleteMesProduceOrderProduct } from '/@/api/mes/mesProduceOrderProduct';

  	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>({
		produceOrderNo: [{required: true, message: '请输入生产订单号！', trigger: 'blur',},],
		approveStatus: [{required: true, message: '请输入审批状态！', trigger: 'blur',},],
		materialCode: [{required: true, message: '请输入物料编码！', trigger: 'blur',},],
		num: [{required: true, message: '请输入生产数量！', trigger: 'blur',},],
		mrpNum: [{required: true, message: '请输入MRP净算量！', trigger: 'blur',},],
		alreadyNum: [{required: true, message: '请输入已投产量！', trigger: 'blur',},],
		finishNum: [{required: true, message: '请输入已完成数量！', trigger: 'blur',},],
	});

	// 页面加载时
	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 detailMesProduceOrderProduct(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 (isValid) {
				let values = state.ruleForm;
				if (state.ruleForm.id == undefined || state.ruleForm.id == null || state.ruleForm.id == "" || state.ruleForm.id == 0) {
					await addMesProduceOrderProduct(values);
				} else {
					await updateMesProduceOrderProduct(values);
				}
				closeDialog();
			} else {
				ElMessage({
					message: `表单有${Object.keys(fields).length}处验证失败，请修改后再提交`,
					type: "error",
				});
			}
		});
	};






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




