<?php
$user = $user ?? null;
if (!isset($_SESSION["user"])) {
    header("Location: index.php?pfad=login");
    exit();
}
include_once 'php/controller/showArticle-controller.php';

// Aktuelle Blockliste ermitteln:
$rawContent = '[]';
if (isset($_SESSION['old_content']) && !empty($_SESSION['old_content'])) {
    $rawContent = $_SESSION['old_content'];
    unset($_SESSION['old_content']);
} elseif (isset($content) && !empty($content)) {
    $rawContent = $content;
}

$blocks = json_decode($rawContent, true);
if (!is_array($blocks)) {
    $blocks = [];
}
$blocks = array_values($blocks);
?>
<!--
    Seite: Beitrag bearbeiten
    Inhalt: Formular für die Bearbeitung eines Beitrags
-->
<form method="post" action="php/controller/updateArticle-controller.php?id=<?php if(isset($id) && !empty($id)){echo htmlspecialchars($id);}else{$_SESSION["message"] = "missing_id";} ?>" id="editor-form" enctype="multipart/form-data" class="article-editor-scope.editor-container article-editor-scope editor-container">

    <main class="editor-main">
        <?php include_once "includes/alertMessages.php"?>

        <input type="text" id="title" name="title"
               value="<?php
               if (isset($_SESSION['old_title']) && !empty($_SESSION['old_title'])){
                   echo htmlspecialchars($_SESSION['old_title']);
                   unset($_SESSION['old_title']);
               }elseif (isset($title) && !empty($title)){
                   echo htmlspecialchars($title);
               }

               ?>"
               placeholder="Titel hier eingeben" required>

        <!--
            Content-Blöcke: werden serverseitig als echte, benannte Formularfelder gerendert
            (blocks[i][type]...). Dadurch funktioniert das Hinzufügen/Entfernen von Blöcken und
            der Bild-Upload auch ganz ohne JavaScript über einen normalen Formular-Submit.
            Ist JavaScript aktiv, fängt js/editor.js diese Submits ab und erledigt dieselbe
            Änderung lokal im DOM, ohne den Server zu belasten.
        -->
        <div id="block-container">
            <?php foreach ($blocks as $i => $block): ?>
                <?php
                $blockType = $block['type'] ?? '';
                if ($blockType !== 'text' && $blockType !== 'image') {
                    continue; // unbekannter/kaputter Block wird übersprungen
                }
                ?>
                <div class="editor-block article-editor-scope" data-index="<?php echo (int)$i; ?>">
                    <input type="hidden" name="blocks[<?php echo (int)$i; ?>][type]" value="<?php echo htmlspecialchars($blockType); ?>">

                    <?php if ($blockType === 'text'): ?>
                        <textarea name="blocks[<?php echo (int)$i; ?>][text]"
                                  placeholder="Schreibe deinen Textblock..."><?php echo htmlspecialchars($block['value'] ?? ''); ?></textarea>
                    <?php else: /* image */ ?>
                        <?php if (!empty($block['value'])): ?>
                            <img src="<?php echo htmlspecialchars($block['value']); ?>"
                                 class="block-image-preview"
                                 style="max-width:200px;display:block;margin-top:10px;">
                            <input type="hidden" name="blocks[<?php echo (int)$i; ?>][existing_image]" value="<?php echo htmlspecialchars($block['value']); ?>">
                        <?php endif; ?>
                        <input type="file" name="blocks[<?php echo (int)$i; ?>][image]" accept="image/*">
                    <?php endif; ?>

                    <button type="submit" name="editor_action" value="delete_block:<?php echo (int)$i; ?>" class="delete-block-btn">✕</button>
                </div>
            <?php endforeach; ?>
        </div>

        <div id="add-block-control" class="article-editor-scope add-block-control">
            <button type="button" id="plus-button" class="article-editor-scope plus-button">+</button>
            <div id="block-popup" class="article-editor-scope block-popup">
                <button type="submit" name="editor_action" value="add_text" data-type="text">Textblock</button>
                <button type="submit" name="editor_action" value="add_image" data-type="image">Bild einfügen</button>
            </div>
        </div>
    </main>

    <!-- Seitenleiste -->
    <aside class="editor-sidebar">

        <div class="sidebar-block">
            <button type="submit" id="editor-button" class="button">Änderungen speichern</button>
        </div>

        <div class="sidebar-block">
            <label for="category">Kategorie <span class="required">*</span></label>
            <select id="category" name="category" required>
                <option disabled <?php
                if ((!isset($_SESSION['old_category']) || empty($_SESSION['old_category'])) && (!isset($category) || empty($category))) {
                    echo 'selected';
                }
                ?>>Kategorie wählen...</option>

                <optgroup label="Sprachen">
                    <option value="deutsch" <?php
                    if (isset($_SESSION['old_category']) && !empty($_SESSION['old_category'])) {
                        if ($_SESSION['old_category'] === 'deutsch') { echo 'selected'; }
                    } elseif (isset($category) && !empty($category)) {
                        if ($category === 'deutsch') { echo 'selected'; }
                    }
                    ?>>Deutsch</option>
                    <option value="englisch" <?php
                    if (isset($_SESSION['old_category']) && !empty($_SESSION['old_category'])) {
                        if ($_SESSION['old_category'] === 'englisch') { echo 'selected'; }
                    } elseif (isset($category) && !empty($category)) {
                        if ($category === 'englisch') { echo 'selected'; }
                    }
                    ?>>Englisch</option>
                    <option value="franzoesisch" <?php
                    if (isset($_SESSION['old_category']) && !empty($_SESSION['old_category'])) {
                        if ($_SESSION['old_category'] === 'franzoesisch') { echo 'selected'; }
                    } elseif (isset($category) && !empty($category)) {
                        if ($category === 'franzoesisch') { echo 'selected'; }
                    }
                    ?>>Französisch</option>
                    <option value="latein" <?php
                    if (isset($_SESSION['old_category']) && !empty($_SESSION['old_category'])) {
                        if ($_SESSION['old_category'] === 'latein') { echo 'selected'; }
                    } elseif (isset($category) && !empty($category)) {
                        if ($category === 'latein') { echo 'selected'; }
                    }
                    ?>>Latein</option>
                    <option value="literatur" <?php
                    if (isset($_SESSION['old_category']) && !empty($_SESSION['old_category'])) {
                        if ($_SESSION['old_category'] === 'literatur') { echo 'selected'; }
                    } elseif (isset($category) && !empty($category)) {
                        if ($category === 'literatur') { echo 'selected'; }
                    }
                    ?>>Literatur</option>
                </optgroup>

                <optgroup label="MINT">
                    <option value="mathe" <?php
                    if (isset($_SESSION['old_category']) && !empty($_SESSION['old_category'])) {
                        if ($_SESSION['old_category'] === 'mathe') { echo 'selected'; }
                    } elseif (isset($category) && !empty($category)) {
                        if ($category === 'mathe') { echo 'selected'; }
                    }
                    ?>>Mathematik</option>
                    <option value="biologie" <?php
                    if (isset($_SESSION['old_category']) && !empty($_SESSION['old_category'])) {
                        if ($_SESSION['old_category'] === 'biologie') { echo 'selected'; }
                    } elseif (isset($category) && !empty($category)) {
                        if ($category === 'biologie') { echo 'selected'; }
                    }
                    ?>>Biologie</option>
                    <option value="chemie" <?php
                    if (isset($_SESSION['old_category']) && !empty($_SESSION['old_category'])) {
                        if ($_SESSION['old_category'] === 'chemie') { echo 'selected'; }
                    } elseif (isset($category) && !empty($category)) {
                        if ($category === 'chemie') { echo 'selected'; }
                    }
                    ?>>Chemie</option>
                    <option value="physik" <?php
                    if (isset($_SESSION['old_category']) && !empty($_SESSION['old_category'])) {
                        if ($_SESSION['old_category'] === 'physik') { echo 'selected'; }
                    } elseif (isset($category) && !empty($category)) {
                        if ($category === 'physik') { echo 'selected'; }
                    }
                    ?>>Physik</option>
                    <option value="informatik" <?php
                    if (isset($_SESSION['old_category']) && !empty($_SESSION['old_category'])) {
                        if ($_SESSION['old_category'] === 'informatik') { echo 'selected'; }
                    } elseif (isset($category) && !empty($category)) {
                        if ($category === 'informatik') { echo 'selected'; }
                    }
                    ?>>Informatik</option>
                    <option value="astronomie" <?php
                    if (isset($_SESSION['old_category']) && !empty($_SESSION['old_category'])) {
                        if ($_SESSION['old_category'] === 'astronomie') { echo 'selected'; }
                    } elseif (isset($category) && !empty($category)) {
                        if ($category === 'astronomie') { echo 'selected'; }
                    }
                    ?>>Astronomie</option>
                </optgroup>

                <optgroup label="Gesellschaft & Werte">
                    <option value="geschichte" <?php
                    if (isset($_SESSION['old_category']) && !empty($_SESSION['old_category'])) {
                        if ($_SESSION['old_category'] === 'geschichte') { echo 'selected'; }
                    } elseif (isset($category) && !empty($category)) {
                        if ($category === 'geschichte') { echo 'selected'; }
                    }
                    ?>>Geschichte</option>
                    <option value="erdkunde" <?php
                    if (isset($_SESSION['old_category']) && !empty($_SESSION['old_category'])) {
                        if ($_SESSION['old_category'] === 'erdkunde') { echo 'selected'; }
                    } elseif (isset($category) && !empty($category)) {
                        if ($category === 'erdkunde') { echo 'selected'; }
                    }
                    ?>>Erdkunde</option>
                    <option value="sozialkunde" <?php
                    if (isset($_SESSION['old_category']) && !empty($_SESSION['old_category'])) {
                        if ($_SESSION['old_category'] === 'sozialkunde') { echo 'selected'; }
                    } elseif (isset($category) && !empty($category)) {
                        if ($category === 'sozialkunde') { echo 'selected'; }
                    }
                    ?>>Sozialkunde</option>
                    <option value="wirtschaft" <?php
                    if (isset($_SESSION['old_category']) && !empty($_SESSION['old_category'])) {
                        if ($_SESSION['old_category'] === 'wirtschaft') { echo 'selected'; }
                    } elseif (isset($category) && !empty($category)) {
                        if ($category === 'wirtschaft') { echo 'selected'; }
                    }
                    ?>>Wirtschaftskunde</option>
                    <option value="religion" <?php
                    if (isset($_SESSION['old_category']) && !empty($_SESSION['old_category'])) {
                        if ($_SESSION['old_category'] === 'religion') { echo 'selected'; }
                    } elseif (isset($category) && !empty($category)) {
                        if ($category === 'religion') { echo 'selected'; }
                    }
                    ?>>Religion</option>
                    <option value="ethik" <?php
                    if (isset($_SESSION['old_category']) && !empty($_SESSION['old_category'])) {
                        if ($_SESSION['old_category'] === 'ethik') { echo 'selected'; }
                    } elseif (isset($category) && !empty($category)) {
                        if ($category === 'ethik') { echo 'selected'; }
                    }
                    ?>>Ethikunterricht</option>
                    <option value="philosophie" <?php
                    if (isset($_SESSION['old_category']) && !empty($_SESSION['old_category'])) {
                        if ($_SESSION['old_category'] === 'philosophie') { echo 'selected'; }
                    } elseif (isset($category) && !empty($category)) {
                        if ($category === 'philosophie') { echo 'selected'; }
                    }
                    ?>>Philosophie</option>
                    <option value="psychologie" <?php
                    if (isset($_SESSION['old_category']) && !empty($_SESSION['old_category'])) {
                        if ($_SESSION['old_category'] === 'psychologie') { echo 'selected'; }
                    } elseif (isset($category) && !empty($category)) {
                        if ($category === 'psychologie') { echo 'selected'; }
                    }
                    ?>>Psychologie</option>
                    <option value="kunst" <?php
                    if (isset($_SESSION['old_category']) && !empty($_SESSION['old_category'])) {
                        if ($_SESSION['old_category'] === 'kunst') { echo 'selected'; }
                    } elseif (isset($category) && !empty($category)) {
                        if ($category === 'kunst') { echo 'selected'; }
                    }
                    ?>>Kunst</option>
                    <option value="musik" <?php
                    if (isset($_SESSION['old_category']) && !empty($_SESSION['old_category'])) {
                        if ($_SESSION['old_category'] === 'musik') { echo 'selected'; }
                    } elseif (isset($category) && !empty($category)) {
                        if ($category === 'musik') { echo 'selected'; }
                    }
                    ?>>Musik</option>
                    <option value="theater" <?php
                    if (isset($_SESSION['old_category']) && !empty($_SESSION['old_category'])) {
                        if ($_SESSION['old_category'] === 'theater') { echo 'selected'; }
                    } elseif (isset($category) && !empty($category)) {
                        if ($category === 'theater') { echo 'selected'; }
                    }
                    ?>>Theater</option>
                </optgroup>

                <optgroup label="Technik & Praxis">
                    <option value="technik" <?php
                    if (isset($_SESSION['old_category']) && !empty($_SESSION['old_category'])) {
                        if ($_SESSION['old_category'] === 'technik') { echo 'selected'; }
                    } elseif (isset($category) && !empty($category)) {
                        if ($category === 'technik') { echo 'selected'; }
                    }
                    ?>>Technik</option>
                    <option value="werken" <?php
                    if (isset($_SESSION['old_category']) && !empty($_SESSION['old_category'])) {
                        if ($_SESSION['old_category'] === 'werken') { echo 'selected'; }
                    } elseif (isset($category) && !empty($category)) {
                        if ($category === 'werken') { echo 'selected'; }
                    }
                    ?>>Werken</option>
                    <option value="hauswirtschaft" <?php
                    if (isset($_SESSION['old_category']) && !empty($_SESSION['old_category'])) {
                        if ($_SESSION['old_category'] === 'hauswirtschaft') { echo 'selected'; }
                    } elseif (isset($category) && !empty($category)) {
                        if ($category === 'hauswirtschaft') { echo 'selected'; }
                    }
                    ?>>Hauswirtschaft</option>
                    <option value="sport" <?php
                    if (isset($_SESSION['old_category']) && !empty($_SESSION['old_category'])) {
                        if ($_SESSION['old_category'] === 'sport') { echo 'selected'; }
                    } elseif (isset($category) && !empty($category)) {
                        if ($category === 'sport') { echo 'selected'; }
                    }
                    ?>>Sport</option>
                </optgroup>

                <?php
                if (isset($_SESSION['old_category'])) {
                    unset($_SESSION['old_category']);
                }
                ?>
            </select>
        </div>

        <div class="sidebar-block">
            <label for="tags">Schlagwörter</label>
            <input type="text" id="tags" name="tags"
                   value="<?php
                   if (isset($_SESSION['old_tags']) && !empty($_SESSION['old_tags'])){
                       echo htmlspecialchars($_SESSION['old_tags']);
                       unset($_SESSION['old_tags']);
                   } elseif (isset($tags) && !empty($tags)){
                       echo htmlspecialchars($tags);
                   }
                   ?>"
                   placeholder="z.B. Technik, IT (mit Komma trennen)">
        </div>

    </aside>

</form>