<?php $h=fn($v)=>htmlspecialchars((string)$v,ENT_QUOTES,'UTF-8'); ?>
<main class="axiom-studio-page rule-builder">
<header class="studio-hero"><div><span class="axiom-studio-kicker">AXIOM STUDIO 9.4.9</span><h1>Rule Builder</h1><p>Créez, simulez et publiez des règles métier sans écrire de code.</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/rule-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>Règle<select name="rule_id" onchange="this.form.submit()"><option value="0">Nouvelle règle</option><?php foreach($rules as $r):?><option value="<?= (int)$r['id']?>" <?= (int)($rule['id']??0)===(int)$r['id']?'selected':''?>><?= $h($r['name'])?> · <?= $h($r['status'])?></option><?php endforeach;?></select></label></form></section>
<div class="rule-grid"><section class="studio-panel"><h2><?= $rule?'Paramètres de la règle':'Nouvelle règle'?></h2><form class="report-form" method="post" action="/public/administration/axiom/rule-builder/enregistrer"><?= \App\Core\Csrf::field()?><input type="hidden" name="module_id" value="<?= $moduleId?>"><input type="hidden" name="rule_id" value="<?= (int)($rule['id']??0)?>"><label>Nom<input name="name" required value="<?= $h($rule['name']??'')?>"></label><label>Code<input name="code" value="<?= $h($rule['code']??'')?>"></label><label>Description<textarea name="description"><?= $h($rule['description']??'')?></textarea></label><label>Entité<select name="entity_id" required><?php foreach($entities as $e):?><option value="<?= (int)$e['id']?>" <?= (int)($rule['entity_id']??0)===(int)$e['id']?'selected':''?>><?= $h($e['name'])?></option><?php endforeach;?></select></label><div class="report-two"><label>Déclencheur<select name="trigger_event"><?php foreach($triggers as $k=>$label):?><option value="<?= $h($k)?>" <?= ($rule['trigger_event']??'manual')===$k?'selected':''?>><?= $h($label)?></option><?php endforeach;?></select></label><label>Logique<select name="condition_logic"><option value="AND" <?= ($rule['condition_logic']??'AND')==='AND'?'selected':''?>>Toutes les conditions (ET)</option><option value="OR" <?= ($rule['condition_logic']??'')==='OR'?'selected':''?>>Au moins une condition (OU)</option></select></label><label>Priorité<input type="number" min="0" max="9999" name="priority" value="<?= (int)($rule['priority']??100)?>"></label><label class="check-line"><input type="checkbox" name="stop_on_match" <?= !empty($rule['stop_on_match'])?'checked':''?>> Arrêter les règles suivantes si correspondance</label></div><button class="studio-primary wide">Enregistrer</button></form></section>
<section class="studio-panel"><h2>Lecture métier</h2><?php if($rule):?><div class="rule-sentence"><strong>SI</strong> les conditions sont satisfaites selon <b><?= $h($rule['condition_logic'])?></b><br><strong>ALORS</strong> exécuter <?= count($actions)?> action(s).</div><p><b>Déclencheur :</b> <?= $h($triggers[$rule['trigger_event']]??$rule['trigger_event'])?></p><p><b>Statut :</b> <?= $h($rule['status'])?> · version <?= (int)$rule['current_version']?></p><form method="post" action="/public/administration/axiom/rule-builder/publier"><?= \App\Core\Csrf::field()?><input type="hidden" name="module_id" value="<?= $moduleId?>"><input type="hidden" name="rule_id" value="<?= (int)$rule['id']?>"><button class="studio-primary">Publier la règle</button></form><?php else:?><div class="dashboard-empty">Enregistrez d’abord une règle.</div><?php endif;?></section></div>
<?php if($rule):?><div class="rule-grid"><section class="studio-panel"><h2>Conditions</h2><form class="report-form" method="post" action="/public/administration/axiom/rule-builder/conditions"><?= \App\Core\Csrf::field()?><input type="hidden" name="module_id" value="<?= $moduleId?>"><input type="hidden" name="rule_id" value="<?= (int)$rule['id']?>"><div class="report-two"><label>Champ<select name="field_id" required><?php foreach($fields as $f):?><option value="<?= (int)$f['id']?>"><?= $h($f['name'])?> · <?= $h($f['column_name'])?></option><?php endforeach;?></select></label><label>Opérateur<select name="operator"><?php foreach($operators as $k=>$label):?><option value="<?= $h($k)?>"><?= $h($label)?></option><?php endforeach;?></select></label><label>Valeur 1<input name="value_one"></label><label>Valeur 2<input name="value_two" placeholder="Pour Entre"></label><label>Groupe<input type="number" min="1" name="group_number" value="1"></label><label>Connecteur<select name="connector"><option>AND</option><option>OR</option></select></label></div><button class="studio-primary">Ajouter la condition</button></form><div class="rule-list"><?php foreach($conditions as $c):?><article><div><b>Groupe <?= (int)$c['group_number']?> · <?= $h($c['connector'])?></b><br><?= $h($c['field_name'])?> <code><?= $h($operators[$c['operator']]??$c['operator'])?></code> <?= $h($c['value_one'])?> <?= $c['value_two']!==null&&$c['value_two']!==''?' / '.$h($c['value_two']):''?></div><form method="post" action="/public/administration/axiom/rule-builder/archiver"><?= \App\Core\Csrf::field()?><input type="hidden" name="module_id" value="<?= $moduleId?>"><input type="hidden" name="rule_id" value="<?= (int)$rule['id']?>"><input type="hidden" name="object_type" value="condition"><input type="hidden" name="object_id" value="<?= (int)$c['id']?>"><button class="studio-secondary">Retirer</button></form></article><?php endforeach;?></div></section>
<section class="studio-panel"><h2>Actions</h2><form class="report-form" method="post" action="/public/administration/axiom/rule-builder/actions"><?= \App\Core\Csrf::field()?><input type="hidden" name="module_id" value="<?= $moduleId?>"><input type="hidden" name="rule_id" value="<?= (int)$rule['id']?>"><label>Type<select name="action_type"><?php foreach($actionTypes as $k=>$label):?><option value="<?= $h($k)?>"><?= $h($label)?></option><?php endforeach;?></select></label><label>Libellé<input name="label"></label><div class="report-two"><label>Champ cible<input name="target_field"></label><label>Valeur<input name="action_value"></label><label>Destinataire<input name="recipient"></label><label>URL API<input name="url"></label></div><label>Message<textarea name="message"></textarea></label><label class="check-line"><input type="checkbox" name="continue_on_error"> Continuer si cette action échoue</label><button class="studio-primary">Ajouter l’action</button></form><div class="rule-list"><?php foreach($actions as $a):?><article><div><b><?= $h($a['label'])?></b><br><code><?= $h($actionTypes[$a['action_type']]??$a['action_type'])?></code></div><form method="post" action="/public/administration/axiom/rule-builder/archiver"><?= \App\Core\Csrf::field()?><input type="hidden" name="module_id" value="<?= $moduleId?>"><input type="hidden" name="rule_id" value="<?= (int)$rule['id']?>"><input type="hidden" name="object_type" value="action"><input type="hidden" name="object_id" value="<?= (int)$a['id']?>"><button class="studio-secondary">Retirer</button></form></article><?php endforeach;?></div></section></div>
<div class="rule-grid"><section class="studio-panel"><h2>Simulateur</h2><p>Collez un objet JSON utilisant les noms de colonnes de l’entité. La simulation est sans effet externe et est journalisée.</p><form class="report-form" method="post" action="/public/administration/axiom/rule-builder/simuler"><?= \App\Core\Csrf::field()?><input type="hidden" name="module_id" value="<?= $moduleId?>"><input type="hidden" name="rule_id" value="<?= (int)$rule['id']?>"><label>Données de test<textarea class="rule-json" name="record_json">{
  "id": 1
}</textarea></label><button class="studio-primary">Simuler la règle</button></form><?php if($simulation):?><pre class="api-openapi"><?= $h(json_encode($simulation,JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES))?></pre><?php endif;?></section><section class="studio-panel"><h2>Journal d’exécution</h2><div class="report-table-wrap"><table><thead><tr><th>Date</th><th>Résultat</th><th>Durée</th><th>Clé</th></tr></thead><tbody><?php foreach($logs as $l):?><tr><td><?= $h($l['created_at'])?></td><td><?= !empty($l['matched'])?'Correspond':'Ne correspond pas'?> · <?= $h($l['status'])?></td><td><?= (int)$l['duration_ms']?> ms</td><td><?= $h($l['record_key']??'—')?></td></tr><?php endforeach;?></tbody></table></div></section></div><?php endif;?></main>
