<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors', 1);

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

include '../include/config.php';

$staff_id = $_SESSION['staff_id'];
$staff_name = $_SESSION['staff_name'];
$staff_department = $_SESSION['staff_department'] ?? 'Administration';
$staff_position = $_SESSION['staff_position'] ?? 'Staff Member';
$staff_role = $_SESSION['staff_role'] ?? 'staff';

// Create staff notifications table if not exists
$conn->query("CREATE TABLE IF NOT EXISTS staff_notifications (
    id INT AUTO_INCREMENT PRIMARY KEY,
    staff_id INT NOT NULL,
    sender_type VARCHAR(50),
    sender_id INT,
    title VARCHAR(255),
    message TEXT,
    is_read TINYINT DEFAULT 0,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)");

// Get unread notifications count
$unread_notifications = 0;
$notif_result = $conn->query("SELECT COUNT(*) as cnt FROM staff_notifications WHERE staff_id = $staff_id AND is_read = 0");
if ($notif_result) $unread_notifications = $notif_result->fetch_assoc()['cnt'] ?? 0;

// Get unread chat messages count
$unread_chats = 0;
$chat_check = $conn->query("SHOW TABLES LIKE 'chat_messages'");
if ($chat_check && $chat_check->num_rows > 0) {
    $chat_result = $conn->query("SELECT COUNT(*) as cnt FROM chat_messages WHERE receiver_id = $staff_id AND receiver_type = 'staff' AND is_read = 0");
    if ($chat_result) $unread_chats = $chat_result->fetch_assoc()['cnt'] ?? 0;
}

// Get counts based on role
$totalPatients = 0;
$patients_result = $conn->query("SELECT COUNT(*) as cnt FROM patients");
if ($patients_result) $totalPatients = $patients_result->fetch_assoc()['cnt'] ?? 0;

$totalAppointments = 0;
$appointments_result = $conn->query("SELECT COUNT(*) as cnt FROM appointments");
if ($appointments_result) $totalAppointments = $appointments_result->fetch_assoc()['cnt'] ?? 0;

$pendingAppointments = 0;
$pending_result = $conn->query("SELECT COUNT(*) as cnt FROM appointments WHERE status='pending'");
if ($pending_result) $pendingAppointments = $pending_result->fetch_assoc()['cnt'] ?? 0;

$totalDoctors = 0;
$doctors_result = $conn->query("SELECT COUNT(*) as cnt FROM doctors WHERE status='active'");
if ($doctors_result) $totalDoctors = $doctors_result->fetch_assoc()['cnt'] ?? 0;

$totalNurses = 0;
$nurses_result = $conn->query("SELECT COUNT(*) as cnt FROM nurses WHERE status='active'");
if ($nurses_result) $totalNurses = $nurses_result->fetch_assoc()['cnt'] ?? 0;

$totalStaff = 0;
$staff_result = $conn->query("SELECT COUNT(*) as cnt FROM staff WHERE status='active'");
if ($staff_result) $totalStaff = $staff_result->fetch_assoc()['cnt'] ?? 0;

// Get total revenue
$totalRevenue = 0;
$revenue_result = $conn->query("SELECT SUM(amount) as total FROM payments WHERE status='completed'");
if ($revenue_result) $totalRevenue = $revenue_result->fetch_assoc()['total'] ?? 0;

