<?= $this->session->flashdata('pesan'); ?>
<div class="card">
    <div class="card-header d-flex justify-content-between align-items-center w-100">
        <h3 class="card-title mb-1" style="margin-top: 5px;">Halaman Mengirim dan Melihat Pesan</h3>
    </div>

    <div class="row no-gutters px-4 py-3">
        <!-- Kolom kiri: Form Kirim Pesan -->
        <div class="col-lg-6 d-flex flex-column pr-3">
            <h5 class="mb-3">Pesan</h5>
            <div>
                <form id="formPesan" method="POST" action="<?= base_url('chat/sendChatFromAdmin') ?>">
                    <input type="hidden" name="id_wali_santri" value="<?= $id_wali_santri ?>">
                    <input type="hidden" name="sender" value="admin">

                    <div class="form-group">
                        <textarea name="message" class="form-control" rows="4" placeholder="Tulis pesan sebagai admin..." required></textarea>
                    </div>
                </form>
            </div>
            <!-- Tombol Aksi -->
            <div class="mt-3">
                <a href="<?= base_url('walisantri') ?>" class="btn btn-danger btn-sm">
                    <i class="fas fa-solid fa-angle-left"></i> Kembali
                </a>
                <button type="submit" class="btn btn-primary btn-sm" form="formPesan">Kirim</button>
            </div>
        </div>


        <!-- Kolom kanan: Riwayat Chat -->
        <div class="col-lg-6 d-flex flex-column pl-3">
            <h5 class="mb-3">Riwayat Pesan</h5>
            <div class="card flex-fill">
                <div class="card-body" style="background-color: #f8f9fa; max-height: 400px; overflow-y: auto;">
                    <?php if ($chat): ?>
                        <?php foreach ($chat as $c): ?>
                            <div class="mb-3 p-2 <?= ($c['sender'] === 'admin') ? 'bg-light border-left border-primary' : 'bg-white border-left border-success' ?>">
                                <strong><?= ($c['sender'] === 'admin') ? '🧑‍💼 Admin' : '👤 Wali' ?>:</strong>
                                <p class="mb-1"><?= htmlspecialchars($c['message']) ?></p>
                                <small class="text-muted"><?= date('d M Y H:i', strtotime($c['timestamp'])) ?></small>
                            </div>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <p class="text-muted">Belum ada pesan.</p>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
</div>