<?php $h=fn($v)=>htmlspecialchars((string)$v,ENT_QUOTES,'UTF-8'); ?>
<main class="axiom-studio-page api-builder"><header class="studio-hero"><div><span class="axiom-studio-kicker">AXIOM STUDIO 9.4.8</span><h1>API Builder</h1><p>Publiez des entités AXIOM sous forme d’API REST sécurisées.</p></div><a class="studio-secondary" href="/public/administration/axiom">Retour au Studio</a></header>
<section class="studio-panel"><form method="get" action="/public/administration/axiom/api-builder" class="report-toolbar"><label>Module<select name="module_id" onchange="this.form.submit()"><?php foreach($modules as $m):?><option value="<?= (int)$m['id']?>" <?= $moduleId===(int)$m['id']?'selected':''?>><?= $h($m['name'])?></option><?php endforeach;?></select></label><label>API<select name="definition_id" onchange="this.form.submit()"><option value="0">Nouvelle API</option><?php foreach($definitions as $d):?><option value="<?= (int)$d['id']?>" <?= (int)($definition['id']??0)===(int)$d['id']?'selected':''?>><?= $h($d['name'])?> · <?= $h($d['status'])?></option><?php endforeach;?></select></label></form></section>
<?php if($plainToken):?><section class="studio-panel"><h2>Clé créée</h2><p>Copiez-la maintenant. Elle ne sera plus affichée.</p><code class="api-token"><?= $h($plainToken)?></code></section><?php endif;?>
<div class="report-grid"><section class="studio-panel"><h2><?= $definition?'Modifier l’API':'Nouvelle API'?></h2><form method="post" action="/public/administration/axiom/api-builder/enregistrer" class="report-form"><?= \App\Core\Csrf::field()?><input type="hidden" name="module_id" value="<?= $moduleId?>"><input type="hidden" name="definition_id" value="<?= (int)($definition['id']??0)?>"><label>Nom<input name="name" required value="<?= $h($definition['name']??'')?>"></label><label>Code<input name="code" value="<?= $h($definition['code']??'')?>" placeholder="ex. adherents"></label><label>Description<textarea name="description"><?= $h($definition['description']??'')?></textarea></label><label>Entité<select name="entity_id" required><?php foreach($entities as $e):?><option value="<?= (int)$e['id']?>" <?= (int)($definition['entity_id']??0)===(int)$e['id']?'selected':''?>><?= $h($e['name'])?> · <?= $h($e['table_name'])?></option><?php endforeach;?></select></label><fieldset><legend>Méthodes autorisées</legend><label><input type="checkbox" name="allow_get" <?= !isset($definition)||!empty($definition['allow_get'])?'checked':''?>> GET</label><label><input type="checkbox" name="allow_post" <?= !empty($definition['allow_post'])?'checked':''?>> POST</label><label><input type="checkbox" name="allow_put" <?= !empty($definition['allow_put'])?'checked':''?>> PUT</label><label><input type="checkbox" name="allow_delete" <?= !empty($definition['allow_delete'])?'checked':''?>> DELETE</label></fieldset><label>Limite par minute<input type="number" min="1" max="10000" name="rate_limit_minute" value="<?= (int)($definition['rate_limit_minute']??100)?>"></label><button class="studio-primary wide">Enregistrer</button></form></section>
<section class="studio-panel"><h2>Publication et documentation</h2><?php if($definition):?><p><strong>Endpoint :</strong> <code>/public/api/axiom?api=<?= $h($definition['code'])?></code></p><p><strong>Statut :</strong> <?= $h($definition['status'])?></p><div class="report-actions"><form method="post" action="/public/administration/axiom/api-builder/publier"><?= \App\Core\Csrf::field()?><input type="hidden" name="module_id" value="<?= $moduleId?>"><input type="hidden" name="definition_id" value="<?= (int)$definition['id']?>"><button class="studio-primary">Publier</button></form><a class="studio-secondary" target="_blank" href="/public/administration/axiom/api-builder/openapi?definition_id=<?= (int)$definition['id']?>">OpenAPI JSON</a></div><pre class="api-openapi"><?= $h(json_encode($openApi,JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES))?></pre><?php else:?><div class="dashboard-empty">Enregistrez une API pour générer sa documentation.</div><?php endif;?></section></div>
<?php if($definition):?><div class="report-grid"><section class="studio-panel"><h2>Clés API</h2><form method="post" action="/public/administration/axiom/api-builder/cles" class="report-form"><?= \App\Core\Csrf::field()?><input type="hidden" name="module_id" value="<?= $moduleId?>"><input type="hidden" name="definition_id" value="<?= (int)$definition['id']?>"><label>Libellé<input name="label" required></label><label>Expiration<input type="datetime-local" name="expires_at"></label><button class="studio-primary">Créer une clé</button></form><div class="report-table-wrap"><table><thead><tr><th>Clé</th><th>Expiration</th><th>Dernier usage</th><th></th></tr></thead><tbody><?php foreach($keys as $k):?><tr><td><?= $h($k['label'])?> · <code><?= $h($k['token_prefix'])?>…</code></td><td><?= $h($k['expires_at']??'—')?></td><td><?= $h($k['last_used_at']??'—')?></td><td><?php if(empty($k['revoked_at'])):?><form method="post" action="/public/administration/axiom/api-builder/cles/revoquer"><?= \App\Core\Csrf::field()?><input type="hidden" name="module_id" value="<?= $moduleId?>"><input type="hidden" name="definition_id" value="<?= (int)$definition['id']?>"><input type="hidden" name="key_id" value="<?= (int)$k['id']?>"><button class="studio-secondary">Révoquer</button></form><?php else:?>Révoquée<?php endif;?></td></tr><?php endforeach;?></tbody></table></div></section><section class="studio-panel"><h2>Journal des appels</h2><div class="report-table-wrap"><table><thead><tr><th>Date</th><th>Méthode</th><th>Statut</th><th>Durée</th><th>IP</th></tr></thead><tbody><?php foreach($logs as $l):?><tr><td><?= $h($l['created_at'])?></td><td><?= $h($l['http_method'])?></td><td><?= (int)$l['status_code']?></td><td><?= (int)$l['duration_ms']?> ms</td><td><?= $h($l['ip_address'])?></td></tr><?php endforeach;?></tbody></table></div></section></div><?php endif;?></main>
