﻿<template>
  <div class="mesMaterialType-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="存货分类编码" prop="materialTypeCode">
              <el-input v-model="ruleForm.materialTypeCode" 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="materialTypeName">
              <el-input v-model="ruleForm.materialTypeName" 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="productType">
              <el-select clearable v-model="ruleForm.productType" placeholder="请选择分类">
                <el-option v-for="(item, index) in dl('mes_materialtype_type')" :key="index" :value="item.code" :label="`${item.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="parentMaterialTypeCode">
              <el-cascader :options="mesMaterialTypeTreeData" :props="{ checkStrictly: true, emitPath: false, value: 'materialTypeCode', label: 'materialTypeName' }" placeholder="请选择上级存货分类编码" clearable="" class="w100" v-model="ruleForm.parentMaterialTypeCode">
                <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="是否同步U8数据" prop="isSyncData">
              <el-select clearable v-model="ruleForm.isSyncData" placeholder="请选择是否同步U8数据">
                <el-option v-for="(item, index) in  ifStatus" :key="index" :value="item.value" :label="item.label"></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="productTypeCode">
              <el-select clearable v-model="ruleForm.productTypeCode" placeholder="请选择产品分类">
                <el-option v-for="(item, index) in mesProductTypeDropdownList" :key="index" :value="item.productTypeCode" :label="`${item.productTypeName}`"></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="sort">
              <el-input-number v-model="ruleForm.sort" placeholder="请输入排序" 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 { addMesMaterialType, updateMesMaterialType, detailMesMaterialType } from '/@/api/mes/mesMaterialType';
import { getMesMaterialTypeTree } from '/@/api/mes/mesMaterialType';
// import { pageMesProductType } from '/@/api/mes/mesProductType';

//父级传递来的参数
var props = defineProps({
	title: {
		type: String,
		default: '',
	},
});
//父级传递来的函数，用于回调
const emit = defineEmits(['reloadTable']);
const ruleFormRef = ref();
const isShowDialog = ref(false);
const ruleForm = ref<any>({});
const dataRow = ref<any>({});
const ifStatus = ref([
	{ label: '否', value: 0 },
	{ label: '是', value: 1 },
]);
//自行添加其他规则
const rules = ref<FormRules>({
	materialTypeCode: [{ required: true, message: '请输入存货分类编码！', trigger: 'blur' }],
	materialTypeName: [{ 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));
	dataRow.value = rowData;
	getMesMaterialTypeTreeData();
  // getMesProductTypeDropdownList();
	if (rowData.id) {
		ruleForm.value = (await detailMesMaterialType(rowData.id)).data.result;
	} else {
		ruleForm.value = rowData;
		ruleForm.value.isSyncData = 1;
	}
	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 = ruleForm.value;
			if (ruleForm.value.id == undefined || ruleForm.value.id == null || ruleForm.value.id == '' || ruleForm.value.id == 0) {
				await addMesMaterialType(values);
			} else {
				await updateMesMaterialType(values);
			}
			closeDialog();
		} else {
			ElMessage({
				message: `表单有${Object.keys(fields).length}处验证失败，请修改后再提交`,
				type: 'error',
			});
		}
	});
};

const mesMaterialTypeTreeData = ref<any>([]);
const getMesMaterialTypeTreeData = async () => {
	let list = await getMesMaterialTypeTree();
	const recFunc = (_list: any, isDis: boolean) => {
		_list.forEach((el: any) => {
			var dis = false;
			if (isDis || el.id == dataRow.value.id) dis = true;
			el.disabled = dis;
			if (el.children) {
				recFunc(el.children, el.disabled);
			}
		});
	};
	if (dataRow.value && dataRow.value.id) recFunc(list.data.result, false);
	mesMaterialTypeTreeData.value = list.data.result ?? [];
};

// const mesProductTypeDropdownList = ref<any>([]);
// /**
//  * 异步获取Mes产品类型下拉列表的数据。
//  * 
//  * @returns {void} 本函数没有返回值，但它会更新名为mesProductTypeDropdownList的ref变量的值。
//  */
// const getMesProductTypeDropdownList = async () => {
// 	let list = await pageMesProductType({ page: 1, limit: 1000 });
// 	mesProductTypeDropdownList.value = list.data.result.items ?? [];
// };

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

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




