<?php
// This is a SIMPLE READ-ONLY HR page simulation.
// It reads from HRMS DB directly.
$host = "localhost";
$user = "root";
$pass = "";
$dbname = "brookeho_hrms";

$conn = new mysqli($host, $user, $pass, $dbname);
if ($conn->connect_error) { die("DB connection failed"); }
$conn->set_charset("utf8mb4");

$q =
"SELECT a.att_date, a.check_in_time, a.check_out_time, a.status, a.remark,
        a.distance_m, a.accuracy_m,
        e.emp_id, e.emp_name, e.username
 FROM attendance_log a
 JOIN employee e ON a.emp_id = e.emp_id
 ORDER BY a.att_date DESC, a.check_in_time DESC
 LIMIT 500";

$res = $conn->query($q);
?>
<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>HRMS Attendance Logs</title>
  <style>
    body { font-family: Arial, sans-serif; padding:16px; background:#f8fafc; }
    h2 { margin: 0 0 12px 0; }
    table { border-collapse: collapse; width: 100%; background:white; }
    th, td { border:1px solid #e5e7eb; padding:10px; font-size:14px; vertical-align: top; }
    th { background:#f3f4f6; text-align:left; }
    .small { color:#6b7280; font-size:12px; }
  </style>
</head>
<body>
  <h2>Attendance Logs (HRMS)</h2>
  <div class="small">Read-only view. Data is written by the mobile app via API.</div>
  <br/>

  <table>
    <thead>
      <tr>
        <th>Date</th>
        <th>Employee</th>
        <th>Username</th>
        <th>Check In</th>
        <th>Check Out</th>
        <th>Status</th>
        <th>Remark</th>
        <th>Distance/Accuracy</th>
      </tr>
    </thead>
    <tbody>
      <?php if ($res && $res->num_rows > 0): ?>
        <?php while($r = $res->fetch_assoc()): ?>
          <tr>
            <td><?= htmlspecialchars($r["att_date"]) ?></td>
            <td><?= htmlspecialchars($r["emp_name"]) ?> (<?= intval($r["emp_id"]) ?>)</td>
            <td><?= htmlspecialchars($r["username"]) ?></td>
            <td><?= htmlspecialchars($r["check_in_time"] ?? "-") ?></td>
            <td><?= htmlspecialchars($r["check_out_time"] ?? "-") ?></td>
            <td><?= htmlspecialchars($r["status"]) ?></td>
            <td><?= htmlspecialchars($r["remark"] ?? "") ?></td>
            <td>
              <?= htmlspecialchars($r["distance_m"] ?? "-") ?> m
              /
              <?= htmlspecialchars($r["accuracy_m"] ?? "-") ?> m
            </td>
          </tr>
        <?php endwhile; ?>
      <?php else: ?>
        <tr><td colspan="8">No records found.</td></tr>
      <?php endif; ?>
    </tbody>
  </table>
</body>
</html>