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

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

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

if (!$row) {
    header('Location: berita_admin.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']);
    $kategori = mysqli_real_escape_string($host, $_POST['kategori']);
    $jenis = mysqli_real_escape_string($host, $_POST['jenis']);
    $gambar = $row['gambar'];
    $iframe = $row['iframe'];

    // 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;
                $jenis = 'foto';
                $iframe = '';
            }
        }
    }

    // Handle iframe
    if (!empty($_POST['iframe'])) {
        $iframe = mysqli_real_escape_string($host, $_POST['iframe']);
        $jenis = 'video';
        $gambar = '';
    }

    if (!empty($judul) && !empty($deskripsi)) {
        $update_query = "UPDATE tb_post SET judul_post='$judul', deskripsi='$deskripsi', tanggal='$tanggal', kategori='$kategori', jenis='$jenis', gambar='$gambar', iframe='$iframe' WHERE id_post=$id";
        if (mysqli_query($host, $update_query)) {
            echo "<script>alert('Berita berhasil diupdate!'); window.location.href='berita_admin.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 Berita | 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: 1000px;
            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="date"] {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 14px;
        }
        textarea {
            height: 100px;
            resize: vertical;
        }
        .radio-group {
            display: flex;
            gap: 20px;
            margin-bottom: 20px;
        }
        .radio-group label {
            font-weight: normal;
        }
        .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-media {
            margin-top: 10px;
            font-size: 12px;
            color: #666;
        }

        /* Mobile Responsiveness */
        @media (max-width: 768px) {
            .container {
                max-width: 500px;
                margin: 20px 5% 40px 15%;  
                padding: 20px;
            }
            h1 {
                font-size: 24px;
            }
            .radio-group {
                flex-direction: column;
                gap: 10px;
            }
            .form-actions {
                display: flex;
                flex-direction: column;
                gap: 10px;
            }
            .btn {
                width: 100%;
            }
            .btn-secondary {
                margin-left: 0;
            }
            input[type="text"], textarea, input[type="file"], input[type="date"] {
                padding: 8px;
                font-size: 13px;
            }
            textarea {
                height: 80px;
            }
        }
    </style>
</head>
<body>
    <?php include 'navbar_admin.php'; ?>
    <div class="container">
        <h1>Edit Berita</h1>
        <form method="POST" enctype="multipart/form-data">
            <div class="form-group">
                <label for="judul">Nama Berita:</label>
                <input type="text" id="judul" name="judul" value="<?php echo htmlspecialchars($row['judul_post']); ?>" 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="tanggal">Tanggal:</label>
                <input type="date" id="tanggal" name="tanggal" value="<?php echo $row['tanggal']; ?>" required>
            </div>
            <div class="form-group">
                <label for="kategori">Kategori:</label>
                <input type="text" id="kategori" name="kategori" value="<?php echo htmlspecialchars($row['kategori']); ?>" required>
            </div>
            <div class="radio-group">
                <label>
                    <input type="radio" name="media_type" value="gambar" <?php echo ($row['jenis'] == 'foto') ? 'checked' : ''; ?> onchange="toggleMediaInput()">
                    Gambar
                </label>
                <label>
                    <input type="radio" name="media_type" value="iframe" <?php echo ($row['jenis'] == 'video') ? 'checked' : ''; ?> onchange="toggleMediaInput()">
                    Iframe (Video)
                </label>
            </div>
            <div class="form-group" id="gambar-group" style="display: <?php echo ($row['jenis'] == 'foto') ? 'block' : 'none'; ?>;">
                <label for="gambar">Upload Gambar Baru:</label>
                <input type="file" id="gambar" name="gambar" accept="image/*">
                <div class="current-media">
                    <?php if (!empty($row['gambar'])): ?>
                        Gambar saat ini: <a href="upload/<?php echo htmlspecialchars($row['gambar']); ?>" target="_blank"><?php echo htmlspecialchars($row['gambar']); ?></a>
                    <?php endif; ?>
                </div>
            </div>
            <div class="form-group" id="iframe-group" style="display: <?php echo ($row['jenis'] == 'video') ? 'block' : 'none'; ?>;">
                <label for="iframe">Iframe Video:</label>
                <textarea id="iframe" name="iframe" placeholder="Masukkan kode iframe video di sini"><?php echo htmlspecialchars($row['iframe']); ?></textarea>
            </div>
            <div class="form-group">
                <label for="jenis">Jenis:</label>
                <input type="text" id="jenis" name="jenis" value="<?php echo htmlspecialchars($row['jenis']); ?>" readonly>
            </div>
            <div class="form-actions">
                <button type="submit" class="btn">Update Berita</button>
                <a href="berita_admin.php" class="btn btn-secondary">Batal</a>
            </div>
        </form>
    </div>
    <script>
        function toggleMediaInput() {
            const mediaType = document.querySelector('input[name="media_type"]:checked').value;
            const gambarGroup = document.getElementById('gambar-group');
            const iframeGroup = document.getElementById('iframe-group');
            const jenisInput = document.getElementById('jenis');

            if (mediaType === 'gambar') {
                gambarGroup.style.display = 'block';
                iframeGroup.style.display = 'none';
                jenisInput.value = 'foto';
            } else {
                gambarGroup.style.display = 'none';
                iframeGroup.style.display = 'block';
                jenisInput.value = 'video';
            }
        }
    </script>
</body>
</html>

