<?= $this->extend('layout/default') ?>

<?= $this->section('content') ?>
<div class="container pt-100 pb-100">
    <div class="row">
        <div class="col-md-12">
            <div class="card shadow border-0">
                <div class="card-header bg-success text-white d-flex justify-content-between align-items-center">
                    <h4 class="mb-0 text-white"><i class="fa fa-newspaper-o"></i> Manajemen Berita SMKN 1 Sukalarang</h4>
                    <a href="<?= base_url('admin/berita/tambah') ?>" class="btn btn-light btn-sm fw-bold">
                        <i class="fa fa-plus"></i> Tambah Berita
                    </a>
                </div>
                <div class="card-body p-4">
                    
                    <?php if (session()->getFlashdata('success')) : ?>
                        <div class="alert alert-success alert-dismissible fade show" role="alert">
                            <?= session()->getFlashdata('success') ?>
                            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                        </div>
                    <?php endif; ?>

                    <div class="table-responsive">
                        <table class="table table-hover align-middle">
                            <thead class="table-light">
                                <tr>
                                    <th width="50">No</th>
                                    <th width="100">Gambar</th>
                                    <th>Judul Berita</th>
                                    <th>Keterangan Foto</th>
                                    <th>Tanggal</th>
                                    <th>Status</th>
                                    <th width="150" class="text-center">Aksi</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php $no = 1; foreach ($berita as $b) : ?>
                                <tr>
                                    <td><?= $no++ ?></td>
                                    <td>
                                        <img src="<?= base_url('assets/img/berita/' . ($b['gambar'] ?: 'default.jpg')) ?>" 
                                             class="rounded" style="width: 70px; height: 50px; object-fit: cover;">
                                    </td>
                                    <td>
                                        <span class="fw-bold"><?= $b['judul'] ?></span><br>
                                        <small class="text-muted"><?= $b['slug'] ?></small>
                                    </td>
                                    <td><?= $b['caption_foto'] ?></td>
                                    <td><?= date('d/m/Y', strtotime($b['tanggal_post'])) ?></td>
                                    <td>
                                        <span class="badge <?= $b['status'] == 'publish' ? 'bg-success' : 'bg-secondary' ?>">
                                            <?= ucfirst($b['status']) ?>
                                        </span>
                                    </td>
                                    <td class="text-center">
                                        <div class="btn-group">
                                            <a href="<?= base_url('admin/berita/edit/' . $b['id_berita']) ?>" class="btn btn-warning btn-sm" title="Edit">
                                                <i class="fa fa-edit"></i>
                                            </a>
                                            <a href="<?= base_url('admin/berita/hapus/' . $b['id_berita']) ?>" 
                                               class="btn btn-danger btn-sm" 
                                               onclick="return confirm('Apakah Bapak yakin ingin menghapus berita ini?')" 
                                               title="Hapus">
                                                <i class="fa fa-trash"></i>
                                            </a>
                                        </div>
                                    </td>
                                </tr>
                                <?php endforeach; ?>

                                <?php if (empty($berita)) : ?>
                                <tr>
                                    <td colspan="6" class="text-center py-4 text-muted fst-italic">Belum ada berita. Silakan klik Tambah Berita.</td>
                                </tr>
                                <?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<?= $this->endSection() ?>