<?= $this->extend('layout/main_layout') ?>
<?= $this->section('content') ?>
<div class="container-fluid">
    <div class="row">
        <main class="col-12 px-md-4">
            <div class="pt-3 pb-2 mb-3 dashboard-title">
                <h1 class="h2">Messages</h1>
                <h5 class="h5 mt-2">Send and receive messages from teachers and school administrators.</h5>
            </div>

            <!-- Section to display received messages -->
            <div class="mb-4">
                <h3>Received Messages</h3>
                <?php if (!empty($receivedMessages)): ?>
                    <div class="table-responsive">
                        <table class="table table-striped">
                            <thead>
                                <tr>
                                    <th scope="col">Message Number</th>
                                    <th scope="col">From</th>
                                    <th scope="col">Subject</th>
                                    <th scope="col">Message</th>
                                    <th scope="col">Date</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach ($receivedMessages as $message): ?>
                                    <tr>
                                        <td><?= $message['message_number'] ?></td>
                                        <td><?= $message['sender_name'] ?></td>
                                        <td><?= $message['subject'] ?></td>
                                        <td><?= $message['message'] ?></td>
                                        <td><?= !empty($message['date_sent']) ? esc(local_datetime($message['date_sent'], 'm-d-Y H:i')) : '' ?></td>
                                    </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                <?php else: ?>
                    <p>No messages found.</p>
                <?php endif; ?>
            </div>

            <!-- Section to send new messages -->
            <div class="mb-4">
                <h3>Send a Message</h3>
                <form action="<?= base_url('messages/send') ?>" method="post">
                    <div class="form-group">
                        <label for="recipient">Recipient</label>
                        <select class="form-control" id="recipient" name="recipient" required>
                            <option value="teacher">Teacher</option>
                            <option value="administration">Administration</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="subject">Subject</label>
                        <input type="text" class="form-control" id="subject" name="subject" required>
                    </div>
                    <div class="form-group">
                        <label for="message">Message</label>
                        <textarea class="form-control" id="message" name="message" rows="4" required></textarea>
                    </div>
                    <button type="submit" class="btn btn-primary">Send Message</button>
                </form>
            </div>
        </main>
    </div>
</div>
<?= $this->endSection() ?>
