<?php
session_start();
include 'config.php';

$allowed_roles = ['superadmin', 'hr', 'md', 'admin', 'manager'];
if (!isset($_SESSION['role']) || !in_array($_SESSION['role'], $allowed_roles)) {
    header("Location: login.php"); exit();
}

$current_user_role = $_SESSION['role'];
$current_user_name = $_SESSION['username'] ?? 'Admin';

function h($s){ return htmlspecialchars($s??'',ENT_QUOTES,'UTF-8'); }

$users_stmt = $conn->prepare("SELECT id, username, first_name, last_name FROM users WHERE role = 'employee' ORDER BY first_name, last_name");
$users_stmt->execute();
$all_employees = $users_stmt->get_result()->fetch_all(MYSQLI_ASSOC);

$selected_date    = $_GET['att_date'] ?? date('Y-m-d');
$selected_user_id = $_GET['user_id']  ?? 'all';

$att_sql = "SELECT ma.*, u.username, CONCAT(u.first_name,' ',u.last_name) AS full_name, u.photo FROM mobileattendance ma JOIN users u ON ma.user_id=u.id WHERE ma.attendance_date=?";
$params  = [$selected_date]; $types = "s";
if ($selected_user_id !== 'all') { $att_sql .= " AND ma.user_id=?"; $params[]=$selected_user_id; $types.="i"; }
$att_sql .= " ORDER BY ma.login_time DESC";
$stmt = $conn->prepare($att_sql); $stmt->bind_param($types,...$params); $stmt->execute();
$date_attendance = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);

$today = date('Y-m-d');
$today_stmt = $conn->prepare("SELECT ma.*, CONCAT(u.first_name,' ',u.last_name) AS full_name, u.photo FROM mobileattendance ma JOIN users u ON ma.user_id=u.id WHERE ma.attendance_date=? ORDER BY ma.login_time DESC");
$today_stmt->bind_param("s",$today); $today_stmt->execute();
$today_attendance = $today_stmt->get_result()->fetch_all(MYSQLI_ASSOC);

/* Stats */
$total_today  = count($today_attendance);
$active_count = count(array_filter($today_attendance, fn($r) => empty($r['logout_time'])));
$done_count   = count(array_filter($today_attendance, fn($r) => !empty($r['logout_time'])));
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>MPC · Daily Attendance Report</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Sora:wght@300;400;500;600;700&family=JetBrains+Mono:wght@400;500&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<style>
*,*::before,*::after{box-sizing:border-box;margin:0;padding:0;}
:root{
  --ink:#0f172a;--ink-2:#334155;--muted:#94a3b8;--border:#e2e8f0;
  --surface:#ffffff;--bg:#f1f5f9;--sidebar-bg:#0f172a;--sidebar-w:260px;
  --radius:12px;--shadow:0 1px 3px rgba(15,23,42,.06),0 4px 16px rgba(15,23,42,.06);
  --accent:#3b82f6;--success:#10b981;--danger:#ef4444;--warn:#f59e0b;
  font-family:'Sora',system-ui,sans-serif;
}
html,body{height:100%;}
body{background:var(--bg);color:var(--ink);font-size:14px;line-height:1.6;-webkit-font-smoothing:antialiased;}
.layout{display:flex;min-height:100vh;}

