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

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

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $judul = $_POST['judul'];
    $isi   = $_POST['isi']; // HTML dari editor akan tersimpan di sini
    $kat   = $_POST['kategori'];
    $nama_file = null;

    if (!empty($_FILES['gambar']['name'])) {
        $nama_file = time() . "_" . $_FILES['gambar']['name'];
        move_uploaded_file($_FILES['gambar']['tmp_name'], "../assets/img/berita/" . $nama_file);
    }

    $stmt = $pdo->prepare("INSERT INTO berita (judul, isi, gambar, kategori) VALUES (?, ?, ?, ?)");
    if ($stmt->execute([$judul, $isi, $nama_file, $kat])) {
        echo "<script>alert('Berita berhasil diterbitkan!'); window.location='manage_berita.php';</script>";
    }
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Tulis Berita Profesional</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.ckeditor.com/ckeditor5/39.0.1/classic/ckeditor.js"></script>
    <style>
        /* Mengatur tinggi minimal editor agar nyaman di HP */
        .ck-editor__editable {
            min-height: 300px;
        }
    </style>
</head>
<body class="bg-light">
    <?php include 'sidebar.php'; ?>
    <div class="main-content">
        <div class="container-fluid">
            <div class="card border-0 shadow-sm rounded-4 mx-auto" style="max-width: 900px;">
                <div class="card-body p-4">
                    <h5 class="fw-bold mb-4">Buat Artikel Baru</h5>
                    <form method="POST" enctype="multipart/form-data">
                        <div class="row">
                            <div class="col-md-8">
                                <div class="mb-3">
                                    <label class="small fw-bold">Judul Berita</label>
                                    <input type="text" name="judul" class="form-control" placeholder="Masukkan judul yang menarik..." required>
                                </div>
                                <div class="mb-3">
                                    <label class="small fw-bold">Isi Berita</label>
                                    <textarea name="isi" id="editor"></textarea>
                                </div>
                            </div>
                            <div class="col-md-4">
                                <div class="mb-3">
                                    <label class="small fw-bold">Kategori</label>
                                    <select name="kategori" class="form-select">
                                        <option>Info Kesehatan</option>
                                        <option>Kegiatan</option>
                                        <option>Pengumuman</option>
                                    </select>
                                </div>
                                <div class="mb-3">
                                    <label class="small fw-bold">Foto Utama</label>
                                    <input type="file" name="gambar" class="form-control" accept="image/*">
                                    <div class="form-text">Gunakan foto landscape untuk hasil terbaik.</div>
                                </div>
                                <hr>
                                <button type="submit" class="btn btn-primary w-100 py-3 fw-bold shadow">
                                    <i class="fa fa-paper-plane me-2"></i>Terbitkan
                                </button>
                                <a href="manage_berita.php" class="btn btn-link w-100 mt-2 text-decoration-none text-muted small">Batal</a>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <script>
        // Inisialisasi CKEditor
        ClassicEditor
            .create( document.querySelector( '#editor' ), {
                toolbar: [ 'heading', '|', 'bold', 'italic', 'link', 'bulletedList', 'numberedList', 'blockQuote', 'undo', 'redo' ]
            } )
            .catch( error => {
                console.error( error );
            } );
    </script>
</body>
</html>