<!--  -->
<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>{{titleName}}   版本列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider class="divi"></el-divider>

    <el-form :inline="true"  @keyup.enter.native="getDataList(this.$route.query.algorithmNameToVersion)">
      <el-form-item>
        <el-button v-if="isAuth('version:save')" type="primary" @click="addHandle()">新建版本</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="versionName" header-align="center" align="center" label="版本名称">
      </el-table-column>
      <el-table-column prop="versionStatus" header-align="center" align="center" label="可编辑状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.versionStatus === 0" size="small">可编辑</el-tag>
          <el-tag v-else size="small" type="danger">不可编辑</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="creationTime" 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:info')" type="text" size="small" @click="userwatch(scope.row.versionId)">查看
          </el-button>

          <el-button v-if="isAuth('version:edit')" type="text" size="small" @click="versionsList(scope.row.versionId)">编辑
          </el-button>

          <el-button v-if="isAuth('version:train')" type="text" size="small"
            @click="startTrain(scope.row.versionId)">开始训练</el-button>

          <el-button v-if="isAuth('version:delete')" type="text" size="small" @click="deleteHandle(scope.row.versionId)">
            删除</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>
    <Watch v-if="newWatchVisible" ref="newWatch"></Watch>.
    <Add v-if="addVisible" ref="addS" @refreshDataList="getDataList"></Add>
  </div>
</template>

<script>
import Watch from "./verison-watch.vue"
import Add from "./version-add"
import { MessageBox } from 'element-ui'
import { Message } from "element-ui"
export default {
  //import引入的组件需要注入到对象中才能使用
  data() {
    return {
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      newWatchVisible: false,
      addVisible: false,
      titleName: "",
      algorithmNameToVersion: null
    };
  },
  components: {
    // AddOrUpdate,
    Watch,
    Add
  },

  activated() {
    //   处理参数，根据参数初始化数据
    let algorithmNameToVersion = this.$route.query.algorithmNameToVersion;
    this.init(algorithmNameToVersion);
    this.getDataList(algorithmNameToVersion);
  },
  methods: {
    //   初始化面包屑的名称
    init(algorithmNameToVersion){
        this.algorithmNameToVersion = algorithmNameToVersion;
        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(algorithmNameToVersion) {
      if (algorithmNameToVersion > 0) {
        this.dataListLoading = true;

        this.$http({
          url: this.$http.adornUrl("/version/list/" + algorithmNameToVersion),
          method: "get",
          params: this.$http.adornParams({
            page: this.pageIndex,
            limit: this.pageSize,
          }),
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list;
            this.totalPage = data.page.totalCount;
          } else {
            this.dataList = [];
            this.totalPage = 0;
          }
          this.dataListLoading = false;
        });
      } 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);
      });
    },
    // 现在这里只处理修改了
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
    },
    // 开始训练
    startTrain(id){
      this.$router.replace({ name: 'starttrain',query:{algorithmId:this.algorithmNameToVersion,versionId:id} });
    },
    // 这里处理新建
    addHandle() {
      this.addVisible = true;
      this.$nextTick(() => {
        this.$refs.addS.init(this.$route.query.algorithmNameToVersion);
      });
    },
    // 删除
    deleteHandle(id) {
      var versionId = id
        ? [id]
        : this.dataListSelections.map((item) => {
            return item.versionId;
          });
      MessageBox.confirm(
        `确定对[id=${versionId.join(",")}]进行[${
          id ? "删除" : "批量删除"
        }]操作?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          this.$http({
            url: this.$http.adornUrl("/version/delete"),
            method: "post",
            data: this.$http.adornData(versionId, false),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              Message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getDataList(this.$route.query.algorithmNameToVersion);
                },
              });
              this.pageIndex = 1;
              this.getDataList(this.$route.query.algorithmNameToVersion);
            } else {
              Message.error(data.msg);
            }
          });
        })
        .catch(() => {});
    },

    //查看文件列表
    versionsList(id){
      this.$router.replace({ name: 'filetest',query:{algorithmNameToVersion:this.algorithmNameToVersion,verisionToFile:id} });
    }
  },
};
</script>
</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>
