<?php
include 'conexion.php';

$desde = $_GET['desde'] ?? '';
$hasta = $_GET['hasta'] ?? '';
$titular = $_GET['titular'] ?? '';
$banco = $_GET['banco'] ?? '';

$sql = "SELECT c.*, s.sup_name 
        FROM cheques_proveedores c
        LEFT JOIN suppliers s ON c.sup_id = s.sup_id
        WHERE 1=1";

if ($desde) {
    $sql .= " AND c.fecha_cobro >= '" . $conn->real_escape_string($desde) . "'";
}
if ($hasta) {
    $sql .= " AND c.fecha_cobro <= '" . $conn->real_escape_string($hasta) . "'";
}
if ($titular) {
    $sql .= " AND c.titular_cuenta LIKE '%" . $conn->real_escape_string($titular) . "%'";
}
if ($banco) {
    $sql .= " AND c.banco = '" . $conn->real_escape_string($banco) . "'";
}

$sql .= " ORDER BY c.fecha_cobro ASC";
$cheques = $conn->query($sql);
?>

<!DOCTYPE html>
<html lang="es">
<head>
  <meta charset="UTF-8">
  <title>Listado de Cheques</title>
  <link rel="stylesheet" href="assets/bootstrap/css/bootstrap.min.css">
</head>
<body>
<div class="container">
  <h3 class="page-header">Listado de Cheques remitidos a Proveedores</h3>

  <form method="get" class="well form-inline">
    <div class="form-group">
      <label>Desde</label>
      <input type="date" name="desde" class="form-control" value="<?php echo htmlspecialchars($desde); ?>">
    </div>
    <div class="form-group">
      <label>Hasta</label>
      <input type="date" name="hasta" class="form-control" value="<?php echo htmlspecialchars($hasta); ?>">
    </div>
    <div class="form-group">
      <label>Titular</label>
      <input type="text" name="titular" class="form-control" placeholder="Buscar titular" value="<?php echo htmlspecialchars($titular); ?>">
    </div>
    <div class="form-group">
      <label>Banco</label>
      <select name="banco" class="form-control">
        <option value="">-- Todos --</option>
        <?php
        $bancos = ['Banco Nacion', 'Banco Provincia', 'Banco Galicia', 'Banco Macro', 'Banco Santander', 'BBVA', 'HSBC', 'Banco Ciudad'];
        foreach ($bancos as $b) {
          $sel = ($b == $banco) ? 'selected' : '';
          echo "<option value=\"$b\" $sel>$b</option>";
        }
        ?>
      </select>
    </div>
    <button type="submit" class="btn btn-primary"><span class="glyphicon glyphicon-search"></span> Filtrar</button>
    <a href="cheques_listado.php" class="btn btn-default">Reiniciar</a>
  </form>

  <div class="table-responsive">
    <table class="table table-bordered table-hover">
      <thead class="bg-info">
        <tr>
          <th>Fecha Cobro</th>
          <th>Proveedor</th>
          <th>Importe</th>
          <th>N° Cheque</th>
          <th>Beneficiario</th>
          <th>Banco</th>
          <th>Origen</th>
          <th>Tipo</th>
          <th>Estado</th>
        </tr>
      </thead>
      <tbody>
        <?php if ($cheques->num_rows > 0): ?>
          <?php while ($c = $cheques->fetch_assoc()): 
            $hoy = date('Y-m-d');
            if ($c['fecha_cobro'] < $hoy) {
              $estado = '<span class="label label-danger">Vencido</span>';
            } elseif ($c['fecha_cobro'] == $hoy) {
              $estado = '<span class="label label-warning">Hoy</span>';
            } elseif ($c['fecha_cobro'] <= date('Y-m-d', strtotime('+2 days'))) {
              $estado = '<span class="label label-danger">Prox. Vence</span>';
            } else {
              $estado = '<span class="label label-success">Activo</span>';
            }
          ?>
          <tr>
            <td><?php echo $c['fecha_cobro']; ?></td>
            <td><?php echo $c['sup_name'] ?: 'Sin proveedor'; ?></td>
            <td>$<?php echo number_format($c['importe_cheque'], 2); ?></td>
            <td><?php echo $c['numero_cheque']; ?></td>
            <td><?php echo $c['beneficiario']; ?></td>
            <td><?php echo $c['banco']; ?></td>
            <td><?php echo ucfirst($c['origen_cheque']); ?></td>
            <td><?php echo ucfirst($c['tipo_cheque']); ?></td>
            <td><?php echo $estado; ?></td>
          </tr>
          <?php endwhile; ?>
        <?php else: ?>
          <tr><td colspan="9" class="text-center text-muted">No se encontraron cheques con los filtros aplicados.</td></tr>
        <?php endif; ?>
      </tbody>
    </table>
  </div>
</div>

<script src="assets/bootstrap/js/jquery.min.js"></script>
<script src="assets/bootstrap/js/bootstrap.min.js"></script>
</body>
</html>
