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

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

// Fetch tomorrow's appointments
$date = date('Y-m-d', strtotime('+1 day'));
$sql = "SELECT a.*, p.first_name, p.last_name, p.phone_number, u.email 
        FROM appointments a
        JOIN patients p ON a.patient_id = p.patient_id
        JOIN users u ON p.user_id = u.user_id
        WHERE DATE(a.appointment_date) = '$date' AND a.status = 'SCHEDULED'";
$result = $conn->query($sql);

if (isset($_POST['send_bulk'])) {
    // Logic to loop and send simulated reminders
    $_SESSION['success'] = "Bulk reminders queued for tomorrow's appointments.";
    header("Location: send_reminders.php");
    exit();
}
?>
<?php include 'layouts/main.php'; ?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Appointment Reminders')); ?>
    <?php include 'layouts/head-css.php'; ?>
</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">Appointment Reminders (Tomorrow: <?php echo $date; ?>)</h4>
                                <div class="page-title-right">
                                    <form method="POST">
                                        <button type="submit" name="send_bulk" class="btn btn-success"><i
                                                class="ri-send-plane-fill"></i> Send All Reminders</button>
                                    </form>
                                </div>
                            </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 class="table table-bordered table-striped">
                                        <thead>
                                            <tr>
                                                <th>Time</th>
                                                <th>Patient</th>
                                                <th>Phone</th>
                                                <th>Email</th>
                                                <th>Status</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php
                                            if ($result->num_rows > 0) {
                                                while ($row = $result->fetch_assoc()): ?>
                                                    <tr>
                                                        <td><?php echo date('H:i', strtotime($row['appointment_date'])); ?></td>
                                                        <td><?php echo htmlspecialchars($row['first_name'] . ' ' . $row['last_name']); ?>
                                                        </td>
                                                        <td><?php echo $row['phone_number']; ?></td>
                                                        <td><?php echo $row['email']; ?></td>
                                                        <td><span class="badge bg-warning">Pending</span></td>
                                                    </tr>
                                                <?php endwhile;
                                            } else {
                                                echo '<tr><td colspan="5" class="text-center">No appointments scheduled for tomorrow.</td></tr>';
                                            }
                                            ?>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>
    <?php include 'layouts/vendor-scripts.php'; ?>
</body>

</html>