<link rel="stylesheet" href="/public/assets/css/axiom-objects.css">
<?php $csrf = \App\Core\Csrf::token(); ?>
<main class="aom-page">
    <header class="aom-hero">
        <div>
            <a class="aom-back" href="/public/administration/axiom/studio">← Retour à AXIOM Studio</a>
            <span class="aom-kicker">AXIOM OBJECT MODEL 1.0</span>
            <h2>Registre universel des objets métier</h2>
            <p>Créez, versionnez, recherchez et historisez les objets issus du Meta Framework.</p>
        </div>
        <div class="aom-badge"><strong><?= count($objects) ?></strong><span>objets affichés</span></div>
    </header>

    <?php if ($flashSuccess): ?><div class="aom-alert success"><?= htmlspecialchars((string)$flashSuccess) ?></div><?php endif; ?>
    <?php if ($flashError): ?><div class="aom-alert error"><?= htmlspecialchars((string)$flashError) ?></div><?php endif; ?>

    <section class="aom-layout">
        <div class="aom-main">
            <form class="aom-filter" method="get" action="/public/administration/axiom/objects">
                <label>Entité
                    <select name="entity">
                        <option value="0">Toutes les entités</option>
                        <?php foreach ($entities as $entity): ?>
                            <option value="<?= (int)$entity['id'] ?>" <?= $entityId === (int)$entity['id'] ? 'selected' : '' ?>><?= htmlspecialchars((string)$entity['label']) ?></option>
                        <?php endforeach; ?>
                    </select>
                </label>
                <label>Recherche
                    <input type="search" name="q" value="<?= htmlspecialchars($search) ?>" placeholder="Référence, libellé ou statut">
                </label>
                <button type="submit">Filtrer</button>
                <a href="/public/administration/axiom/objects">Réinitialiser</a>
            </form>

            <section class="aom-panel">
                <div class="aom-panel-head"><div><span class="aom-kicker">REGISTRE</span><h3>Objets actifs</h3></div></div>
                <?php if (!$objects): ?>
                    <p class="aom-empty">Aucun objet ne correspond aux critères.</p>
                <?php else: ?>
                    <div class="aom-table-wrap"><table class="aom-table">
                        <thead><tr><th>Référence</th><th>Objet</th><th>Entité</th><th>Statut</th><th>Version</th><th>Événements</th><th></th></tr></thead>
                        <tbody><?php foreach ($objects as $object): ?><tr>
                            <td><code><?= htmlspecialchars((string)$object['reference']) ?></code></td>
                            <td><strong><?= htmlspecialchars((string)$object['label']) ?></strong><small><?= htmlspecialchars((string)$object['uuid']) ?></small></td>
                            <td><?= htmlspecialchars((string)$object['entity_label']) ?></td>
                            <td><span class="aom-status"><?= htmlspecialchars((string)$object['status']) ?></span></td>
                            <td>v<?= (int)$object['version_no'] ?></td>
                            <td><?= (int)$object['event_count'] ?></td>
                            <td><a class="aom-open" href="/public/administration/axiom/objects?object=<?= urlencode((string)$object['uuid']) ?>">Ouvrir</a></td>
                        </tr><?php endforeach; ?></tbody>
                    </table></div>
                <?php endif; ?>
            </section>
        </div>

        <aside class="aom-side">
            <?php if ($canManage): ?>
            <section class="aom-panel">
                <div class="aom-panel-head"><div><span class="aom-kicker"><?= $selected ? 'MODIFICATION' : 'CRÉATION' ?></span><h3><?= $selected ? 'Modifier l’objet' : 'Nouvel objet' ?></h3></div></div>
                <form method="post" action="/public/administration/axiom/objects/save" class="aom-form">
                    <input type="hidden" name="_csrf" value="<?= htmlspecialchars($csrf) ?>">
                    <input type="hidden" name="uuid" value="<?= htmlspecialchars((string)($selected['uuid'] ?? '')) ?>">
                    <label>Entité métier *<select name="entity_id" required><option value="">Choisir</option><?php foreach ($entities as $entity): ?><option value="<?= (int)$entity['id'] ?>" <?= (int)($selected['entity_id'] ?? 0)===(int)$entity['id']?'selected':'' ?>><?= htmlspecialchars((string)$entity['label']) ?></option><?php endforeach; ?></select></label>
                    <label>Référence<input name="reference" maxlength="190" value="<?= htmlspecialchars((string)($selected['reference'] ?? '')) ?>" placeholder="Automatique si vide"></label>
                    <label>Libellé *<input name="label" maxlength="255" required value="<?= htmlspecialchars((string)($selected['label'] ?? '')) ?>"></label>
                    <label>Statut<input name="status" maxlength="80" value="<?= htmlspecialchars((string)($selected['status'] ?? 'draft')) ?>"></label>
                    <label>Données JSON<textarea name="payload_json" rows="8" spellcheck="false"><?= htmlspecialchars((string)($selected['payload_json'] ?? "{}")) ?></textarea></label>
                    <button type="submit"><?= $selected ? 'Enregistrer une nouvelle version' : 'Créer l’objet' ?></button>
                </form>
                <?php if ($selected): ?><form method="post" action="/public/administration/axiom/objects/archive" onsubmit="return confirm('Archiver cet objet ?');" class="aom-archive"><input type="hidden" name="_csrf" value="<?= htmlspecialchars($csrf) ?>"><input type="hidden" name="uuid" value="<?= htmlspecialchars((string)$selected['uuid']) ?>"><button type="submit">Archiver</button></form><?php endif; ?>
            </section>
            <?php endif; ?>

            <?php if ($selected): ?>
            <section class="aom-panel">
                <div class="aom-panel-head"><div><span class="aom-kicker">TIMELINE</span><h3>Historique de l’objet</h3></div></div>
                <div class="aom-timeline">
                    <?php foreach ($events as $event): ?><article><span></span><div><strong><?= htmlspecialchars((string)$event['label']) ?></strong><small><?= htmlspecialchars((string)$event['event_type']) ?> · <?= htmlspecialchars((string)$event['occurred_at']) ?></small></div></article><?php endforeach; ?>
                    <?php if (!$events): ?><p class="aom-empty">Aucun événement.</p><?php endif; ?>
                </div>
            </section>
            <?php endif; ?>
        </aside>
    </section>
</main>
