<template>
  <div class="te-file-panel">
    <div class="te-file-list-header" :class="fixed ? 'fixed' : ''">
      <i class="iconfont">&#xe632;</i>
      <input
        type="text"
        placeholder="输入文字搜索文件"
        @input="filter()"
        v-model="query"
      />
      <i
        class="iconfont"
        @click="mode = 'list'"
        :class="mode == 'list' ? 'active' : ''"
        >&#xe61b;</i
      >
      <i
        class="iconfont"
        @click="mode = 'grid'"
        :class="mode == 'grid' ? 'active' : ''"
        >&#xe6f1;</i
      >
      <i v-if="enable_upload" @click="selectFile" class="iconfont upload"
        >&#xe656;
        <input
          type="file"
          ref="file"
          multiple="true"
          @change="upload($event)"
          name="upload"
        />
      </i>
    </div>
    <ul
      class="te-file-list grid"
      v-if="mode == 'grid'"
      :class="fixed ? 'fixed' : ''"
    >
      <li class="row">
        <div
          v-for="(item, idx) in list"
          :key="idx"
          class="item"
          @dblclick="open(item)"
        >
          <div class="icon">
            <i v-if="item.type == 'doc' || item.type == 'docx'" class="iconfont"
              >&#xe607;</i
            >
            <i
              v-else-if="
                item.type == 'xlsx' || item.type == 'xls' || item.type == 'csv'
              "
              class="iconfont"
              >&#xe608;</i
            >
            <i
              v-else-if="item.type == 'pptx' || item.type == 'ppt'"
              class="iconfont"
              >&#xe6e8;</i
            >
            <i v-else-if="item.type == 'png'" class="iconfont">&#xe8da;</i>
            <i v-else-if="item.type == 'pdf'" class="iconfont">&#xe67d;</i>
            <i v-else-if="item.type == 'txt'" class="iconfont">&#xe6e9;</i>
            <i v-else-if="item.type == 'dir'" class="iconfont">&#xe683;</i>
            <i v-else class="iconfont">&#xe671;</i>
          </div>
          <div class="title" :title="item.name">
            {{ item.fname }}
          </div>
        </div>
      </li>
    </ul>
    <ul
      class="te-file-list list"
      v-if="mode == 'list'"
      :class="fixed ? 'fixed' : ''"
    >
      <li class="row header">
        <div class="item col-4">名称</div>
        <div class="item col-1">大小</div>
        <div class="item col-1">类型</div>
        <div class="item col-2">更改时间</div>
        <div class="item col-1 text-center">处理</div>
      </li>
      <li
        v-for="(item, idx) in list"
        :key="idx"
        class="row"
        @dblclick="open(item)"
      >
        <div class="item col-4">
          <span class="icon">
            <i v-if="item.type == 'doc' || item.type == 'docx'" class="iconfont"
              >&#xe607;</i
            >
            <i
              v-else-if="
                item.type == 'xlsx' || item.type == 'xls' || item.type == 'csv'
              "
              class="iconfont"
              >&#xe608;</i
            >
            <i
              v-else-if="item.type == 'pptx' || item.type == 'ppt'"
              class="iconfont"
              >&#xe6e8;</i
            >
            <i v-else-if="item.type == 'png'" class="iconfont">&#xe8da;</i>
            <i v-else-if="item.type == 'pdf'" class="iconfont">&#xe67d;</i>
            <i v-else-if="item.type == 'txt'" class="iconfont">&#xe6e9;</i>
            <i v-else-if="item.type == 'dir'" class="iconfont">&#xe683;</i>
            <i v-else class="iconfont">&#xe671;</i>
          </span>
          <span class="title" :title="item.name">{{
            $theia.str.cut(item.fname)
          }}</span>
        </div>
        <div class="item col-1">
          {{ item.fsize }}
        </div>
        <div class="item col-1">
          {{ item.fext == "dir" ? "目录" : item.fext }}
        </div>
        <div class="item col-2">
          {{ item.time || "-" }}
        </div>
        <div class="item col-1 text-center">
          <i class="iconfont opt" @click="open(item)">&#xe6b5;</i>
          <i class="iconfont opt" @click="download(item)">&#xe66c;</i>
        </div>
      </li>
    </ul>
  </div>
