<?php
session_start();
include 'layouts/dbconn.php';
include 'layouts/session.php';
if (!isset($_SESSION['user_id'])) {
    header('Location: login.php');
    exit();
}
// Get dispensing ID
$dispensing_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if (!$dispensing_id) {
    header('Location: pharmacy_dispensing_history.php');
    exit();
}
// Fetch dispensing details with related information
$dispensing_sql = "SELECT d.*,
                          ip.provider_name, ip.phone as provider_phone,
                          u.full_name as dispensed_by_name,
                          p.prescription_number, p.medication_name as prescribed_medication,
                          pt.patient_number, doc.date_of_birth, doc.gender, pt.blood_group,
                          pu.email as patient_email,
                          pv.visit_number, pv.visit_type,
                          doc.full_name as prescribing_doctor
                   FROM pharmacy_dispensing d
                   LEFT JOIN insurance_providers ip ON d.insurance_provider_id = ip.provider_id
                   LEFT JOIN users u ON d.dispensed_by = u.user_id
                   LEFT JOIN prescriptions p ON d.prescription_id = p.prescription_id
                   LEFT JOIN patients pt ON d.patient_id = pt.patient_id
                   LEFT JOIN users pu ON pt.user_id = pu.user_id
                   LEFT JOIN patient_visits pv ON d.visit_id = pv.visit_id
                   LEFT JOIN users doc ON p.doctor_id = doc.user_id
                   WHERE d.dispensing_id = ?";
$stmt = $conn->prepare($dispensing_sql);
$stmt->bind_param('i', $dispensing_id);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows === 0) {
    header('Location: pharmacy_dispensing_history.php');
    exit();
}
$dispensing = $result->fetch_assoc();
// Fetch dispensing items
$items_sql = "SELECT di.*,
                     pi.item_code, pi.dosage_form, pi.strength, pi.generic_name,
                     pi.manufacturer, pi.category
              FROM pharmacy_dispensing_items di
              LEFT JOIN pharmacy_inventory pi ON di.inventory_id = pi.inventory_id
              WHERE di.dispensing_id = ?
              ORDER BY di.item_id";
