﻿<template>
	<div class="mesMaterial-container">
		<el-dialog v-model="isShowDialog" :width="800" draggable="">
			<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="ruleForm" ref="ruleFormRef" label-width="auto" :rules="rules">
				<el-row :gutter="35">
					<el-form-item v-show="false">
						<el-input v-model="ruleForm.id" />
					</el-form-item>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="MES编码" prop="materialCode">
							<el-input v-model="ruleForm.materialCode" placeholder="请输入MES编码" 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="MES名称" prop="materialName">
							<el-input v-model="ruleForm.materialName" placeholder="请输入MES名称" 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="materialTypeCode">
							<el-cascader :options="mesMaterialTypeTreeData"
								:props="{ checkStrictly: true, emitPath: false, value: 'materialTypeCode', label: 'materialTypeName' }"
								placeholder="请选择存货分类" clearable="" class="w100" v-model="ruleForm.materialTypeCode">
								<template #default="{ node, data }">
									<span>{{ data.materialTypeName }}</span>
									<span v-if="!node.isLeaf"> ({{ data.children.length }}) </span>
								</template>
							</el-cascader>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="存货类型" prop="productType">
							<el-select  v-model="ruleForm.productType" >
								<el-option label="原材料" :value="0"></el-option>
								<el-option label="半成品" :value="1"></el-option>
								<el-option label="产成品" :value="2"></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="specs">
							<el-input v-model="ruleForm.specs" placeholder="请输入规格型号" maxlength="200" 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="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="unitGroupName">
							<el-input v-model="ruleForm.unitGroupName" 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="unitName">
							<el-input v-model="ruleForm.unitName" 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="workHour">
							<el-input-number v-model="ruleForm.workHour" placeholder="请输入工时" :min="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="unitWeightLength">
							<el-input v-model="ruleForm.unitWeightLength" 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="inOutRecord">
							<el-input v-model="ruleForm.inOutRecord" placeholder="请输入出库跟踪入库" maxlength="300" 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="ABC分类" prop="aBCType">
							<el-input v-model="ruleForm.aBCType" placeholder="请输入ABC分类" 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="featureSelection">
							<el-input v-model="ruleForm.featureSelection" 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="activeDate">
							<el-date-picker v-model="ruleForm.activeDate" 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="disabledDate">
							<el-date-picker v-model="ruleForm.disabledDate" 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="mustNodes">
							<el-select  v-model="ruleForm.mustNodes" multiple>
								<el-option label="Bom" value="HasBom"></el-option>
								<el-option label="工艺路线" value="HasProcess"></el-option>
								<el-option label="工艺文件" value="HasCraftFile"></el-option>
								<el-option label="PFMEA" value="HasPFmea"></el-option>
								<el-option label="控制计划" value="HasControlPlan"></el-option>
								<el-option label="特殊清单" value="HasTs"></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="price">
							<el-input-number v-model="ruleForm.price" placeholder="请输入单价" 
								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 scoped>
:deep(.el-select),
:deep(.el-input-number) {
	width: 100%;
}
</style>
<script lang="ts" setup>
import { ref, onMounted } from "vue";
import { getDictDataItem as di, getDictDataList as dl } from '/@/utils/dict-utils';
import { ElMessage } from "element-plus";
import type { FormRules } from "element-plus";
import { addMesMaterial, updateMesMaterial, detailMesMaterial } from "/@/api/mes/mesMaterial";
import { getMesMaterialTypeTree } from '/@/api/mes/mesMaterial';

//父级传递来的参数
var props = defineProps({
	title: {
		type: String,
		default: "",
	},
});
//父级传递来的函数，用于回调
const emit = defineEmits(["reloadTable"]);
const ruleFormRef = ref();
const isShowDialog = ref(false);
const ruleForm = ref<any>({});
//自行添加其他规则
const rules = ref<FormRules>({
	materialCode: [{ required: true, message: '请输入MES编码！', trigger: 'blur', },],
	materialName: [{ required: true, message: '请输入MES名称！', trigger: 'blur', },],
	materialTypeCode: [{ required: true, message: '请选择存货分类！', trigger: 'blur', },],
	productType: [{ required: true, message: '请选择存货类型！', trigger: 'blur', },],
});

// 打开弹窗
const openDialog = async (row: any) => {
	// ruleForm.value = JSON.parse(JSON.stringify(row));
	// 改用detail获取最新数据来编辑
	let rowData = JSON.parse(JSON.stringify(row));
	getMesMaterialTypeTreeData();

	if (rowData.id){
		ruleForm.value = (await detailMesMaterial(rowData.id)).data.result;
		if(ruleForm.value.mustNodes){
			ruleForm.value.mustNodes = ruleForm.value.mustNodes.split(',')
		}
	}
	else
		ruleForm.value = rowData;
	isShowDialog.value = true;
};

// 关闭弹窗
const closeDialog = () => {
	emit("reloadTable");
	isShowDialog.value = false;
};

// 取消
const cancel = () => {
	isShowDialog.value = false;
};

// 提交
const submit = async () => {
	ruleFormRef.value.validate(async (isValid: boolean, fields?: any) => {
		if (isValid) {
			let values = JSON.parse(JSON.stringify( ruleForm.value));
			if(values.mustNodes) values.mustNodes = values.mustNodes.join(',')
			if (ruleForm.value.id == undefined || ruleForm.value.id == null || ruleForm.value.id == "" || ruleForm.value.id == 0) {
				await addMesMaterial(values);
			} else {
				await updateMesMaterial(values);
			}
			closeDialog();
		} else {
			ElMessage({
				message: `表单有${Object.keys(fields).length}处验证失败，请修改后再提交`,
				type: "error",
			});
		}
	});
};


const mesMaterialTypeTreeData = ref<any>([]);
const getMesMaterialTypeTreeData = async () => {
	let list = await getMesMaterialTypeTree();
	mesMaterialTypeTreeData.value = list.data.result ?? [];
};

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

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




