<template>
  <folder-page type="case" @click="folderHandleSelected">
    <div v-if="queryParams.groupId && queryParams.groupId !== 0">
      <el-table v-loading="loading" :data="dataList" ref="multipleTable">
        <el-table-column label="用例名称" align="center" prop="name"/>
        <el-table-column label="创建人" align="center" prop="createBy"/>
        <el-table-column label="用例状态" align="center" prop="status" :formatter="row => ['','草稿', '通过', '不通过'][row.status]"/>
        <el-table-column label="创建时间" align="center" prop="createTime"/>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <span v-if="caseList.findIndex(item => item.id === scope.row.id) > -1">已添加</span>
            <el-button v-else size="mini" type="text" icon="el-icon-delete" @click="handleAdd(scope.row.id)">添加</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>
    <el-empty v-else/>
  </folder-page>
</template>
<script>
import FolderPage from "@/components/FolderPage/index.vue";
import {listCase} from "@/api/test/case";
import {addTaskCase} from "@/api/test/taskCase";

export default {
  components: {FolderPage},
  props: {
    caseList: {
      type: Array,
    },
    taskId: {
      type: Number,
    }
  },
  data() {
    return {
      loading: false,
      total: 0,
      dataList: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        groupId: null,
      },
    };
  },
  methods: {
    folderHandleSelected(id) {
      if (id) {
        this.queryParams.groupId = id;
        this.getList();
      } else {
        this.dataList = [];
      }
    },
    getList() {
      this.loading = true;
      listCase(this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleAdd(id) {
      this.loading = true;
      addTaskCase({
        taskId: this.taskId,
        caseId: id,
      }).then(res => {
        this.loading = false;
        this.$emit("success");
      })
    },
  },
};
</script>
<style lang="scss" scoped>
::v-deep .el-container {
  height: unset;
}
</style>
