<template>
  <div>
    <el-tree
      :props="props"
      node-key="filePath"
      :load="ls"
      lazy
      :expand-on-click-node="false"
      highlight-current
      :data="files"
    >
      <span slot-scope="{ node, data }" @click="setCurrentFile(data, node)">{{data.fileName}}</span>
    </el-tree>
  </div>
</template>

<script>
import {ls} from "@/api/file/filesystem";

export default {
  name: 'ServerFileChooser',
  props: {
    value: {
      type: String,
      default: null,
    },
    fileType: {
      type: Number,
      default: 0,
    },
    showHidden: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      props: {
        label: 'fileName',
        children: 'files',
        isLeaf: (data) => data.fileType & 2,
      },
      files: [],
    };
  },
  created() {
  },
  methods: {
    ls(node, resolve) {
      let query = {
        filePath: node.data ? node.data.filePath : null,
        fileType: this.fileType,
        showHidden: this.showHidden,
      };
      ls(query).then((resp) => {
        resolve(resp.data);
      });
    },
    setCurrentFile(data, node, tree) {
      this.$emit('input', data.filePath);
      this.$emit('selected', data);
    },
  }
};
</script>

<style scoped lang="scss">

</style>
