<?php
session_start();
include 'layouts/dbconn.php';
include 'layouts/session.php';

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

$allowed_roles = ['SUPER_ADMIN', 'HOSPITAL_ADMIN', 'RECEPTIONIST', 'ENT_DOCTOR', 'ENT_CONSULTANT', 'NURSE'];
if (!in_array($_SESSION['role_code'], $allowed_roles)) {
    header('Location: dashboard.php');
    exit();
}

// Pagination
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$records_per_page = 20;
$offset = ($page - 1) * $records_per_page;

// Filters
$filter_status = isset($_GET['status']) ? $_GET['status'] : '';
$filter_doctor = isset($_GET['doctor_id']) ? (int)$_GET['doctor_id'] : 0;
$filter_patient = isset($_GET['patient_id']) ? (int)$_GET['patient_id'] : 0;
$filter_date = isset($_GET['date']) ? $_GET['date'] : '';
$filter_search = isset($_GET['search']) ? trim($_GET['search']) : '';

// Build query
$where_conditions = [];
$params = [];
$types = '';

// If user is a doctor, show only their appointments
if (in_array($_SESSION['role_code'], ['ENT_DOCTOR', 'ENT_CONSULTANT'])) {
    $where_conditions[] = "a.doctor_id = (SELECT doctor_id FROM doctors WHERE user_id = ?)";
    $params[] = $_SESSION['user_id'];
    $types .= 'i';
}

if ($filter_status) {
    $where_conditions[] = "a.status = ?";
    $params[] = $filter_status;
    $types .= 's';
}

if ($filter_doctor) {
    $where_conditions[] = "a.doctor_id = ?";
    $params[] = $filter_doctor;
    $types .= 'i';
}

if ($filter_patient) {
    $where_conditions[] = "a.patient_id = ?";
    $params[] = $filter_patient;
    $types .= 'i';
}

if ($filter_date) {
    $where_conditions[] = "a.appointment_date = ?";
    $params[] = $filter_date;
    $types .= 's';
}

if ($filter_search) {
    $where_conditions[] = "(pu.full_name LIKE ? OR du.full_name LIKE ? OR p.patient_number LIKE ? OR a.appointment_number LIKE ?)";
    $search_param = "%$filter_search%";
    $params[] = $search_param;
    $params[] = $search_param;
    $params[] = $search_param;
    $params[] = $search_param;
    $types .= 'ssss';
}

$where_clause = !empty($where_conditions) ? 'WHERE ' . implode(' AND ', $where_conditions) : '';

// Get total count
$count_sql = "SELECT COUNT(*) as total 
              FROM appointments a
              INNER JOIN patients p ON a.patient_id = p.patient_id
              INNER JOIN users pu ON p.user_id = pu.user_id
              INNER JOIN doctors d ON a.doctor_id = d.doctor_id
              INNER JOIN users du ON d.user_id = du.user_id
              $where_clause";
$stmt = $conn->prepare($count_sql);
if (!empty($params)) {
    $stmt->bind_param($types, ...$params);
}
$stmt->execute();
$total_records = $stmt->get_result()->fetch_assoc()['total'];
$total_pages = ceil($total_records / $records_per_page);

// Get appointments
$appointments_sql = "SELECT 
                        a.*,
                        p.patient_number,
                        pu.full_name as patient_name,
                        pu.phone_primary as patient_phone,
                        pu.gender as patient_gender,
                        du.full_name as doctor_name,
                        d.specialization as doctor_specialization,
                        pay.payment_id,
                        pay.amount as payment_amount,
                        pay.status,
                        pay.payment_method
                     FROM appointments a
                     INNER JOIN patients p ON a.patient_id = p.patient_id
                     INNER JOIN users pu ON p.user_id = pu.user_id
                     INNER JOIN doctors d ON a.doctor_id = d.doctor_id
                     INNER JOIN users du ON d.user_id = du.user_id
                     LEFT JOIN payments pay ON a.appointment_id = pay.visit_id
                     $where_clause
                     ORDER BY a.appointment_date DESC, a.appointment_time DESC
                     LIMIT ? OFFSET ?";

