<template>
  <Dialog v-model="visible" diaTitle="编辑文件" :width="400" @close="closeFun">
    <div class="content">
      <TableForm
        ref="tableFormRef"
        v-model:data="formData"
        :itemNum="1"
        tableName="DATA_OVERVIEW_FILE_DETAIL"
      />
    </div>
    <template #footer>
      <div>
        <el-button @click="closeFun">关闭</el-button>
        <el-button type="primary" @click="submitFun">确定</el-button>
      </div>
    </template>
  </Dialog>
</template>

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

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

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

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

const formData = ref<any>({});

watch(
  () => props.modelValue,
  (val: boolean) => {
    visible.value = val;
    if (val) {
      if (props.id) {
        formData.value.fileName = props.data.originalName;
      }
    } else {
      formData.value = {};
      if (tableFormRef.value) {
        tableFormRef.value.resetFun();
      }
    }
  }
);

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

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