<!-- 用户管理 -->
<template>
  <div class="app-container">
    <el-card shadow="never" class="table-container">
      <template #header>
        <div class="flex justify-between">
          <div>
            <el-button type="primary" @click="handleAdd()"><i-ep-plus />工具链</el-button>
          </div>
          <div>
            <el-form ref="queryFormRef" :model="queryParams" :inline="true">
              <el-form-item label="工具链名称" prop="keywords">
                <el-select
                  v-model="queryParams.tool_type"
                  placeholder="请选择工具类型"
                  clearable
                  style="width: 200px"
                  @change="handleQuery"
                >
                  <el-option
                    v-for="item in listOpt.typeList"
                    :key="item.type"
                    :label="item.name"
                    :value="item.type"
                  />
                </el-select>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="handleQuery"
                  ><i-ep-search />搜索</el-button
                >
                <el-button @click="resetQuery">
                  <i-ep-refresh />
                  重置</el-button
                >
              </el-form-item>
            </el-form>
          </div>
        </div>
      </template>
      <el-table
        v-loading="loading"
        :data="pageData"
        stripe
        @selection-change="handleSelectionChange"
      >
        <el-table-column label="工具链名称" align="left" prop="tool_name" width="250" />
        <el-table-column label="工具类型" align="left" prop="tool_type_name" />
        <el-table-column label="匹配硬件" align="left" prop="cmpt_hardware_type" />
        <el-table-column label="工作状态" align="left" prop="working_state_name" />
        <el-table-column label="连接状态" align="left" prop="connection_state" />
        <el-table-column label="操作" fixed="right" width="280">
          <template #default="scope">
            <el-button text type="primary" size="small" @click="handleDetail(scope.row)"
              ><i-ep-edit />查看</el-button
            >
            <el-button text type="primary" size="small" @click="handleDelete(scope.row)"
              ><i-ep-delete />删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-if="total > 0"
        v-model:total="total"
        v-model:page="queryParams.page_num"
        v-model:limit="queryParams.page_size"
        @pagination="handleQuery"
      />
    </el-card>
  </div>
</template>

<script setup lang="ts">
import ToolChainshApi from "@/api/tool";

const router = useRouter();

const loading = ref(false); //  加载状态
const total = ref(0); // 数据总数
const pageData = ref([]); // 分页数据
const removeIds = ref([]); // 删除ID集合 用于批量删除
const queryFormRef = ref(ElForm); // 查询表单
const queryParams = reactive({
  page_num: 1,
  page_size: 10,
  tool_type: null,
});

// 基础数据列表
let listOpt = reactive({
  typeList: [
    {
      type: "quantizer",
      name: "量化工具",
    },
    {
      type: "compiler",
      name: "编译工具",
    },
    {
      type: "label",
      name: "标注工具",
    },
    {
      type: "other",
      name: "其他工具",
    },
  ],
});

/** 查询 */
function handleQuery() {
  loading.value = true;
  ToolChainshApi.toolStatusList(queryParams)
    .then((res) => {
      pageData.value = res.data.data.tool_status_list;
      total.value = res.data.data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}

/** 重置查询 */
function resetQuery() {
  queryFormRef.value.resetFields();
  queryParams.page_num = 1;
  queryParams.tool_type = null;
  handleQuery();
}

/** 行选中 */
function handleSelectionChange(selection: any) {
  removeIds.value = selection.map((item: any) => item.id);
}

/** 删除数据 */
function handleDelete(row: { [key: string]: any }) {
  ElMessageBox.confirm("确认删除工具链信息?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(function () {
    ToolChainshApi.deleteTool(row.tool_id).then((res) => {
      ElMessage.success("删除成功");
      resetQuery();
    });
  });
}

/** 查看第三方工具链 */
function handleDetail(row: { [key: string]: any }) {
  router.push({ path: "/tester/otherToolDetail", query: { id: row.tool_id } });
}

/** 新增第三方工具链 */
function handleAdd() {
  router.push({ path: "/tester/otherToolAdd" });
}

onMounted(() => {
  handleQuery();
});
</script>