<?php
$editing=is_array($account);
?>
<style>
.cda-account-form{padding:28px;max-width:1100px}
.cda-account-form *{box-sizing:border-box}
.cda-account-form__head{display:flex;justify-content:space-between;align-items:flex-start;gap:20px;margin-bottom:22px}
.cda-account-form__eyebrow{font-size:.75rem;font-weight:800;letter-spacing:.14em;color:#718096;text-transform:uppercase;margin-bottom:8px}
.cda-account-form h1{font-size:2rem;line-height:1.15;margin:0 0 8px;color:#102348}
.cda-account-form__subtitle{margin:0;color:#65738a}
.cda-form-card{background:#fff;border:1px solid #e1e7f0;border-radius:18px;padding:24px;box-shadow:0 10px 30px rgba(28,46,78,.06)}
.cda-form-grid{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:18px}
.cda-field{display:flex;flex-direction:column;gap:7px}
.cda-field--wide{grid-column:1/-1}
.cda-field span,.cda-roles legend{font-weight:800;color:#263753}
.cda-field input{width:100%;padding:13px 14px;border:1px solid #d9e1ec;border-radius:11px;font:inherit;outline:none}
.cda-field input:focus{border-color:#5b6ff5;box-shadow:0 0 0 4px rgba(91,111,245,.11)}
.cda-roles{grid-column:1/-1;border:1px solid #e0e6ef;border-radius:15px;padding:17px}
.cda-role-grid{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:12px;margin-top:8px}
.cda-role-card{display:flex;align-items:center;gap:11px;border:1px solid #e2e8f1;border-radius:12px;padding:13px;cursor:pointer;background:#fff}
.cda-role-card:hover{background:#f8faff;border-color:#bdc8ef}
.cda-role-card input{width:18px;height:18px}
.cda-role-card strong{display:block;color:#102348}
.cda-role-card small{display:block;color:#7a879b;margin-top:2px}
.cda-form-actions{grid-column:1/-1;display:flex;gap:10px;justify-content:flex-end;padding-top:6px}
.cda-btn{display:inline-flex;align-items:center;justify-content:center;gap:8px;border:0;border-radius:11px;padding:12px 17px;font-weight:800;text-decoration:none;cursor:pointer}
.cda-btn--primary{background:linear-gradient(135deg,#3767ef,#7c4df6);color:#fff;box-shadow:0 10px 24px rgba(79,89,235,.22)}
.cda-btn--light{background:#eef2f8;color:#34445f}
@media(max-width:760px){
 .cda-account-form{padding:16px}
 .cda-account-form__head{flex-direction:column}
 .cda-form-grid,.cda-role-grid{grid-template-columns:1fr}
 .cda-form-actions{flex-direction:column-reverse}
 .cda-btn{width:100%}
}
</style>

<div class="cda-account-form">
    <div class="cda-account-form__head">
        <div>
            <div class="cda-account-form__eyebrow">Administration</div>
            <h1><?= $editing ? 'Modifier le compte' : 'Créer un compte' ?></h1>
            <p class="cda-account-form__subtitle">
                <?= $editing
                    ? 'Mettez à jour les informations et les rôles de cet utilisateur.'
                    : 'Créez un accès sécurisé et attribuez les rôles adaptés.' ?>
            </p>
        </div>
        <a class="cda-btn cda-btn--light" href="/public/administration/comptes">
            ← Retour à la liste
        </a>
    </div>

    <form class="cda-form-card cda-form-grid"
          method="post"
          action="<?= $editing ? '/public/administration/comptes/modifier' : '/public/administration/comptes' ?>">
        <input type="hidden" name="_csrf" value="<?= htmlspecialchars($_SESSION['_csrf'] ?? '') ?>">
        <?php if($editing): ?>
            <input type="hidden" name="id" value="<?= (int)$account['id'] ?>">
        <?php endif; ?>

        <label class="cda-field">
            <span>Nom</span>
            <input name="nom" required autocomplete="family-name"
                   value="<?= htmlspecialchars($account['nom'] ?? '') ?>">
        </label>

        <label class="cda-field">
            <span>Prénom</span>
            <input name="prenom" required autocomplete="given-name"
                   value="<?= htmlspecialchars($account['prenom'] ?? '') ?>">
        </label>

        <label class="cda-field cda-field--wide">
            <span>Adresse e-mail</span>
            <input type="email" name="email" required autocomplete="email"
                   value="<?= htmlspecialchars($account['email'] ?? '') ?>">
        </label>

        <label class="cda-field cda-field--wide">
            <span><?= $editing ? 'Nouveau mot de passe' : 'Mot de passe initial' ?></span>
            <input type="password" name="mot_de_passe"
                   <?= $editing ? '' : 'required' ?>
                   minlength="10"
                   autocomplete="new-password"
                   placeholder="<?= $editing ? 'Laisser vide pour conserver le mot de passe actuel' : '10 caractères minimum' ?>">
        </label>

        <fieldset class="cda-roles">
            <legend>Rôles et niveau d’accès</legend>
            <div class="cda-role-grid">
                <?php foreach($roles as $role): ?>
                    <label class="cda-role-card">
                        <input type="checkbox"
                               name="roles[]"
                               value="<?= (int)$role['id'] ?>"
                               <?= in_array((int)$role['id'],$selectedRoles,true) ? 'checked' : '' ?>>
                        <span>
                            <strong><?= htmlspecialchars($role['libelle']) ?></strong>
                            <small><?= htmlspecialchars($role['code']) ?></small>
                        </span>
                    </label>
                <?php endforeach; ?>
            </div>
        </fieldset>

        <fieldset class="cda-roles">
            <legend>Autorisations personnelles de communication</legend>
            <p style="margin:4px 0 14px;color:#6b7890">Ces droits sont attribués à cette personne, indépendamment de son rôle. Aucun mailing collectif n’est accordé automatiquement aux administrateurs du conseil d’administration.</p>
            <div class="cda-role-grid">
                <?php foreach($communicationPermissions as $permission): ?>
                    <label class="cda-role-card">
                        <input type="checkbox" name="communication_permissions[]"
                               value="<?= (int)$permission['id'] ?>"
                               <?= in_array((int)$permission['id'],$selectedCommunicationPermissions,true) ? 'checked' : '' ?>>
                        <span><strong><?= htmlspecialchars($permission['libelle']) ?></strong><small><?= htmlspecialchars($permission['code']) ?></small></span>
                    </label>
                <?php endforeach; ?>
            </div>
        </fieldset>

        <div class="cda-form-actions">
            <a class="cda-btn cda-btn--light" href="/public/administration/comptes">
                Annuler
            </a>
            <button class="cda-btn cda-btn--primary" type="submit">
                <?= $editing ? 'Enregistrer les modifications' : 'Créer le compte' ?>
            </button>
        </div>
    </form>
</div>
