<?php
$title = 'Histórico do equipamento';
require_once __DIR__ . '/auth.php';
require_login();
check_csrf();

$id = (int)($_GET['id'] ?? 0);
$eq = $pdo->prepare("SELECT e.*, et.name tipo FROM equipment e
                     JOIN equipment_types et ON et.id=e.type_id
                     WHERE e.id=?");
$eq->execute([$id]);
$eq = $eq->fetch();
if (!$eq) { http_response_code(404); exit('Equipamento não encontrado'); }

$hist = $pdo->prepare("
  SELECT l.*, u.name user_name, lo.id AS loan_num, em.matricula, em.nome AS emp_nome
  FROM equipment_log l
  LEFT JOIN users u  ON u.id = l.user_id
  LEFT JOIN loans lo ON lo.id = l.loan_id
  LEFT JOIN employees em ON em.id = lo.employee_id
  WHERE l.equipment_id = ?
  ORDER BY l.created_at DESC, l.id DESC
");
$hist->execute([$id]);
$hist = $hist->fetchAll();

include __DIR__ . '/header.php';
?>
<div class="container-fluid">
  <div class="d-flex justify-content-between align-items-center mb-3">
    <h2 class="h5 m-0">Histórico — #<?= (int)$eq['id'] ?> • <?= h($eq['tipo']) ?><?= $eq['modelo'] ? ' • '.h($eq['modelo']) : '' ?></h2>
    <a class="btn btn-outline-secondary" href="<?= base_url('equipment.php') ?>">Voltar</a>
  </div>

  <div class="card border-0 shadow-sm mb-3">
    <div class="card-body">
      <div class="row g-3">
        <div class="col-md-3"><strong>Patrimônio:</strong> <?= h($eq['patrimonio'] ?: '—') ?></div>
        <div class="col-md-3"><strong>Serial:</strong> <?= h($eq['serial'] ?: '—') ?></div>
        <div class="col-md-3"><strong>Status atual:</strong> <span class="badge bg-secondary"><?= t_status($eq['status']) ?></span></div>
        <div class="col-md-3"><strong>TAG:</strong> <?= h($eq['tag_qr'] ?: '—') ?></div>
      </div>
    </div>
  </div>

  <div class="card border-0 shadow-sm">
    <div class="table-responsive">
      <table class="table align-middle mb-0">
        <thead>
          <tr>
            <th>Quando</th>
            <th>Ação</th>
            <th>De → Para</th>
            <th>Por</th>
            <th>Empréstimo</th>
            <th>Colaborador</th>
            <th>Notas</th>
          </tr>
        </thead>
        <tbody>
          <?php foreach ($hist as $h): ?>
            <tr>
              <td><?= br_datetime($h['created_at'] ?? '') ?></td>
              <td><?= h(t_action($h['action'])) ?></td>
              <td>
                <?php
                  $de = $h['from_status'] ? t_status($h['from_status']) : '—';
                  $para = $h['to_status'] ? t_status($h['to_status']) : '—';
                  echo h($de.' → '.$para);
                ?>
              </td>
              <td><?= h($h['user_name'] ?? '—') ?></td>
              <td><?= $h['loan_num'] ? '#'.(int)$h['loan_num'] : '—' ?></td>
              <td><?= !empty($h['matricula']) ? h($h['matricula'].' • '.$h['emp_nome']) : '—' ?></td>
              <td><?= h($h['notes'] ?? '') ?></td>
            </tr>
          <?php endforeach; ?>
          <?php if (!$hist): ?>
            <tr><td colspan="7" class="text-muted">Sem eventos registrados.</td></tr>
          <?php endif; ?>
        </tbody>
      </table>
    </div>
  </div>
</div>
<?php include __DIR__ . '/footer.php'; ?>

