<?php include 'koneksi.php';
session_start();
if (!isset($_SESSION['login_admin'])) {
    header('Location: dashboard.php');
    exit;
}

if (!isset($_GET['id'])) {
    header('Location: manage_pengumuman.php');
    exit;
}

$id = intval($_GET['id']);
$query = "SELECT * FROM tb_pengumuman WHERE id_pengumuman = $id";
$result = mysqli_query($host, $query);
$row = mysqli_fetch_assoc($result);

if (!$row) {
    header('Location: manage_pengumuman.php');
    exit;
}

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $judul = mysqli_real_escape_string($host, $_POST['judul']);
    $deskripsi = mysqli_real_escape_string($host, $_POST['deskripsi']);
    $tanggal = mysqli_real_escape_string($host, $_POST['tanggal']);
    $gambar = $row['gambar_pengumuman'];

    // Handle image upload
    if (!empty($_FILES['gambar']['name'])) {
        $target_dir = "upload/";
        $unique_name = time() . '_' . basename($_FILES["gambar"]["name"]);
        $target_file = $target_dir . $unique_name;
        $imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));

        $check = getimagesize($_FILES["gambar"]["tmp_name"]);
        if ($check !== false && in_array($imageFileType, ['jpg', 'png', 'jpeg', 'gif'])) {
            if (move_uploaded_file($_FILES["gambar"]["tmp_name"], $target_file)) {
                $gambar = $unique_name;
            }
        }
    }

    if (!empty($judul) && !empty($deskripsi)) {
        $update_query = "UPDATE tb_pengumuman SET judul_pengumuman='$judul', deskripsi_pengumuman='$deskripsi', tanggal='$tanggal', gambar_pengumuman='$gambar' WHERE id_pengumuman=$id";
        if (mysqli_query($host, $update_query)) {
            echo "<script>alert('Pengumuman berhasil diupdate!'); window.location.href='manage_pengumuman.php';</script>";
        } else {
            echo "<script>alert('Error: " . mysqli_error($host) . "');</script>";
        }
    } else {
        echo "<script>alert('Judul dan deskripsi harus diisi!');</script>";
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head><link rel="icon" href="upload/bn2.png" type="image/png">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Edit Pengumuman | SMK Budiniah 2</title>
    <link rel="stylesheet" href="style_admin.css">
    <style>
        body {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            font-family: 'Arial', sans-serif;
            margin: 0;
            padding: 0;
            min-height: 100vh;
        }
        .container {
            max-width: 800px;
            margin: 50px auto;
            padding: 30px;
            background: #fff;
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.2);
        }
        h1 {
            text-align: center;
            color: #0037fd;
            margin-bottom: 30px;
            font-size: 28px;
            font-weight: bold;
        }
        .form-group {
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            color: #333;
            font-weight: bold;
        }
        input[type="text"], textarea, input[type="file"], input[type="datetime-local"] {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 14px;
        }
        textarea {
            height: 100px;
            resize: vertical;
        }
        .btn {
            background: linear-gradient(135deg, #0037fd, #5b8cffff);
            color: #fff;
            padding: 10px 20px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            font-weight: bold;
            transition: all 0.3s ease;
        }
        .btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(0,55,253,0.4);
        }
        .btn-secondary {
            background: #6c757d;
            margin-left: 10px;
        }
        .btn-secondary:hover {
            background: #5a6268;
        }
        .form-actions {
            text-align: center;
            margin-top: 30px;
        }

        /* Responsive Design */
        @media (max-width: 768px) {
            .container {
                margin: 30px 20px 0 15%;
                max-width: 500px;
                padding: 20px;
            }
            h1 {
                font-size: 24px;
                margin-bottom: 20px;
            }
            input[type="text"], textarea, input[type="file"] {
                padding: 8px;
                font-size: 13px;
            }
            .btn {
                padding: 8px 16px;
                font-size: 14px;
            }
            .btn-secondary {
                margin-left: 5px;
            }
        }

        @media (max-width: 480px) {
            .container {
                margin: 20px 20px 0 15%;
                max-width: 400px;
                padding: 15px;
            }
            h1 {
                font-size: 20px;
                margin-bottom: 15px;
            }
            .form-group {
                margin-bottom: 15px;
            }
            input[type="text"], textarea, input[type="file"] {
                padding: 6px;
                font-size: 12px;
            }
            textarea {
                height: 80px;
            }
            .btn {
                padding: 6px 12px;
                font-size: 12px;
            }
            .form-actions {
                margin-top: 20px;
            }
        }
        .current-media {
            margin-top: 10px;
            font-size: 12px;
            color: #666;
        }
    </style>
</head>
<body>
    <?php include 'navbar_admin.php'; ?>
    <div class="container">
        <h1>Edit Pengumuman</h1>
        <form method="POST" enctype="multipart/form-data">
            <div class="form-group">
                <label for="judul">Judul Pengumuman:</label>
                <input type="text" id="judul" name="judul" value="<?php echo htmlspecialchars($row['judul_pengumuman']); ?>" required>
            </div>
            <div class="form-group">
                <label for="deskripsi">Deskripsi:</label>
                <textarea id="deskripsi" name="deskripsi" required><?php echo htmlspecialchars($row['deskripsi_pengumuman']); ?></textarea>
            </div>
            <div class="form-group">
                <label for="tanggal">Tanggal:</label>
                <input type="datetime-local" id="tanggal" name="tanggal" value="<?php echo date('Y-m-d\TH:i', strtotime($row['tanggal'])); ?>" required>
            </div>
            <div class="form-group">
                <label for="gambar">Upload Gambar Baru (Opsional):</label>
                <input type="file" id="gambar" name="gambar" accept="image/*">
                <div class="current-media">
                    <?php if (!empty($row['gambar_pengumuman'])): ?>
                        Gambar saat ini: <a href="upload/<?php echo htmlspecialchars($row['gambar_pengumuman']); ?>" target="_blank"><?php echo htmlspecialchars($row['gambar_pengumuman']); ?></a>
                    <?php endif; ?>
                </div>
            </div>
            <div class="form-actions">
                <button type="submit" class="btn">Update Pengumuman</button>
                <a href="manage_pengumuman.php" class="btn btn-secondary">Batal</a>
            </div>
        </form>
    </div>
</body>
</html>

