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

$pageTitle = 'Informe de Cartera Vencida';
$activeMenu = 'informes';
$activePage = 'cartera';
$basePath = '..';

$conn = db();

$sqlResumenVendedor = "SELECT v.nombre,
    SUM(CASE WHEN car.dias_vencidos <= 0 THEN car.saldo ELSE 0 END) AS al_dia,
    SUM(CASE WHEN car.dias_vencidos BETWEEN 1 AND 30 THEN car.saldo ELSE 0 END) AS r1_30,
    SUM(CASE WHEN car.dias_vencidos BETWEEN 31 AND 60 THEN car.saldo ELSE 0 END) AS r31_60,
    SUM(CASE WHEN car.dias_vencidos > 60 THEN car.saldo ELSE 0 END) AS r60_mas,
    SUM(car.saldo) AS total
    FROM cartera car
    JOIN vendedores v ON v.cod_vendedor = car.cod_vendedor
    GROUP BY car.cod_vendedor, v.nombre
    ORDER BY total DESC";
$resumen = $conn->query($sqlResumenVendedor);

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-graph-up-arrow"></i> Cartera vencida por vendedor</h4>
</div>

<div class="card shadow-sm mb-4">
  <div class="card-body p-0">
    <table class="table table-striped mb-0">
      <thead>
        <tr>
          <th>Vendedor</th><th class="text-end">Al día</th><th class="text-end">1-30 días</th>
          <th class="text-end">31-60 días</th><th class="text-end">+60 días</th><th class="text-end">Total</th>
        </tr>
      </thead>
      <tbody>
        <?php $granTotal = 0; while ($r = $resumen->fetch_assoc()): $granTotal += $r['total']; ?>
        <tr>
          <td><?= h($r['nombre']) ?></td>
          <td class="text-end"><?= money((float) $r['al_dia']) ?></td>
          <td class="text-end"><?= money((float) $r['r1_30']) ?></td>
          <td class="text-end text-warning"><?= money((float) $r['r31_60']) ?></td>
          <td class="text-end text-danger"><?= money((float) $r['r60_mas']) ?></td>
          <td class="text-end fw-semibold"><?= money((float) $r['total']) ?></td>
        </tr>
        <?php endwhile; ?>
      </tbody>
      <tfoot>
        <tr class="table-light fw-bold">
          <td colspan="5" class="text-end">Total general</td>
          <td class="text-end"><?= money((float) $granTotal) ?></td>
        </tr>
      </tfoot>
    </table>
  </div>
</div>

<div class="card shadow-sm">
  <div class="card-body">
    <div class="d-flex justify-content-between align-items-center mb-3">
      <h6 class="mb-0">Detalle de facturas vencidas (más de 30 días)</h6>
      <button id="btnExportar" class="btn btn-outline-success btn-sm">
        <i class="bi bi-file-earmark-excel"></i> Exportar a Excel
      </button>
    </div>
    <table id="tblCarteraVencida" 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>
initServerTable('#tblCarteraVencida', '../api/datatable_cartera.php', [
  {data: 0}, {data: 1}, {data: 2}, {data: 3}, {data: 4}, {data: 5}, {data: 6}
], function () {
  return { dias_min: 31 };
});

document.getElementById('btnExportar').addEventListener('click', function () {
  window.location.href = '../api/exportar_cartera.php?dias_min=31';
});
</script>
HTML;
require __DIR__ . '/../includes/footer.php';
?>
