<link rel="stylesheet" href="/public/assets/css/axiom-workflow.css">
<?php $csrf=\App\Core\Csrf::token(); $repo=new \App\Workflow\WorkflowRepository(); ?>
<main class="wf-page">
<header class="wf-hero"><div><a class="wf-back" href="/public/administration/axiom">← Retour à AXIOM Studio</a><span class="wf-kicker">AXIOM WORKFLOW ENGINE 1.1</span><h2>Processus, validations et tâches</h2><p>Moteur d’exécution isolé du Workflow Designer pour garantir la stabilité des instances.</p></div><div class="wf-badge"><strong><?=count($instances)?></strong><span>instances</span></div></header>
<?php if($flashSuccess):?><div class="wf-alert success"><?=htmlspecialchars((string)$flashSuccess)?></div><?php endif;?>
<?php if($flashError):?><div class="wf-alert error"><?=htmlspecialchars((string)$flashError)?></div><?php endif;?>
<section class="wf-grid">
<div class="wf-main">
<section class="wf-panel"><div class="wf-head"><div><span class="wf-kicker">CATALOGUE</span><h3>Workflows</h3></div></div><div class="wf-cards">
<?php foreach($workflows as $w):?><a class="wf-card" href="/public/administration/axiom/workflows?workflow=<?=(int)$w['id']?>"><strong><?=htmlspecialchars((string)$w['name'])?></strong><code><?=htmlspecialchars((string)$w['code'])?></code><small><?=(int)$w['state_count']?> états · <?=(int)$w['active_instances']?> instances actives</small></a><?php endforeach;?>
<?php if(!$workflows):?><p class="wf-empty">Aucun workflow défini.</p><?php endif;?></div></section>
<?php if($selectedWorkflow):?>
<section class="wf-panel"><div class="wf-head"><div><span class="wf-kicker">MODÈLE</span><h3><?=htmlspecialchars((string)$selectedWorkflow['name'])?></h3></div></div>
<div class="wf-flow"><?php foreach($states as $s):?><div class="wf-state <?=!empty($s['is_initial'])?'initial':''?> <?=!empty($s['is_final'])?'final':''?>"><strong><?=htmlspecialchars((string)$s['name'])?></strong><code><?=htmlspecialchars((string)$s['code'])?></code><small><?=!empty($s['is_initial'])?'Initial ':''?><?=!empty($s['is_final'])?'Final':''?></small></div><?php endforeach;?></div>
<div class="wf-table-wrap"><table><thead><tr><th>Transition</th><th>Depuis</th><th>Vers</th><th>Tâche créée</th></tr></thead><tbody><?php foreach($transitions as $t):?><tr><td><strong><?=htmlspecialchars((string)$t['name'])?></strong><small><?=htmlspecialchars((string)$t['code'])?></small></td><td><?=htmlspecialchars((string)$t['from_name'])?></td><td><?=htmlspecialchars((string)$t['to_name'])?></td><td><?=htmlspecialchars((string)($t['task_title']?:'—'))?></td></tr><?php endforeach;?></tbody></table></div>
</section><?php endif;?>
<section class="wf-panel"><div class="wf-head"><div><span class="wf-kicker">EXÉCUTION</span><h3>Instances en cours et terminées</h3></div></div>
<div class="wf-table-wrap"><table><thead><tr><th>Objet</th><th>Workflow</th><th>État</th><th>Action</th></tr></thead><tbody><?php foreach($instances as $i):?><tr><td><strong><?=htmlspecialchars((string)$i['object_label'])?></strong><small><?=htmlspecialchars((string)$i['object_reference'])?></small></td><td><?=htmlspecialchars((string)$i['workflow_name'])?></td><td><span class="wf-status"><?=htmlspecialchars((string)$i['state_name'])?></span></td><td><?php if($canExecute && !$i['completed_at']):$available=$repo->availableTransitions((int)$i['id']); if($available):?><form method="post" action="/public/administration/axiom/workflows/instance/transition" class="wf-inline"><input type="hidden" name="_csrf" value="<?=htmlspecialchars($csrf)?>"><input type="hidden" name="instance_id" value="<?=(int)$i['id']?>"><select name="transition_id" required><option value="">Transition…</option><?php foreach($available as $a):?><option value="<?=(int)$a['id']?>"><?=htmlspecialchars((string)$a['name'])?></option><?php endforeach;?></select><input name="note" placeholder="Note"><button>Appliquer</button></form><?php else:?>—<?php endif; else:?>Terminée<?php endif;?></td></tr><?php endforeach;?></tbody></table></div></section>
<section class="wf-panel"><div class="wf-head"><div><span class="wf-kicker">TÂCHES</span><h3>File de travail</h3></div></div><div class="wf-tasks"><?php foreach($tasks as $t):?><article><div><strong><?=htmlspecialchars((string)$t['title'])?></strong><small><?=htmlspecialchars((string)($t['object_label']?:'Tâche transverse'))?> · <?=htmlspecialchars((string)($t['workflow_name']?:'AXIOM'))?></small></div><span><?=htmlspecialchars((string)($t['assigned_role_code']?:'Non affectée'))?></span><?php if($canExecute):?><form method="post" action="/public/administration/axiom/workflows/task/complete"><input type="hidden" name="_csrf" value="<?=htmlspecialchars($csrf)?>"><input type="hidden" name="task_id" value="<?=(int)$t['id']?>"><button>Terminer</button></form><?php endif;?></article><?php endforeach;?><?php if(!$tasks):?><p class="wf-empty">Aucune tâche ouverte.</p><?php endif;?></div></section>
</div>
<aside class="wf-side">
<?php if($canManage):?><section class="wf-panel"><div class="wf-head"><div><span class="wf-kicker">CONCEPTION</span><h3>Nouveau workflow</h3></div></div><form class="wf-form" method="post" action="/public/administration/axiom/workflows/save"><input type="hidden" name="_csrf" value="<?=htmlspecialchars($csrf)?>"><label>Code *<input name="code" required placeholder="inscription-cda"></label><label>Nom *<input name="name" required></label><label>Description<textarea name="description" rows="3"></textarea></label><button>Enregistrer</button></form></section><?php endif;?>
<?php if($canManage && $selectedWorkflow):?><section class="wf-panel"><div class="wf-head"><div><span class="wf-kicker">ÉTAT</span><h3>Ajouter un état</h3></div></div><form class="wf-form" method="post" action="/public/administration/axiom/workflows/state/save"><input type="hidden" name="_csrf" value="<?=htmlspecialchars($csrf)?>"><input type="hidden" name="workflow_id" value="<?=(int)$selectedWorkflow['id']?>"><label>Code *<input name="code" required></label><label>Nom *<input name="name" required></label><label>Ordre<input type="number" name="sort_order" min="0" value="10"></label><label class="wf-check"><input type="checkbox" name="is_initial" value="1"> État initial</label><label class="wf-check"><input type="checkbox" name="is_final" value="1"> État final</label><button>Ajouter l’état</button></form></section>
<section class="wf-panel"><div class="wf-head"><div><span class="wf-kicker">TRANSITION</span><h3>Ajouter une transition</h3></div></div><form class="wf-form" method="post" action="/public/administration/axiom/workflows/transition/save"><input type="hidden" name="_csrf" value="<?=htmlspecialchars($csrf)?>"><input type="hidden" name="workflow_id" value="<?=(int)$selectedWorkflow['id']?>"><label>Code *<input name="code" required></label><label>Nom *<input name="name" required></label><label>Depuis *<select name="from_state_id" required><option value="">Choisir</option><?php foreach($states as $s):?><option value="<?=(int)$s['id']?>"><?=htmlspecialchars((string)$s['name'])?></option><?php endforeach;?></select></label><label>Vers *<select name="to_state_id" required><option value="">Choisir</option><?php foreach($states as $s):?><option value="<?=(int)$s['id']?>"><?=htmlspecialchars((string)$s['name'])?></option><?php endforeach;?></select></label><label>Permission requise<input name="permission_code" placeholder="factures.validate"></label><label>Tâche à créer<input name="task_title" placeholder="Vérifier le dossier"></label><label>Rôle affecté<input name="task_role_code" placeholder="direction"></label><label>Échéance (jours)<input type="number" name="task_due_days" min="0" value="0"></label><button>Ajouter la transition</button></form></section><?php endif;?>
<?php if($canExecute):?><section class="wf-panel"><div class="wf-head"><div><span class="wf-kicker">DÉMARRAGE</span><h3>Lancer un workflow</h3></div></div><form class="wf-form" method="post" action="/public/administration/axiom/workflows/instance/start"><input type="hidden" name="_csrf" value="<?=htmlspecialchars($csrf)?>"><label>Workflow *<select name="workflow_id" required><option value="">Choisir</option><?php foreach($workflows as $w):?><option value="<?=(int)$w['id']?>"><?=htmlspecialchars((string)$w['name'])?></option><?php endforeach;?></select></label><label>Objet AXIOM *<select name="object_uuid" required><option value="">Choisir</option><?php foreach($objects as $o):?><option value="<?=htmlspecialchars((string)$o['uuid'])?>"><?=htmlspecialchars((string)$o['reference'].' — '.$o['label'])?></option><?php endforeach;?></select></label><button>Démarrer</button></form></section><?php endif;?>
</aside></section></main>
