<?php
session_start();
require_once '../config.php';

if (!isset($_SESSION['admin_id'])) {
    header('Location: login.php');
    exit();
}

$page_title = 'Admin Dashboard';

$stats['total_patients']    = $conn->query("SELECT COUNT(*) FROM patients")->fetch_row()[0];
$stats['total_doctors']     = $conn->query("SELECT COUNT(*) FROM doctors")->fetch_row()[0];
$stats['pending_doctors']   = $conn->query("SELECT COUNT(*) FROM doctors WHERE verification_status='pending'")->fetch_row()[0];
$stats['total_appointments']= $conn->query("SELECT COUNT(*) FROM appointments")->fetch_row()[0];
$stats['today_appointments']= $conn->query("SELECT COUNT(*) FROM appointments WHERE DATE(appointment_date)=CURDATE()")->fetch_row()[0];

include 'header.php';
?>

<style>
  .stat-card { border-radius:14px; padding:18px; display:flex; align-items:center; gap:14px; box-shadow:0 4px 20px rgba(11,74,203,.12); transition:transform .2s; position:relative; overflow:hidden; }
  .stat-card:hover { transform:translateY(-4px); }
  .stat-card::after { content:''; position:absolute; right:-20px; top:-20px; width:80px; height:80px; border-radius:50%; background:rgba(255,255,255,.1); }
  .stat-icon { width:46px; height:46px; border-radius:14px; background:rgba(255,255,255,.22); display:grid; place-items:center; flex-shrink:0; font-size:20px; color:#fff; }
  .stat-num { font-size:26px; font-weight:800; color:#fff; line-height:1; }
  .stat-lbl { font-size:11px; color:rgba(255,255,255,.8); margin-top:3px; font-weight:500; }
  @media(max-width:576px){ .stat-num{font-size:20px;} .stat-card{padding:14px 12px;gap:10px;} }
</style>

<?php displayFlash(); ?>

<!-- Page Header -->
<div class="d-flex justify-content-between align-items-center mb-4 flex-wrap gap-2">
  <div>
    <h5 class="mb-0 fw-800" style="color:#0f172a;"><i class="fas fa-tachometer-alt me-2 text-primary"></i>Admin Dashboard</h5>
    <small class="text-muted"><?= date('l, d M Y') ?></small>
  </div>
  <span class="badge bg-success"><i class="fas fa-circle me-1" style="font-size:8px;"></i>System Online</span>
</div>

<!-- Stat Cards -->
<div class="row g-3 mb-4">
  <?php $cards = [
    ['Total Patients',       $stats['total_patients'],     'fa-users',        'linear-gradient(135deg,#0B4ACB,#3B63E6)'],
    ['Total Doctors',        $stats['total_doctors'],      'fa-user-md',      'linear-gradient(135deg,#059669,#10b981)'],
    ['Pending Verification', $stats['pending_doctors'],    'fa-user-check',   'linear-gradient(135deg,#d97706,#f59e0b)'],
    ['Total Appointments',   $stats['total_appointments'], 'fa-calendar-alt', 'linear-gradient(135deg,#7c3aed,#a855f7)'],
    ["Today's Appointments", $stats['today_appointments'], 'fa-calendar-day', 'linear-gradient(135deg,#0891b2,#06b6d4)'],
  ]; ?>
  <?php foreach ($cards as [$lbl,$val,$icon,$grad]): ?>
  <div class="col-6 col-xl">
    <div class="stat-card" style="background:<?= $grad ?>;">
      <div class="stat-icon"><i class="fas <?= $icon ?>"></i></div>
      <div>
        <div class="stat-num"><?= $val ?></div>
        <div class="stat-lbl"><?= $lbl ?></div>
      </div>
    </div>
  </div>
  <?php endforeach; ?>
</div>

<!-- Quick Actions + System Status -->
<div class="row g-4">
  <div class="col-lg-8">
    <div class="admin-card">
      <div class="p-3 border-bottom"><h6 class="mb-0 fw-700"><i class="fas fa-bolt me-2 text-warning"></i>Quick Actions</h6></div>
      <div class="p-3">
        <div class="row g-3">
          <div class="col-md-6">
            <a href="doctor-verification.php" class="btn btn-outline-primary w-100">
              <i class="fas fa-user-check me-1"></i>Verify Doctors
              <?php if ($stats['pending_doctors'] > 0): ?><span class="badge bg-danger ms-1"><?= $stats['pending_doctors'] ?></span><?php endif; ?>
            </a>
          </div>
          <div class="col-md-6">
            <a href="appointments.php" class="btn btn-outline-success w-100"><i class="fas fa-calendar-alt me-1"></i>View Appointments</a>
          </div>
          <div class="col-md-6">
            <a href="patients.php" class="btn btn-outline-info w-100"><i class="fas fa-users me-1"></i>Manage Patients</a>
          </div>
          <div class="col-md-6">
            <a href="reports.php" class="btn btn-outline-warning w-100"><i class="fas fa-chart-bar me-1"></i>View Reports</a>
          </div>
          <div class="col-md-6">
            <a href="doctors.php" class="btn btn-outline-secondary w-100"><i class="fas fa-user-md me-1"></i>Manage Doctors</a>
          </div>
          <div class="col-md-6">
            <a href="analytics.php" class="btn btn-outline-dark w-100"><i class="fas fa-chart-line me-1"></i>Analytics</a>
          </div>
        </div>
      </div>
    </div>
  </div>

  <div class="col-lg-4">
    <div class="admin-card">
      <div class="p-3 border-bottom"><h6 class="mb-0 fw-700"><i class="fas fa-info-circle me-2 text-info"></i>System Status</h6></div>
      <div class="p-3">
        <?php $statuses = [
          ['Database Connection', 'success', 'Active'],
          ['Web Server',          'success', 'Online'],
          ['All Services',        'success', 'Running'],
          ['File Storage',        'success', 'Available'],
        ]; ?>
        <?php foreach ($statuses as [$label,$color,$status]): ?>
        <div class="d-flex align-items-center justify-content-between py-2" style="border-bottom:1px solid #f0f4ff;">
          <small style="color:#4a5568;"><?= $label ?></small>
          <span class="badge bg-<?= $color ?>"><?= $status ?></span>
        </div>
        <?php endforeach; ?>
      </div>
    </div>
  </div>
</div>

<?php include 'footer.php'; ?>
