<?php /** @var array $incident */ /** @var array|null $finalReport */ /** @var array $atts */ ?>
<!DOCTYPE html>
<html lang="fr">
<head>
  <meta charset="utf-8">
  <title>Rapport final - <?= 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; }
    .att-list a { text-decoration:none; }
  </style>
</head>
<body class="p-4">
  <h1 class="mb-3">Rapport final
    <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>
        <tr>
          <th>Statut actuel</th>
          <td><?= htmlspecialchars($incident['status_label'] ?? '—') ?></td>
        </tr>
      </tbody>
    </table>
  </div>
  <h2 class="h5">Données du rapport final</h2>
  <?php if ($finalReport): ?>
    <table class="table table-bordered table-sm">
      <tbody>
        <tr>
          <th style="width:200px">Date de remontée</th>
          <td><?= $finalReport['restoration_at'] ? date('d/m/Y H:i', strtotime($finalReport['restoration_at'])) : '<em>Non renseignée</em>' ?></td>
        </tr>
        <tr>
          <th>Superviseur Client</th>
          <td><?= !empty($finalReport['client_supervisor']) ? htmlspecialchars($finalReport['client_supervisor']) : '<em>Non renseigné</em>' ?></td>
        </tr>
        <tr>
          <th>Commentaire</th>
          <td><?= $finalReport['comments'] ? nl2br(htmlspecialchars($finalReport['comments'])) : '<em>Aucun</em>' ?></td>
        </tr>
        <?php if (!empty($interventionData)): ?>
        <tr>
          <th>Durée intervention</th>
          <td>
            <strong><?= htmlspecialchars($interventionData['duration_human']) ?></strong>
            <?php if ($interventionData['pause_count'] > 0): ?>
              <span class="text-muted">(<?= (int)$interventionData['pause_count'] ?> pause<?= $interventionData['pause_count'] > 1 ? 's' : '' ?>)</span>
            <?php endif; ?>
            <br><small class="text-muted">Technicien: <?= htmlspecialchars($interventionData['technician_name']) ?></small>
          </td>
        </tr>
        <?php endif; ?>
      </tbody>
    </table>
    <h3 class="h6">Pièces jointes</h3>
    <?php if (empty($atts)): ?>
      <p class="text-muted"><em>Aucune pièce jointe</em></p>
    <?php else: ?>
      <ul class="att-list list-unstyled">
        <?php foreach ($atts as $a): $path = $a['path']; $clean = ltrim($path,'/'); if (str_starts_with($clean,'storage/uploads/')) { $clean = substr($clean, strlen('storage/uploads/')); } if (!str_starts_with($clean,'incidents/')) { $clean = 'incidents/'.($incident['id'] ?? 0).'/final/'.basename($clean); } $url = upload_url($clean); ?>
          <li class="mb-1"><a href="<?= htmlspecialchars($url) ?>" target="_blank"><i class="fas fa-paperclip me-1"></i><?= htmlspecialchars(basename($clean)) ?></a></li>
        <?php endforeach; ?>
      </ul>
    <?php endif; ?>
  <?php else: ?>
    <div class="alert alert-warning">Aucun rapport final enregistré pour cet incident.</div>
  <?php endif; ?>
  <hr>
  <div class="text-muted small">Généré le <?= date('d/m/Y H:i') ?>.</div>
</body>
</html>
