<template>
  <el-drawer
    v-model="visible"
    title="新建文件夹"
    :size="400"
    :close-on-click-modal="false"
    @close="closeFun"
  >
    <div class="content">
      <TableForm ref="tableFormRef" tableName="DATA_OVERVIEW_ADD_DIR" />
    </div>
    <template #footer>
      <div>
        <el-button @click="closeFun">关闭</el-button>
        <el-button type="primary" @click="submitFun">确定</el-button>
      </div>
    </template>
  </el-drawer>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue';
import { ElMessage } from 'element-plus';
import TableForm from '@/components/common/table/tableForm.vue';
import { dataOverViewCreateSimulationNodeDirApi } from '@/api/data/dataOverView';

const emit = defineEmits(['update:modelValue', 'submit']);
const visible = ref(false);
const tableFormRef = ref<any>();

interface Props {
  modelValue: boolean;
  data: any;
}

const props = withDefaults(defineProps<Props>(), {
  modelValue: false,
  data: {},
});

watch(() => props.modelValue, (val: boolean) => {
  visible.value = val;
  if (!val) {
    if (tableFormRef.value) {
      tableFormRef.value.resetFun();
    }
  }
});

const submitFun = async() => {
  const valid = await tableFormRef.value.validateFun();
  if (valid) {
    const fromData = tableFormRef.value.getFormDataFun();
    const params = {
      parDirId: props.data.id,
      ...fromData,
    };
    dataOverViewCreateSimulationNodeDirApi(params).then((res: any) => {
      if (res.code === 200) {
        ElMessage.success('操作成功');
        closeFun();
        emit('submit');
      }
    });
  }
};

const closeFun = () => {
  emit('update:modelValue', false);
};
</script>
