<?= $this->extend('layouts/main') ?>
<?= $this->section('content') ?>

<div class="row g-3">
    <!-- Header Card -->
    <div class="col-12">
        <div class="card border-0 shadow-sm" style="border-radius: 12px; overflow: hidden; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);">
            <div class="card-body p-4 text-white">
                <div class="d-flex align-items-center justify-content-between flex-wrap gap-3">
                    <div class="d-flex align-items-center gap-3">
                        <div class="bg-white bg-opacity-20 p-3 rounded-circle text-white" style="font-size: 2rem; line-height: 1;">
                            <i class="bi bi-chat-left-heart-fill"></i>
                        </div>
                        <div>
                            <h4 class="fw-bold mb-1">Pengaduan & E-Konseling Siswa</h4>
                            <p class="mb-0 text-white-50">Hubungi guru, konsultasikan masalah akademis/pribadi secara tertutup, atau laporkan aduan secara aman.</p>
                        </div>
                    </div>
                    <a href="/siswa/counseling/create" class="btn btn-white text-primary fw-bold px-4 py-2" style="border-radius: 8px; background-color: #fff; border: none; box-shadow: 0 4px 6px rgba(0,0,0,0.1);">
                        <i class="bi bi-plus-lg me-2"></i>Buat Aduan / Curhat Baru
                    </a>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content List -->
    <div class="col-12">
        <?php if (session()->getFlashdata('success')): ?>
            <div class="alert alert-success border-0 shadow-sm mb-3" style="border-radius: 8px;">
                <i class="bi bi-check-circle-fill me-2"></i><?= session()->getFlashdata('success') ?>
            </div>
        <?php endif; ?>
        <?php if (session()->getFlashdata('error')): ?>
            <div class="alert alert-danger border-0 shadow-sm mb-3" style="border-radius: 8px;">
                <i class="bi bi-exclamation-triangle-fill me-2"></i><?= session()->getFlashdata('error') ?>
            </div>
        <?php endif; ?>

        <div class="card border-0 shadow-sm" style="border-radius: 12px;">
            <div class="card-header bg-white border-0 pt-4 px-4">
                <h6 class="fw-bold text-dark mb-0"><i class="bi bi-clock-history text-primary me-2"></i>Riwayat Pengaduan & Konseling Anda</h6>
                <small class="text-muted">Klik pada tiket di bawah untuk masuk ke ruang obrolan bimbingan.</small>
            </div>
            <div class="card-body px-4 pb-4">
                <?php if (empty($tickets)): ?>
                    <div class="text-center py-5 text-muted">
                        <div class="bg-light p-4 rounded-circle d-inline-block mb-3" style="width: 80px; height: 80px;">
                            <i class="bi bi-chat-left-dots fs-1 text-muted opacity-50"></i>
                        </div>
                        <h6 class="fw-bold text-dark">Belum ada riwayat</h6>
                        <p class="mb-0 small text-muted">Jika ada kendala sarana prasarana, keluhan belajar, atau ingin curhat pribadi, silakan klik tombol di atas.</p>
                    </div>
                <?php else: ?>
                    <div class="row g-3 mt-2">
                        <?php foreach ($tickets as $t): ?>
                            <div class="col-md-6 col-lg-4">
                                <div class="card border-0 bg-light h-100 p-3" style="border-radius: 10px; transition: transform 0.2s, box-shadow 0.2s;" onmouseover="this.style.transform='translateY(-2px)'; this.style.boxShadow='0 8px 15px rgba(0,0,0,0.05)';" onmouseout="this.style.transform='none'; this.style.boxShadow='none';">
                                    <div class="d-flex align-items-center justify-content-between mb-3">
                                        <span class="badge px-3 py-1.5 rounded-pill <?= $t['status'] === 'open' ? 'bg-success bg-opacity-10 text-success' : 'bg-secondary bg-opacity-10 text-secondary' ?>" style="font-size: 0.75rem; font-weight: 600;">
                                            <i class="bi <?= $t['status'] === 'open' ? 'bi-chat-left-dots-fill' : 'bi-check-circle-fill' ?> me-1"></i>
                                            <?= $t['status'] === 'open' ? 'Aktif' : 'Selesai' ?>
                                        </span>
                                        <small class="text-muted" style="font-size: 0.72rem;"><?= date('d M Y, H:i', strtotime($t['created_at'])) ?></small>
                                    </div>
                                    <h6 class="fw-bold text-dark text-truncate mb-2"><?= esc($t['subject']) ?></h6>
                                    <div class="text-muted small mb-3">
                                        <div class="mb-1"><i class="bi bi-person-fill text-muted me-1"></i>Guru: <strong><?= esc($t['teacher_name']) ?></strong></div>
                                        <div class="mb-1"><i class="bi bi-tag-fill text-muted me-1"></i>Kategori: <span class="badge bg-secondary bg-opacity-10 text-secondary" style="font-size: 0.7rem;"><?= esc(get_counseling_category_label($t['category'])) ?></span></div>
                                        <div><i class="bi bi-shield-lock-fill text-muted me-1"></i>Sifat: <?= (int)$t['is_anonymous'] === 1 ? '<span class="text-danger">Anonim</span>' : '<span class="text-success">Terbuka</span>' ?></div>
                                    </div>
                                    <div class="mt-auto pt-2 border-top border-light d-flex justify-content-between align-items-center">
                                        <span class="text-muted text-uppercase" style="font-size: 0.7rem; font-weight: bold; letter-spacing: 0.5px;">
                                            <?= esc($t['type']) ?>
                                        </span>
                                        <a href="/siswa/counseling/chat/<?= $t['id'] ?>" class="btn btn-primary btn-sm px-3 fw-bold" style="border-radius: 6px;">
                                            <i class="bi bi-box-arrow-in-right me-1"></i>Masuk Chat
                                        </a>
                                    </div>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    </div>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>

<?= $this->endSection() ?>