$params[] = $records_per_page;
$params[] = $offset;
$types .= 'ii';

$stmt = $conn->prepare($appointments_sql);
$stmt->bind_param($types, ...$params);
$stmt->execute();
$appointments = $stmt->get_result();

// Get doctors for filter
$doctors_sql = "SELECT d.doctor_id, u.full_name 
                FROM doctors d 
                INNER JOIN users u ON d.user_id = u.user_id 
                WHERE d.is_active = 1 
                ORDER BY u.full_name";
$doctors = $conn->query($doctors_sql);

// Get statistics
$stats_sql = "SELECT 
                COUNT(*) as total,
                SUM(CASE WHEN status = 'SCHEDULED' THEN 1 ELSE 0 END) as scheduled,
                SUM(CASE WHEN status = 'CONFIRMED' THEN 1 ELSE 0 END) as confirmed,
                SUM(CASE WHEN status = 'COMPLETED' THEN 1 ELSE 0 END) as completed,
                SUM(CASE WHEN status = 'CANCELLED' THEN 1 ELSE 0 END) as cancelled,
                SUM(CASE WHEN status = 'NO_SHOW' THEN 1 ELSE 0 END) as no_show,
                SUM(CASE WHEN appointment_date = CURDATE() THEN 1 ELSE 0 END) as today
              FROM appointments" . (in_array($_SESSION['role_code'], ['ENT_DOCTOR', 'ENT_CONSULTANT']) ? 
              " WHERE doctor_id = (SELECT doctor_id FROM doctors WHERE user_id = {$_SESSION['user_id']})" : "");
$stats = $conn->query($stats_sql)->fetch_assoc();
?>

