<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_SEARCH" />
    </div>
    <template #footer>
      <div>
        <el-button @click="closeFun">关闭</el-button>
        <el-button type="primary" @click="searchFun">搜索</el-button>
      </div>
    </template>
  </el-drawer>
</template>

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

const emit = defineEmits(['update:modelValue', 'search']);
const tableFormRef = ref();
const visible = ref(false);

interface Props {
  modelValue: boolean;
  dirId: number;
}

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

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

const searchFun = async() => {
  const valid = await tableFormRef.value.validateFun();
  if (valid) {
    const fromData = tableFormRef.value.getFormDataFun();
    const params = {
      ...fromData,
      parentDirId: props.dirId,
      current: 1,
      size: 9999,
    };
    dataOverViewSearchSimulationNodeFilesApi(params).then((res: any) => {
      if (res.code === 200) {
        ElMessage.success('操作成功');
        emit('search', res.data);
        closeFun();
      }
    });
  }
};

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