<?php
$title = 'Recibo de Devolução';
require_once __DIR__ . '/auth.php';
require_login();

$id = (int)($_GET['id'] ?? 0);
$lr = $pdo->prepare("SELECT lr.*, l.employee_id, e.matricula, e.nome, l.id loan_id, u.name recebedor
  FROM loan_returns lr
  JOIN loans l ON l.id=lr.loan_id
  JOIN employees e ON e.id=l.employee_id
  JOIN users u ON u.id=lr.received_by
  WHERE lr.id=?");
$lr->execute([$id]);
$lr = $lr->fetch();
if (!$lr) { http_response_code(404); exit('Devolução não encontrada'); }

$items = $pdo->prepare("SELECT ri.*, et.name tipo, eq.modelo, eq.patrimonio, eq.serial
  FROM loan_return_items ri
  JOIN equipment eq ON eq.id=ri.equipment_id
  JOIN equipment_types et ON et.id=eq.type_id
  WHERE ri.loan_return_id=?");
$items->execute([$id]);
$items = $items->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">Recibo de devolução • Empréstimo #<?= (int)$lr['loan_id'] ?></h2>
    <div>
      <a class="btn btn-outline-secondary" href="<?= base_url('loan_view.php?id='.(int)$lr['loan_id']) ?>">Voltar</a>
      <button class="btn btn-primary" onclick="window.print()"><i class="bi bi-printer me-1"></i>Imprimir</button>
    </div>
  </div>

  <div class="card border-0 shadow-sm mb-3">
    <div class="card-body">
      <div><strong>Colaborador:</strong> <?= h($lr['matricula'].' • '.$lr['nome']) ?></div>
      <div><strong>Conferido por:</strong> <?= h($lr['recebedor']) ?></div>
      <div><strong>Data/Hora:</strong> <?= h($lr['returned_at']) ?></div>
      <?php if (!empty($lr['notes'])): ?>
        <div class="mt-2"><strong>Obs. gerais:</strong> <span><?= h($lr['notes']) ?></span></div>
      <?php endif; ?>
    </div>
  </div>

  <div class="table-responsive">
    <table class="table table-bordered align-middle">
      <thead>
        <tr>
          <th>#</th><th>Tipo/Modelo</th><th>Patrimônio</th><th>Serial</th>
          <th>Funciona</th><th>Fonte</th><th>Cabos</th><th>Periféricos</th><th>Avarias</th><th>Observações</th><th>Foto</th>
        </tr>
      </thead>
      <tbody>
        <?php foreach ($items as $r): ?>
          <tr>
            <td>#<?= (int)$r['equipment_id'] ?></td>
            <td><?= h(($r['tipo'] ?? '—') . ($r['modelo'] ? ' • '.$r['modelo'] : '')) ?></td>
            <td><?= h($r['patrimonio'] ?? '—') ?></td>
            <td><?= h($r['serial'] ?? '—') ?></td>
            <td><?= h($r['funciona']) ?></td>
            <td><?= $r['fonte'] ? 'Sim' : 'Não' ?></td>
            <td><?= $r['cabos'] ? 'Sim' : 'Não' ?></td>
            <td><?= h($r['perifericos'] ?? '—') ?></td>
            <td><?= h($r['avarias']) ?></td>
            <td><?= h($r['observacoes'] ?? '—') ?></td>
            <td>
              <?php if (!empty($r['photo_path'])): ?>
                <img src="<?= base_url('uploads/returns/'.h($r['photo_path'])) ?>" alt="foto" style="height:40px">
              <?php else: ?>
                —
              <?php endif; ?>
            </td>
          </tr>
        <?php endforeach; ?>
        <?php if (!$items): ?><tr><td colspan="11" class="text-muted">Sem itens.</td></tr><?php endif; ?>
      </tbody>
    </table>
  </div>
</div>
<?php include __DIR__ . '/footer.php'; ?>
