<!-- 用户管理 -->
<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="handleAddPcie()"><i-ep-plus />PCLE设备</el-button>
            <el-button type="primary" @click="handleAddNet()"><i-ep-plus />网络设备</el-button>
          </div>
          <!-- <div>
            <el-form ref="queryFormRef" :model="queryParams" :inline="true">
              <el-form-item label="设备名称" prop="keywords">
                <el-input
                  v-model="queryParams.keywords"
                  placeholder="请输入设备名称"
                  clearable
                  style="width: 200px"
                  @keyup.enter="handleQuery"
                />
              </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="device_name" width="250" />
        <el-table-column label="芯片信息" align="left" prop="hardware_chip" />
        <el-table-column label="推理任务名称" align="left" prop="infer_task_name" />
        <el-table-column label="模型名称" align="left" prop="model_name" />
        <el-table-column label="模型类型" align="left" prop="modl_net_type" />
        <el-table-column label="连接状态" align="left" prop="connection_state" />
        <el-table-column label="工作状态" align="left" prop="working_state_name" />
        <el-table-column label="CPU使用率" width="120" align="left" prop="cpu_usage">
          <template #default="scope">
            <el-tag :type="scope.row.cpu_usage > 1024 ? 'danger' : 'success'" effect="dark">
              {{ scope.row.cpu_usage + " MB" }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="内核温度" width="80" align="left" prop="tempurature">
          <template #default="scope">
            <el-tag :type="scope.row.tempurature > 80 ? 'danger' : 'success'" effect="plain">
              {{ scope.row.tempurature + " ℃" }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" width="320">
          <template #default="scope">
            <el-button text type="primary" size="small" @click="handleDetail(scope.row)"><i-ep-view />详细</el-button>
            <el-button text type="primary" size="small" @click="handleUpdate(scope.row)"><i-ep-edit />修改</el-button>
            <el-button v-if="scope.row.id != 2" text type="primary" size="small"
              @click="openUpGrades(scope.row)"><i-ep-upload />升级</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.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="handleQuery"
      /> -->
    </el-card>
  </div>
</template>

<script setup lang="ts">
import DeviceApi from "@/api/device";

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,
});

/** 设备状态列表 */
function handleQuery() {
  loading.value = true;
  DeviceApi.getStatus(queryParams)
    .then((res) => {
      pageData.value = res.data.data.device_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 () {
    DeviceApi.deleteDevices(row.device_id).then((res) => {
      if (res.data.code == 0) {
        ElMessage.success("删除成功");
        resetQuery();
      }
    });
  });
}

/** 新增Pcie设备 */
function handleAddPcie() {
  router.push({ path: "/tester/equipmentAddPcie" });
}

/** 新增网络设备 */
function handleAddNet() {
  router.push({ path: "/tester/equipmentAddNet" });
}

/** 修改设备信息 */
function handleUpdate(row: { [key: string]: any }) {
  router.push({ path: "/tester/equipmentEdit", query: { id: row.device_id } });
}

/** 设备信息详情 */
function handleDetail(row: { [key: string]: any }) {
  router.push({ path: "/tester/equipmentDetail", query: { id: row.device_id } });
}

/** 设备升级 */
function openUpGrades(row: { [key: string]: any }) {
  ElMessage.error("【error】待提供！");
}

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