<?php use App\Core\Csrf; $esc=static fn($v)=>htmlspecialchars((string)$v,ENT_QUOTES,'UTF-8'); ?>
<link rel="stylesheet" href="/public/assets/css/axiom-documents.css">
<div class="ax-doc">
  <div class="toolbar"><a class="btn" href="/public/administration/axiom">← Retour à AXIOM Studio</a></div>
  <h1>AXIOM Document Engine</h1><div class="sub">GED transverse · versions · objets liés · modèles · signatures</div>
  <?php if($flashSuccess):?><div class="notice success"><?=$esc($flashSuccess)?></div><?php endif;?><?php if($flashError):?><div class="notice error"><?=$esc($flashError)?></div><?php endif;?>
  <div class="stats"><div class="stat"><strong><?=$dashboard['documents']?></strong>Documents actifs</div><div class="stat"><strong><?=$dashboard['versions']?></strong>Versions</div><div class="stat"><strong><?=$dashboard['signatures']?></strong>Signatures</div><div class="stat"><strong><?=$dashboard['templates']?></strong>Modèles</div></div>
  <div class="layout">
   <main class="panel">
    <form class="filters" method="get" action="/public/administration/axiom/documents"><label>Recherche<input name="q" value="<?=$esc($query)?>" placeholder="Titre, référence, fichier…"></label><label>Catégorie<select name="category"><option value="0">Toutes</option><?php foreach($categories as $c):?><option value="<?=$c['id']?>" <?=$categoryFilter===$c['id']?'selected':''?>><?=$esc($c['name'])?></option><?php endforeach;?></select></label><button class="btn primary">Rechercher</button></form>
    <table><thead><tr><th>Document</th><th>Catégorie</th><th>Version</th><th>Liens</th><th>Mise à jour</th></tr></thead><tbody><?php foreach($documents as $d):?><tr><td><a class="doc-title" href="?document=<?=$d['id']?>"><?=$esc($d['title'])?></a><div class="muted"><?=$esc($d['reference'])?> · <?=$esc($d['original_name']??'')?></div></td><td><span class="badge"><?=$esc($d['category_name']??'Sans catégorie')?></span></td><td>v<?=$esc($d['version_no']??'-')?> / <?=$esc($d['version_count'])?></td><td><?=$esc($d['link_count'])?></td><td><?=$esc($d['updated_at'])?></td></tr><?php endforeach;?></tbody></table>
    <div class="mobile-cards"><?php foreach($documents as $d):?><div class="mobile-card"><a class="doc-title" href="?document=<?=$d['id']?>"><?=$esc($d['title'])?></a><div class="muted"><?=$esc($d['reference'])?></div><p><span class="badge"><?=$esc($d['category_name']??'Sans catégorie')?></span> · v<?=$esc($d['version_no']??'-')?> · <?=$esc($d['link_count'])?> lien(s)</p></div><?php endforeach;?></div>
    <?php if(!$documents):?><p class="muted">Aucun document trouvé.</p><?php endif;?>
   </main>
   <aside class="panel stack">
    <?php if($canManage):?><details open><summary>Ajouter un document</summary><form method="post" action="/public/administration/axiom/documents/upload" enctype="multipart/form-data" class="stack"><input type="hidden" name="_csrf" value="<?=$esc(Csrf::token())?>"><label>Titre<input name="title" required></label><label>Catégorie<select name="category_id"><option value="">Sans catégorie</option><?php foreach($categories as $c):?><option value="<?=$c['id']?>"><?=$esc($c['name'])?></option><?php endforeach;?></select></label><label>Objet lié<select name="object_uuid"><option value="">Aucun</option><?php foreach($objects as $o):?><option value="<?=$esc($o['uuid'])?>"><?=$esc($o['reference'].' — '.$o['label'])?></option><?php endforeach;?></select></label><label>Description<textarea name="description"></textarea></label><label class="file-drop">Fichier (25 Mo max.)<input type="file" name="document_file" required></label><button class="btn primary">Téléverser</button></form></details><?php endif;?>
    <?php if($selectedDocument):?><h2><?=$esc($selectedDocument['title'])?></h2><div class="muted"><?=$esc($selectedDocument['reference'])?> · <?=$esc($selectedDocument['category_name']??'Sans catégorie')?></div>
      <h3 class="section-title">Versions</h3><?php foreach($versions as $v):?><div><strong>v<?=$v['version_no']?></strong> — <?=$esc($v['original_name'])?><br><span class="muted"><?=number_format(((int)$v['size_bytes'])/1024,1,',',' ')?> Ko · <?=$esc($v['created_at'])?></span><br><a href="/public/administration/axiom/documents/download?version=<?=$v['id']?>">Télécharger</a></div><?php endforeach;?>
      <?php if($canManage):?><details><summary>Ajouter une version</summary><form class="stack" method="post" enctype="multipart/form-data" action="/public/administration/axiom/documents/version/add"><input type="hidden" name="_csrf" value="<?=$esc(Csrf::token())?>"><input type="hidden" name="document_id" value="<?=$selectedDocument['id']?>"><label>Fichier<input type="file" name="document_file" required></label><label>Notes<textarea name="notes"></textarea></label><button class="btn primary">Ajouter la version</button></form></details>
      <details><summary>Rattacher à un objet</summary><form class="stack" method="post" action="/public/administration/axiom/documents/link"><input type="hidden" name="_csrf" value="<?=$esc(Csrf::token())?>"><input type="hidden" name="document_id" value="<?=$selectedDocument['id']?>"><label>Objet<select name="object_uuid" required><?php foreach($objects as $o):?><option value="<?=$esc($o['uuid'])?>"><?=$esc($o['reference'].' — '.$o['label'])?></option><?php endforeach;?></select></label><label>Rôle<input name="link_role" value="attachment"></label><button class="btn">Rattacher</button></form></details><?php endif;?>
      <h3 class="section-title">Objets liés</h3><?php foreach($links as $l):?><div><?=$esc($l['object_reference'].' — '.$l['object_label'])?> <span class="badge"><?=$esc($l['link_role'])?></span></div><?php endforeach;?><?php if(!$links):?><div class="muted">Aucun objet lié.</div><?php endif;?>
      <h3 class="section-title">Signatures</h3><?php foreach($signatures as $s):?><div><?=$esc($s['signer_name'])?> · <span class="badge"><?=$esc($s['status'])?></span><?php if($canSign&&$s['status']==='requested'):?><form method="post" action="/public/administration/axiom/documents/signature/mark" style="display:inline"><input type="hidden" name="_csrf" value="<?=$esc(Csrf::token())?>"><input type="hidden" name="document_id" value="<?=$selectedDocument['id']?>"><input type="hidden" name="signature_id" value="<?=$s['id']?>"><button class="btn">Valider</button></form><?php endif;?></div><?php endforeach;?>
      <?php if($canSign):?><details><summary>Nouvelle demande de signature</summary><form class="stack" method="post" action="/public/administration/axiom/documents/signature/request"><input type="hidden" name="_csrf" value="<?=$esc(Csrf::token())?>"><input type="hidden" name="document_id" value="<?=$selectedDocument['id']?>"><label>Signataire<input name="signer_name" required></label><label>E-mail<input type="email" name="signer_email"></label><label>Type<select name="signature_type"><option value="approval">Validation</option><option value="handwritten">Manuscrite</option><option value="electronic">Électronique</option></select></label><label>Expiration (jours)<input type="number" name="expires_days" value="30" min="1"></label><button class="btn primary">Créer la demande</button></form></details><?php endif;?>
      <?php if($canManage):?><form method="post" action="/public/administration/axiom/documents/archive" onsubmit="return confirm('Archiver ce document ?')"><input type="hidden" name="_csrf" value="<?=$esc(Csrf::token())?>"><input type="hidden" name="document_id" value="<?=$selectedDocument['id']?>"><button class="btn danger">Archiver</button></form><?php endif;?>
    <?php endif;?>
   </aside>
  </div>
  <?php if($canManage):?><div class="layout"><section class="panel"><h2>Catégories</h2><form class="row" method="post" action="/public/administration/axiom/documents/category/save"><input type="hidden" name="_csrf" value="<?=$esc(Csrf::token())?>"><label>Code<input name="code" required></label><label>Nom<input name="name" required></label><label>Ordre<input type="number" name="sort_order" value="0"></label><button class="btn primary">Enregistrer</button></form></section><section class="panel"><h2>Modèles</h2><form class="stack" method="post" action="/public/administration/axiom/documents/template/save"><input type="hidden" name="_csrf" value="<?=$esc(Csrf::token())?>"><label>Code<input name="code" required></label><label>Nom<input name="name" required></label><label>Format<select name="output_format"><option>pdf</option><option>html</option><option>docx</option></select></label><label>Contenu HTML<textarea name="body_html" required placeholder="Bonjour {{prenom}}…"></textarea></label><button class="btn primary">Enregistrer le modèle</button></form><div class="muted"><?=count($templates)?> modèle(s) actif(s)</div></section></div><?php endif;?>
</div>
