<?php
$packages = is_array($packageManagerPackages ?? null) ? $packageManagerPackages : [];
$selected = is_array($packageManagerSelected ?? null) ? $packageManagerSelected : null;
$metrics = is_array($packageManagerMetrics ?? null) ? $packageManagerMetrics : [];
$categories = is_array($packageManagerCategories ?? null) ? $packageManagerCategories : [];
$statuses = is_array($packageManagerStatuses ?? null) ? $packageManagerStatuses : [];
$recent = is_array($packageManagerRecentOperations ?? null) ? $packageManagerRecentOperations : [];
$filters = is_array($packageManagerFilters ?? null) ? $packageManagerFilters : [];
$h = static fn($v): string => htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8');
$statusLabel = static fn(string $s): string => match($s){'INSTALLED'=>'Installé','SYSTEM'=>'Système','AVAILABLE'=>'Disponible','BROKEN'=>'Erreur','REPAIR_REQUIRED'=>'À réparer','DISABLED'=>'Désactivé','UPDATE_AVAILABLE'=>'Mise à jour','REMOVED'=>'Supprimé',default=>$s};
$statusClass = static fn(string $s): string => match($s){'INSTALLED','SYSTEM'=>'ok','BROKEN','REPAIR_REQUIRED'=>'bad','UPDATE_AVAILABLE'=>'warn','DISABLED','REMOVED'=>'muted',default=>'info'};
$score = static fn($v): string => $v === null || $v === '' ? '—' : ((int)$v).'/100';
?>
<style>
.pmx{display:grid;gap:18px}.pmx *{box-sizing:border-box}.pmx-hero{border-radius:24px;padding:26px;background:linear-gradient(135deg,#10223c,#1c4f79 58%,#2671a8);color:#fff;display:flex;justify-content:space-between;gap:24px;align-items:center;box-shadow:0 18px 44px rgba(15,43,70,.18)}.pmx-hero h1{margin:5px 0 8px;font-size:clamp(28px,4vw,42px)}.pmx-hero p{margin:0;max-width:800px;color:#dbeafe}.pmx-actions,.pmx-nav{display:flex;gap:9px;flex-wrap:wrap}.pmx-btn{border:1px solid #cbd8e6;border-radius:11px;background:#fff;color:#1c4e77;padding:10px 14px;font-weight:800;text-decoration:none;cursor:pointer;display:inline-flex;align-items:center;justify-content:center}.pmx-btn.primary{background:#1f67a2;border-color:#1f67a2;color:#fff}.pmx-btn.ghost{background:rgba(255,255,255,.1);border-color:rgba(255,255,255,.35);color:#fff}.pmx-grid{display:grid;grid-template-columns:repeat(auto-fit,minmax(175px,1fr));gap:12px}.pmx-card{background:#fff;border:1px solid #dbe5ef;border-radius:18px;padding:18px;box-shadow:0 8px 26px rgba(23,50,77,.055)}.pmx-kpi small{color:#64748b}.pmx-kpi strong{display:block;font-size:30px;color:#173b5a;margin-top:4px}.pmx-toolbar{display:grid;grid-template-columns:minmax(220px,1.4fr) repeat(2,minmax(170px,.5fr)) auto;gap:10px;align-items:end}.pmx-toolbar label{font-size:12px;font-weight:850;color:#516579;display:block;margin-bottom:5px}.pmx-toolbar input,.pmx-toolbar select{width:100%;padding:11px;border:1px solid #ccd9e6;border-radius:10px;background:#fff}.pmx-packages{display:grid;grid-template-columns:repeat(auto-fill,minmax(280px,1fr));gap:14px}.pmx-package{position:relative;display:grid;gap:13px}.pmx-package h3{margin:0;color:#173b5a;font-size:17px}.pmx-package p{margin:0;color:#64748b;font-size:13px}.pmx-head{display:flex;justify-content:space-between;gap:12px}.pmx-icon{width:44px;height:44px;border-radius:13px;background:#e7f2fb;color:#1f67a2;display:grid;place-items:center;font-size:22px;font-weight:900}.pmx-badge{display:inline-flex;padding:6px 9px;border-radius:999px;font-size:11px;font-weight:900}.pmx-badge.ok{background:#dcfce7;color:#166534}.pmx-badge.bad{background:#fee2e2;color:#991b1b}.pmx-badge.warn{background:#fef3c7;color:#92400e}.pmx-badge.info{background:#e0f2fe;color:#075985}.pmx-badge.muted{background:#e5e7eb;color:#475569}.pmx-scores{display:grid;grid-template-columns:1fr 1fr;gap:9px}.pmx-score{padding:10px;border-radius:12px;background:#f6f9fc}.pmx-score span{display:block;font-size:11px;color:#64748b}.pmx-score strong{font-size:18px;color:#173b5a}.pmx-detail{display:grid;grid-template-columns:minmax(0,1.35fr) minmax(280px,.65fr);gap:14px}.pmx-tabs{display:flex;gap:7px;overflow:auto;padding-bottom:4px}.pmx-tab{white-space:nowrap;padding:9px 12px;border-radius:10px;background:#edf4fa;color:#315a7c;font-weight:800;font-size:12px}.pmx-meta{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:9px}.pmx-meta div{background:#f7f9fc;border-radius:11px;padding:11px}.pmx-meta span{display:block;color:#64748b;font-size:11px}.pmx-table-wrap{overflow:auto}.pmx-table{width:100%;border-collapse:collapse;min-width:700px}.pmx-table th,.pmx-table td{padding:10px;border-bottom:1px solid #e6edf4;text-align:left}.pmx-table th{font-size:11px;text-transform:uppercase;color:#64748b}.pmx-empty{text-align:center;padding:34px;color:#64748b}.pmx-section-title{display:flex;justify-content:space-between;gap:12px;align-items:center;margin-bottom:13px}.pmx-section-title h2{margin:0;color:#173b5a}.pmx-progress{height:8px;border-radius:99px;background:#e6edf4;overflow:hidden;margin-top:7px}.pmx-progress i{display:block;height:100%;background:#2370aa}@media(max-width:900px){.pmx-hero{align-items:flex-start;flex-direction:column}.pmx-toolbar,.pmx-detail{grid-template-columns:1fr}.pmx-meta{grid-template-columns:1fr}}@media(max-width:560px){.pmx-packages{grid-template-columns:1fr}.pmx-grid{grid-template-columns:repeat(2,1fr)}}
</style>
<div class="pmx">
  <section class="pmx-hero"><div><small>PLATFORM-023.3.1 · AXIOM 10.2.0</small><h1>Enterprise Package Manager</h1><p>Registre central, pilotage des versions, confiance, compatibilité, certification et historique des composants AXIOM.</p></div><div class="pmx-actions"><a class="pmx-btn ghost" href="/public/studio/updates">Centre de mises à jour</a><a class="pmx-btn ghost" href="/public/administration/plateforme/rapport-compatibilite">Rapports Enterprise</a><a class="pmx-btn ghost" href="/public/administration/plateforme/packages/dependances">Dépendances</a><a class="pmx-btn ghost" href="/public/administration/plateforme/packages/catalogue">Catalogue</a></div></section>
  <div class="pmx-grid">
    <?php foreach(['total'=>'Packages','installed'=>'Installés','system'=>'Système','updates'=>'Mises à jour','repair'=>'À réparer','trust_average'=>'Trust moyen','compatibility_average'=>'Compatibilité'] as $key=>$label): ?>
    <article class="pmx-card pmx-kpi"><small><?= $h($label) ?></small><strong><?= (int)($metrics[$key]??0) ?><?= str_contains($key,'average')?'/100':'' ?></strong></article><?php endforeach; ?>
  </div>
  <section class="pmx-card"><form method="get" class="pmx-toolbar"><div><label>Recherche instantanée</label><input name="q" value="<?= $h($filters['q']??'') ?>" placeholder="Nom, version, éditeur ou empreinte"></div><div><label>Catégorie</label><select name="category"><option value="">Toutes</option><?php foreach($categories as $c): ?><option <?= ($filters['category']??'')===$c?'selected':'' ?>><?= $h($c) ?></option><?php endforeach; ?></select></div><div><label>État</label><select name="status"><option value="">Tous</option><?php foreach($statuses as $s): ?><option <?= ($filters['status']??'')===$s?'selected':'' ?>><?= $h($s) ?></option><?php endforeach; ?></select></div><button class="pmx-btn primary">Filtrer</button></form></section>
  <section class="pmx-card"><div class="pmx-section-title"><div><h2>Catalogue installé</h2><small><?= count($packages) ?> package(s) affiché(s)</small></div><div class="pmx-actions"><form method="post" action="/public/administration/plateforme/packages/synchroniser"><input type="hidden" name="_csrf" value="<?= $h(\App\Core\Csrf::token()) ?>"><button class="pmx-btn" type="submit">Synchroniser</button></form><a class="pmx-btn" href="/public/administration/plateforme/packages/export.csv">Exporter CSV</a></div></div>
    <div class="pmx-packages"><?php foreach($packages as $p): ?><article class="pmx-card pmx-package"><div class="pmx-head"><div style="display:flex;gap:11px"><div class="pmx-icon">◈</div><div><span class="pmx-badge info"><?= $h($p['category']) ?></span><h3><?= $h($p['package_name']) ?></h3><p><?= $h($p['publisher']) ?> · <?= $h($p['version']) ?></p></div></div><span class="pmx-badge <?= $statusClass((string)$p['package_status']) ?>"><?= $h($statusLabel((string)$p['package_status'])) ?></span></div><div class="pmx-scores"><div class="pmx-score"><span>Trust</span><strong><?= $score($p['trust_score']) ?></strong><div class="pmx-progress"><i style="width:<?= (int)($p['trust_score']??0) ?>%"></i></div></div><div class="pmx-score"><span>Compatibilité</span><strong><?= $score($p['compatibility_score']) ?></strong><div class="pmx-progress"><i style="width:<?= (int)($p['compatibility_score']??0) ?>%"></i></div></div></div><div><span class="pmx-badge info"><?= $h($p['certification_status']) ?></span> <span class="pmx-badge muted"><?= $h($p['policy_decision']) ?></span></div><a class="pmx-btn primary" href="?package_id=<?= (int)$p['id'] ?>">Ouvrir la fiche</a></article><?php endforeach; ?><?php if(!$packages): ?><div class="pmx-empty">Aucun package ne correspond aux filtres.</div><?php endif; ?></div>
  </section>
  <?php if($selected): ?><section class="pmx-detail"><article class="pmx-card"><div class="pmx-section-title"><div><span class="pmx-badge <?= $statusClass((string)$selected['package_status']) ?>"><?= $h($statusLabel((string)$selected['package_status'])) ?></span><h2><?= $h($selected['package_name']) ?></h2><small><?= $h($selected['version']) ?> · <?= $h($selected['category']) ?></small></div><a class="pmx-btn" href="/public/administration/plateforme/packages">Fermer</a></div><div class="pmx-tabs"><?php foreach(['Vue générale','Validation','Trust','Compatibilité','Certification','Politique','Dépendances','Fichiers','Migrations','Historique','Journal','Performance'] as $tab): ?><span class="pmx-tab"><?= $h($tab) ?></span><?php endforeach; ?></div><div class="pmx-meta" style="margin-top:14px"><div><span>Clé registre</span><strong><?= $h($selected['package_key']) ?></strong></div><div><span>Éditeur</span><strong><?= $h($selected['publisher']) ?></strong></div><div><span>Certification</span><strong><?= $h($selected['certification_status']) ?></strong></div><div><span>Politique</span><strong><?= $h($selected['policy_decision']) ?></strong></div><div><span>Installé le</span><strong><?= $h($selected['installed_at']??'—') ?></strong></div><div><span>Taille</span><strong><?= number_format(((int)$selected['file_size'])/1024,1,',',' ') ?> Ko</strong></div></div><h3>Évaluations liées</h3><div class="pmx-grid"><?php foreach((array)($selected['tabs']??[]) as $key=>$report): ?><div class="pmx-score"><span><?= $h(ucfirst($key)) ?></span><strong><?= is_array($report)?$score($report['score']??null):'—' ?></strong><small><?= is_array($report)?$h($report['decision']??''):'Non évalué' ?></small></div><?php endforeach; ?></div></article><aside class="pmx-card"><h2>Chronologie</h2><div class="pmx-table-wrap"><table class="pmx-table" style="min-width:0"><tbody><?php foreach((array)($selected['operations']??[]) as $op): ?><tr><td><strong><?= $h($op['operation_type']) ?></strong><br><small><?= $h($op['created_at']) ?></small></td><td><span class="pmx-badge <?= ($op['status']??'')==='SUCCESS'?'ok':'warn' ?>"><?= $h($op['status']) ?></span></td></tr><?php endforeach; ?><?php if(empty($selected['operations'])):?><tr><td>Aucune opération historisée.</td></tr><?php endif; ?></tbody></table></div></aside></section><?php endif; ?>
  <section class="pmx-card"><div class="pmx-section-title"><h2>Activité récente</h2><small>Registre Enterprise · <?= $h($packageManagerGeneratedAt??'') ?></small></div><div class="pmx-table-wrap"><table class="pmx-table"><thead><tr><th>Date</th><th>Package</th><th>Opération</th><th>Version</th><th>État</th><th>Message</th></tr></thead><tbody><?php foreach($recent as $op): ?><tr><td><?= $h($op['created_at']) ?></td><td><?= $h($op['package_name']) ?></td><td><?= $h($op['operation_type']) ?></td><td><?= $h($op['target_version']??'') ?></td><td><span class="pmx-badge <?= ($op['status']??'')==='SUCCESS'?'ok':'warn' ?>"><?= $h($op['status']) ?></span></td><td><?= $h($op['message']??'') ?></td></tr><?php endforeach; ?><?php if(!$recent): ?><tr><td colspan="6">Le registre vient d’être initialisé. Les prochaines opérations apparaîtront ici.</td></tr><?php endif; ?></tbody></table></div></section>
</div>
