<?php
require 'auth.php';
require 'conexao.php';
$inicio = $_GET['inicio'] ?? '';
$fim = $_GET['fim'] ?? '';

$sql = "SELECT * FROM ordens_servico WHERE 1=1 ";
if ($inicio) $sql .= " AND DATE(data_entrada) >= '". $mysqli->real_escape_string($inicio) ."'";
if ($fim)    $sql .= " AND DATE(data_entrada) <= '". $mysqli->real_escape_string($fim) ."'";
$sql .= " ORDER BY data_entrada DESC";

$res = $mysqli->query($sql);
?>
<!doctype html>
<html lang="pt-br">
<head>
<meta charset="utf-8">
<title>Histórico — Manutenção TI</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-4">
<div class="container">
  <div class="d-flex justify-content-between align-items-center mb-3">
    <h4>Histórico de Ordens</h4>
    <a href="listar_os.php" class="btn btn-outline-secondary btn-sm">Voltar</a>
  </div>

  <form class="row g-2 mb-3">
    <div class="col"><input type="date" name="inicio" value="<?= htmlspecialchars($inicio) ?>" class="form-control"></div>
    <div class="col"><input type="date" name="fim" value="<?= htmlspecialchars($fim) ?>" class="form-control"></div>
    <div class="col"><button class="btn btn-primary">Filtrar</button></div>
  </form>

  <table class="table table-hover">
    <thead><tr><th>OS</th><th>Solicitante</th><th>Entrada</th><th>Status</th><th></th></tr></thead>
    <tbody>
      <?php while ($r = $res->fetch_assoc()): ?>
        <tr>
          <td><?= htmlspecialchars($r['numero_os']) ?></td>
          <td><?= htmlspecialchars($r['nome_solicitante']) ?></td>
          <td><?= htmlspecialchars($r['data_entrada']) ?></td>
          <td><?= htmlspecialchars($r['status']) ?></td>
          <td class="text-end">
            <a href="exportar_os_pdf.php?id=<?= $r['id'] ?>" class="btn btn-sm btn-danger">PDF</a>
            <a href="finalizar_os.php?id=<?= $r['id'] ?>" class="btn btn-sm btn-outline-primary">Ver</a>
          </td>
        </tr>
      <?php endwhile; ?>
    </tbody>
  </table>

</div>
</body>
</html>