<?php include 'layouts/main.php'; ?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Appointments')); ?>
    <?php include 'layouts/head-css.php'; ?>
    <link href="assets/libs/sweetalert2/sweetalert2.min.css" rel="stylesheet">
    
    <style>
        .appointments-header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            padding: 2rem;
            border-radius: 12px;
            color: white;
            margin-bottom: 2rem;
        }
        
        .stats-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
            gap: 1rem;
            margin-bottom: 2rem;
        }
        
        .stat-card {
            background: white;
            padding: 1.5rem;
            border-radius: 12px;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
            text-align: center;
            border: 1px solid #e2e8f0;
        }
        
        .stat-value {
            font-size: 2rem;
            font-weight: 700;
            margin-bottom: 0.5rem;
        }
        
        .stat-label {
            color: #64748b;
            font-size: 0.875rem;
        }
        
        .filter-card {
            background: white;
            border-radius: 12px;
            padding: 1.5rem;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
            border: 1px solid #e2e8f0;
            margin-bottom: 1.5rem;
        }
        
        .table-card {
            background: white;
            border-radius: 12px;
            padding: 1.5rem;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
            border: 1px solid #e2e8f0;
        }
        
        .appointment-table {
            width: 100%;
            border-collapse: collapse;
        }
        
        .appointment-table th {
            background: #f8fafc;
            padding: 1rem;
            text-align: left;
            font-weight: 600;
            color: #475569;
            font-size: 0.875rem;
            border-bottom: 2px solid #e2e8f0;
        }
        
        .appointment-table td {
            padding: 1rem;
            border-bottom: 1px solid #f1f5f9;
        }
        
        .appointment-table tr:hover {
            background: #f8fafc;
        }
        
        .badge {
            padding: 0.375rem 0.75rem;
            border-radius: 6px;
            font-weight: 500;
            font-size: 0.75rem;
            text-transform: uppercase;
        }
        
        .badge-scheduled { background: #e0e7ff; color: #4338ca; }
        .badge-confirmed { background: #dbeafe; color: #1e40af; }
        .badge-completed { background: #d1fae5; color: #065f46; }
        .badge-cancelled { background: #fee2e2; color: #991b1b; }
        .badge-no_show { background: #fef3c7; color: #92400e; }
        .badge-rescheduled { background: #f3e8ff; color: #7c3aed; }
        
        .badge-paid { background: #d1fae5; color: #065f46; }
        .badge-pending { background: #fef3c7; color: #92400e; }
        .badge-failed { background: #fee2e2; color: #991b1b; }
        
        .action-btn {
            padding: 0.375rem 0.75rem;
            border: none;
            border-radius: 6px;
            cursor: pointer;
            font-size: 0.875rem;
            transition: all 0.3s;
            text-decoration: none;
            display: inline-flex;
            align-items: center;
            gap: 0.375rem;
        }
        
        .btn-view {
            background: #667eea;
            color: white;
        }
        
        .btn-view:hover {
            background: #5568d3;
            color: white;
        }
        
        .btn-cancel {
            background: #fee2e2;
            color: #991b1b;
        }
        
        .btn-cancel:hover {
            background: #fecaca;
        }
        
        .patient-info {
            display: flex;
            align-items: center;
            gap: 0.75rem;
        }
        
        .patient-avatar {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: 700;
            font-size: 0.875rem;
        }
        
        .patient-details h6 {
            margin: 0;
            font-size: 0.875rem;
            color: #1e293b;
        }
        
        .patient-details p {
            margin: 0;
            font-size: 0.75rem;
            color: #64748b;
        }
    </style>
</head>

<body>
    <div id="layout-wrapper">
        <?php include 'layouts/menu.php'; ?>
        
        <div class="main-content">
            <div class="page-content">
                <div class="container-fluid">
                    <!-- Page Title -->
                    <div class="row">
                        <div class="col-12">
                            <div class="page-title-box d-sm-flex align-items-center justify-content-between">
                                <h4 class="mb-sm-0">Appointments</h4>
                                <div class="page-title-right">
                                    <ol class="breadcrumb m-0">
                                        <li class="breadcrumb-item"><a href="dashboard.php">Dashboard</a></li>
                                        <li class="breadcrumb-item active">Appointments</li>
                                    </ol>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Header -->
                    <div class="appointments-header">
                        <div class="row align-items-center">
                            <div class="col-md-8">
                                <h3 class="mb-2">Appointments Management</h3>
                                <p class="mb-0" style="opacity: 0.9;">
                                    <i class="ri-calendar-line me-1"></i>
                                    Manage and track all patient appointments
                                </p>
                            </div>
                            <div class="col-md-4 text-md-end mt-3 mt-md-0">
                                <?php if (in_array($_SESSION['role_code'], ['SUPER_ADMIN', 'HOSPITAL_ADMIN', 'RECEPTIONIST'])): ?>
                                <a href="new_appointment.php" class="btn btn-light">
                                    <i class="ri-add-line me-1"></i> New Appointment
                                </a>
                                <?php endif; ?>
                            </div>
                        </div>
                    </div>

                    <!-- Statistics -->
                    <div class="stats-grid">
                        <div class="stat-card">
                            <div class="stat-value"><?php echo number_format($stats['total']); ?></div>
                            <div class="stat-label">Total</div>
                        </div>
                        <div class="stat-card">
                            <div class="stat-value text-info"><?php echo number_format($stats['scheduled']); ?></div>
                            <div class="stat-label">Scheduled</div>
                        </div>
                        <div class="stat-card">
                            <div class="stat-value text-primary"><?php echo number_format($stats['confirmed']); ?></div>
                            <div class="stat-label">Confirmed</div>
                        </div>
                        <div class="stat-card">
                            <div class="stat-value text-success"><?php echo number_format($stats['completed']); ?></div>
                            <div class="stat-label">Completed</div>
                        </div>
                        <div class="stat-card">
                            <div class="stat-value text-danger"><?php echo number_format($stats['cancelled']); ?></div>
                            <div class="stat-label">Cancelled</div>
                        </div>
                        <div class="stat-card">
                            <div class="stat-value text-warning"><?php echo number_format($stats['today']); ?></div>
                            <div class="stat-label">Today</div>
                        </div>
                    </div>

                    <!-- Filters -->
                    <div class="filter-card">
                        <form method="GET" action="">
                            <div class="row g-3">
                                <div class="col-md-3">
                                    <label class="form-label">Search</label>
                                    <input type="text" class="form-control" name="search" 
                                           value="<?php echo htmlspecialchars($filter_search); ?>" 
                                           placeholder="Patient, Doctor, Appt #">
                                </div>
                                <div class="col-md-2">
                                    <label class="form-label">Status</label>
                                    <select class="form-select" name="status">
                                        <option value="">All Status</option>
                                        <option value="SCHEDULED" <?php echo $filter_status === 'SCHEDULED' ? 'selected' : ''; ?>>Scheduled</option>
                                        <option value="CONFIRMED" <?php echo $filter_status === 'CONFIRMED' ? 'selected' : ''; ?>>Confirmed</option>
                                        <option value="COMPLETED" <?php echo $filter_status === 'COMPLETED' ? 'selected' : ''; ?>>Completed</option>
                                        <option value="CANCELLED" <?php echo $filter_status === 'CANCELLED' ? 'selected' : ''; ?>>Cancelled</option>
                                        <option value="NO_SHOW" <?php echo $filter_status === 'NO_SHOW' ? 'selected' : ''; ?>>No Show</option>
                                        <option value="RESCHEDULED" <?php echo $filter_status === 'RESCHEDULED' ? 'selected' : ''; ?>>Rescheduled</option>
                                    </select>
                                </div>
                                <?php if (!in_array($_SESSION['role_code'], ['ENT_DOCTOR', 'ENT_CONSULTANT'])): ?>
                                <div class="col-md-3">
                                    <label class="form-label">Doctor</label>
                                    <select class="form-select" name="doctor_id">
                                        <option value="">All Doctors</option>
                                        <?php while($doc = $doctors->fetch_assoc()): ?>
                                            <option value="<?php echo $doc['doctor_id']; ?>" 
                                                    <?php echo $filter_doctor == $doc['doctor_id'] ? 'selected' : ''; ?>>
                                                Dr. <?php echo htmlspecialchars($doc['full_name']); ?>
                                            </option>
                                        <?php endwhile; ?>
                                    </select>
                                </div>
                                <?php endif; ?>
                                <div class="col-md-2">
                                    <label class="form-label">Date</label>
                                    <input type="date" class="form-control" name="date" 
                                           value="<?php echo htmlspecialchars($filter_date); ?>">
                                </div>
                                <div class="col-md-2 d-flex align-items-end">
                                    <button type="submit" class="btn btn-primary w-100">
                                        <i class="ri-search-line me-1"></i> Filter
                                    </button>
                                </div>
                            </div>
                            <?php if ($filter_search || $filter_status || $filter_doctor || $filter_date): ?>
                            <div class="mt-2">
                                <a href="list_appointments.php" class="btn btn-sm btn-outline-secondary">
                                    <i class="ri-close-line me-1"></i> Clear Filters
                                </a>
                            </div>
                            <?php endif; ?>
                        </form>
                    </div>

                    <!-- Appointments Table -->
                    <div class="table-card">
                        <div class="d-flex justify-content-between align-items-center mb-3">
                            <h5 class="mb-0">
                                Appointments List 
                                <span class="text-muted">(<?php echo number_format($total_records); ?> records)</span>
                            </h5>
                            <div>
                                <button class="btn btn-sm btn-outline-primary" onclick="window.print()">
                                    <i class="ri-printer-line me-1"></i> Print
                                </button>
                                <button class="btn btn-sm btn-outline-success" onclick="exportToExcel()">
                                    <i class="ri-file-excel-line me-1"></i> Export
                                </button>
                            </div>
                        </div>

                        <div class="table-responsive">
                            <?php if ($appointments->num_rows > 0): ?>
                            <table class="appointment-table">
                                <thead>
                                    <tr>
                                        <th>Appt #</th>
                                        <th>Patient</th>
                                        <th>Doctor</th>
                                        <th>Date & Time</th>
                                        <th>Reason</th>
                                        <th>Payment</th>
                                        <th>Status</th>
                                        <th>Actions</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php while($apt = $appointments->fetch_assoc()): ?>
                                    <tr>
                                        <td>
                                            <strong><?php echo htmlspecialchars($apt['appointment_number']); ?></strong>
                                        </td>
                                        <td>
                                            <div class="patient-info">
                                                <div class="patient-avatar">
                                                    <?php echo strtoupper(substr($apt['patient_name'], 0, 2)); ?>
                                                </div>
                                                <div class="patient-details">
                                                    <h6><?php echo htmlspecialchars($apt['patient_name']); ?></h6>
                                                    <p><?php echo htmlspecialchars($apt['patient_number']); ?></p>
                                                </div>
                                            </div>
                                        </td>
                                        <td>
                                            <div>
                                                <div class="fw-bold">Dr. <?php echo htmlspecialchars($apt['doctor_name']); ?></div>
                                                <small class="text-muted"><?php echo htmlspecialchars($apt['doctor_specialization']); ?></small>
                                            </div>
                                        </td>
                                        <td>
                                            <div>
                                                <i class="ri-calendar-line me-1"></i>
                                                <?php echo date('M d, Y', strtotime($apt['appointment_date'])); ?>
                                            </div>
                                            <div class="text-muted small">
                                                <i class="ri-time-line me-1"></i>
                                                <?php echo date('h:i A', strtotime($apt['appointment_time'])); ?>
                                            </div>
                                        </td>
                                        <td>
                                            <small><?php echo $apt['reason'] ? htmlspecialchars(substr($apt['reason'], 0, 50)) . '...' : '-'; ?></small>
                                        </td>
                                        <td>
                                            <?php if ($apt['payment_id']): ?>
                                                <div class="fw-bold">KES <?php echo number_format($apt['payment_amount'], 2); ?></div>
                                                <span class="badge badge-<?php echo strtolower($apt['payment_status']); ?>">
                                                    <?php echo $apt['payment_status']; ?>
                                                </span>
                                            <?php else: ?>
                                                <span class="text-muted">-</span>
                                            <?php endif; ?>
                                        </td>
                                        <td>
                                            <span class="badge badge-<?php echo strtolower($apt['status']); ?>">
                                                <?php echo str_replace('_', ' ', $apt['status']); ?>
                                            </span>
                                        </td>
                                        <td>
                                            <div class="d-flex gap-1">
                                                <a href="view_appointment.php?id=<?php echo $apt['appointment_id']; ?>" 
                                                   class="action-btn btn-view" title="View Details">
                                                    <i class="ri-eye-line"></i>
                                                </a>
                                                <?php if (in_array($apt['status'], ['SCHEDULED', 'CONFIRMED']) && 
                                                          in_array($_SESSION['role_code'], ['SUPER_ADMIN', 'HOSPITAL_ADMIN', 'RECEPTIONIST'])): ?>
                                                <button onclick="cancelAppointment(<?php echo $apt['appointment_id']; ?>)" 
                                                        class="action-btn btn-cancel" title="Cancel">
                                                    <i class="ri-close-line"></i>
                                                </button>
                                                <?php endif; ?>
                                            </div>
                                        </td>
                                    </tr>
                                    <?php endwhile; ?>
                                </tbody>
                            </table>

                            <!-- Pagination -->
                            <?php if ($total_pages > 1): ?>
                            <div class="d-flex justify-content-between align-items-center mt-4">
                                <div class="text-muted">
                                    Showing <?php echo $offset + 1; ?> to <?php echo min($offset + $records_per_page, $total_records); ?> 
                                    of <?php echo number_format($total_records); ?> records
                                </div>
                                <nav>
                                    <ul class="pagination mb-0">
                                        <?php if ($page > 1): ?>
                                        <li class="page-item">
                                            <a class="page-link" href="?page=<?php echo $page - 1; ?><?php echo $filter_status ? '&status='.$filter_status : ''; ?><?php echo $filter_doctor ? '&doctor_id='.$filter_doctor : ''; ?><?php echo $filter_date ? '&date='.$filter_date : ''; ?><?php echo $filter_search ? '&search='.$filter_search : ''; ?>">
                                                Previous
                                            </a>
                                        </li>
                                        <?php endif; ?>
                                        
                                        <?php for ($i = max(1, $page - 2); $i <= min($total_pages, $page + 2); $i++): ?>
                                        <li class="page-item <?php echo $i === $page ? 'active' : ''; ?>">
                                            <a class="page-link" href="?page=<?php echo $i; ?><?php echo $filter_status ? '&status='.$filter_status : ''; ?><?php echo $filter_doctor ? '&doctor_id='.$filter_doctor : ''; ?><?php echo $filter_date ? '&date='.$filter_date : ''; ?><?php echo $filter_search ? '&search='.$filter_search : ''; ?>">
                                                <?php echo $i; ?>
                                            </a>
                                        </li>
                                        <?php endfor; ?>
                                        
                                        <?php if ($page < $total_pages): ?>
                                        <li class="page-item">
                                            <a class="page-link" href="?page=<?php echo $page + 1; ?><?php echo $filter_status ? '&status='.$filter_status : ''; ?><?php echo $filter_doctor ? '&doctor_id='.$filter_doctor : ''; ?><?php echo $filter_date ? '&date='.$filter_date : ''; ?><?php echo $filter_search ? '&search='.$filter_search : ''; ?>">
                                                Next
                                            </a>
                                        </li>
                                        <?php endif; ?>
                                    </ul>
                                </nav>
                            </div>
                            <?php endif; ?>
                            <?php else: ?>
                                <div class="text-center text-muted py-5">
                                    <i class="ri-calendar-line" style="font-size: 4rem; opacity: 0.3;"></i>
                                    <p class="mt-2">No appointments found</p>
                                </div>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>

    <?php include 'layouts/vendor-scripts.php'; ?>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="assets/libs/sweetalert2/sweetalert2.min.js"></script>
    <script src="assets/js/app.js"></script>

    <script>
        function cancelAppointment(appointmentId) {
            Swal.fire({
                title: 'Cancel Appointment?',
                text: "This action cannot be undone",
                icon: 'warning',
                showCancelButton: true,
                confirmButtonColor: '#d33',
                cancelButtonColor: '#3085d6',
                confirmButtonText: 'Yes, cancel it',
                cancelButtonText: 'No, keep it'
            }).then((result) => {
                if (result.isConfirmed) {
                    // Add cancellation reason prompt
                    Swal.fire({
                        title: 'Cancellation Reason',
                        input: 'textarea',
                        inputLabel: 'Please provide a reason for cancellation',
                        inputPlaceholder: 'Enter reason here...',
                        showCancelButton: true,
                        confirmButtonText: 'Submit',
                        preConfirm: (reason) => {
                            if (!reason) {
                                Swal.showValidationMessage('Reason is required');
                            }
                            return reason;
                        }
                    }).then((reasonResult) => {
                        if (reasonResult.isConfirmed) {
                            $.ajax({
                                url: 'cancel_appointment.php',
                                method: 'POST',
                                data: {
                                    appointment_id: appointmentId,
                                    cancellation_reason: reasonResult.value
                                },
                                dataType: 'json',
                                success: function(response) {
                                    if (response.success) {
                                        Swal.fire({
                                            icon: 'success',
                                            title: 'Cancelled',
                                            text: response.message,
                                            timer: 2000
                                        }).then(() => {
                                            window.location.reload();
                                        });
                                    } else {
                                        Swal.fire('Error', response.message, 'error');
                                    }
                                },
                                error: function() {
                                    Swal.fire('Error', 'Failed to cancel appointment', 'error');
                                }
                            });
                        }
                    });
                }
            });
        }

        function exportToExcel() {
            Swal.fire({
                title: 'Exporting...',
                text: 'Please wait',
                allowOutsideClick: false,
                didOpen: () => {
                    Swal.showLoading();
                }
            });
            
            // Redirect to export script with current filters
            const params = new URLSearchParams(window.location.search);
            params.set('export', 'excel');
            window.location.href = 'export_appointments.php?' + params.toString();
            
            setTimeout(() => {
                Swal.close();
            }, 2000);
        }

        // Show success/error messages
        const urlParams = new URLSearchParams(window.location.search);
        if (urlParams.get('success') === 'cancelled') {
            Swal.fire({
                icon: 'success',
                title: 'Appointment Cancelled',
                text: 'The appointment has been cancelled successfully',
                timer: 3000,
                showConfirmButton: false
            });
        }
    </script>
</body>
</html>