<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="85px">
      <el-form-item label="版本号" prop="versionName">
        <el-input
          v-model="queryParams.versionName"
          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-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['dataCenter:version:delete']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-close"
          size="mini"
          @click="handleClose"
        >关闭</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table  style="width: 100%" v-loading="loading"  :data="list"  :height="tableHeight"  @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column type="index" label="序号" :index="indexMethod" width="50" />
      <el-table-column label="数据集名称" align="center" show-overflow-tooltip prop="datasetName" min-width="100" />
      <el-table-column label="版本号" align="center" show-overflow-tooltip prop="versionName" min-width="100" />
      <el-table-column label="发布人" align="center" show-overflow-tooltip prop="releaseUserName" min-width="100" />
      <el-table-column label="发布时间" align="center" show-overflow-tooltip prop="createTime" min-width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="文件数量" align="center" show-overflow-tooltip prop="totalCount" min-width="100" />
      <el-table-column label="任务名称" align="center" show-overflow-tooltip prop="taskName" min-width="100" />
      <el-table-column label="标注类型" align="center" show-overflow-tooltip prop="annotationType" min-width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ai_annotate_type" :value="scope.row.annotationType"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" min-width="100" class-name="small-padding fixed-width" >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleExport(scope.row)"
            v-hasPermi="['dataCenter:version:delete']"
          >导出</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['dataCenter:version:delete']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

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


import {list,remove,annotationsExport} from '@/api/dataCenter/releaseVersion'


export default {
  dicts: ['ai_annotate_type'],
  name: "DataSet",
  data() {
    return {
      datasetId: 0,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      tableHeight: 0,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 是否显示弹出层
      addOpen: false,
      releaseOpen: false,
      annotationOpen:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        versionName:undefined
      },
    };
  },
  created() {
    this.datasetId = this.$route.params && this.$route.params.dataSetId;
    this.getList();
    // 初始化表格高度
    this.updateTableHeight();
    // 监听窗口大小变化
    window.addEventListener("resize", this.updateTableHeight);
  },
  watch:{
    datasetId(newVal, oldVal){
      this.getList();
    },
    $route(to, from) {
      // 每次路由变化时重新加载数据
      if (to.fullPath !== from.fullPath) {
        this.dataType = this.$route.query && this.$route.query.dataType
        this.datasetId = this.$route.params && this.$route.params.dataSetId
        this.getList()
        // 初始化表格高度
        this.updateTableHeight()
        // 监听窗口大小变化
        window.addEventListener('resize', this.updateTableHeight)
      }
    }
  },
  methods: {
    indexMethod(index){
      console.log(index);
      return (this.queryParams.pageNum - 1) * this.queryParams.pageSize + index + 1;
    },
    updateTableHeight() {
      // 设置表格高度为窗口高度减去其他元素高度
      const headerHeight = 200; // 头部高度，可以调整
      const footerHeight = 80; // 底部高度，可以调整
      this.tableHeight = window.innerHeight - headerHeight - footerHeight;
    },
    /**获取数据 **/
    getList(){
      this.loading =true
      this.queryParams.datasetId = this.datasetId;
      list(this.queryParams).then(response => {
        console.log(response);
        this.list = response.rows;
        this.total = response.total;
        this.loading =false
      })
    },
    /** 返回按钮操作 */
    handleClose() {
      const obj = { path: "/dataCenter/dataSet" };
      this.$tab.closeOpenPage(obj);
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleExport(row){

      this.download('/ai/annotations/export/' + row.versionId,{}, new Date().getTime()+'.zip',{timeout: 600000})
    },
    handleDelete(row){
      const ids = row.versionId || this.ids;
      this.$modal.confirm('是否确认删除数据项？').then(function() {
        return remove(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.versionId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
  }
}
</script>
<style scoped>
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>

