<?php
$search_patient = $_GET['patient'] ?? '';
$search_status = $_GET['status'] ?? '';

$query = "
    SELECT b.*, p.name as patient_name 
    FROM bills b 
    JOIN patients p ON b.patient_id = p.id 
    WHERE 1=1";
$params = [];

if ($search_patient) {
    $query .= " AND p.name LIKE ?";
    $params[] = "%$search_patient%";
}
if ($search_status) {
    $query .= " AND b.status = ?";
    $params[] = $search_status;
}

$query .= " ORDER BY b.created_at DESC";
$stmt = $db->prepare($query);
$stmt->execute($params);
$bills = $stmt->fetchAll();
?>

<div class="d-flex justify-content-between align-items-center mb-4">
    <h3 class="fw-bold text-secondary"><?php echo __('billing'); ?></h3>
</div>

<!-- Search Bar -->
<div class="card shadow-sm border-0 mb-4">
    <div class="card-body">
        <form method="GET" action="" class="row g-3">
            <div class="col-md-6">
                <div class="input-group">
                    <span class="input-group-text bg-light border-end-0 text-muted"><i class="bi bi-search"></i></span>
                    <input type="text" name="patient" class="form-control bg-light border-start-0" placeholder="<?php echo __('patient'); ?>" value="<?php echo htmlspecialchars($search_patient); ?>">
                </div>
            </div>
            <div class="col-md-4">
                <select name="status" class="form-select bg-light">
                    <option value=""><?php echo __('status'); ?> (<?php echo __('all'); ?>)</option>
                    <option value="Pending" <?php echo $search_status == 'Pending' ? 'selected' : ''; ?>><?php echo __('Pending'); ?></option>
                    <option value="Paid" <?php echo $search_status == 'Paid' ? 'selected' : ''; ?>><?php echo __('Paid'); ?></option>
                </select>
            </div>
            <div class="col-md-2">
                <button type="submit" class="btn btn-secondary w-100"><?php echo __('search'); ?></button>
            </div>
        </form>
    </div>
</div>

<div class="card shadow-sm border-0">
    <div class="card-body p-0">
        <div class="table-responsive">
            <table class="table table-hover align-middle mb-0">
                <thead class="table-light text-secondary">
                    <tr>
                        <th class="px-4 py-3">ID</th>
                        <th class="py-3"><?php echo __('date'); ?></th>
                        <th class="py-3"><?php echo __('patient'); ?></th>
                        <th class="py-3"><?php echo __('total'); ?></th>
                        <th class="py-3"><?php echo __('insurance_covered'); ?></th>
                        <th class="py-3"><?php echo __('patient_payable'); ?></th>
                        <th class="py-3"><?php echo __('status'); ?></th>
                        <th class="py-3 text-end px-4"><?php echo __('actions'); ?></th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($bills)): ?>
                    <tr>
                        <td colspan="8" class="text-center py-5 text-muted">
                            <i class="bi bi-receipt display-4 d-block mb-3"></i>
                            No bills found.
                        </td>
                    </tr>
                    <?php else: ?>
                    <?php foreach ($bills as $b): ?>
                    <tr>
                        <td class="px-4 text-secondary">#<?php echo $b['id']; ?></td>
                        <td class="text-secondary"><?php echo date('Y-m-d H:i', strtotime($b['created_at'])); ?></td>
                        <td class="fw-semibold text-dark"><?php echo htmlspecialchars($b['patient_name']); ?></td>
                        <td class="text-dark"><?php echo format_currency($b['total_amount']); ?></td>
                        <td class="text-primary"><?php echo format_currency($b['insurance_covered']); ?></td>
                        <td class="fw-bold text-dark"><?php echo format_currency($b['patient_payable']); ?></td>
                        <td>
                            <span class="badge <?php echo $b['status'] === 'Paid' ? 'bg-success bg-opacity-10 text-success border border-success border-opacity-25' : 'bg-warning bg-opacity-10 text-warning border border-warning border-opacity-25'; ?> px-2 py-1">
                                <?php echo __($b['status']); ?>
                            </span>
                        </td>
                        <td class="text-end px-4">
                            <?php if ($b['status'] === 'Pending'): ?>
                            <form action="<?php echo $_SERVER['PHP_SELF']; ?>?section=billing" method="POST" class="d-inline">
                                <input type="hidden" name="action" value="mark_paid">
                                <input type="hidden" name="bill_id" value="<?php echo $b['id']; ?>">
                                <button type="submit" class="btn btn-sm btn-success px-3">
                                    <i class="bi bi-check-circle me-1"></i> <?php echo __('mark_as_paid'); ?>
                                </button>
                            </form>
                            <?php endif; ?>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>