<template>
  <div class="file-upload">
    <div>文件检索路径：</div>
    <a-cascader
      v-model="current_path"
      :options="options"
      :style="{ width: '320px' }"
      placeholder="请选择"
      check-strictly
      allow-clear
      :loading="loading_tree"
      @change="handlePathChange"
    />
  </div>
  <div class="loading" v-if="is_loading">
    <img class="icon" src="../../assets//loading.svg" alt="" />
    <div class="tips">文件解析中，请耐心等待！</div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { Message } from '@arco-design/web-vue';
import { getFileTree, syncData, getParsedFilePath } from '../../apis/home';
const options = ref([]);
const current_path = ref('');
const loading_tree = ref(false);
const is_loading = ref(false);

const getTree = async () => {
  loading_tree.value = true;
  try {
    const res = await getFileTree();
    if (res.code === 200) {
      options.value = res.data.node;
    }
  } catch (err) {
    console.log(err);
  } finally {
    loading_tree.value = false;
  }
};
const syncDataFn = async (path) => {
  is_loading.value = true;
  try {
    const res = await syncData(path);
    if (res.code === 200) {
      Message.success('数据同步成功');
    } else {
      Message.error('数据同步失败');
    }
  } catch (err) {
    console.log(err);
    Message.error('数据同步失败');
  } finally {
    is_loading.value = false;
  }
};
const handlePathChange = (path) => {
  console.log(path);
  if (path && path.length) {
    syncDataFn(path);
  }
};
const getParsedFilePathFn = async () => {
  try {
    const res = await getParsedFilePath();
    if (res.code === 200) {
      current_path.value = res.data.value;
    }
  } catch (err) {
    console.log(err);
  }
};

onMounted(() => {
  getTree();
  getParsedFilePathFn();
});
</script>
<style scoped lang="css">
.file-upload {
  min-width: 500px;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
}
.loading {
  position: absolute;
  left: 0;
  top: 60px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 100vw;
  height: calc(100vh - 60px);
  background-color: rgba(0, 0, 0, 0.5);
  z-index: 999;
  .icon {
    width: 50px;
    margin-bottom: 10px;
    animation: spin 3s linear infinite;
  }
  .tips {
    color: #fff;
    font-weight: 600;
    font-size: 14px;
  }
}
@keyframes spin {
  0% {
    transform: rotate(0deg); /* 初始状态 */
  }
  100% {
    transform: rotate(360deg); /* 完成一圈旋转 */
  }
}
</style>
