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

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

// Action Handler
if (isset($_POST['update_status'])) {
    $appid = $_POST['appointment_id'];
    $status = $_POST['status'];
    $stmt = $conn->prepare("UPDATE appointments SET status = ? WHERE appointment_id = ?");
    $stmt->bind_param("si", $status, $appid);
    $stmt->execute();
    $_SESSION['success'] = "Appointment status updated to $status";
    header("Location: manage_appointments.php");
    exit();
}

// Fetch all future appointments
$sql = "SELECT a.*, 
               p.first_name AS pat_fname, p.last_name AS pat_lname, p.phone_number,
               u.full_name AS doc_name
        FROM appointments a
        JOIN patients p ON a.patient_id = p.patient_id
        JOIN users u ON a.doctor_id = u.user_id
        WHERE a.appointment_date >= CURDATE()
        ORDER BY a.appointment_date ASC";
$result = $conn->query($sql);
?>
<?php include 'layouts/main.php'; ?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Manage Appointments')); ?>
    <?php include 'layouts/head-css.php'; ?>
    <link href="assets/libs/datatables.net-bs5/css/dataTables.bootstrap5.min.css" rel="stylesheet" />
</head>

<body>
    <div id="layout-wrapper">
        <?php include 'layouts/menu.php'; ?>
        <div class="main-content">
            <div class="page-content">
                <div class="container-fluid">
                    <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">Manage Appointments</h4>
                            </div>
                        </div>
                    </div>

                    <?php if (isset($_SESSION['success'])): ?>
                        <div class="alert alert-success"><?php echo $_SESSION['success'];
                        unset($_SESSION['success']); ?>
                        </div>
                    <?php endif; ?>

                    <div class="row">
                        <div class="col-12">
                            <div class="card">
                                <div class="card-body">
                                    <table id="apptTable"
                                        class="table table-bordered dt-responsive nowrap table-striped">
                                        <thead>
                                            <tr>
                                                <th>Date & Time</th>
                                                <th>Patient</th>
                                                <th>Contact</th>
                                                <th>Doctor</th>
                                                <th>Status</th>
                                                <th>Action</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php while ($row = $result->fetch_assoc()): ?>
                                                <tr>
                                                    <td><?php echo date('M d, Y H:i', strtotime($row['appointment_date'])); ?>
                                                    </td>
                                                    <td><?php echo htmlspecialchars($row['pat_fname'] . ' ' . $row['pat_lname']); ?>
                                                    </td>
                                                    <td><?php echo htmlspecialchars($row['phone_number']); ?></td>
                                                    <td><?php echo htmlspecialchars($row['doc_name']); ?></td>
                                                    <td>
                                                        <?php
                                                        $badge = 'secondary';
                                                        if ($row['status'] == 'SCHEDULED')
                                                            $badge = 'primary';
                                                        if ($row['status'] == 'CONFIRMED')
                                                            $badge = 'info';
                                                        if ($row['status'] == 'CANCELLED')
                                                            $badge = 'danger';
                                                        ?>
                                                        <span
                                                            class="badge bg-<?php echo $badge; ?>"><?php echo $row['status']; ?></span>
                                                    </td>
                                                    <td>
                                                        <div class="dropdown">
                                                            <button class="btn btn-sm btn-light dropdown-toggle"
                                                                type="button" data-bs-toggle="dropdown">Action</button>
                                                            <ul class="dropdown-menu">
                                                                <li>
                                                                    <form method="POST" style="display:inline;">
                                                                        <input type="hidden" name="appointment_id"
                                                                            value="<?php echo $row['appointment_id']; ?>">
                                                                        <input type="hidden" name="status"
                                                                            value="CANCELLED">
                                                                        <button type="submit" name="update_status"
                                                                            class="dropdown-item text-danger">Cancel</button>
                                                                    </form>
                                                                </li>
                                                                <li>
                                                                    <form method="POST" style="display:inline;">
                                                                        <input type="hidden" name="appointment_id"
                                                                            value="<?php echo $row['appointment_id']; ?>">
                                                                        <input type="hidden" name="status"
                                                                            value="CONFIRMED">
                                                                        <button type="submit" name="update_status"
                                                                            class="dropdown-item">Confirm</button>
                                                                    </form>
                                                                </li>
                                                                <li><a class="dropdown-item"
                                                                        href="appointment_edit.php?id=<?php echo $row['appointment_id']; ?>">Reschedule</a>
                                                                </li>
                                                            </ul>
                                                        </div>
                                                    </td>
                                                </tr>
                                            <?php endwhile; ?>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>
    <?php include 'layouts/vendor-scripts.php'; ?>
    <script src="assets/libs/datatables.net/js/jquery.dataTables.min.js"></script>
    <script src="assets/libs/datatables.net-bs5/js/dataTables.bootstrap5.min.js"></script>
    <script>$('#apptTable').DataTable();</script>
</body>

</html>