<?php
session_start();
require_once '../config.php';

if (!isset($_SESSION['role']) || $_SESSION['role'] !== 'admin') {
    header("Location: ../login.php"); exit;
}

// Logika Upload Foto
if (isset($_POST['upload'])) {
    $judul = $_POST['judul_slider'];
    $sub   = $_POST['sub_judul'];
    $file_name = $_FILES['gambar']['name'];
    $file_tmp  = $_FILES['gambar']['tmp_name'];
    $target    = "../assets/img/slider/" . time() . "_" . $file_name;

    if (move_uploaded_file($file_tmp, $target)) {
        $stmt = $pdo->prepare("INSERT INTO slider (gambar, judul_slider, sub_judul) VALUES (?, ?, ?)");
        $stmt->execute([$target, $judul, $sub]);
        echo "<script>alert('Foto Slider Berhasil Ditambah!'); window.location='manage_slider.php';</script>";
    }
}

// Logika Hapus
if (isset($_GET['hapus'])) {
    $id = $_GET['hapus'];
    $stmt = $pdo->prepare("SELECT gambar FROM slider WHERE id_slider = ?");
    $stmt->execute([$id]);
    $img = $stmt->fetch();
    unlink($img['gambar']); // Hapus file fisik
    $pdo->prepare("DELETE FROM slider WHERE id_slider = ?")->execute([$id]);
    header("Location: manage_slider.php");
}

$sliders = $pdo->query("SELECT * FROM slider ORDER BY id_slider DESC")->fetchAll();
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Kelola Slider</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <?php include 'sidebar.php'; ?>
    <div class="main-content">
        <div class="container-fluid">
            <h4 class="fw-bold mb-4">Pengaturan Slider Website</h4>

            <div class="card border-0 shadow-sm rounded-4 mb-4">
                <div class="card-body p-4">
                    <h6 class="fw-bold">Tambah Foto Slider Baru</h6>
                    <form method="POST" enctype="multipart/form-data" class="row g-3">
                        <div class="col-md-4">
                            <label class="small fw-bold">Pilih Foto (Rekomendasi 1920x800)</label>
                            <input type="file" name="gambar" class="form-control" required>
                        </div>
                        <div class="col-md-3">
                            <label class="small fw-bold">Judul Utama</label>
                            <input type="text" name="judul_slider" class="form-control" placeholder="Contoh: Selamat Datang">
                        </div>
                        <div class="col-md-3">
                            <label class="small fw-bold">Sub Judul</label>
                            <input type="text" name="sub_judul" class="form-control" placeholder="Contoh: Posyandu Desa Maju">
                        </div>
                        <div class="col-md-2 d-flex align-items-end">
                            <button type="submit" name="upload" class="btn btn-primary w-100 fw-bold">Upload</button>
                        </div>
                    </form>
                </div>
            </div>

            <div class="row g-3">
                <?php foreach($sliders as $s): ?>
                <div class="col-md-4 col-sm-6">
                    <div class="card border-0 shadow-sm rounded-4 overflow-hidden">
                        <img src="<?= $s['gambar'] ?>" class="card-img-top" style="height: 150px; object-fit: cover;">
                        <div class="card-body p-3">
                            <h6 class="mb-1 fw-bold"><?= $s['judul_slider'] ?></h6>
                            <p class="small text-muted mb-3"><?= $s['sub_judul'] ?></p>
                            <a href="?hapus=<?= $s['id_slider'] ?>" class="btn btn-sm btn-outline-danger w-100" onclick="return confirm('Hapus foto ini?')">Hapus</a>
                        </div>
                    </div>
                </div>
                <?php endforeach; ?>
            </div>
        </div>
    </div>
</body>
</html>