<template>
  <folder-page type="case" @click="folderHandleSelected">
    <div v-if="queryParams.groupId && queryParams.groupId !== 0">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新建用例</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
      <el-table v-loading="loading" :data="dataList" @row-click="handleRowClick">
        <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">
            <el-button size="mini" type="text" icon="el-icon-caret-right" @click.native.stop="handleRun(scope.row.id)">执行</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click.native.stop="handleDelete(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/>
    <el-dialog title="操作日志详细" :visible.sync="open" >
      <div v-if="open">
        <report :sid="caseSID"/>
      </div>
    </el-dialog>
  </folder-page>
</template>

<script>
import FolderPage from "@/components/FolderPage/index.vue";
import Report from "@/views/test/case/report/index.vue";
import {addCase, delCase, listCase, runCaseSync} from "@/api/test/case";

export default {
  name: "Case",
  components: {FolderPage, Report},
  data() {
    return {
      loading: false,
      open: false,
      showSearch: true,
      total: 0,
      dataList: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        groupId: null,
      },
      caseSID: 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() {
      this.$prompt('请输入名称', '新增', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPattern: /^(?!\s*$).+/,
        inputErrorMessage: '名称不能为空'
      }).then(({value}) => {
        if (value) {
          addCase({
            groupId: this.queryParams.groupId,
            name: value
          }).then(res => {
            this.$message.success("添加成功")
            this.getList()
          })
        }
      });
    },
    handleRun(id) {
      this.loading = true;
      this.$modal.confirm('是否确认执行用例？').then(function () {
        return runCaseSync(id);
      }).then((res) => {
        this.$modal.msgSuccess("执行成功");
        this.caseSID = res.data
        this.open = true;
        this.loading = false;
      });
    },
    handleRowClick(row) {
      this.$tab.openPage(`用例[${row.name}]`, "/case/detail", {id: row.id});
    },
    handleDelete(id) {
      this.$modal.confirm('是否确认删除用例？').then(function () {
        return delCase(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      });
    }
  }
}
</script>

<style scoped lang="scss">
::v-deep .el-table__row {
  cursor: pointer;
}
</style>

