<% layout('layouts/main') %>
<% const adminBase = typeof adminBaseUrl !== 'undefined' ? adminBaseUrl : '/admin'; %>
<% const selectedProfile = employee.employee_profile || 'field'; %>
<div class="app-shell record-360-page">
  <%- partial('partials/admin-nav') %>
  <section class="content">
    <div class="page-head compact-page-head">
      <div>
        <p class="eyebrow">Fiche employé 360</p>
        <h1><%= employee.first_name %> <%= employee.last_name %></h1>
        <p class="muted"><%= employee.employee_profile_label %> · <%= employee.email %></p>
      </div>
      <div class="actions">
        <a class="button secondary" href="<%= adminBase %>/employes">Équipe</a>
        <a class="button secondary" href="<%= adminBase %>/rh#soldes-rh">Soldes RH</a>
      </div>
    </div>

    <section class="record-kpi-row">
      <article><span>Statut</span><strong><%= ui.label(employee.status) %></strong></article>
      <article><span>Accès</span><strong><%= employee.employee_profile_label %></strong></article>
      <article><span>Interventions</span><strong><%= monthStats.interventions_count || 0 %></strong></article>
      <article><span>Heures du mois</span><strong><%= ui.duration(monthStats.month_minutes) %></strong></article>
    </section>

    <div class="record-layout">
      <section class="panel compact-panel">
        <div class="inline-heading"><h2>Identité & accès</h2><span class="badge"><%= employee.is_responsible ? 'Responsable' : 'Standard' %></span></div>
        <div class="detail-grid compact">
          <div class="detail-item"><span>Email</span><strong><%= employee.email %></strong></div>
          <div class="detail-item"><span>Téléphone</span><strong><%= employee.phone || 'Non renseigné' %></strong></div>
          <div class="detail-item"><span>Coût horaire interne</span><strong><%= employee.internal_hourly_rate ? ui.money(employee.internal_hourly_rate) : 'Non renseigné' %></strong></div>
          <div class="detail-item"><span>Rôle portail</span><strong><%= employee.role === 'admin' ? 'Administrateur' : 'Employé' %></strong></div>
        </div>
        <details class="ops-drawer slim-drawer">
          <summary><span>Modifier la fiche</span><small>Identité, profil d’accès, coût interne et statut</small></summary>
          <form class="ops-form compact form-grid two" method="post" action="<%= adminBase %>/employes/<%= employee.id %>">
            <input type="hidden" name="_csrf" value="<%= csrfToken %>">
            <label>Prénom<input name="first_name" value="<%= employee.first_name %>" required></label>
            <label>Nom<input name="last_name" value="<%= employee.last_name %>" required></label>
            <label>Téléphone<input name="phone" value="<%= employee.phone || '' %>"></label>
            <label>Tarif interne horaire<input name="internal_hourly_rate" value="<%= employee.internal_hourly_rate || '' %>" inputmode="decimal"></label>
            <label>Statut<select name="status"><option value="active" <%= employee.status==='active'?'selected':'' %>>Actif</option><option value="blocked" <%= employee.status==='blocked'?'selected':'' %>>Bloqué</option></select></label>
            <% if (employee.role === 'employee') { %>
              <label>Profil d’accès<select name="employee_profile">
                <% profileOptions.forEach(profile => { %><option value="<%= profile.id %>" <%= selectedProfile === profile.id ? 'selected' : '' %>><%= profile.label %> - <%= profile.detail %></option><% }) %>
              </select></label>
              <label class="check premium-check"><input type="checkbox" name="is_responsible" <%= employee.is_responsible ? 'checked' : '' %>><span><strong>Employé responsable</strong><small>Accès opérationnel global selon profil.</small></span></label>
            <% } %>
            <button>Enregistrer</button>
          </form>
        </details>
      </section>

      <section class="panel compact-panel">
        <div class="inline-heading"><h2>Dossier RH</h2><a href="<%= adminBase %>/rh#soldes-rh">Ouvrir</a></div>
        <div class="empty-state slim success-state"><strong>Suivi centralisé</strong><span>Absences, vacances, certificats, accès et activité terrain restent regroupés dans cette fiche.</span></div>
        <div class="detail-grid compact">
          <div class="detail-item"><span>Profil d’accès</span><strong><%= employee.employee_profile_label %></strong></div>
          <div class="detail-item"><span>Responsabilité</span><strong><%= employee.is_responsible ? 'Responsable' : 'Standard' %></strong></div>
          <div class="detail-item"><span>Demandes RH</span><strong><%= absences.length %></strong></div>
          <div class="detail-item"><span>Portail</span><strong><%= employee.must_change_password ? 'À activer' : 'Activé' %></strong></div>
        </div>
      </section>
    </div>

    <div class="grid cards dashboard-panels">
      <section class="panel compact-panel">
        <div class="inline-heading"><h2>Interventions récentes</h2><a href="<%= adminBase %>/interventions?staff_id=<%= employee.id %>">Filtrer</a></div>
        <% interventions.forEach((item) => { %>
          <a class="result-row" href="<%= adminBase %>/interventions/<%= item.id %>"><strong><%= item.service_type %></strong><span><%= item.client_first_name %> <%= item.client_last_name %> · <%= ui.date(item.starts_at, true) %> · <%= ui.label(item.status) %></span></a>
        <% }) %>
        <% if (!interventions.length) { %><div class="empty-state slim"><strong>Aucune intervention</strong><span>Les affectations terrain de cet employé apparaîtront ici.</span><a class="button secondary small" href="<%= adminBase %>/interventions">Planifier</a></div><% } %>
      </section>

      <section class="panel compact-panel">
        <div class="inline-heading"><h2>Absences</h2><a href="<%= adminBase %>/rh#planning-rh">Planning RH</a></div>
        <% absences.forEach((absence) => { %>
          <div class="result-row"><strong><%= ui.label(absence.type) %></strong><span><%= ui.date(absence.starts_on) %> - <%= ui.date(absence.ends_on) %> · <%= ui.label(absence.status) %></span></div>
        <% }) %>
        <% if (!absences.length) { %><div class="empty-state slim"><strong>Aucune absence enregistrée</strong><span>Les vacances, maladies et certificats seront visibles ici.</span></div><% } %>
      </section>

      <section class="panel compact-panel">
        <div class="inline-heading"><h2>Timeline</h2><span class="badge"><%= activity.length %></span></div>
        <% activity.forEach((item) => { %>
          <div class="result-row"><strong><%= ui.label(item.action) %></strong><span><%= item.actor_first_name || 'Système' %> <%= item.actor_last_name || '' %> · <%= ui.date(item.created_at, true) %></span></div>
        <% }) %>
        <% if (!activity.length) { %><div class="empty-state slim"><strong>Aucune activité</strong><span>Les changements importants de la fiche seront listés ici.</span></div><% } %>
      </section>
    </div>
  </section>
</div>
