<?php
require_once __DIR__ . '/api/config.php';

$ADMIN_PASSWORD = 'AdminCEGIC2026.Com1';

if (isset($_POST['logout'])) {
    unset($_SESSION['cegic_admin']);
    header('Location: admin.php');
    exit;
}

if (isset($_POST['admin_password'])) {
    if ($_POST['admin_password'] === $ADMIN_PASSWORD) {
        $_SESSION['cegic_admin'] = true;
        header('Location: admin.php');
        exit;
    }

    $error = 'Contraseña incorrecta.';
}

if (empty($_SESSION['cegic_admin'])):
?>
<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <title>Administrador CEGIC</title>
    <style>
        body { margin: 0; font-family: Arial, sans-serif; background: #071522; color: white; display: grid; min-height: 100vh; place-items: center; }
        .box { width: min(420px, 92vw); background: #0d2235; border: 1px solid #1b9bd7; padding: 28px; border-radius: 12px; }
        h1 { margin-top: 0; color: #35b8ff; }
        input, button { width: 100%; padding: 14px; margin-top: 12px; border-radius: 8px; border: 0; font-size: 16px; }
        button { background: #1da1f2; color: white; font-weight: bold; cursor: pointer; }
        .error { color: #ff6b57; margin-top: 12px; }
    </style>
</head>
<body>
    <form class="box" method="post">
        <h1>Administrador CEGIC</h1>
        <p>Panel privado para administrar publicaciones.</p>
        <input type="password" name="admin_password" placeholder="Contraseña de administrador" required>
        <button type="submit">Entrar</button>
        <?php if (!empty($error)): ?>
            <div class="error"><?php echo htmlspecialchars($error); ?></div>
        <?php endif; ?>
    </form>
</body>
</html>
<?php
exit;
endif;

$posts = cegic_posts();

if (isset($_POST['delete_id'])) {
    $deleteId = $_POST['delete_id'];

    $posts = array_values(array_filter($posts, function($post) use ($deleteId) {
        return ($post['id'] ?? '') !== $deleteId;
    }));

    cegic_save_posts($posts);

    header('Location: admin.php');
    exit;
}

usort($posts, function($a, $b) {
    return strcmp($b['id'] ?? '', $a['id'] ?? '');
});
?>
<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <title>Panel Administrador CEGIC</title>
    <style>
        body { margin: 0; font-family: Arial, sans-serif; background: #071522; color: white; }
        header { background: #0d2235; border-bottom: 1px solid #1b9bd7; padding: 20px; display: flex; justify-content: space-between; align-items: center; }
        h1 { margin: 0; color: #35b8ff; }
        main { max-width: 1000px; margin: 30px auto; padding: 0 16px; }
        .post { background: #0d2235; border: 1px solid #1b9bd7; border-radius: 10px; padding: 18px; margin-bottom: 16px; }
        .meta { color: #9ed8ff; font-size: 14px; margin-bottom: 10px; }
        .title { font-size: 20px; font-weight: bold; margin-bottom: 8px; }
        img { max-width: 100%; border-radius: 8px; margin-top: 12px; }
        button { background: #e53935; color: white; border: 0; padding: 10px 14px; border-radius: 8px; cursor: pointer; font-weight: bold; }
        .logout { background: #1da1f2; }
        .empty { background: #0d2235; border: 1px dashed #1b9bd7; padding: 24px; border-radius: 10px; text-align: center; color: #9ed8ff; }
    </style>
</head>
<body>
    <header>
        <h1>Panel Administrador CEGIC</h1>
        <form method="post">
            <button class="logout" name="logout" value="1">Salir</button>
        </form>
    </header>

    <main>
        <?php if (empty($posts)): ?>
            <div class="empty">No hay publicaciones guardadas.</div>
        <?php endif; ?>

        <?php foreach ($posts as $post): ?>
            <div class="post">
                <div class="meta">
                    <?php echo htmlspecialchars($post['committee'] ?? 'Sin comité'); ?> |
                    <?php echo htmlspecialchars($post['date'] ?? ''); ?>
                </div>

                <div class="title"><?php echo htmlspecialchars($post['title'] ?? ''); ?></div>
                <div><?php echo nl2br(htmlspecialchars($post['text'] ?? '')); ?></div>

                <?php if (!empty($post['image'])): ?>
                    <img src="<?php echo htmlspecialchars($post['image']); ?>" alt="">
                <?php endif; ?>

                <form method="post" onsubmit="return confirm('¿Eliminar esta publicación?');" style="margin-top: 14px;">
                    <input type="hidden" name="delete_id" value="<?php echo htmlspecialchars($post['id'] ?? ''); ?>">
                    <button type="submit">Eliminar</button>
                </form>
            </div>
        <?php endforeach; ?>
    </main>
</body>
</html>