<?php /** @var array $incident */ /** @var array $events */ /** @var array $summary */ ?>
<!DOCTYPE html>
<html lang="fr">
<head>
  <meta charset="utf-8">
  <title>Métriques Intervention - <?= htmlspecialchars($incident['ticket_id'] ?? ('INC-'.$incident['id'])) ?></title>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
  <style>
    body { font-size: 14px; }
    h1 { font-size: 1.35rem; }
    .table td, .table th { vertical-align: top; }
    .badge { font-size: .75rem; }
    .timeline-item { border-left: 3px solid #dee2e6; padding-left: 15px; margin-bottom: 15px; }
    .timeline-item.start { border-color: #198754; }
    .timeline-item.pause { border-color: #ffc107; }
    .timeline-item.resume { border-color: #0d6efd; }
  </style>
</head>
<body class="p-4">
  <h1 class="mb-3">Métriques d'intervention
    <small class="text-muted">— <?= htmlspecialchars($incident['ticket_id'] ?? ('INC-'.$incident['id'])) ?></small>
  </h1>
  
  <div class="mb-4">
    <table class="table table-sm">
      <tbody>
        <tr>
          <th style="width:180px">Client</th>
          <td><?= htmlspecialchars($incident['client_name'] ?? '') ?></td>
        </tr>
        <tr>
          <th>Localisation</th>
          <td><?= htmlspecialchars($incident['location_name'] ?? '') ?></td>
        </tr>
        <tr>
          <th>Titre incident</th>
          <td><?= htmlspecialchars($incident['title'] ?? '') ?></td>
        </tr>
        <tr>
          <th>Priorité</th>
          <td><?= htmlspecialchars($incident['priority'] ?? 'N/A') ?></td>
        </tr>
        <tr>
          <th>Date déclaration</th>
          <td><?= !empty($incident['declared_at']) ? date('d/m/Y H:i', strtotime($incident['declared_at'])) : '—' ?></td>
        </tr>
      </tbody>
    </table>
  </div>

  <h2 class="h5">Résumé de l'intervention</h2>
  <div class="row mb-4">
    <div class="col-md-3">
      <div class="card text-center">
        <div class="card-body">
          <h3 class="card-title text-primary"><?= htmlspecialchars($summary['duration_human']) ?></h3>
          <p class="card-text small text-muted">Durée totale</p>
        </div>
      </div>
    </div>
    <div class="col-md-3">
      <div class="card text-center">
        <div class="card-body">
          <h3 class="card-title text-warning"><?= (int)$summary['pause_count'] ?></h3>
          <p class="card-text small text-muted">Pause<?= $summary['pause_count'] > 1 ? 's' : '' ?></p>
        </div>
      </div>
    </div>
    <div class="col-md-3">
      <div class="card text-center">
        <div class="card-body">
          <h3 class="card-title text-info"><?= (int)$summary['event_count'] ?></h3>
          <p class="card-text small text-muted">Événement<?= $summary['event_count'] > 1 ? 's' : '' ?></p>
        </div>
      </div>
    </div>
    <div class="col-md-3">
      <div class="card text-center">
        <div class="card-body">
          <h3 class="card-title text-success"><?= htmlspecialchars($summary['technician_name']) ?></h3>
          <p class="card-text small text-muted">Technicien</p>
        </div>
      </div>
    </div>
  </div>

  <h2 class="h5">Chronologie détaillée</h2>
  <?php if (empty($events)): ?>
    <div class="alert alert-info">Aucun événement d'intervention enregistré.</div>
  <?php else: ?>
    <div class="timeline">
      <?php foreach ($events as $event): ?>
        <div class="timeline-item <?= htmlspecialchars($event['action']) ?>">
          <div class="d-flex justify-content-between align-items-start">
            <div>
              <strong><?= date('d/m/Y H:i:s', strtotime($event['happened_at'])) ?></strong>
              <span class="badge bg-<?= match($event['action']) {
                'start' => 'success',
                'pause' => 'warning text-dark',
                'resume' => 'primary',
                default => 'secondary'
              } ?> ms-2">
                <?= match($event['action']) {
                  'start' => 'Démarrage',
                  'pause' => 'Pause',
                  'resume' => 'Reprise',
                  default => ucfirst($event['action'])
                } ?>
              </span>
            </div>
            <small class="text-muted"><?= htmlspecialchars($event['user_name']) ?></small>
          </div>
          <?php if (!empty($event['reason'])): ?>
            <div class="mt-2 p-2 bg-light rounded small">
              <strong>Motif:</strong> <?= nl2br(htmlspecialchars($event['reason'])) ?>
            </div>
          <?php endif; ?>
        </div>
      <?php endforeach; ?>
    </div>
  <?php endif; ?>

  <hr class="mt-4">
  <div class="text-muted small">
    Généré le <?= date('d/m/Y H:i') ?> pour <?= htmlspecialchars($summary['technician_name']) ?>.
  </div>
</body>
</html>