</template>
<script>
export default {
  name: "TeFileList",
  props: {
    files: {
      type: Array,
    },
    enable_upload: {
      type: Boolean,
      default: true,
    },
    fixed: {
      type: Boolean,
    },
  },
  watch: {
    files: function (v) {
      this.query = "";
      this.list = v;
      this.resetFile();
    },
  },
  created: function () {
    this.list = this.files;
    this.resetFile();
  },
  data() {
    return {
      mode: "grid",
      list: [],
      query: "",
    };
  },
  methods: {
    selectFile() {
      this.$refs.file.click();
    },
    upload(event) {
      var files = event.target.files;
      this.$emit("upload", files);
    },
    open(f) {
      this.$emit("open", f);
    },
    download(f) {
      this.$emit("download", f);
    },
    resetFile() {
      let that = this;
      this.list.forEach(function (f) {
        f.fpath = f.path;
        f.fname = f.name;
        f.fext = f.type;
        f.fsize = that.$theia.file.formatSize(f.size);
      });
    },

    filter() {
      let cquery = this.query;
      if (!cquery) {
        this.list = this.files;
        return;
      }
      let nlist = [];
      this.files.forEach(function (n) {
        if (n.name && n.name.indexOf(cquery) >= 0) {
          nlist.push(n);
        }
      });
      this.list = nlist;
    },
  },
};
</script>
<style>
:root {
  --header-height: 80px;
}
.te-file-panel {
  width: 100%;
  position: relative;
}
.te-file-list-header {
  width: 100%;
  height: var(--header-height);
  display: block;
  padding: 5px 0px;
  background-color: var(--page-bg-color);
}
.te-file-list-header.fixed {
  width: calc(100% - var(--menu-width) - var(--submenu-width));
  position: fixed;
  padding: 4px 0px;
  top: calc(var(--banner-height) + 20px);
}
.te-file-list {
  list-style-type: none;
  padding: 0 20px;
  margin: 0;
  height: 100%;
}
.te-file-list.fixed {
  margin-top: var(--header-height);
}

.te-file-list-header i {
  cursor: pointer;
  margin: 5px 10px;
}
.te-file-list-header input[type="text"] {
  width: calc(100% - 3rem - 120px);
  -webkit-appearance: none;
  background-color: #fff;
  border: none;
  border-bottom: 1px solid #dcdfe6;
  color: #606266;
  font-size: 0.88rem;
  height: 36px;
  line-height: 36px;
  outline: 0;
  padding: 0 10px;
  transition: border-color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
}
.te-file-list-header input[type="text"]:hover {
  border-color: #0caaf4;
}
.te-file-list .row {
  cursor: pointer;
  display: block;
  text-decoration: none;
  white-space: nowrap;
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-start;
}
.te-file-list .item .title {
  width: 100%;
  padding: 4px 4px;
  overflow: hidden;
}

.te-file-list .grid {
  font-size: 1rem;
}
.te-file-list.grid .item {
  text-align: center;
  width: 160px;
  margin: 10px 2px;
}

.te-file-list.grid .item i {
  font-size: 2.8rem;
  color: var(--banner-bg-color);
}
.te-file-list.grid .item .title {
  font-size: 0.8rem;
  margin: 0px 0px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  width: 100%;
}

.te-file-list.grid .item:hover {
  background-color: #f1f1f1;
  border-radius: 4px;
}
.te-file-list.grid .item:hover .title {
  color: var(--white);
  background-color: var(--banner-bg-color);
}

.te-file-list.list {
  font-size: 0.9rem;
}
.te-file-list.list .row {
  flex-wrap: nowrap;
}
.te-file-list.list .row.header {
  border-bottom: 1px solid #f1f1f1;
}
.te-file-list.list .item {
  text-align: left;
  margin: 10px 1px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.te-file-list.list .item .opt {
  margin-left: 10px;
}

.te-file-list.list .item i {
  color: var(--banner-bg-color);
}

.te-file-list.list .row:hover:not(.header) {
  background-color: #f1f1f1;
}
.te-file-list.list .item .opt:hover {
  font-weight: 600;
}
input[type="file"] {
  margin-left: -2000px;
  height: 0;
}

@media screen and (max-width: 1200px) {
  .te-file-list {
    padding: 0 2px;
  }

  .te-file-list.grid .item {
    text-align: center;
    width: 32%;
    margin: 1px 1px;
    padding: 1px 1px;
  }
  .te-file-list.grid .item i {
    font-size: 2.6rem;
  }
  .te-file-list.grid .item .title {
    font-size: 0.7rem;
  }
  .te-file-list.grid .item .icon img {
    width: 80px;
    height: 80px;
  }
}

.col-1 {
  width: 10%;
}
.col-2 {
  width: 20%;
}
.col-3 {
  width: 30%;
}
.col-4 {
  width: 40%;
}
.col-5 {
  width: 50%;
}
.col-6 {
  width: 60%;
}
.col-7 {
  width: 70%;
}
.col-8 {
  width: 80%;
}
.col-9 {
  width: 90%;
}
</style>