<template>
  <div class="app-container">
    <!-- 搜索栏 -->
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="输入姓名" prop="name">
        <el-input
            v-model="queryParams.name"
            placeholder="输入员工姓名"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="考勤状态" prop="attendanceStatus">
        <el-select v-model="queryParams.attendanceStatus" placeholder="请选择状态" style="width: 180px;">
          <el-option label="全部" value="" />
          <el-option label="出勤" value="Present" />
          <el-option label="缺勤" value="Absent" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 数据表格 -->
    <el-table v-loading="loading" :data="attendanceRecords" border>
      <el-table-column label="序号" type="index" align="center" width="50" />
      <el-table-column label="员工姓名" align="center" prop="name" />
      <el-table-column label="部门" align="center" prop="department" />
      <el-table-column label="访问位置" align="center" prop="accessLocation" />
      <el-table-column label="访问时间" align="center">
        <template #default="scope">
          {{ scope.row.accessTime }}
        </template>
      </el-table-column>
      <el-table-column label="访问结果" align="center" prop="accessResult" />
      <el-table-column label="考勤状态" align="center" width="100">
        <template #default="scope">
          <el-tag :type="getStatusType(scope.row.attendanceStatus)">
            {{ scope.row.attendanceStatus==='Present'?'出勤':'缺勤' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="相关事件" align="center" prop="relatedEvent" />
      <el-table-column label="日访问次数" align="center" prop="dailyAccessCount" />
    </el-table>

    <!-- 分页器 -->
    <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />
  </div>
</template>
<script setup name="AttendanceRecords">
import { listAccessControlAttendance } from "@/api/accessControl/accessControl";
import { ref, reactive } from "vue";

// 定义响应式变量
const queryRef = ref(null);
const attendanceRecords = ref([]); // 考勤记录数据
const loading = ref(false); // 加载状态
const showSearch = ref(true); // 是否显示搜索栏
const total = ref(0); // 总记录数

// 查询参数
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  name: '',
  attendanceStatus: '',
  interfaceName: "考勤记录"
});

// 获取考勤记录列表
const getList = async () => {
  try {
    loading.value = true;
    const response = await listAccessControlAttendance(queryParams);
    attendanceRecords.value = response.data.list || []; // 确保数据格式正确
    total.value = response.data.total || 0; // 确保总记录数正确
  } catch (error) {
    console.error('获取数据失败:', error);
  } finally {
    loading.value = false;
  }
};

// 搜索按钮操作
function handleQuery() {
  queryParams.pageNum = 1; // 重置到第一页
  getList();
}

// 重置按钮操作
function resetQuery() {
  queryRef.value.resetFields(); // 重置表单字段
  handleQuery(); // 重新查询
}

// 初始化加载数据
getList();

// 根据考勤状态返回标签类型
function getStatusType(status) {
  if (status === "Present") return "success"; // 出勤
  if (status === "Absent") return "danger"; // 缺勤
  return "info"; // 默认
}
</script>
<style scoped>
.el-table .cell {
  white-space: nowrap;
}
</style>
