<?php
declare(strict_types=1);
require_once __DIR__ . '/../includes/db.php';
require_once __DIR__ . '/../includes/functions.php';

$pageTitle = 'Consulta de Cartera';
$activeMenu = 'consultas';
$activePage = 'cartera';
$basePath = '..';

$conn = db();
$vendedores = $conn->query('SELECT cod_vendedor, nombre FROM vendedores ORDER BY nombre');

require __DIR__ . '/../includes/header.php';
?>
<div class="d-flex justify-content-between align-items-center mb-3">
  <h4 class="mb-0"><i class="bi bi-cash-stack"></i> Cartera pendiente</h4>
  <button id="btnExportar" class="btn btn-outline-success btn-sm">
    <i class="bi bi-file-earmark-excel"></i> Exportar a Excel
  </button>
</div>

<div class="card shadow-sm mb-3">
  <div class="card-body">
    <div class="row g-3">
      <div class="col-md-3">
        <label class="form-label">Vendedor</label>
        <select id="fVendedor" class="form-select form-select-sm">
          <option value="">Todos</option>
          <?php while ($v = $vendedores->fetch_assoc()): ?>
          <option value="<?= h($v['cod_vendedor']) ?>"><?= h($v['nombre']) ?></option>
          <?php endwhile; ?>
        </select>
      </div>
      <div class="col-md-3">
        <label class="form-label">Rango de días vencidos</label>
        <select id="fRango" class="form-select form-select-sm">
          <option value="">Todos</option>
          <option value="0-0">Al día (0)</option>
          <option value="1-30">1 a 30 días</option>
          <option value="31-60">31 a 60 días</option>
          <option value="61-9999">Más de 60 días</option>
        </select>
      </div>
    </div>
  </div>
</div>

<div class="card shadow-sm">
  <div class="card-body">
    <table id="tblCartera" class="table table-striped table-hover align-middle w-100">
      <thead>
        <tr><th>Factura</th><th>Cliente</th><th>Vendedor</th><th>Fecha factura</th><th>Valor documento</th><th>Saldo</th><th>Días vencidos</th></tr>
      </thead>
      <tbody></tbody>
    </table>
  </div>
</div>

<?php
$extraScripts = <<<HTML
<script>
let table = initServerTable('#tblCartera', '../api/datatable_cartera.php', [
  {data: 0}, {data: 1}, {data: 2}, {data: 3}, {data: 4}, {data: 5}, {data: 6}
], function () {
  let rango = document.getElementById('fRango').value.split('-');
  return {
    cod_vendedor: document.getElementById('fVendedor').value,
    dias_min: rango[0] || '',
    dias_max: rango[1] || '',
  };
});
['fVendedor', 'fRango'].forEach(function (id) {
  document.getElementById(id).addEventListener('change', function () { table.ajax.reload(); });
});

document.getElementById('btnExportar').addEventListener('click', function () {
  let rango = document.getElementById('fRango').value.split('-');
  const params = new URLSearchParams({
    cod_vendedor: document.getElementById('fVendedor').value,
    dias_min: rango[0] || '',
    dias_max: rango[1] || '',
  });
  window.location.href = '../api/exportar_cartera.php?' + params.toString();
});
</script>
HTML;
require __DIR__ . '/../includes/footer.php';
?>
