<?php $h=fn($v)=>htmlspecialchars((string)$v,ENT_QUOTES,'UTF-8'); ?>
<main class="axiom-studio-page automation-designer">
<header class="studio-hero"><div><span class="axiom-studio-kicker">AXIOM STUDIO 9.5.0</span><h1>Automation Engine</h1><p>Orchestrez règles, workflows, notifications, API, documents et tâches dans une file d’exécution supervisée.</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/automation" 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>Automatisation<select name="automation_id" onchange="this.form.submit()"><option value="0">Nouvelle automatisation</option><?php foreach($automations as $a):?><option value="<?= (int)$a['id']?>" <?= (int)($automation['id']??0)===(int)$a['id']?'selected':''?>><?= $h($a['name'])?> · <?= $h($a['status'])?></option><?php endforeach;?></select></label></form></section>
<div class="rule-grid"><section class="studio-panel"><h2><?= $automation?'Paramètres':'Nouvelle automatisation'?></h2><form class="report-form" method="post" action="/public/administration/axiom/automation/enregistrer"><?= \App\Core\Csrf::field()?><input type="hidden" name="module_id" value="<?= $moduleId?>"><input type="hidden" name="automation_id" value="<?= (int)($automation['id']??0)?>"><label>Nom<input required name="name" value="<?= $h($automation['name']??'')?>"></label><label>Code<input name="code" value="<?= $h($automation['code']??'')?>"></label><label>Description<textarea name="description"><?= $h($automation['description']??'')?></textarea></label><div class="report-two"><label>Déclencheur<select name="trigger_type"><?php foreach($triggerTypes as $k=>$v):?><option value="<?= $h($k)?>" <?= ($automation['trigger_type']??'manual')===$k?'selected':''?>><?= $h($v)?></option><?php endforeach;?></select></label><label>Planification<input name="schedule_expression" placeholder="Ex. 0 8 * * 1" value="<?= $h($automation['schedule_expression']??'')?>"></label><label>Priorité<input type="number" min="1" max="999" name="priority" value="<?= (int)($automation['priority']??100)?>"></label><label>Relances<input type="number" min="0" max="10" name="max_retries" value="<?= (int)($automation['max_retries']??3)?>"></label></div><button class="studio-primary wide">Enregistrer</button></form></section>
<section class="studio-panel"><h2>Publication & exécution</h2><?php if($automation):?><p><b>Statut :</b> <?= $h($automation['status'])?> · version <?= (int)$automation['current_version']?></p><p><b>Étapes :</b> <?= count($steps)?> · <b>Déclencheur :</b> <?= $h($triggerTypes[$automation['trigger_type']]??$automation['trigger_type'])?></p><form method="post" action="/public/administration/axiom/automation/publier"><?= \App\Core\Csrf::field()?><input type="hidden" name="module_id" value="<?= $moduleId?>"><input type="hidden" name="automation_id" value="<?= (int)$automation['id']?>"><button class="studio-primary">Publier</button></form><hr><form class="report-form" method="post" action="/public/administration/axiom/automation/file"><?= \App\Core\Csrf::field()?><input type="hidden" name="module_id" value="<?= $moduleId?>"><input type="hidden" name="automation_id" value="<?= (int)$automation['id']?>"><label>Données JSON<textarea class="rule-json" name="payload_json">{"id":1,"nom":"Test"}</textarea></label><button class="studio-primary">Ajouter à la file</button></form><?php else:?><div class="dashboard-empty">Enregistrez une automatisation.</div><?php endif;?></section></div>
<?php if($automation):?><div class="rule-grid"><section class="studio-panel"><h2>Étapes</h2><form class="report-form" method="post" action="/public/administration/axiom/automation/etapes"><?= \App\Core\Csrf::field()?><input type="hidden" name="module_id" value="<?= $moduleId?>"><input type="hidden" name="automation_id" value="<?= (int)$automation['id']?>"><label>Type<select name="step_type"><?php foreach($stepTypes as $k=>$v):?><option value="<?= $h($k)?>"><?= $h($v)?></option><?php endforeach;?></select></label><label>Libellé<input name="label"></label><div class="report-two"><label>Référence ID<input type="number" name="reference_id"></label><label>Destinataire<input name="recipient"></label><label>Objet<input name="subject"></label><label>URL<input name="url"></label><label>Délai (secondes)<input type="number" min="0" name="delay_seconds"></label><label>Position<input type="number" min="0" name="position"></label></div><label>Message<textarea name="message" placeholder="Variables : {{nom}}, {{id}}..."></textarea></label><label class="check-line"><input type="checkbox" name="continue_on_error"> Continuer en cas d’erreur</label><button class="studio-primary">Ajouter l’étape</button></form><div class="rule-list"><?php foreach($steps as $s):?><article><div><b><?= $h($s['label'])?></b><br><code><?= $h($stepTypes[$s['step_type']]??$s['step_type'])?></code> · position <?= (int)$s['position']?></div><form method="post" action="/public/administration/axiom/automation/etapes/archiver"><?= \App\Core\Csrf::field()?><input type="hidden" name="module_id" value="<?= $moduleId?>"><input type="hidden" name="automation_id" value="<?= (int)$automation['id']?>"><input type="hidden" name="step_id" value="<?= (int)$s['id']?>"><button class="studio-secondary">Retirer</button></form></article><?php endforeach;?></div></section>
<section class="studio-panel"><h2>File d’attente</h2><div class="report-table-wrap"><table><thead><tr><th>#</th><th>Automatisation</th><th>Statut</th><th>Tentatives</th><th>Action</th></tr></thead><tbody><?php foreach($queue as $q):?><tr><td><?= (int)$q['id']?></td><td><?= $h($q['automation_name']??'—')?></td><td><?= $h($q['status'])?></td><td><?= (int)$q['attempts']?> / <?= (int)$q['max_attempts']?></td><td><?php if(in_array($q['status'],['pending','failed'],true)):?><form method="post" action="/public/administration/axiom/automation/executer"><?= \App\Core\Csrf::field()?><input type="hidden" name="module_id" value="<?= $moduleId?>"><input type="hidden" name="automation_id" value="<?= (int)$automation['id']?>"><input type="hidden" name="job_id" value="<?= (int)$q['id']?>"><button class="studio-secondary">Exécuter</button></form><?php endif;?></td></tr><?php endforeach;?></tbody></table></div></section></div>
<div class="rule-grid"><section class="studio-panel"><h2>Historique</h2><div class="report-table-wrap"><table><thead><tr><th>Date</th><th>Statut</th><th>Durée</th><th>Erreur</th></tr></thead><tbody><?php foreach($runs as $r):?><tr><td><?= $h($r['started_at'])?></td><td><?= $h($r['status'])?></td><td><?= (int)$r['duration_ms']?> ms</td><td><?= $h($r['error_message']??'—')?></td></tr><?php endforeach;?></tbody></table></div></section><section class="studio-panel"><h2>Notifications récentes</h2><div class="rule-list"><?php foreach($notifications as $n):?><article><div><b><?= $h($n['title'])?></b><br><?= $h($n['message'])?></div><small><?= $h($n['created_at'])?></small></article><?php endforeach;?></div></section></div><?php endif;?></main>
