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

$id_prestasi = isset($_GET['id_prestasi']) ? intval($_GET['id_prestasi']) : 0;

if ($id_prestasi == 0) {
    header('Location: manage_prestasi.php');
    exit;
}

$query = "SELECT * FROM tb_prestasi WHERE id_prestasi = $id_prestasi";
$result = mysqli_query($host, $query);
if (!$result || mysqli_num_rows($result) == 0) {
    header('Location: manage_prestasi.php');
    exit;
}
$row = mysqli_fetch_assoc($result);

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $nama_prestasi = mysqli_real_escape_string($host, $_POST['nama_prestasi']);
    $deskripsi = mysqli_real_escape_string($host, $_POST['deskripsi']);
    $kejuaraan_dari = mysqli_real_escape_string($host, $_POST['kejuaraan_dari']);
    $gambar_prestasi = $row['gambar_prestasi']; // Keep current image by default

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

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

    if (!empty($nama_prestasi) && !empty($deskripsi) && !empty($kejuaraan_dari)) {
        $query = "UPDATE tb_prestasi SET nama_prestasi = '$nama_prestasi', deskripsi = '$deskripsi', kejuaraan_dari = '$kejuaraan_dari', gambar_prestasi = '$gambar_prestasi' WHERE id_prestasi = $id_prestasi";
        if (mysqli_query($host, $query)) {
            echo "<script>alert('Prestasi berhasil diperbarui!'); window.location.href='manage_prestasi.php';</script>";
        } else {
            echo "<script>alert('Error: " . mysqli_error($host) . "');</script>";
        }
    } else {
        echo "<script>alert('Semua field 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 Prestasi | 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"], select {
            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;
        }
        .current-image {
            margin-top: 10px;
            font-size: 12px;
            color: #666;
        }

        /* Responsive Design */
        @media (max-width: 768px) {
            .container {
                margin: 30px 20px 40px 15%;
                max-width: 600px;
                padding: 20px;
            }
            h1 {
                font-size: 24px;
                margin-bottom: 20px;
            }
            .form-group {
                margin-bottom: 15px;
            }
            input[type="text"], textarea, input[type="file"], select {
                padding: 8px;
                font-size: 13px;
            }
            textarea {
                height: 80px;
            }
            .btn {
                padding: 8px 16px;
                font-size: 14px;
            }
            .btn-secondary {
                margin-left: 8px;
            }
            .form-actions {
                margin-top: 20px;
            }
            .current-image img {
                max-width: 200px;
                height: auto;
            }
        }

        @media (max-width: 480px) {
            .container {
                margin: 20px 20px 40px 15%;
                max-width: 500px;
                padding: 15px;
            }
            h1 {
                font-size: 20px;
                margin-bottom: 15px;
            }
            .form-group {
                margin-bottom: 12px;
            }
            input[type="text"], textarea, input[type="file"], select {
                padding: 6px;
                font-size: 12px;
            }
            textarea {
                height: 70px;
            }
            .btn {
                padding: 6px 12px;
                font-size: 12px;
            }
            .btn-secondary {
                margin-left: 5px;
            }
            .form-actions {
                margin-top: 15px;
            }
            .current-image img {
                max-width: 250px;
                max-height: 250px;
            }
        }
    </style>
</head>
<body>
    <?php include 'navbar_admin.php'; ?>
    <div class="container">
        <h1>Edit Prestasi</h1>
        <form method="POST" enctype="multipart/form-data">
            <div class="form-group">
                <label for="nama_prestasi">Nama Prestasi:</label>
                <input type="text" id="nama_prestasi" name="nama_prestasi" value="<?php echo htmlspecialchars($row['nama_prestasi']); ?>" required>
            </div>
            <div class="form-group">
                <label for="deskripsi">Deskripsi:</label>
                <textarea id="deskripsi" name="deskripsi" required><?php echo htmlspecialchars($row['deskripsi']); ?></textarea>
            </div>
            <div class="form-group">
                <label for="kejuaraan_dari">Kejuaraan Dari:</label>
                <select name="kejuaraan_dari" id="kejuaraan_dari">
                    <option value="jurusan" <?php echo ($row['kejuaraan_dari'] == 'jurusan') ? 'selected' : ''; ?>>Jurusan</option>
                    <option value="Organisasi" <?php echo ($row['kejuaraan_dari'] == 'Organisasi') ? 'selected' : ''; ?>>Organisasi</option>
                </select>
            </div>
            <div class="form-group">
                <label for="gambar_prestasi">Upload Gambar Prestasi Baru (Opsional):</label>
                <input type="file" id="gambar_prestasi" name="gambar_prestasi" accept="image/*">
                <div class="current-image">
                    Gambar saat ini: <img src="upload/<?php echo !empty($row['gambar_prestasi']) ? htmlspecialchars($row['gambar_prestasi']) : 'Tidak ada gambar'; ?>" alt="Current Image" style="max-width: 400px; max-height: 400px;">
                </div>
            </div>
            <div class="form-actions">
                <button type="submit" class="btn">Update Prestasi</button>
                <a href="manage_prestasi.php" class="btn btn-secondary">Batal</a>
            </div>
        </form>
    </div>
</body>
</html>