$stmt = $conn->prepare($items_sql);
$stmt->bind_param('i', $dispensing_id);
$stmt->execute();
$items_result = $stmt->get_result();
$items = $items_result->fetch_all(MYSQLI_ASSOC);
// Calculate statistics
$total_items = count($items);
$total_quantity = array_sum(array_column($items, 'quantity'));
?>
<?php include 'layouts/main.php'; ?>
<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'View Dispensing')); ?>
    <?php include 'layouts/head-css.php'; ?>
    <link href="https://cdn.jsdelivr.net/npm/sweetalert2@11/dist/sweetalert2.min.css" rel="stylesheet">
    <style>
        .info-card {
            border-left: 4px solid #3b82f6;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border-radius: 8px;
            padding: 1.5rem;
            margin-bottom: 1.5rem;
        }
        .info-card h3 {
            color: white;
            margin-bottom: 1rem;
            font-size: 1.5rem;
        }
        .info-row {
            display: flex;
            justify-content: space-between;
            padding: 0.5rem 0;
            border-bottom: 1px solid rgba(255,255,255,0.2);
        }
        .info-row:last-child {
            border-bottom: none;
        }
        .info-label {
            font-weight: 500;
            opacity: 0.9;
        }
        .info-value {
            font-weight: 600;
        }
        .section-card {
            background: white;
            border-radius: 8px;
            padding: 1.5rem;
            margin-bottom: 1.5rem;
            border: 1px solid #e5e7eb;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
        }
        .section-title {
            font-size: 1.25rem;
            font-weight: 600;
            color: #1f2937;
            margin-bottom: 1rem;
            padding-bottom: 0.5rem;
            border-bottom: 2px solid #3b82f6;
        }
        .badge-status {
            padding: 0.5rem 1rem;
            border-radius: 6px;
            font-weight: 600;
            font-size: 0.875rem;
        }
        .badge-paid {
            background-color: #dcfce7;
            color: #166534;
        }
        .badge-pending {
            background-color: #fef3c7;
            color: #92400e;
        }
        .badge-partial {
            background-color: #dbeafe;
            color: #1e40af;
        }
        .item-card {
            border: 1px solid #e5e7eb;
            border-radius: 6px;
            padding: 1rem;
            margin-bottom: 0.75rem;
            transition: all 0.2s;
        }
        .item-card:hover {
            box-shadow: 0 4px 6px rgba(0,0,0,0.1);
            border-color: #3b82f6;
        }
        .item-header {
            display: flex;
            justify-content: space-between;
            align-items: start;
            margin-bottom: 0.5rem;
        }
        .item-name {
            font-weight: 600;
            font-size: 1.1rem;
            color: #1f2937;
        }
        .item-price {
            font-weight: 700;
            font-size: 1.1rem;
            color: #3b82f6;
        }
        .stats-card {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border-radius: 8px;
            padding: 1.5rem;
            text-align: center;
            box-shadow: 0 4px 6px rgba(0,0,0,0.1);
        }
        .stats-value {
            font-size: 2rem;
            font-weight: 700;
            margin-bottom: 0.5rem;
        }
        .stats-label {
            font-size: 0.875rem;
            opacity: 0.9;
        }
        .action-button {
            margin-right: 0.5rem;
            margin-bottom: 0.5rem;
        }
        .timeline-item {
            border-left: 2px solid #3b82f6;
            padding-left: 1rem;
            margin-left: 0.5rem;
            padding-bottom: 1rem;
        }
        .timeline-item:last-child {
            border-left-color: transparent;
        }
        .prescription-badge {
            display: inline-block;
            padding: 0.25rem 0.75rem;
            border-radius: 12px;
            font-size: 0.75rem;
            font-weight: 600;
            background-color: #eff6ff;
            color: #1e40af;
            border: 1px solid #3b82f6;
        }
    </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">
                                    <i class="ri-medicine-bottle-line me-2"></i>Dispensing Details
                                </h4>
                                <div class="page-title-right">
                                    <ol class="breadcrumb m-0">
                                        <li class="breadcrumb-item"><a href="pharmacy_dashboard.php">Pharmacy</a></li>
                                        <li class="breadcrumb-item"><a href="pharmacy_dispensing_history.php">Dispensing History</a></li>
                                        <li class="breadcrumb-item active">View Details</li>
                                    </ol>
                                </div>
                            </div>
                        </div>
                    </div>
                    <!-- Dispensing Header Card -->
                    <div class="info-card">
                        <div class="row align-items-center">
                            <div class="col-md-8">
                                <h3><i class="ri-file-list-3-line me-2"></i><?php echo $dispensing['dispensing_number']; ?></h3>
                                <div class="row">
                                    <div class="col-md-6">
                                        <div class="info-row">
                                            <span class="info-label">Patient:</span>
                                            <span class="info-value"><?php echo htmlspecialchars($dispensing['patient_name']); ?></span>
                                        </div>
                                        <div class="info-row">
                                            <span class="info-label">Patient Type:</span>
                                            <span class="info-value"><?php echo $dispensing['patient_type']; ?></span>
                                        </div>
                                        <?php if ($dispensing['patient_number']): ?>
                                        <div class="info-row">
                                            <span class="info-label">Patient Number:</span>
                                            <span class="info-value"><?php echo $dispensing['patient_number']; ?></span>
                                        </div>
                                        <?php endif; ?>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="info-row">
                                            <span class="info-label">Date:</span>
                                            <span class="info-value"><?php echo date('d M Y, H:i', strtotime($dispensing['dispensing_date'])); ?></span>
                                        </div>
                                        <div class="info-row">
                                            <span class="info-label">Dispensed By:</span>
                                            <span class="info-value"><?php echo htmlspecialchars($dispensing['dispensed_by_name']); ?></span>
                                        </div>
                                        <?php if ($dispensing['prescription_number']): ?>
                                        <div class="info-row">
                                            <span class="info-label">Prescription:</span>
                                            <span class="info-value">
                                                <span class="prescription-badge"><?php echo $dispensing['prescription_number']; ?></span>
                                            </span>
                                        </div>
                                        <?php endif; ?>
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-4">
                                <div class="text-center">
                                    <div class="stats-value">KES <?php echo number_format($dispensing['total_amount'], 2); ?></div>
                                    <div class="stats-label">Total Amount</div>
                                    <div class="mt-3">
                                        <?php
                                        $status_class = '';
                                        switch($dispensing['payment_status']) {
                                            case 'PAID':
                                                $status_class = 'badge-paid';
                                                break;
                                            case 'PENDING':
                                                $status_class = 'badge-pending';
                                                break;
                                            case 'PARTIAL':
                                                $status_class = 'badge-partial';
                                                break;
                                        }
                                        ?>
                                        <span class="badge-status <?php echo $status_class; ?>">
                                            <?php echo $dispensing['payment_status']; ?>
                                        </span>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <!-- Action Buttons -->
                    <div class="row mb-3">
                        <div class="col-12">
                            <a href="generate_dispensing_invoice.php?id=<?php echo $dispensing_id; ?>"
                               class="btn btn-primary action-button" target="_blank">
                                <i class="ri-printer-line me-1"></i>Print Invoice
                            </a>
                            <a href="pharmacy_dispensing_history.php" class="btn btn-outline-secondary action-button">
                                <i class="ri-arrow-left-line me-1"></i>Back to History
                            </a>
                            <?php if ($dispensing['payment_status'] === 'PENDING'): ?>
                            <button type="button" class="btn btn-success action-button" onclick="updatePaymentStatus()">
                                <i class="ri-money-dollar-circle-line me-1"></i>Mark as Paid
                            </button>
                            <?php endif; ?>
                            <?php if (!empty($dispensing['prescription_image'])): ?>
                            <a href="<?php echo $dispensing['prescription_image']; ?>"
                               class="btn btn-info action-button" target="_blank">
                                <i class="ri-image-line me-1"></i>View Prescription Image
                            </a>
                            <?php endif; ?>
                        </div>
                    </div>
                    <div class="row">
                        <!-- Left Column -->
                        <div class="col-lg-8">
                            <!-- Patient Information -->
                            <div class="section-card">
                                <h5 class="section-title"><i class="ri-user-3-line me-2"></i>Patient Information</h5>
                                <div class="row">
                                    <div class="col-md-6">
                                        <table class="table table-sm table-borderless">
                                            <tr>
                                                <td class="text-muted" width="40%">Full Name:</td>
                                                <td><strong><?php echo htmlspecialchars($dispensing['patient_name']); ?></strong></td>
                                            </tr>
                                            <tr>
                                                <td class="text-muted">Phone:</td>
                                                <td><?php echo $dispensing['patient_phone'] ?? 'N/A'; ?></td>
                                            </tr>
                                            <?php if ($dispensing['patient_email']): ?>
                                            <tr>
                                                <td class="text-muted">Email:</td>
                                                <td><?php echo $dispensing['patient_email']; ?></td>
                                            </tr>
                                            <?php endif; ?>
                                            <?php if ($dispensing['patient_number']): ?>
                                            <tr>
                                                <td class="text-muted">Patient Number:</td>
                                                <td><span class="badge bg-primary"><?php echo $dispensing['patient_number']; ?></span></td>
                                            </tr>
                                            <?php endif; ?>
                                        </table>
                                    </div>
                                    <div class="col-md-6">
                                        <table class="table table-sm table-borderless">
                                            <?php if ($dispensing['gender']): ?>
                                            <tr>
                                                <td class="text-muted" width="40%">Gender:</td>
                                                <td><?php echo $dispensing['gender']; ?></td>
                                            </tr>
                                            <?php endif; ?>
                                            <?php if ($dispensing['date_of_birth']): ?>
                                            <tr>
                                                <td class="text-muted">Date of Birth:</td>
                                                <td><?php echo date('d M Y', strtotime($dispensing['date_of_birth'])); ?></td>
                                            </tr>
                                            <tr>
                                                <td class="text-muted">Age:</td>
                                                <td><?php
                                                    $dob = new DateTime($dispensing['date_of_birth']);
                                                    $now = new DateTime();
                                                    echo $dob->diff($now)->y . ' years';
                                                ?></td>
                                            </tr>
                                            <?php endif; ?>
                                            <?php if ($dispensing['blood_group']): ?>
                                            <tr>
                                                <td class="text-muted">Blood Group:</td>
                                                <td><span class="badge bg-danger"><?php echo $dispensing['blood_group']; ?></span></td>
                                            </tr>
                                            <?php endif; ?>
                                        </table>
                                    </div>
                                </div>
                            </div>
                            <!-- Prescription Information -->
                            <?php if ($dispensing['prescription_number']): ?>
                            <div class="section-card">
                                <h5 class="section-title"><i class="ri-file-text-line me-2"></i>Prescription Details</h5>
                                <div class="row">
                                    <div class="col-md-6">
                                        <table class="table table-sm table-borderless">
                                            <tr>
                                                <td class="text-muted" width="40%">Prescription No:</td>
                                                <td><strong><?php echo $dispensing['prescription_number']; ?></strong></td>
                                            </tr>
                                            <tr>
                                                <td class="text-muted">Medication:</td>
                                                <td><?php echo htmlspecialchars($dispensing['prescribed_medication']); ?></td>
                                            </tr>
                                        </table>
                                    </div>
                                    <div class="col-md-6">
                                        <table class="table table-sm table-borderless">
                                            <?php if ($dispensing['prescribing_doctor']): ?>
                                            <tr>
                                                <td class="text-muted" width="40%">Prescribed By:</td>
                                                <td>Dr. <?php echo htmlspecialchars($dispensing['prescribing_doctor']); ?></td>
                                            </tr>
                                            <?php endif; ?>
                                            <?php if ($dispensing['visit_number']): ?>
                                            <tr>
                                                <td class="text-muted">Visit Number:</td>
                                                <td><?php echo $dispensing['visit_number']; ?></td>
                                            </tr>
                                            <?php endif; ?>
                                        </table>
                                    </div>
                                </div>
                            </div>
                            <?php endif; ?>
                            <!-- Dispensed Medications -->
                            <div class="section-card">
                                <h5 class="section-title"><i class="ri-medicine-bottle-line me-2"></i>Dispensed Medications</h5>
                                <?php foreach ($items as $item): ?>
                                <div class="item-card">
                                    <div class="row">
                                        <div class="col-md-7">
                                            <div class="item-name"><?php echo htmlspecialchars($item['item_name']); ?></div>
                                            <?php if ($item['generic_name']): ?>
                                            <small class="text-muted">Generic: <?php echo htmlspecialchars($item['generic_name']); ?></small><br>
                                            <?php endif; ?>
                                            <?php if ($item['strength']): ?>
                                            <span class="badge bg-info me-1"><?php echo $item['strength']; ?></span>
                                            <?php endif; ?>
                                            <?php if ($item['dosage_form']): ?>
                                            <span class="badge bg-secondary me-1"><?php echo $item['dosage_form']; ?></span>
                                            <?php endif; ?>
                                            <?php if ($item['category']): ?>
                                            <span class="badge bg-primary"><?php echo $item['category']; ?></span>
                                            <?php endif; ?>
                                           
                                            <?php if ($item['dosage_instructions']): ?>
                                            <div class="mt-2 p-2" style="background: #f3f4f6; border-radius: 4px;">
                                                <small class="text-muted"><i class="ri-information-line me-1"></i><strong>Dosage:</strong></small>
                                                <p class="mb-0 mt-1"><?php echo htmlspecialchars($item['dosage_instructions']); ?></p>
                                            </div>
                                            <?php endif; ?>
                                        </div>
                                        <div class="col-md-5">
                                            <table class="table table-sm table-borderless mb-0">
                                                <tr>
                                                    <td class="text-muted">Quantity:</td>
                                                    <td class="text-end"><strong><?php echo $item['quantity']; ?></strong></td>
                                                </tr>
                                                <tr>
                                                    <td class="text-muted">Unit Price:</td>
                                                    <td class="text-end">KES <?php echo number_format($item['unit_price'], 2); ?></td>
                                                </tr>
                                                <tr>
                                                    <td class="text-muted"><strong>Total:</strong></td>
                                                    <td class="text-end item-price">KES <?php echo number_format($item['total_price'], 2); ?></td>
                                                </tr>
                                            </table>
                                            <?php if ($item['batch_number']): ?>
                                            <small class="text-muted d-block mt-2">Batch: <?php echo $item['batch_number']; ?></small>
                                            <?php endif; ?>
                                            <?php if ($item['expiry_date']): ?>
                                            <small class="text-muted d-block">Expiry: <?php echo date('M Y', strtotime($item['expiry_date'])); ?></small>
                                            <?php endif; ?>
                                        </div>
                                    </div>
                                </div>
                                <?php endforeach; ?>
                            </div>
                            <!-- Notes -->
                            <?php if ($dispensing['notes']): ?>
                            <div class="section-card">
                                <h5 class="section-title"><i class="ri-file-text-line me-2"></i>Additional Notes</h5>
                                <p class="mb-0"><?php echo nl2br(htmlspecialchars($dispensing['notes'])); ?></p>
                            </div>
                            <?php endif; ?>
                        </div>
                        <!-- Right Column -->
                        <div class="col-lg-4">
                            <!-- Statistics -->
                            <div class="row mb-3">
                                <div class="col-md-12 mb-3">
                                    <div class="stats-card">
                                        <div class="stats-value"><?php echo $total_items; ?></div>
                                        <div class="stats-label">Different Items</div>
                                    </div>
                                </div>
                                <div class="col-md-12 mb-3">
                                    <div class="stats-card">
                                        <div class="stats-value"><?php echo $total_quantity; ?></div>
                                        <div class="stats-label">Total Units</div>
                                    </div>
                                </div>
                            </div>
                            <!-- Payment Details -->
                            <div class="section-card">
                                <h5 class="section-title"><i class="ri-money-dollar-circle-line me-2"></i>Payment Details</h5>
                                <table class="table table-sm table-borderless">
                                    <tr>
                                        <td class="text-muted">Payment Method:</td>
                                        <td class="text-end"><strong><?php echo $dispensing['payment_method']; ?></strong></td>
                                    </tr>
                                    <tr>
                                        <td class="text-muted">Subtotal:</td>
                                        <td class="text-end">KES <?php echo number_format($dispensing['total_amount'], 2); ?></td>
                                    </tr>
                                    <tr>
                                        <td class="text-muted">Amount Paid:</td>
                                        <td class="text-end text-success"><strong>KES <?php echo number_format($dispensing['amount_paid'], 2); ?></strong></td>
                                    </tr>
                                    <tr style="border-top: 2px solid #e5e7eb;">
                                        <td class="text-muted"><strong>Balance:</strong></td>
                                        <td class="text-end <?php echo $dispensing['balance'] > 0 ? 'text-danger' : 'text-success'; ?>">
                                            <strong>KES <?php echo number_format($dispensing['balance'], 2); ?></strong>
                                        </td>
                                    </tr>
                                </table>
                            </div>
                            <!-- Insurance Details -->
                            <?php if ($dispensing['payment_method'] === 'INSURANCE'): ?>
                            <div class="section-card">
                                <h5 class="section-title"><i class="ri-shield-check-line me-2"></i>Insurance Information</h5>
                                <table class="table table-sm table-borderless mb-0">
                                    <tr>
                                        <td class="text-muted">Provider:</td>
                                        <td class="text-end"><strong><?php echo htmlspecialchars($dispensing['provider_name']); ?></strong></td>
                                    </tr>
                                    <?php if ($dispensing['provider_phone']): ?>
                                    <tr>
                                        <td class="text-muted">Contact:</td>
                                        <td class="text-end"><?php echo $dispensing['provider_phone']; ?></td>
                                    </tr>
                                    <?php endif; ?>
                                    <?php if ($dispensing['insurance_claim_number']): ?>
                                    <tr>
                                        <td class="text-muted">Claim Number:</td>
                                        <td class="text-end"><span class="badge bg-info"><?php echo $dispensing['insurance_claim_number']; ?></span></td>
                                    </tr>
                                    <?php endif; ?>
                                </table>
                               
                                <?php if ($dispensing['payment_status'] === 'PENDING'): ?>
                                <div class="alert alert-warning mt-3 mb-0">
                                    <small><i class="ri-information-line me-1"></i><strong>Pending Payment:</strong> This transaction is awaiting insurance reconciliation.</small>
                                </div>
                                <?php endif; ?>
                            </div>
                            <?php endif; ?>
                            <!-- Timeline -->
                            <div class="section-card">
                                <h5 class="section-title"><i class="ri-time-line me-2"></i>Timeline</h5>
                                <div class="timeline-item">
                                    <small class="text-muted"><?php echo date('d M Y, H:i', strtotime($dispensing['created_at'])); ?></small>
                                    <p class="mb-0 mt-1"><strong>Dispensing Created</strong></p>
                                    <small class="text-muted">By <?php echo htmlspecialchars($dispensing['dispensed_by_name']); ?></small>
                                </div>
                                <?php if ($dispensing['payment_status'] === 'PAID'): ?>
                                <div class="timeline-item">
                                    <small class="text-muted"><?php echo date('d M Y, H:i', strtotime($dispensing['updated_at'])); ?></small>
                                    <p class="mb-0 mt-1"><strong>Payment Completed</strong></p>
                                    <small class="text-success">KES <?php echo number_format($dispensing['amount_paid'], 2); ?> received</small>
                                </div>
                                <?php endif; ?>
                            </div>
                        </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="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
    <script src="assets/js/app.js"></script>
   
    <script>
    function updatePaymentStatus() {
        Swal.fire({
            title: 'Update Payment Status',
            html: `
                <div class="text-start">
                    <p>Are you sure you want to mark this dispensing as <strong>PAID</strong>?</p>
                    <div class="mb-3">
                        <label class="form-label">Amount Paid:</label>
                        <input type="number" id="amount_paid" class="form-control"
                               value="<?php echo $dispensing['total_amount']; ?>"
                               step="0.01" min="0" max="<?php echo $dispensing['total_amount']; ?>">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Payment Reference (Optional):</label>
                        <input type="text" id="payment_reference" class="form-control"
                               placeholder="Transaction ID, Receipt Number, etc.">
                    </div>
                </div>
            `,
            icon: 'question',
            showCancelButton: true,
            confirmButtonText: 'Mark as Paid',
            confirmButtonColor: '#10b981',
            cancelButtonText: 'Cancel',
            preConfirm: () => {
                const amount = document.getElementById('amount_paid').value;
                const reference = document.getElementById('payment_reference').value;
               
                if (!amount || parseFloat(amount) <= 0) {
                    Swal.showValidationMessage('Please enter a valid amount');
                    return false;
                }
               
                return { amount: amount, reference: reference };
            }
        }).then((result) => {
            if (result.isConfirmed) {
                $.ajax({
                    url: 'pharmacy_update_payment.php',
                    type: 'POST',
                    data: {
                        dispensing_id: <?php echo $dispensing_id; ?>,
                        amount_paid: result.value.amount,
                        payment_reference: result.value.reference
                    },
                    dataType: 'json',
                    success: function(response) {
                        if (response.success) {
                            Swal.fire({
                                title: 'Success!',
                                text: 'Payment status updated successfully.',
                                icon: 'success',
                                confirmButtonText: 'OK'
                            }).then(() => {
                                location.reload();
                            });
                        } else {
                            Swal.fire({
                                title: 'Error!',
                                text: response.message || 'An error occurred while updating the payment status.',
                                icon: 'error',
                                confirmButtonText: 'OK'
                            });
                        }
                    },
                    error: function() {
                        Swal.fire({
                            title: 'Error!',
                            text: 'An error occurred while updating the payment status. Please try again.',
                            icon: 'error',
                            confirmButtonText: 'OK'
                        });
                    }
                });
            }
        });
    }
    </script>
</body>
</html>