// Get recent appointments - FIXED with proper column names
$recentAppointments = $conn->query("
    SELECT a.*, p.first_name, p.last_name, p.phone, d.name as doctor_name
    FROM appointments a 
    JOIN patients p ON a.patient_id = p.patient_id 
    LEFT JOIN doctors d ON a.doctor_id = d.doctor_id
    ORDER BY a.created_at DESC LIMIT 10
");

// Get recent patients
$recentPatients = $conn->query("
    SELECT * FROM patients 
    ORDER BY created_at DESC LIMIT 10
");

// Get recent payments
$recentPayments = $conn->query("
    SELECT p.*, pat.first_name, pat.last_name
    FROM payments p
    JOIN patients pat ON p.patient_id = pat.patient_id
    ORDER BY p.created_at DESC LIMIT 5
");

// Get staff profile
$staff_info = $conn->query("SELECT * FROM staff WHERE staff_id = $staff_id")->fetch_assoc();

// Profile picture
$profile_pic = 'https://ui-avatars.com/api/?background=8B7355&color=fff&name=' . urlencode($staff_name);
if (!empty($staff_info['profile_pic']) && file_exists("../uploads/" . $staff_info['profile_pic'])) {
    $profile_pic = "../uploads/" . $staff_info['profile_pic'];
}

// Helper function to safely format date
function safeDateFormat($date, $format = 'M d, Y') {
    if (empty($date) || $date == '0000-00-00' || $date == 'NULL') {
        return 'Not set';
    }
    $timestamp = strtotime($date);
    if ($timestamp === false) {
        return 'Invalid date';
    }
    return date($format, $timestamp);
}

// Helper function to safely format time
function safeTimeFormat($time) {
    if (empty($time) || $time == '00:00:00' || $time == 'NULL') {
        return '--:--';
    }
    $timestamp = strtotime($time);
    if ($timestamp === false) {
        return $time;
    }
    return date('h:i A', $timestamp);
}

// Handle quick actions
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['action'])) {
    if ($_POST['action'] == 'update_appointment_status') {
        $appointment_id = intval($_POST['appointment_id']);
        $status = $_POST['status'];
        $conn->query("UPDATE appointments SET status = '$status' WHERE id = $appointment_id");
        echo json_encode(['success' => true]);
        exit();
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Staff Dashboard - Unity Healthcare</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.6.0/css/all.min.css">
    <style>
        body { 
            background: #3d2b1f; 
            font-family: 'Segoe UI', sans-serif; 
            display: flex; 
            flex-direction: column; 
            min-height: 100vh; 
        }
        .navbar { background: #2c1a0e !important; box-shadow: 0 2px 10px rgba(0,0,0,0.3); padding: 12px 0; border-bottom: 2px solid #d4a017; }
        .navbar-brand img { height: 45px; margin-right: 12px; }
        .brand-title { font-size: 1.4rem; font-weight: 800; line-height: 1.2; color: #d4a017; }
        .brand-sub { font-size: 0.7rem; font-weight: 400; letter-spacing: 1px; color: #c0c0c0; }
        .icon-btn { background: transparent; border: none; color: white; font-size: 1.2rem; position: relative; margin-left: 15px; transition: 0.3s; }
        .icon-btn:hover { color: #d4a017; transform: scale(1.1); }
        .badge-count { position: absolute; top: -8px; right: -12px; background: #dc3545; color: white; border-radius: 50%; padding: 2px 6px; font-size: 10px; font-weight: bold; }
        
        .profile-card { background: linear-gradient(135deg, #2c1a0e, #1a0f08); border-radius: 20px; padding: 20px; color: white; margin-bottom: 20px; border: 1px solid #d4a017; }
        .profile-pic { width: 80px; height: 80px; border-radius: 50%; object-fit: cover; border: 3px solid #d4a017; }
        
        .stat-card { background: #2c1a0e; border-radius: 15px; padding: 20px; text-align: center; color: white; transition: 0.3s; border: 1px solid #d4a017; cursor: pointer; }
        .stat-card:hover { transform: translateY(-5px); background: #3d2b1f; }
        
        .feature-card { background: #2c1a0e; border: 1px solid #d4a017; border-radius: 15px; padding: 20px 10px; text-align: center; transition: 0.3s; cursor: pointer; color: white; margin-bottom: 15px; }
        .feature-card:hover { transform: translateY(-5px); background: #3d2b1f; }
        .feature-card i { font-size: 2rem; margin-bottom: 10px; color: #d4a017; }
        
        .section-card { background: #2c1a0e; border-radius: 15px; margin-bottom: 20px; border: 1px solid #d4a017; overflow: hidden; }
        .section-header { background: #1a0f08; color: #d4a017; padding: 15px 20px; border-bottom: 1px solid #d4a017; }
        .section-body { padding: 20px; }
        
        .table-custom { color: white; }
        .table-custom thead th { border-bottom-color: #d4a017; color: #d4a017; padding: 12px; }
        .table-custom td { border-color: #3d2b1f; padding: 12px; vertical-align: middle; }
        
        .badge-status { padding: 5px 12px; border-radius: 20px; font-size: 11px; font-weight: 600; }
        .badge-pending { background: #ffc107; color: #333; }
        .badge-confirmed { background: #28a745; color: white; }
        .badge-completed { background: #17a2b8; color: white; }
        .badge-cancelled { background: #dc3545; color: white; }
        .badge-scheduled { background: #17a2b8; color: white; }
        
        .btn-gold { background: #d4a017; color: #1a0f08; border: none; padding: 8px 20px; border-radius: 8px; }
        .btn-gold:hover { background: #b8860b; color: white; }
        .btn-outline-gold { border-color: #d4a017; color: #d4a017; background: transparent; }
        .btn-outline-gold:hover { background: #d4a017; color: #1a0f08; }
        
        footer { background: #1a0f08; color: #c0c0c0; margin-top: auto; padding: 30px 0 20px; border-top: 1px solid #d4a017; }
        footer a { color: #d4a017; transition: 0.2s; }
        footer a:hover { color: white; text-decoration: underline; }
        
        .status-select { background: #3d2b1f; border: 1px solid #d4a017; color: white; padding: 5px 10px; border-radius: 5px; }
        
        @media (max-width: 768px) { 
            .container { padding: 10px; } 
            .profile-pic { width: 60px; height: 60px; }
            .stat-card { margin-bottom: 10px; }
            .table-responsive { font-size: 12px; }
        }
    </style>
</head>
<body>

<!-- Professional Header -->
<nav class="navbar navbar-dark">
    <div class="container">
        <a class="navbar-brand d-flex align-items-center" href="../index.php">
            <img src="https://od.lk/s/MzdfODgyMDMwNTZf/IMG-20260414-WA0014%20%281%29.jpg" alt="UHD Logo">
            <div>
                <div class="brand-title">UNITY HEALTHCARE DATABASE</div>
                <div class="brand-sub">NIGERIA | STAFF PORTAL</div>
            </div>
        </a>
        <div class="d-flex align-items-center">
            <a href="#" class="icon-btn" title="Notifications">
                <i class="fas fa-bell fa-lg"></i>
                <?php if($unread_notifications > 0): ?>
                    <span class="badge-count"><?= $unread_notifications ?></span>
                <?php endif; ?>
            </a>
            <a href="chat.php" class="icon-btn" title="Messages">
                <i class="fas fa-envelope fa-lg"></i>
                <?php if($unread_chats > 0): ?>
                    <span class="badge-count"><?= $unread_chats ?></span>
                <?php endif; ?>
            </a>
            <span class="text-white mx-2 d-none d-sm-block">
                <i class="fas fa-building"></i> <?= htmlspecialchars($staff_department) ?>
            </span>
            <span class="text-white mx-2 d-none d-sm-block"><?= htmlspecialchars($staff_name) ?></span>
            <a href="logout.php" class="btn btn-danger btn-sm"><i class="fas fa-sign-out-alt"></i> Logout</a>
        </div>
    </div>
</nav>

<div class="container mt-4">
    <!-- Staff Profile Card -->
    <div class="profile-card">
        <div class="row align-items-center">
            <div class="col-auto">
                <img src="<?= $profile_pic ?>" class="profile-pic" alt="Profile">
            </div>
            <div class="col">
                <h3><?= htmlspecialchars($staff_name) ?></h3>
                <p class="mb-1"><i class="fas fa-id-badge"></i> <?= htmlspecialchars($staff_position) ?></p>
                <p class="mb-1"><i class="fas fa-building"></i> Department: <?= ucfirst(htmlspecialchars($staff_department)) ?></p>
                <p class="mb-0"><i class="fas fa-user-tag"></i> Staff ID: <?= $staff_id ?></p>
            </div>
            <div class="col-auto">
                <button class="btn btn-outline-gold btn-sm" data-bs-toggle="modal" data-bs-target="#patientModal">
                    <i class="fas fa-user-plus"></i> Register Patient
                </button>
                <button class="btn btn-gold btn-sm ms-2" data-bs-toggle="modal" data-bs-target="#appointmentModal">
                    <i class="fas fa-calendar-plus"></i> New Appointment
                </button>
            </div>
        </div>
    </div>

    <!-- Statistics Row -->
    <div class="row mb-4 g-3">
        <div class="col-6 col-md-3">
            <div class="stat-card" onclick="window.location.href='manage_patients.php'">
                <i class="fas fa-users fa-2x mb-2"></i>
                <h3><?= number_format($totalPatients) ?></h3>
                <p>Total Patients</p>
            </div>
        </div>
        <div class="col-6 col-md-3">
            <div class="stat-card" onclick="window.location.href='manage_appointments.php'">
                <i class="fas fa-calendar-check fa-2x mb-2"></i>
                <h3><?= number_format($totalAppointments) ?></h3>
                <p>Appointments</p>
            </div>
        </div>
        <div class="col-6 col-md-3">
            <div class="stat-card">
                <i class="fas fa-hourglass-half fa-2x mb-2"></i>
                <h3><?= number_format($pendingAppointments) ?></h3>
                <p>Pending</p>
            </div>
        </div>
        <div class="col-6 col-md-3">
            <div class="stat-card">
                <i class="fas fa-money-bill-wave fa-2x mb-2"></i>
                <h3>₦<?= number_format($totalRevenue, 0) ?></h3>
                <p>Revenue</p>
            </div>
        </div>
    </div>

    <!-- Features Grid -->
    <div class="row g-4 mb-4">
        <div class="col-6 col-md-3">
            <div class="feature-card" onclick="window.location.href='manage_patients.php'">
                <i class="fas fa-users"></i><h6>Manage Patients</h6><small>View & search patients</small>
            </div>
        </div>
        <div class="col-6 col-md-3">
            <div class="feature-card" onclick="window.location.href='manage_appointments.php'">
                <i class="fas fa-calendar-alt"></i><h6>Appointments</h6><small>Schedule & manage</small>
            </div>
        </div>
        <div class="col-6 col-md-3">
            <div class="feature-card" data-bs-toggle="modal" data-bs-target="#patientModal">
                <i class="fas fa-user-plus"></i><h6>Register Patient</h6><small>New patient</small>
            </div>
        </div>
        <div class="col-6 col-md-3">
            <div class="feature-card" onclick="window.location.href='billing.php'">
                <i class="fas fa-credit-card"></i><h6>Billing</h6><small>Process payments</small>
            </div>
        </div>
        <div class="col-6 col-md-3">
            <div class="feature-card" onclick="window.location.href='reports.php'">
                <i class="fas fa-chart-line"></i><h6>Reports</h6><small>Generate reports</small>
            </div>
        </div>
        <div class="col-6 col-md-3">
            <div class="feature-card" onclick="window.location.href='staff_chat.php'">
                <i class="fas fa-comments"></i><h6>Staff Chat</h6><small>Team communication</small>
            </div>
        </div>
        <div class="col-6 col-md-3">
            <div class="feature-card" onclick="window.location.href='view_patients.php'">
                <i class="fas fa-eye"></i><h6>View Patients</h6><small>Patient directory</small>
            </div>
        </div>
        <div class="col-6 col-md-3">
            <div class="feature-card" onclick="window.location.href='messages.php'">
                <i class="fas fa-envelope"></i><h6>Messages</h6><small>View communications</small>
            </div>
        </div>
    </div>

    <!-- Recent Appointments Section - FIXED -->
    <div class="section-card">
        <div class="section-header">
            <h5 class="mb-0"><i class="fas fa-calendar-alt me-2"></i>Recent Appointments</h5>
        </div>
        <div class="section-body">
            <?php if($recentAppointments && $recentAppointments->num_rows > 0): ?>
                <div class="table-responsive">
                    <table class="table table-custom">
                        <thead>
                            <tr>
                                <th>Patient</th>
                                <th>Doctor</th>
                                <th>Date</th>
                                <th>Time</th>
                                <th>Status</th>
                                <th>Action</th>
                            </tr>
                        </thead>
                        <tbody>
                        <?php while($a = $recentAppointments->fetch_assoc()): ?>
                            <tr>
                                <td>
                                    <strong><?= htmlspecialchars($a['first_name'] . ' ' . $a['last_name']) ?></strong>
                                    <br><small class="text-muted">ID: <?= $a['patient_id'] ?></small>
                                </td>
                                <td><?= htmlspecialchars($a['doctor_name'] ?? 'Not assigned') ?></td>
                                <td><?= safeDateFormat($a['appointment_date'] ?? null) ?></td>
                                <td><?= safeTimeFormat($a['appointment_time'] ?? null) ?></td>
                                <td>
                                    <select class="status-select" onchange="updateStatus(<?= $a['id'] ?>, this.value)">
                                        <option value="pending" <?= ($a['status'] ?? '') == 'pending' ? 'selected' : '' ?>>Pending</option>
                                        <option value="confirmed" <?= ($a['status'] ?? '') == 'confirmed' ? 'selected' : '' ?>>Confirmed</option>
                                        <option value="completed" <?= ($a['status'] ?? '') == 'completed' ? 'selected' : '' ?>>Completed</option>
                                        <option value="cancelled" <?= ($a['status'] ?? '') == 'cancelled' ? 'selected' : '' ?>>Cancelled</option>
                                    </select>
                                </td>
                                <td>
                                    <button class="btn btn-sm btn-outline-gold" onclick="viewAppointment(<?= $a['id'] ?>)">
                                        <i class="fas fa-eye"></i>
                                    </button>
                                </td>
                            </tr>
                        <?php endwhile; ?>
                        </tbody>
                    </table>
                </div>
            <?php else: ?>
                <p class="text-muted text-center mb-0">No recent appointments found.</p>
            <?php endif; ?>
        </div>
    </div>

    <!-- Recent Patients Section -->
    <div class="section-card">
        <div class="section-header">
            <h5 class="mb-0"><i class="fas fa-user-plus me-2"></i>Recently Registered Patients</h5>
        </div>
        <div class="section-body">
            <?php if($recentPatients && $recentPatients->num_rows > 0): ?>
                <div class="table-responsive">
                    <table class="table table-custom">
                        <thead>
                            <tr><th>ID</th><th>Name</th><th>Phone</th><th>Email</th><th>Registered</th><th>Action</th></tr>
                        </thead>
                        <tbody>
                        <?php while($p = $recentPatients->fetch_assoc()): ?>
                            <tr>
                                <td><?= $p['patient_id'] ?></td>
                                <td><?= htmlspecialchars($p['first_name'] . ' ' . $p['last_name']) ?></td>
                                <td><?= $p['phone'] ?? 'N/A' ?></td>
                                <td><?= $p['email'] ?? 'N/A' ?></td>
                                <td><?= safeDateFormat($p['created_at'] ?? null) ?></td>
                                <td>
                                    <button class="btn btn-sm btn-outline-gold" onclick="viewPatient(<?= $p['patient_id'] ?>)">
                                        View
                                    </button>
                                </td>
                            </tr>
                        <?php endwhile; ?>
                        </tbody>
                    </table>
                </div>
            <?php else: ?>
                <p class="text-muted text-center mb-0">No patients found.</p>
            <?php endif; ?>
        </div>
    </div>
</div>

<!-- Register Patient Modal -->
<div class="modal fade" id="patientModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title"><i class="fas fa-user-plus"></i> Register New Patient</h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
            </div>
            <form action="register_patient.php" method="POST">
                <div class="modal-body">
                    <div class="mb-3">
                        <label class="form-label">First Name *</label>
                        <input type="text" name="first_name" class="form-control" required>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Last Name *</label>
                        <input type="text" name="last_name" class="form-control" required>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Phone</label>
                        <input type="tel" name="phone" class="form-control">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Email</label>
                        <input type="email" name="email" class="form-control">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Date of Birth</label>
                        <input type="date" name="dob" class="form-control">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Gender</label>
                        <select name="gender" class="form-select">
                            <option value="">Select</option>
                            <option value="male">Male</option>
                            <option value="female">Female</option>
                        </select>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                    <button type="submit" class="btn btn-gold">Register Patient</button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- New Appointment Modal -->
<div class="modal fade" id="appointmentModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title"><i class="fas fa-calendar-plus"></i> Schedule Appointment</h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
            </div>
            <form action="manage_appointments.php" method="POST">
                <div class="modal-body">
                    <div class="mb-3">
                        <label class="form-label">Patient *</label>
                        <select name="patient_id" class="form-select" required>
                            <option value="">Select Patient</option>
                            <?php
                            $allPatients = $conn->query("SELECT patient_id, first_name, last_name FROM patients ORDER BY first_name ASC");
                            while($p = $allPatients->fetch_assoc()):
                            ?>
                                <option value="<?= $p['patient_id'] ?>"><?= htmlspecialchars($p['first_name'] . ' ' . $p['last_name']) ?></option>
                            <?php endwhile; ?>
                        </select>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Doctor *</label>
                        <select name="doctor_id" class="form-select" required>
                            <option value="">Select Doctor</option>
                            <?php
                            $allDoctors = $conn->query("SELECT doctor_id, name FROM doctors WHERE status='active'");
                            while($d = $allDoctors->fetch_assoc()):
                            ?>
                                <option value="<?= $d['doctor_id'] ?>">Dr. <?= htmlspecialchars($d['name']) ?></option>
                            <?php endwhile; ?>
                        </select>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Appointment Date *</label>
                        <input type="date" name="appointment_date" class="form-control" required>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Appointment Time *</label>
                        <input type="time" name="appointment_time" class="form-control" required>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                    <button type="submit" name="add_appointment" class="btn btn-gold">Schedule Appointment</button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- Footer -->
<footer>
    <div class="container">
        <div class="row">
            <div class="col-md-4 mb-4">
                <h5><i class="fas fa-heartbeat me-2"></i> Unity Healthcare DB</h5>
                <p>Nigeria's integrated health data platform connecting patients, doctors, and administrators.</p>
                <div>
                    <a href="#" class="me-3"><i class="fab fa-facebook-f"></i></a>
                    <a href="#" class="me-3"><i class="fab fa-twitter"></i></a>
                    <a href="#" class="me-3"><i class="fab fa-linkedin-in"></i></a>
                    <a href="#" class="me-3"><i class="fab fa-instagram"></i></a>
                </div>
            </div>
            <div class="col-md-4 mb-4">
                <h5>Quick Links</h5>
                <ul class="list-unstyled">
                    <li><a href="../about.php"><i class="fas fa-info-circle"></i> About UHD</a></li>
                    <li><a href="../how-it-works.php"><i class="fas fa-question-circle"></i> How it Works</a></li>
                    <li><a href="#" data-bs-toggle="modal" data-bs-target="#supportModal"><i class="fas fa-headset"></i> Live Support</a></li>
                </ul>
            </div>
            <div class="col-md-4 mb-4">
                <h5>Contact Us</h5>
                <p><i class="fab fa-whatsapp me-2"></i> WhatsApp: <a href="https://wa.me/2347089364492">+234 708 936 4492</a></p>
                <p><i class="fas fa-phone-alt me-2"></i> Phone: <a href="tel:+2347089364492">+234 708 936 4492</a></p>
                <p><i class="fas fa-envelope me-2"></i> Email: <a href="mailto:inq@uhd.com.ng">inq@uhd.com.ng</a></p>
            </div>
        </div>
        <div class="text-center pt-3 border-top border-secondary">
            <small>&copy; <?= date('Y') ?> Unity Healthcare Database. All rights reserved.</small><br>
            <small>Powered by <a href="https://www.fulgentdigit.com.ng" target="_blank">Fulgent Digital Clinic (R)</a></small>
        </div>
    </div>
</footer>

<!-- Support Modal -->
<div class="modal fade" id="supportModal" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header bg-warning text-dark">
                <h5 class="modal-title"><i class="fas fa-headset"></i> Live System Support</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <p>Our support team is available 24/7. Reach us via:</p>
                <ul class="list-unstyled">
                    <li><i class="fab fa-whatsapp text-success me-2"></i> WhatsApp: <a href="https://wa.me/2347089364492">+234 708 936 4492</a></li>
                    <li><i class="fas fa-phone-alt text-primary me-2"></i> Phone: <a href="tel:+2347089364492">+234 708 936 4492</a></li>
                    <li><i class="fas fa-envelope text-danger me-2"></i> Email: <a href="mailto:inq@uhd.com.ng">inq@uhd.com.ng</a></li>
                </ul>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<script>
function updateStatus(appointmentId, status) {
    $.ajax({
        url: window.location.href,
        type: 'POST',
        data: {
            action: 'update_appointment_status',
            appointment_id: appointmentId,
            status: status
        },
        dataType: 'json',
        success: function(response) {
            if (response.success) {
                alert('Appointment status updated!');
                location.reload();
            }
        }
    });
}

function viewPatient(patientId) {
    window.location.href = 'view_patients.php?id=' + patientId;
}

function viewAppointment(appointmentId) {
    window.location.href = 'manage_appointments.php?id=' + appointmentId;
}
</script>
</body>
</html>