<template>
  <div>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px" @submit.native.prevent>
      <el-form-item label="文件名" prop="fileName">
        <el-input
          v-model="queryParams.fileName"
          placeholder="请输入文件名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
    <el-col :span="1.5">
      <el-button
        type="danger"
        plain
        icon="el-icon-refresh"
        size="mini"
        :disabled="multiple"
        @click="dataRecovery"
        v-hasPermi="['dataCenter:dataSetBasicFile:dataRecovery']"
      >还原</el-button>
      <el-button
        type="danger"
        plain
        icon="el-icon-refresh"
        size="mini"
        @click="emptyRecycleBin"
        v-hasPermi="['dataCenter:dataSetBasicFile:dataRecovery']"
      >清空回收站</el-button>
    </el-col>
    <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" size="medium" :data="fileList" :height="tableHeight" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="名称" prop="fileName" min-width="200" />
      <el-table-column label="类型" align="center" prop="fileUrl" min-width="120">
        <template slot-scope="scope">
          <span>
            {{scope.row.isDirectory === '1'?'文件夹':scope.row.fileName.split('.').pop()}}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="文件大小" align="center" prop="fileSize" min-width="120">
        <template slot-scope="scope">
          <span>{{scope.row.isDirectory === '1'?'-':(scope.row.fileSize/1024/1024).toFixed(2)+'MB'}}</span>
        </template>
      </el-table-column>
      <el-table-column label="标签类型" align="center" prop="" min-width="120" />
      <el-table-column label="上传时间" align="center" prop="createTime" min-width="120" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-refresh"
            @click="dataRecovery(scope.row)"
            v-hasPermi="['dataCenter:dataSetBasicFile:dataRecovery']"
          >还原</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      style="background-color: transparent"
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { delList, dataRecovery,emptyRecycleBin} from "@/api/dataCenter/basicFile";
export default {
  name: "File",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      tableHeight: 0,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 文件基础表格数据
      fileList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fileName: null,
      },
    };
  },
  created() {
    this.getList();
    this.updateTableHeight()
    // 监听窗口大小变化
    window.addEventListener("resize", this.updateTableHeight);
  },
  watch: {
    folderName(newVal, oldVal) {
      this.parentId = newVal.length > 0 ? newVal[newVal.length - 1].fileId : 0;
      this.fileUrl = newVal.map(item => item.fileName).join("/");
      this.url = "我的样本库/" + this.fileUrl + (this.fileUrl ? "/" : "")
      this.getList();
    }
  },
  methods: {
    updateTableHeight() {
      // 设置表格高度为窗口高度减去其他元素高度
      const headerHeight = 350; // 头部高度，可以调整
      const footerHeight = 80; // 底部高度，可以调整
      this.tableHeight = window.innerHeight - headerHeight - footerHeight;
    },
    /** 查询文件基础列表 */
    getList() {
      this.loading = true;
      this.queryParams.parentId = this.parentId;
      delList(this.queryParams).then(response => {
        this.fileList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.fileId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    dataRecovery(row){
      const fileIds = row.fileId || this.ids;
      this.$modal.confirm('是否确认还原数据项？').then(function() {
        return dataRecovery(fileIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("还原成功");
      }).catch(() => {});
    },
    emptyRecycleBin(){
      this.$modal.confirm('是否确认清空回收站？').then(function() {
        return emptyRecycleBin();
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("清空成功");
      }).catch(() => {});
    }
  }
};
</script>
