<?php
require_once '../functions/truncate.php';

if (isset($_SESSION['admin'])) {
?>

    <table class="table table-striped table-hover table-sm table-responsive">
        <thead class="text-white bg-dark">
            <tr>
                <th scope="col">Name</th>
                <th scope="col">Phone</th>
                <th scope="col">Email</th> 
                <th scope="col">Therapy</th>
                <th scope="col">Date</th>
                <th scope="col">Status</th>
                <th scope="col"></th>
            </tr>
        </thead>
        <tbody>
            <?php
            $dbHandler = DbHandler();
            $query = $dbHandler->prepare("SELECT * FROM tbl_consultation ORDER BY created_at desc");
            $query->execute();
            while ($row = $query->fetch()) {
                $id = $row['id'];
                $name = $row['a_fullname'];
                $email = $row['a_email'];
                $countryCode = $row['a_countryCode'];
                $phone = $row['a_phone'];
                $therapy = $row['a_therapy'];
                $appointmentDate = $row['a_date'];
                $status = $row['a_status'];
            ?>
                <tr>
                    <td>
                        &nbsp;<?php echo $name; ?>
                    </td>
                    <td>
                        &nbsp;(+<?php echo $countryCode; ?>)<?php echo $phone; ?>
                    </td>
                    <td>
                        &nbsp;<?php echo $email; ?>
                    </td>
                    <td>
                        &nbsp;<?php echo $therapy; ?>
                    </td>
                    <td>
                        &nbsp;<?php echo $appointmentDate; ?>
                    </td>
                    <td>
                        &nbsp;<?php echo $status; ?>
                    </td>
                    <td>
                        <a class="btn btn-success btn-sm rounded" href="./edit-consultation.php?id=<?php echo base64_url_encode($id); ?>">Edit</a>
                        <a class="btn btn-danger btn-sm rounded" href="../functions/deleteConsultation.php?id=<?php echo base64_url_encode($id); ?>" onclick="return confirm('Are you sure?')">Delete</a>
                    </td>
                </tr>
        </tbody>
<?php
            }
        } else {
            echo "<script> window.location.assign('../logout.php'); </script>";
        }
?>
    </table>