<?php
use App\Core\Csrf;
$h=static fn($v)=>htmlspecialchars((string)$v,ENT_QUOTES,'UTF-8');
?>
<link rel="stylesheet" href="/public/assets/css/axiom-meta.css">
<main class="meta-page">
  <div class="meta-breadcrumb"><a href="/public/administration/axiom">AXIOM Studio</a><span>›</span><strong>Meta Framework</strong></div>
  <div class="meta-toolbar"><a class="meta-back" href="/public/administration/axiom">← Retour à AXIOM Studio</a></div>
  <section class="meta-hero"><div><span>MODÉLISATION MÉTIER</span><h2>AXIOM Meta Framework</h2><p>Décrivez les entités et leurs champs avant toute génération SQL, MVC ou API.</p></div><div class="meta-version"><small>Engine</small><strong>1.0.0</strong></div></section>
  <?php if($flashSuccess): ?><div class="meta-alert success"><?= $h($flashSuccess) ?></div><?php endif; ?>
  <?php if($flashError): ?><div class="meta-alert error"><?= $h($flashError) ?></div><?php endif; ?>
  <section class="meta-kpis"><article><span>Entités actives</span><strong><?= count($entities) ?></strong></article><article><span>Types du dictionnaire</span><strong><?= count($dictionary) ?></strong></article><article><span>Champs du modèle sélectionné</span><strong><?= count($fields) ?></strong></article></section>
  <div class="meta-layout">
    <aside class="meta-panel"><div class="panel-head"><h3>Entités métier</h3></div><nav class="entity-list"><?php foreach($entities as $entity): ?><a class="<?= $selected && (int)$selected['id']===(int)$entity['id']?'active':'' ?>" href="/public/administration/axiom/meta?entity=<?= (int)$entity['id'] ?>"><span><?= $h($entity['icon'] ?: '◇') ?></span><div><strong><?= $h($entity['label']) ?></strong><small><?= $h($entity['module_code']) ?> · <?= $h($entity['code']) ?></small></div></a><?php endforeach; ?><?php if(!$entities): ?><p class="empty">Aucune entité définie.</p><?php endif; ?></nav></aside>
    <section class="meta-main">
      <?php if($canManage): ?><article class="meta-panel form-panel"><div class="panel-head"><h3>Créer ou mettre à jour une entité</h3></div><form method="post" action="/public/administration/axiom/meta/entity/save" class="meta-form"><?= Csrf::field() ?><label>Nom métier<input name="label" required placeholder="Ex. Professeur"></label><label>Code technique<input name="code" placeholder="professeur"></label><label>Module<input name="module_code" value="cda" required></label><label>Table cible<input name="table_name" placeholder="professeurs"></label><label>Icône<input name="icon" value="◇" maxlength="8"></label><label class="wide">Description<textarea name="description" rows="2"></textarea></label><div class="wide actions"><button type="submit">Enregistrer l'entité</button></div></form></article><?php endif; ?>
      <?php if($selected): ?><article class="meta-panel"><div class="panel-head"><div><span>MODÈLE</span><h3><?= $h($selected['label']) ?></h3><small><?= $h($selected['code']) ?> → <?= $h($selected['table_name']) ?></small></div><?php if($canManage): ?><form method="post" action="/public/administration/axiom/meta/entity/archive" onsubmit="return confirm('Archiver cette entité ?')"><?= Csrf::field() ?><input type="hidden" name="id" value="<?= (int)$selected['id'] ?>"><button class="danger" type="submit">Archiver</button></form><?php endif; ?></div>
      <div class="field-table"><div class="field-row header"><span>Champ</span><span>Type</span><span>Dictionnaire</span><span>Options</span></div><?php foreach($fields as $field): ?><div class="field-row"><span><strong><?= $h($field['label']) ?></strong><small><?= $h($field['code']) ?></small></span><span><code><?= $h($field['field_type']) ?></code></span><span><?= $h($field['dictionary_label'] ?: '—') ?></span><span class="badges"><?php if($field['required']): ?><b>Requis</b><?php endif; ?><?php if($field['searchable']): ?><b>Recherche</b><?php endif; ?><?php if($field['listable']): ?><b>Liste</b><?php endif; ?></span></div><?php endforeach; ?><?php if(!$fields): ?><p class="empty">Aucun champ. Ajoutez le premier champ ci-dessous.</p><?php endif; ?></div></article>
      <?php if($canManage): ?><article class="meta-panel form-panel"><div class="panel-head"><h3>Ajouter un champ</h3></div><form method="post" action="/public/administration/axiom/meta/field/save" class="meta-form"><?= Csrf::field() ?><input type="hidden" name="entity_id" value="<?= (int)$selected['id'] ?>"><label>Libellé<input name="label" required></label><label>Code<input name="code"></label><label>Type<select name="field_type"><?php foreach(['string','text','integer','decimal','money','boolean','date','datetime','email','phone','postal_code','city','relation','reference','file'] as $type): ?><option value="<?= $h($type) ?>"><?= $h($type) ?></option><?php endforeach; ?></select></label><label>Dictionnaire<select name="dictionary_code"><option value="">Aucun</option><?php foreach($dictionary as $item): ?><option value="<?= $h($item['code']) ?>"><?= $h($item['category'].' · '.$item['label']) ?></option><?php endforeach; ?></select></label><label>Position<input type="number" name="position" value="10" min="1"></label><label>Valeur par défaut<input name="default_value"></label><label class="check"><input type="checkbox" name="required" value="1"> Requis</label><label class="check"><input type="checkbox" name="searchable" value="1" checked> Recherchable</label><label class="check"><input type="checkbox" name="listable" value="1" checked> Visible en liste</label><label class="wide">Validation JSON<textarea name="validation_json" rows="2" placeholder='{"max":100}'></textarea></label><div class="wide actions"><button type="submit">Ajouter le champ</button></div></form></article><?php endif; ?>
      <?php else: ?><article class="meta-panel meta-welcome"><h3>Sélectionnez une entité</h3><p>Choisissez une entité dans la colonne de gauche ou créez-en une nouvelle.</p></article><?php endif; ?>
    </section>
  </div>
  <section class="meta-panel dictionary"><div class="panel-head"><div><span>DICTIONNAIRE AXIOM</span><h3>Types réutilisables</h3></div></div><div class="dictionary-grid"><?php foreach($dictionary as $item): ?><article><strong><?= $h($item['label']) ?></strong><code><?= $h($item['code']) ?></code><small><?= $h($item['sql_type']) ?> · <?= $h($item['category']) ?></small></article><?php endforeach; ?></div></section>
</main>
