<?php
// Pastikan TIDAK ADA spasi atau baris kosong sebelum tag <?php di atas!
require_once __DIR__ . '/../includes/config.php';
require_once __DIR__ . '/../includes/db.php';
require_once __DIR__ . '/../includes/functions.php';
require_once __DIR__ . '/../includes/auth.php';
requireAdmin();

$pdo = db();
$statusMsg = '';

// 1. PROSES SIMPAN DATA
if (isset($_POST['simpan_histori'])) {
    $thn = $_POST['tahun'];
    $jdl = $_POST['judul'];
    $dsc = $_POST['deskripsi'];

    try {
        $sql = "INSERT INTO histori (tahun, judul, deskripsi) VALUES (:thn, :jdl, :dsc)";
        $stmt = $pdo->prepare($sql);
        $eksekusi = $stmt->execute([
            ':thn' => $thn,
            ':jdl' => $jdl,
            ':dsc' => $dsc
        ]);

        if ($eksekusi) {
            header("Location: kelola_histori.php?msg=sukses");
            exit();
        } else {
            $statusMsg = "gagal_simpan";
        }
    } catch (PDOException $e) {
        // Jika error database, tampilkan di sini
        die("Gagal Simpan ke Database: " . $e->getMessage());
    }
}

// 2. PROSES HAPUS
if (isset($_GET['hapus'])) {
    $id = $_GET['hapus'];
    $pdo->prepare("DELETE FROM histori WHERE id = ?")->execute([$id]);
    header("Location: kelola_histori.php?msg=hapus");
    exit();
}

// Ambil pesan dari URL
if (isset($_GET['msg'])) {
    if ($_GET['msg'] == 'sukses') $statusMsg = 'Berhasil menyimpan data!';
    if ($_GET['msg'] == 'hapus') $statusMsg = 'Data berhasil dihapus!';
}

// 3. AMBIL DATA
$dataHistori = $pdo->query("SELECT * FROM histori ORDER BY tahun ASC")->fetchAll(PDO::FETCH_ASSOC);

include __DIR__ . '/admin_layout.php';
?>

<div class="container-fluid mt-4">
    <h2>Kelola Histori Toko</h2>
    <hr>

    <?php if (!empty($statusMsg)): ?>
        <div class="alert alert-info"><?= $statusMsg ?></div>
    <?php endif; ?>

    <div class="row">
        <div class="col-md-4">
            <div class="card p-3 shadow-sm">
                <form action="kelola_histori.php" method="POST">
                    <div class="mb-3">
                        <label>Tahun</label>
                        <input type="number" name="tahun" class="form-control" required>
                    </div>
                    <div class="mb-3">
                        <label>Judul</label>
                        <input type="text" name="judul" class="form-control" required>
                    </div>
                    <div class="mb-3">
                        <label>Deskripsi</label>
                        <textarea name="deskripsi" class="form-control" rows="3" required></textarea>
                    </div>
                    <button type="submit" name="simpan_histori" class="btn btn-primary w-100">Simpan Sekarang</button>
                </form>
            </div>
        </div>

        <div class="col-md-8">
            <div class="card shadow-sm">
                <table class="table table-striped mb-0">
                    <thead class="table-dark">
                        <tr>
                            <th>Tahun</th>
                            <th>Peristiwa</th>
                            <th>Aksi</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if (empty($dataHistori)): ?>
                            <tr><td colspan="3" class="text-center">Data kosong di database.</td></tr>
                        <?php else: ?>
                            <?php foreach ($dataHistori as $h): ?>
                            <tr>
                                <td><?= $h['tahun'] ?></td>
                                <td>
                                    <strong><?= htmlspecialchars($h['judul']) ?></strong><br>
                                    <small><?= htmlspecialchars($h['deskripsi']) ?></small>
                                </td>
                                <td>
                                    <a href="?hapus=<?= $h['id'] ?>" class="btn btn-danger btn-sm" onclick="return confirm('Hapus?')">Hapus</a>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<?php include __DIR__ . '/admin_footer.php'; ?>