<template>
  <folder-page type="automation" @click="folderHandleSelected" ref="folder">
    <div class="header">
      <el-dropdown @command="handleCommand">
        <span class="el-dropdown-link">
          <i style="border: 1px solid #DCDFE6; padding: 5px 15px; border-radius: 4px;" class="el-icon-plus"></i>
        </span>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item command="1">创建场景</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
      <div class="searchInput">
        <i class="el-icon-search"></i>
        <el-input class="input" v-model="searchForm.keyword" placeholder="根据名称搜索" @blur="searchList"></el-input>
      </div>
    </div>
    <div class="list">
      <el-table :data="automationList">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="id" label="ID" width="50" align="center" />
        <el-table-column prop="name" label="场景名称" min-width="150" align="center" sortable />
        <el-table-column prop="caseLevel" label="用例等级" min-width="150" align="center" sortable>
          <template slot-scope="scope">
            <el-tag v-if="scope.row.caseLevel" :type="priorityColor[scope.row.caseLevel]">{{ scope.row.caseLevel
            }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" min-width="150" align="center" sortable>
          <template slot-scope="scope">
            <div v-if="scope.row.status === '1'">未开始</div>
            <div v-if="scope.row.status === '2'">进行中</div>
            <div v-if="scope.row.status === '3'">已完成</div>
          </template>
        </el-table-column>
        </el-table-column>
        <el-table-column prop="label" label="标签" min-width="150" align="center" />
        <el-table-column prop="createBy" label="创建人" min-width="150" align="center" sortable />
        <el-table-column prop="dutyBy" label="责任人" min-width="150" align="center" sortable />
        <el-table-column prop="updateTime" label="最后更新时间" min-width="180" align="center" sortable />
        <el-table-column prop="stepsNumber" label="步骤数" min-width="80" align="center" />
        <el-table-column prop="executionResult" label="执行结果" min-width="150" align="center" sortable />
        <el-table-column prop="passRate" label="通过率" min-width="150" align="center" sortable />
        <el-table-column prop="action" label="操作" align="center" fixed="right" width="200px">
          <template slot-scope="scope">
            <el-button type="text" icon="el-icon-edit-outline" @click="hadleClickEdit(scope.row)">编辑</el-button>
            <el-button type="text" icon="el-icon-view" @click="handleClickDetail(scope.row)"
              style="color: green;">执行</el-button>
            <el-button type="text" icon="el-icon-delete" style="color: red;"
              @click="hadleClickDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :page-size="searchForm.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total" />
    </div>
  </folder-page>
</template>

<script>
import FolderPage from "@/components/FolderPage/index.vue"
import { getAutomationList, deleteAutomation, executeAutomationData } from "../../../api/uiTest/automationTest";

export default {
  name: "AutomationTest",
  components: { FolderPage },
  data() {
    return {
      priorityColor: {
        P0: 'danger',  // 红色
        P1: 'warning', // 黄色
        P2: 'info',    // 蓝色
        P3: 'success', // 绿色
        p4: 'success'
      },
      automationList: [{}],
      searchForm: {
        pageNum: 1,
        pageSize: 10,
        keyword: null,
        groupId: null,
      },
      total: 0,
    }
  },
  mounted() {
  },
  methods: {
    folderHandleSelected(id) {
      if (id) {
        // 获取列表
        this.searchForm.groupId = id;
        this.getAutomationListData();
      } else {
        this.automationList = [];
      }
    },
    // 获取列表
    getAutomationListData() {
      getAutomationList(this.searchForm).then(res => {
        if (res.code === 200) {
          this.automationList = res.rows
          this.total = res.total
        }
      })
    },
    // 搜索
    searchList() {
      this.getAutomationListData();
    },
    // 创建元素
    handleCommand(command) {
      this.$tab.openPage("创建场景", "/ui-test/automation/add");
    },
    // 编辑
    hadleClickEdit(val) {
      this.$tab.openPage("编辑场景", "/ui-test/automation/edit", { id: val.id });
    },
    // 删除
    hadleClickDelete(val) {
      this.$modal.confirm('确认删除元素' + '？').then(() => {
        deleteAutomation(val.id).then(res => {
          if (res.code === 200) {
            this.$modal.msgSuccess("删除成功");
            this.getAutomationListData();
          } else {
            this.$modal.msgError("删除失败");
          }
        })
      }).catch(() => { })
    },
    // 查看
    handleClickDetail(val) {
      executeAutomationData({ id: val.id }).then(res => {
        if (res.code === 200) {
          this.$modal.msgSuccess("执行成功");
        } else {
          this.$modal.msgSuccess("执行失败");
        }
      })
    },
    // 分页
    handleSizeChange(val) {
      this.searchForm.pageSize = val
      this.getAutomationListData();
    },
    handleCurrentChange(val) {
      this.searchForm.pageNum = val
      this.getAutomationListData();
    },
  }

}

</script>



<style scoped lang="scss">
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;

  .searchInput {
    display: flex;
    align-items: center;
    border: 1px solid #DCDFE6;
    border-radius: 4px;
    padding: 0 10px;

    .input {
      width: 200px;
    }

    /* 去除 el-input 的边框 */
    ::v-deep .el-input__inner {
      border: none !important;
    }
  }
}

.list {
  margin-top: 20px;
  display: flex;
  flex-direction: column;
  gap: 20px;
  align-items: flex-end;

  .caseLevel-wrap {
    padding: 0px 5px;
    background-color: #F56C6D;
    color: white;
    font-weight: 500;
    border-radius: 4px;
    display: inline-block;
  }
}
</style>
