<?php

require_once '../includes/config.php';

$db = getDB();

if(isset($_GET['status']) && isset($_GET['id'])){

    $status = $_GET['status'];
    $id = (int)$_GET['id'];

    $stmt = $db->prepare("
    UPDATE appointments
    SET status=?
    WHERE id=?
    ");

    $stmt->execute([$status, $id]);

    header("Location: appointments.php");
    exit;
}

$stmt = $db->query("
SELECT appointments.*, lawyers.name AS lawyer_name
FROM appointments
LEFT JOIN lawyers ON lawyers.id = appointments.lawyer_id
ORDER BY appointments.id DESC
");

$appointments = $stmt->fetchAll();

require_once '../includes/header.php';

?>

<section class="section-light py-5">

<div class="container">

<h2 class="fw-bold mb-4">
Manage Appointments
</h2>

<div class="card border-0 shadow">

<div class="card-body">

<div class="table-responsive">

<table class="table table-bordered">

<tr>
<th>Booking ID</th>
<th>Lawyer</th>
<th>Date</th>
<th>Status</th>
<th>Type</th>
<th>Action</th>
</tr>

<?php foreach($appointments as $appointment): ?>

<tr>

<td>
<?= htmlspecialchars($appointment['booking_id']) ?>
</td>

<td>
<?= htmlspecialchars($appointment['lawyer_name']) ?>
</td>

<td>
<?= htmlspecialchars($appointment['appointment_date']) ?>
</td>

<td>
<?= htmlspecialchars($appointment['status']) ?>
</td>

<td>
<?= htmlspecialchars($appointment['consultation_type']) ?>
</td>

<td>

<a href="?status=confirmed&id=<?= $appointment['id'] ?>"
   class="btn btn-success btn-sm">
Confirm
</a>

<a href="?status=completed&id=<?= $appointment['id'] ?>"
   class="btn btn-primary btn-sm">
Complete
</a>

<a href="?status=cancelled&id=<?= $appointment['id'] ?>"
   class="btn btn-danger btn-sm">
Cancel
</a>

</td>

</tr>

<?php endforeach; ?>

</table>

</div>

</div>

</div>

</div>

</section>

<?php require_once '../includes/footer.php'; ?>