<?php
use App\Core\Csrf;
use App\Core\Permission;
?>
<link rel="stylesheet" href="/public/assets/css/cda-communications.css">

<section class="communication-hero">
<div>
<span class="page-kicker">Aperçu avant envoi</span>
<h2><?= htmlspecialchars((string)$campaign['libelle']) ?></h2>
<p><?= htmlspecialchars((string)$campaign['statut']) ?> · <?= (int)$campaign['nb_destinataires'] ?> destinataire(s)</p>
</div>

<a class="button button-secondary" href="/public/communications">Retour</a>
</section>

<section class="communication-grid">
<article class="communication-card">
<h3>Message</h3>
<p><strong>Objet :</strong> <?= htmlspecialchars((string)$campaign['objet']) ?></p>
<div class="mail-preview">
<?= $campaign['corps_html'] ?>
</div>
</article>

<article class="communication-card">
<h3>Destinataires</h3>

<div class="recipient-list">
<?php foreach($recipients as $recipient): ?>
<div>
<span>
<strong><?= htmlspecialchars((string)$recipient['nom_destinataire']) ?></strong>
<small><?= htmlspecialchars((string)$recipient['email_destinataire']) ?></small>
</span>
<b class="status-<?= htmlspecialchars((string)$recipient['statut']) ?>">
<?= htmlspecialchars((string)$recipient['statut']) ?>
</b>
</div>
<?php endforeach; ?>
</div>
</article>
</section>

<section class="communication-card">
<div class="campaign-actions">
<?php if(
    $campaign['statut']==='a_valider'
    && Permission::can('communications.validate')
): ?>
<form method="post" action="/public/communications/valider">
<?= Csrf::field() ?>
<input type="hidden" name="id" value="<?= (int)$campaign['id'] ?>">
<button class="button button-secondary">Valider la campagne</button>
</form>
<?php endif; ?>

<?php if(
    $campaign['statut']==='validee'
    && Permission::can('communications.send.group')
): ?>
<form method="post" action="/public/communications/envoyer"
onsubmit="return confirm('Envoyer ce message à tous les destinataires affichés ?');">
<?= Csrf::field() ?>
<input type="hidden" name="id" value="<?= (int)$campaign['id'] ?>">
<button class="button button-primary">Envoyer maintenant</button>
</form>
<?php endif; ?>
</div>
</section>
