<template>
    <div class=''>
    <el-breadcrumb class="divi2" separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: 'alg-algs' }">算法管理</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ name: 'version',query:{algorithmNameToVersion:this.algorithmNameToVersion} }">{{titleName}}   版本列表</el-breadcrumb-item>
      <el-breadcrumb-item>文件列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider class="divi"></el-divider>

    <el-form :inline="true">
      <el-form-item>
        <el-button v-if="isAuth('version:save')" type="primary" @click="createFile()">上传文件</el-button>
        <!-- <el-button v-if="isAuth('version:delete')" type="danger" @click="deleteHandle()"
          :disabled="dataListSelections.length <= 0">批量删除</el-button> -->
        
      </el-form-item>
    </el-form>


    <el-table :data="dataList" border v-loading="dataListLoading" @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column type="selection" header-align="center" align="center" width="50">
      </el-table-column>
      <el-table-column prop="filename" header-align="center" align="center" label="文件名称">
      </el-table-column>
      
      <el-table-column prop="filesize" header-align="center" align="center" label="文件大小">
      </el-table-column>

      <el-table-column prop="lastModifiedTime" header-align="center" align="center" label="修改时间">
      </el-table-column>
      
      <el-table-column fixed="right" header-align="center" align="center" width="250" label="操作">
        <template slot-scope="scope">
          <el-button v-if="isAuth('version:edit')" type="text" size="small" :disabled="editDisableHandle(scope.row.filename)"
            @click="editFile(scope.row.filename)">编辑
          </el-button>

          <el-button v-if="isAuth('version:delete')" type="text" size="small" @click="deleteHandle(scope.row.filename)">
            删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <AddFile v-if="addVisible" ref="addS"></AddFile>
  </div>
</template>

<script>
import AddFile from "./version-uploadFile"
import { Message } from "element-ui"
export default {
  //import引入的组件需要注入到对象中才能使用
  data() {
    return {
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      addVisible: false,
      titleName: "",
      algorithmNameToVersion: null,
      verisionToFile:null
    };
  },
  components: {
    AddFile
  },

  activated() {
    //   处理参数，根据参数初始化数据
    let algorithmNameToVersion = this.$route.query.algorithmNameToVersion;
    let verisionToFile=this.$route.query.verisionToFile;
    this.init(algorithmNameToVersion,verisionToFile);
    this.getDataList();
  },
  methods: {
    //   初始化面包屑的名称
    init(algorithmNameToVersion,verisionToFile){
        this.algorithmNameToVersion = algorithmNameToVersion;
        this.verisionToFile=verisionToFile;
        console.log(this.algorithmNameToVersion,this.verisionToFile);

        //必须加这一行，添加文件弹出框才不会自动显示，需研究一下
        this.addVisible=false;


        this.$http({
          url: this.$http.adornUrl("/algs/info/" + algorithmNameToVersion),
          method: "get",
          params: this.$http.adornParams(),
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.titleName = data.algs.algorithmName;
          } else {
            this.titleName = "版本列表"
          }
        });
    },

    // 获取数据列表
    getDataList() {
        this.dataListLoading = true;
        this.$http({
          url: this.$http.adornUrl("/version/listFiles/"),
          method: "get",
          params: this.$http.adornParams({
            page: this.pageIndex,
            limit: this.pageSize,
            algorithmNameToVersion:this.algorithmNameToVersion,
            verisionToFile:this.verisionToFile
          }),
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.dataList = data.fileList;
            this.totalPage = data.fileList.length;
          } else {
            this.dataList = [];
            this.totalPage = 0;
          }
          this.dataListLoading = false;
        });
    
    //   if (algorithmNameToVersion > 0) {
        
    //   } else {
    //     this.dataListLoading = true;
    //   }
    },
    stateFormat1(row, column) {
      if (row.datasetStatus === 1) {
        return "不可编辑";
      } else {
        return "可编辑";
      }
    },
    stateFormat2(row, column) {
      if (row.datasetStatus === 1) {
        return "已训练";
      } else {
        return "未训练";
      }
    },
    //去重
    unique(arr) {
      var ss = [];
      for (var i = 0; i < arr.length; i++) {
        ss[i] = arr[i].categoryName;
      }
      return Array.from(new Set(ss));
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList(this.$route.query.algorithmNameToVersion);
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList(this.$route.query.algorithmNameToVersion);
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 查看
    userwatch(id) {
      this.newWatchVisible = true;
      this.$nextTick(() => {
        this.$refs.newWatch.init(id);
      });
    },
    
    // 编辑文件
    editFile(fileName){
        this.$router.replace({ name: 'fileedit',query:{algorithmNameToVersion:this.algorithmNameToVersion,verisionToFile:this.verisionToFile,fileName:fileName} });
    },
    //删除文件
    deleteHandle(fileName){
        this.$http({
          url:this.$http.adornUrl('/minio/deleteFile'),
          method:'get',
          params:this.$http.adornParams({
            algorithmNameToVersion:this.algorithmNameToVersion,
            versionToFile:this.verisionToFile,
            fileName:fileName
          }),
        }).then(({data})=>{
          if (data && data.code === 0) {
              Message({
                message: "操作成功",
                type: "success",
                duration: 1500,
              
              });
              this.getDataList();
            } else {
              Message.error(data.msg);
            }
        });
    },
    editDisableHandle(filename){
      var suffix=filename.substr(filename.lastIndexOf(".")+1);
      if(suffix=="py"||suffix=="java")
        return false;
      else
        return true;
    },
    createFile(){
      this.addVisible = true;
      this.$nextTick(() => {
        this.$refs.addS.init(this.$route.query.algorithmNameToVersion,this.$route.query.verisionToFile);
      });
    }
   
    
  },
};
</script>
<style scoped>
.divi {
  display: block;
  height: 1px;
  width: 100%;
  margin: 24px 0;
  background-color: #dcdfe6;
  position: relative;
}
.divi2 {
  display: block;
  height: 1px;
  width: 100%;
  position: relative;
}
.sele {
  border: 1px solid #409eff;
  border-radius: 5px;
  box-sizing: border-box;
  padding: 5px 0px;
  margin: 10px;
}
</style>