/* ── SIDEBAR ── */
.sidebar{width:var(--sidebar-w);background:var(--sidebar-bg);position:fixed;inset:0 auto 0 0;z-index:200;display:flex;flex-direction:column;overflow-y:auto;overflow-x:hidden;transition:transform .3s cubic-bezier(.4,0,.2,1);scrollbar-width:none;}
.sidebar::-webkit-scrollbar{display:none;}
.sidebar.hidden{transform:translateX(-100%);}
.sidebar-brand{padding:24px 20px 16px;display:flex;align-items:center;gap:10px;border-bottom:1px solid rgba(255,255,255,.06);margin-bottom:8px;}
.brand-logo{width:36px;height:36px;background:linear-gradient(135deg,#3b82f6,#6366f1);border-radius:8px;display:flex;align-items:center;justify-content:center;font-size:16px;font-weight:700;color:#fff;flex-shrink:0;}
.brand-name{font-size:15px;font-weight:700;color:#fff;}
.brand-sub{font-size:10px;color:rgba(255,255,255,.4);letter-spacing:1.5px;text-transform:uppercase;margin-top:1px;}
.nav-section{padding:0 12px 6px;}
.nav-item{list-style:none;}
.nav-link,.nav-dropdown-toggle{display:flex;align-items:center;gap:10px;padding:8px 10px;border-radius:8px;color:rgba(255,255,255,.65);text-decoration:none;font-size:13px;font-weight:500;cursor:pointer;border:none;background:none;width:100%;text-align:left;transition:background .15s,color .15s;}
.nav-link:hover,.nav-dropdown-toggle:hover{background:rgba(255,255,255,.07);color:#fff;}
.nav-link.active{background:rgba(59,130,246,.2);color:#60a5fa;}
.nav-link i,.nav-dropdown-toggle i{width:16px;text-align:center;font-size:13px;}
.nav-dropdown-toggle .arrow{margin-left:auto;transition:transform .2s;font-size:10px;}
.nav-dropdown-toggle.open .arrow{transform:rotate(180deg);}
.nav-submenu{list-style:none;overflow:hidden;max-height:0;transition:max-height .3s ease;padding-left:26px;}
.nav-submenu.open{max-height:600px;}
.nav-submenu .nav-link{font-size:12px;padding:6px 10px;color:rgba(255,255,255,.5);}
.nav-submenu .nav-link:hover{color:#fff;background:rgba(255,255,255,.05);}
.sidebar-footer{margin-top:auto;padding:12px;border-top:1px solid rgba(255,255,255,.06);}

/* ── CONTENT ── */
.content{flex:1;margin-left:var(--sidebar-w);min-width:0;display:flex;flex-direction:column;transition:margin .3s cubic-bezier(.4,0,.2,1);}
.content.expanded{margin-left:0;}

/* ── TOPBAR ── */
.topbar{height:60px;background:var(--surface);border-bottom:1px solid var(--border);display:flex;align-items:center;gap:12px;padding:0 24px;position:sticky;top:0;z-index:100;}
.hamburger-btn{width:36px;height:36px;border-radius:8px;border:1px solid var(--border);background:var(--surface);display:flex;align-items:center;justify-content:center;cursor:pointer;color:var(--ink-2);font-size:15px;flex-shrink:0;transition:background .15s;}
.hamburger-btn:hover{background:var(--bg);}
.topbar-breadcrumb{display:flex;align-items:center;gap:6px;font-size:13px;color:var(--muted);}
.topbar-breadcrumb .current{color:var(--ink);font-weight:600;}
.topbar-spacer{flex:1;}
.topbar-user{display:flex;align-items:center;gap:8px;font-size:13px;color:var(--ink-2);font-weight:500;}
.topbar-avatar{width:30px;height:30px;border-radius:50%;background:linear-gradient(135deg,#3b82f6,#6366f1);display:flex;align-items:center;justify-content:center;color:#fff;font-size:12px;font-weight:700;}

/* ── PAGE ── */
.page{padding:24px;flex:1;}
.page-header{margin-bottom:20px;display:flex;align-items:flex-start;justify-content:space-between;flex-wrap:wrap;gap:12px;}
.page-title{font-size:22px;font-weight:700;color:var(--ink);letter-spacing:-.4px;}
.page-subtitle{font-size:13px;color:var(--muted);margin-top:2px;}
.role-badge{display:inline-flex;align-items:center;gap:5px;padding:4px 10px;border-radius:20px;font-size:11px;font-weight:700;background:rgba(59,130,246,.1);color:var(--accent);}

/* ── KPI CARDS ── */
.kpi-grid{display:grid;grid-template-columns:repeat(auto-fit,minmax(150px,1fr));gap:14px;margin-bottom:22px;}
.kpi-card{background:var(--surface);border:1px solid var(--border);border-radius:var(--radius);padding:16px 18px;position:relative;overflow:hidden;transition:box-shadow .18s,transform .18s;box-shadow:var(--shadow);}
.kpi-card:hover{box-shadow:0 4px 20px rgba(0,0,0,.08);transform:translateY(-2px);}
.kpi-card::before{content:'';position:absolute;top:0;left:0;right:0;height:3px;}
.kpi-card.blue::before{background:var(--accent);}
.kpi-card.green::before{background:var(--success);}
.kpi-card.warn::before{background:var(--warn);}
.kpi-card.purple::before{background:#7c3aed;}
.kpi-icon{width:36px;height:36px;border-radius:8px;display:flex;align-items:center;justify-content:center;font-size:14px;margin-bottom:10px;}
.kpi-icon.blue{background:#dbeafe;color:var(--accent);}
.kpi-icon.green{background:#d1fae5;color:var(--success);}
.kpi-icon.warn{background:#fef3c7;color:var(--warn);}
.kpi-icon.purple{background:#f5f3ff;color:#7c3aed;}
.kpi-label{font-size:10px;font-weight:700;text-transform:uppercase;letter-spacing:.8px;color:var(--muted);}
.kpi-value{font-family:'JetBrains Mono',monospace;font-size:1.75rem;font-weight:700;color:var(--ink);line-height:1.1;margin:4px 0 2px;}
.kpi-meta{font-size:11px;color:var(--muted);}

/* ── FILTER CARD ── */
.filter-card{background:var(--surface);border:1px solid var(--border);border-radius:var(--radius);padding:20px;margin-bottom:22px;box-shadow:var(--shadow);}
.filter-title{font-size:14px;font-weight:700;color:var(--ink);margin-bottom:16px;display:flex;align-items:center;gap:8px;}
.filter-row{display:flex;flex-wrap:wrap;gap:12px;align-items:flex-end;}
.filter-group{display:flex;flex-direction:column;gap:4px;flex:1;min-width:140px;}
.filter-label{font-size:10px;font-weight:700;text-transform:uppercase;letter-spacing:.8px;color:var(--muted);}
.form-ctrl{padding:8px 10px;border:1px solid var(--border);border-radius:8px;font-size:13px;font-family:'Sora',sans-serif;color:var(--ink);background:var(--bg);outline:none;transition:border-color .15s;width:100%;}
.form-ctrl:focus{border-color:var(--accent);box-shadow:0 0 0 3px rgba(59,130,246,.1);background:var(--surface);}
.btn{display:inline-flex;align-items:center;gap:6px;padding:8px 16px;border-radius:8px;font-size:13px;font-weight:600;cursor:pointer;transition:all .15s;border:none;font-family:'Sora',sans-serif;text-decoration:none;white-space:nowrap;}
.btn-primary{background:var(--accent);color:#fff;}
.btn-primary:hover{background:#1d4ed8;color:#fff;}
.btn-ghost{background:transparent;color:var(--ink-2);border:1px solid var(--border);}
.btn-ghost:hover{background:var(--bg);}

/* ── TABLE CARD ── */
.table-card{background:var(--surface);border:1px solid var(--border);border-radius:var(--radius);overflow:hidden;box-shadow:var(--shadow);margin-bottom:24px;}
.table-header{padding:14px 20px;border-bottom:1px solid var(--border);display:flex;align-items:center;gap:10px;background:var(--ink);}
.table-title{color:#fff;font-weight:700;font-size:14px;flex:1;display:flex;align-items:center;gap:8px;}
.table-badge{background:rgba(255,255,255,.15);color:#fff;padding:2px 8px;border-radius:10px;font-size:11px;}
.table-scroll{overflow-x:auto;}
.data-table{width:100%;border-collapse:collapse;font-size:13px;}
.data-table thead th{background:#1e293b;color:rgba(255,255,255,.85);padding:10px 12px;font-size:10px;font-weight:700;text-transform:uppercase;letter-spacing:.06em;white-space:nowrap;border-bottom:2px solid #334155;}
.data-table tbody td{padding:10px 12px;vertical-align:middle;border-bottom:1px solid var(--border);}
.data-table tbody tr:last-child td{border-bottom:none;}
.data-table tbody tr:hover{background:#f8faff;}
.emp-photo{width:38px;height:38px;border-radius:10px;object-fit:cover;border:1.5px solid var(--border);}
.emp-photo-placeholder{width:38px;height:38px;border-radius:10px;background:linear-gradient(135deg,var(--accent),#6366f1);display:flex;align-items:center;justify-content:center;color:#fff;font-size:14px;font-weight:700;}
.map-link{display:inline-flex;align-items:center;gap:4px;color:var(--accent);font-weight:600;text-decoration:none;font-size:12px;padding:3px 8px;background:#dbeafe;border-radius:6px;}
.map-link:hover{background:var(--accent);color:#fff;}
.time-badge{font-family:'JetBrains Mono',monospace;font-size:12px;font-weight:600;color:var(--ink-2);}
.status-active{display:inline-flex;align-items:center;gap:5px;padding:3px 10px;border-radius:20px;font-size:11px;font-weight:700;background:#fef3c7;color:#92400e;}
.status-done{display:inline-flex;align-items:center;gap:5px;padding:3px 10px;border-radius:20px;font-size:11px;font-weight:700;background:#d1fae5;color:#065f46;}
.hours-badge{font-family:'JetBrains Mono',monospace;font-weight:700;font-size:12px;padding:3px 10px;border-radius:20px;background:#d1fae5;color:#065f46;}
.selfie-link{display:inline-flex;align-items:center;gap:4px;color:var(--success);font-weight:600;text-decoration:none;font-size:12px;padding:3px 8px;background:#d1fae5;border-radius:6px;}
.selfie-link:hover{background:var(--success);color:#fff;}

/* ── TODAY GRID ── */
.emp-grid{display:grid;grid-template-columns:repeat(auto-fill,minmax(220px,1fr));gap:16px;}
.emp-card{background:var(--surface);border:1px solid var(--border);border-radius:var(--radius);overflow:hidden;box-shadow:var(--shadow);transition:transform .2s,box-shadow .2s;}
.emp-card:hover{transform:translateY(-4px);box-shadow:0 8px 24px rgba(0,0,0,.1);}
.emp-card-head{background:linear-gradient(135deg,#0f172a,#1e3a8a 50%,#2563eb 100%);padding:18px;display:flex;flex-direction:column;align-items:center;gap:10px;position:relative;}
.emp-card-photo{width:60px;height:60px;border-radius:50%;object-fit:cover;border:3px solid rgba(255,255,255,.3);}
.emp-card-photo-ph{width:60px;height:60px;border-radius:50%;background:rgba(255,255,255,.2);display:flex;align-items:center;justify-content:center;color:#fff;font-size:22px;font-weight:700;border:3px solid rgba(255,255,255,.3);}
.emp-card-name{color:#fff;font-weight:700;font-size:14px;text-align:center;}
.emp-card-body{padding:14px;}
.emp-info-row{display:flex;justify-content:space-between;align-items:center;font-size:12px;color:var(--ink-2);margin-bottom:6px;}
.emp-info-row strong{color:var(--ink);}
.emp-card-foot{padding:10px 14px;background:var(--bg);border-top:1px solid var(--border);display:flex;gap:6px;flex-wrap:wrap;}

/* ── EMPTY ── */
.empty-state{text-align:center;padding:60px 20px;color:var(--muted);}
.empty-state i{font-size:3rem;opacity:.3;display:block;margin-bottom:12px;}

/* ── SECTION HEADER ── */
.section-header{font-size:16px;font-weight:700;color:var(--ink);margin-bottom:14px;display:flex;align-items:center;gap:8px;}

@media(max-width:768px){.sidebar{transform:translateX(-100%);}.sidebar:not(.hidden){transform:none;}.content{margin-left:0;}.page{padding:14px;}.kpi-grid{grid-template-columns:repeat(2,1fr);}.emp-grid{grid-template-columns:1fr 1fr;}}
</style>
</head>
<body>
<div class="layout">

<!-- ── SIDEBAR ── -->
<nav class="sidebar" id="sidebar">
  <div class="sidebar-brand">
    <div class="brand-logo">M</div>
    <div><div class="brand-name">MPC</div><div class="brand-sub">Admin Portal</div></div>
  </div>
  <nav class="nav-section">
    <ul style="list-style:none">
      <li class="nav-item"><a href="dashboard_admin.php" class="nav-link"><i class="fas fa-sitemap"></i> Organisation Chart</a></li>
      <li class="nav-item">
        <button class="nav-dropdown-toggle" onclick="toggleMenu(this)"><i class="fas fa-folder-open"></i> DAP<i class="fas fa-chevron-down arrow"></i></button>
        <ul class="nav-submenu">
          <li><a href="manage_users.php"     class="nav-link"><i class="fas fa-users"></i>        Employee Details</a></li>
          <li><a href="manage_documents.php" class="nav-link"><i class="fas fa-file-alt"></i>     Policies Rolled Out</a></li>
          <li><a href="digital.php"          class="nav-link"><i class="fas fa-check-circle"></i> Acceptance Confirmed</a></li>
          <li><a href="change_password.php"  class="nav-link"><i class="fas fa-key"></i>          Change Password</a></li>
        </ul>
      </li>
      <li class="nav-item">
        <button class="nav-dropdown-toggle open" onclick="toggleMenu(this)"><i class="fas fa-calendar-alt"></i> CAB<i class="fas fa-chevron-down arrow"></i></button>
        <ul class="nav-submenu open">
          <li><a href="attendance.php"              class="nav-link"><i class="fas fa-clock"></i>             Attendance Dashboard</a></li>
          <li><a href="payroll.php"                 class="nav-link"><i class="fas fa-money-bill"></i>        Payroll Dashboard</a></li>
          <li><a href="mobile_attendance.php"       class="nav-link active"><i class="fas fa-mobile-alt"></i> Daily Attendance Report</a></li>
          <li><a href="daily_reports_dashboard.php" class="nav-link"><i class="fas fa-chart-line"></i>       Daily Report Dashboard</a></li>
          <li><a href="company_holidays.php"        class="nav-link"><i class="fas fa-star"></i>             Company Holidays</a></li>
          <li><a href="hr_approval_salary.php"      class="nav-link"><i class="fas fa-hand-holding-usd"></i> Advance Dashboard</a></li>
          <li><a href="expense_dashboard.php"       class="nav-link"><i class="fas fa-receipt"></i>          Expenses Dashboard</a></li>
          <li><a href="leave_dashboard.php"         class="nav-link"><i class="fas fa-umbrella-beach"></i>   Leave Dashboard</a></li>
          <li><a href="grievance_dashboard.php"     class="nav-link"><i class="fas fa-comment-alt"></i>      Grievance Dashboard</a></li>
        </ul>
      </li>
      <li class="nav-item">
        <button class="nav-dropdown-toggle" onclick="toggleMenu(this)"><i class="fas fa-user-tie"></i> PEP<i class="fas fa-chevron-down arrow"></i></button>
        <ul class="nav-submenu">
          <li><a href="individual_rr.php"     class="nav-link"><i class="fas fa-trophy"></i>   Individual R &amp; R</a></li>
          <li><a href="individual_kpi.php"    class="nav-link"><i class="fas fa-bullseye"></i> Individual KPI</a></li>
          <li><a href="login_credentials.php" class="nav-link"><i class="fas fa-id-card"></i>  Login Credentials</a></li>
        </ul>
      </li>
      <li class="nav-item">
        <button class="nav-dropdown-toggle" onclick="toggleMenu(this)"><i class="fas fa-book-open"></i> L&amp;D<i class="fas fa-chevron-down arrow"></i></button>
        <ul class="nav-submenu">
          <li><a href="training_dashboard.php" class="nav-link"><i class="fas fa-chalkboard-teacher"></i> Training Portal</a></li>
          <li><a href="rr.php"                 class="nav-link"><i class="fas fa-award"></i>              Rewards &amp; Recognition</a></li>
        </ul>
      </li>
      <li class="nav-item">
        <button class="nav-dropdown-toggle" onclick="toggleMenu(this)"><i class="fas fa-star"></i> AAA<i class="fas fa-chevron-down arrow"></i></button>
        <ul class="nav-submenu">
          <li><a href="employee_tiy.php"      class="nav-link"><i class="fas fa-user-check"></i> Employee TIY</a></li>
          <li><a href="login_credentials.php" class="nav-link"><i class="fas fa-id-card"></i>   Login Credentials</a></li>
        </ul>
      </li>
    </ul>
  </nav>
  <div class="sidebar-footer">
    <a href="logout.php" class="nav-link" style="color:#f87171;"><i class="fas fa-sign-out-alt"></i> Logout</a>
  </div>
</nav>

<!-- ── CONTENT ── -->
<div class="content" id="content">
  <div class="topbar">
    <button class="hamburger-btn" onclick="toggleSidebar()"><i class="fas fa-bars"></i></button>
    <div class="topbar-breadcrumb">
      <span>CAB</span>
      <i class="fas fa-chevron-right" style="font-size:9px;color:var(--border)"></i>
      <span class="current">Daily Attendance Report</span>
    </div>
    <div class="topbar-spacer"></div>
    <div class="topbar-user">
      <div class="topbar-avatar"><?= strtoupper(substr($current_user_name,0,1)) ?></div>
      <?= h($current_user_name) ?>
    </div>
  </div>

  <div class="page">
    <div class="page-header">
      <div>
        <div class="page-title"><i class="fas fa-mobile-alt" style="color:var(--accent);margin-right:8px;"></i>Daily Attendance Report</div>
        <div class="page-subtitle">Mobile check-in/out records &amp; location tracking</div>
      </div>
      <span class="role-badge"><i class="fas fa-shield-alt"></i> <?= strtoupper($current_user_role) ?></span>
    </div>

    <!-- KPI CARDS -->
    <div class="kpi-grid">
      <div class="kpi-card blue">
        <div class="kpi-icon blue"><i class="fas fa-users"></i></div>
        <div class="kpi-label">Present Today</div>
        <div class="kpi-value"><?= $total_today ?></div>
        <div class="kpi-meta"><?= date('d M Y') ?></div>
      </div>
      <div class="kpi-card warn">
        <div class="kpi-icon warn"><i class="fas fa-user-clock"></i></div>
        <div class="kpi-label">Still Working</div>
        <div class="kpi-value"><?= $active_count ?></div>
        <div class="kpi-meta">Active / logged in</div>
      </div>
      <div class="kpi-card green">
        <div class="kpi-icon green"><i class="fas fa-user-check"></i></div>
        <div class="kpi-label">Checked Out</div>
        <div class="kpi-value"><?= $done_count ?></div>
        <div class="kpi-meta">Completed day</div>
      </div>
      <div class="kpi-card purple">
        <div class="kpi-icon purple"><i class="fas fa-search"></i></div>
        <div class="kpi-label">Date Queried</div>
        <div class="kpi-value"><?= count($date_attendance) ?></div>
        <div class="kpi-meta"><?= date('d M Y',strtotime($selected_date)) ?></div>
      </div>
    </div>

    <!-- FILTER CARD -->
    <div class="filter-card">
      <div class="filter-title"><i class="fas fa-search" style="color:var(--accent)"></i> Search Attendance by Date</div>
      <form method="get" class="filter-row">
        <div class="filter-group">
          <span class="filter-label">Select Date</span>
          <input type="date" name="att_date" class="form-ctrl" value="<?= h($selected_date) ?>" required>
        </div>
        <div class="filter-group">
          <span class="filter-label">Employee</span>
          <select name="user_id" class="form-ctrl">
            <option value="all">All Employees</option>
            <?php foreach($all_employees as $emp): ?>
            <option value="<?= $emp['id'] ?>" <?= $selected_user_id==$emp['id']?'selected':'' ?>>
              <?= h($emp['first_name'].' '.$emp['last_name']) ?>
            </option>
            <?php endforeach; ?>
          </select>
        </div>
        <div style="display:flex;align-items:flex-end;">
          <button type="submit" class="btn btn-primary"><i class="fas fa-search"></i> View Attendance</button>
        </div>
      </form>

      <?php if(!empty($date_attendance)): ?>
      <div style="margin-top:20px;">
        <div class="section-header">
          <i class="fas fa-calendar-day" style="color:var(--accent)"></i>
          Attendance on <?= date('d M Y',strtotime($selected_date)) ?>
          <span style="background:rgba(59,130,246,.1);color:var(--accent);padding:3px 10px;border-radius:20px;font-size:11px;font-weight:700;"><?= count($date_attendance) ?> record<?= count($date_attendance)>1?'s':'' ?></span>
        </div>
        <div class="table-scroll">
          <table class="data-table">
            <thead>
              <tr>
                <th>Photo</th><th>Name</th><th>Login</th><th>Login Location</th>
                <th>Logout</th><th>Logout Location</th><th>Hours</th><th>Status</th><th>Selfie</th>
              </tr>
            </thead>
            <tbody>
              <?php foreach($date_attendance as $row):
                $login_map  = $row['latitude']&&$row['longitude']  ? "https://maps.google.com/?q={$row['latitude']},{$row['longitude']}" : '';
                $logout_map = $row['logout_latitude']&&$row['logout_longitude'] ? "https://maps.google.com/?q={$row['logout_latitude']},{$row['logout_longitude']}" : '';
                $hours = ($row['logout_time']&&$row['login_time'])
                  ? round((strtotime($row['logout_time'])-strtotime($row['login_time']))/3600,2).' hrs'
                  : ($row['logout_time']?'—':'In Progress');
                $is_active = empty($row['logout_time']);
              ?>
              <tr>
                <td>
                  <?php if($row['photo']): ?>
                  <img src="<?= h($row['photo']) ?>" class="emp-photo" onerror="this.style.display='none'">
                  <?php else: ?>
                  <div class="emp-photo-placeholder"><?= strtoupper(substr($row['full_name'],0,1)) ?></div>
                  <?php endif; ?>
                </td>
                <td><strong><?= h($row['full_name']) ?></strong></td>
                <td><span class="time-badge"><?= $row['login_time'] ? date('h:i A',strtotime($row['login_time'])) : '—' ?></span></td>
                <td><?= $login_map ? "<a href='$login_map' target='_blank' class='map-link'><i class='fas fa-map-marker-alt'></i> View</a>" : '<span style="color:var(--muted)">—</span>' ?></td>
                <td><span class="time-badge"><?= $row['logout_time'] ? date('h:i A',strtotime($row['logout_time'])) : '<span class="status-active"><i class="fas fa-circle" style="font-size:6px"></i> Active</span>' ?></span></td>
                <td><?= $logout_map ? "<a href='$logout_map' target='_blank' class='map-link'><i class='fas fa-map-marker-alt'></i> View</a>" : '<span style="color:var(--muted)">—</span>' ?></td>
                <td><span class="hours-badge"><?= $hours ?></span></td>
                <td><?= $is_active ? '<span class="status-active"><i class="fas fa-circle" style="font-size:6px"></i> Working</span>' : '<span class="status-done"><i class="fas fa-check-circle"></i> Done</span>' ?></td>
                <td>
                  <?php if($row['photo_path']&&file_exists($row['photo_path'])): ?>
                  <a href="<?= h($row['photo_path']) ?>" target="_blank" class="selfie-link"><i class="fas fa-camera"></i> View</a>
                  <?php else: ?>
                  <span style="color:var(--muted);font-size:12px;">—</span>
                  <?php endif; ?>
                </td>
              </tr>
              <?php endforeach; ?>
            </tbody>
          </table>
        </div>
      </div>
      <?php else: ?>
      <div class="empty-state" style="padding:40px 20px;">
        <i class="fas fa-calendar-times"></i>
        <div style="font-size:14px;font-weight:600;">No attendance records found for <?= date('d M Y',strtotime($selected_date)) ?></div>
        <div style="font-size:12px;margin-top:4px;">Try selecting a different date or employee</div>
      </div>
      <?php endif; ?>
    </div>

    <!-- TODAY'S ATTENDANCE GRID -->
    <div class="table-card">
      <div class="table-header">
        <div class="table-title">
          <i class="fas fa-users"></i>
          Today's Attendance — <?= date('d M Y') ?>
          <span class="table-badge"><?= $total_today ?> present</span>
          <?php if($active_count>0): ?><span class="table-badge" style="background:rgba(245,158,11,.3)"><?= $active_count ?> still working</span><?php endif; ?>
        </div>
      </div>
      <div style="padding:20px;">
        <?php if(empty($today_attendance)): ?>
        <div class="empty-state">
          <i class="fas fa-user-slash"></i>
          <div style="font-size:14px;font-weight:600;">No attendance marked today yet</div>
          <div style="font-size:12px;margin-top:4px;">Check back later or refresh the page</div>
        </div>
        <?php else: ?>
        <div class="emp-grid">
          <?php foreach($today_attendance as $emp):
            $map_url = $emp['latitude']&&$emp['longitude'] ? "https://maps.google.com/?q={$emp['latitude']},{$emp['longitude']}" : '#';
            $hours   = ($emp['logout_time']&&$emp['login_time'])
              ? round((strtotime($emp['logout_time'])-strtotime($emp['login_time']))/3600,2).' hrs'
              : ($emp['logout_time']?'Completed':'In Progress');
            $is_active = empty($emp['logout_time']);
          ?>
          <div class="emp-card">
            <div class="emp-card-head">
              <?php if($emp['photo']): ?>
              <img src="<?= h($emp['photo']) ?>" class="emp-card-photo" onerror="this.style.display='none'">
              <?php else: ?>
              <div class="emp-card-photo-ph"><?= strtoupper(substr($emp['full_name'],0,1)) ?></div>
              <?php endif; ?>
              <div class="emp-card-name"><?= h($emp['full_name']) ?></div>
              <?php if($is_active): ?>
              <span style="background:rgba(245,158,11,.3);color:#fde68a;padding:2px 10px;border-radius:20px;font-size:10px;font-weight:700;"><i class="fas fa-circle" style="font-size:6px"></i> Active</span>
              <?php else: ?>
              <span style="background:rgba(16,185,129,.2);color:#6ee7b7;padding:2px 10px;border-radius:20px;font-size:10px;font-weight:700;"><i class="fas fa-check-circle"></i> Done</span>
              <?php endif; ?>
            </div>
            <div class="emp-card-body">
              <div class="emp-info-row">
                <span><i class="fas fa-sign-in-alt" style="color:var(--success);margin-right:5px;"></i>Login</span>
                <strong class="time-badge"><?= date('h:i A',strtotime($emp['login_time'])) ?></strong>
              </div>
              <div class="emp-info-row">
                <span><i class="fas fa-sign-out-alt" style="color:var(--danger);margin-right:5px;"></i>Logout</span>
                <strong class="time-badge"><?= $emp['logout_time'] ? date('h:i A',strtotime($emp['logout_time'])) : '—' ?></strong>
              </div>
              <div class="emp-info-row">
                <span><i class="fas fa-clock" style="color:var(--accent);margin-right:5px;"></i>Hours</span>
                <strong style="color:var(--success);font-size:12px;"><?= $hours ?></strong>
              </div>
            </div>
            <?php if($emp['latitude']): ?>
            <div class="emp-card-foot">
              <a href="<?= $map_url ?>" target="_blank" class="map-link" style="flex:1;justify-content:center;"><i class="fas fa-map-marker-alt"></i> View Location</a>
            </div>
            <?php endif; ?>
          </div>
          <?php endforeach; ?>
        </div>
        <?php endif; ?>
      </div>
    </div>

  </div><!-- /page -->
</div><!-- /content -->
</div><!-- /layout -->

<script>
function toggleSidebar() {
  document.getElementById('sidebar').classList.toggle('hidden');
  document.getElementById('content').classList.toggle('expanded');
}
function toggleMenu(btn) {
  btn.nextElementSibling.classList.toggle('open');
  btn.classList.toggle('open');
}
</script>
</body>
</html>
<?php $conn->close(); ?>