<?php
ob_start();
session_start();
require_once __DIR__ . '/../_init.php';

// Seguridad
if (!is_loggedin()) {
  redirect(root_url() . '/index.php?redirect_to=' . url());
}
if (user_group_id() != 1 && !has_permission('access', 'transfer')) {
  redirect(root_url() . '/' . ADMINDIRNAME . '/dashboard.php');
}

$pdo = db();

// Filtros simples por fecha
$desde = isset($_GET['desde']) ? trim($_GET['desde']) : '';
$hasta = isset($_GET['hasta']) ? trim($_GET['hasta']) : '';

$where  = "WHERE 1=1";
$params = [];

if ($desde !== '') {
  $where   .= " AND t.created_at >= ?";
  $params[] = $desde . " 00:00:00";
}
if ($hasta !== '') {
  $where   .= " AND t.created_at <= ?";
  $params[] = $hasta . " 23:59:59";
}

$sql = "
  SELECT 
    t.*,
    ba_from.account_name AS from_account_name,
    ba_to.account_name   AS to_account_name,
    u.username           AS user_name
  FROM cheque_transfer_info t
  LEFT JOIN bank_accounts ba_from ON ba_from.id = t.from_account_id
  LEFT JOIN bank_accounts ba_to   ON ba_to.id   = t.to_account_id
  LEFT JOIN users u               ON u.id       = t.user_id
  $where
  ORDER BY t.created_at DESC
  LIMIT 200
";

$st = $pdo->prepare($sql);
$st->execute($params);
$rows = $st->fetchAll(PDO::FETCH_ASSOC);

include __DIR__ . '/header.php';
include __DIR__ . '/left_sidebar.php';
?>
<div class="content-wrapper">
  <section class="content-header">
    <h1>Transferencias de Cheques <small><?php echo store('name'); ?></small></h1>
    <ol class="breadcrumb">
      <li><a href="dashboard.php"><i class="fa fa-dashboard"></i> <?php echo trans('text_dashboard'); ?></a></li>
      <li class="active">Transferencias de Cheques</li>
    </ol>
  </section>

  <section class="content">

    <div class="box box-primary">
      <div class="box-header with-border">
        <h3 class="box-title">Filtros</h3>
      </div>
      <div class="box-body">
        <form class="form-inline" method="get" action="ver_transferencias.php">
          <div class="form-group">
            <label for="desde">Desde:&nbsp;</label>
            <input type="date" name="desde" id="desde" class="form-control" value="<?php echo htmlspecialchars($desde); ?>">
          </div>
          &nbsp;
          <div class="form-group">
            <label for="hasta">Hasta:&nbsp;</label>
            <input type="date" name="hasta" id="hasta" class="form-control" value="<?php echo htmlspecialchars($hasta); ?>">
          </div>
          &nbsp;
          <button type="submit" class="btn btn-primary">
            <i class="fa fa-search"></i> Buscar
          </button>
          &nbsp;
          <a href="ver_transferencias.php" class="btn btn-default">
            <i class="fa fa-refresh"></i> Limpiar
          </a>
        </form>
      </div>
    </div>

    <div class="box box-primary">
      <div class="box-header with-border">
        <h3 class="box-title">Listado de transferencias</h3>
      </div>
      <div class="box-body table-responsive">
        <table class="table table-bordered table-striped table-condensed">
          <thead>
            <tr>
              <th>#</th>
              <th>Fecha</th>
              <th>Cuenta Origen</th>
              <th>Cuenta Destino</th>
              <th>Usuario</th>
              <th class="text-right">Cant. Cheques</th>
              <th class="text-right">Importe Total</th>
              <th class="text-center">Acciones</th>
            </tr>
          </thead>
          <tbody>
          <?php if (!$rows): ?>
            <tr>
              <td colspan="8" class="text-center text-muted">
                No se encontraron transferencias para el filtro seleccionado.
              </td>
            </tr>
          <?php else: ?>
            <?php foreach ($rows as $r): ?>
              <tr>
                <td><?php echo (int)$r['id']; ?></td>
                <td><?php echo htmlspecialchars($r['created_at']); ?></td>
                <td>
                  <?php
                    $fn = $r['from_account_name'] ?: ('Cuenta #' . $r['from_account_id']);
                    echo htmlspecialchars($fn);
                  ?>
                </td>
                <td>
                  <?php
                    $tn = $r['to_account_name'] ?: ('Cuenta #' . $r['to_account_id']);
                    echo htmlspecialchars($tn);
                  ?>
                </td>
                <td><?php echo htmlspecialchars($r['user_name'] ?: ('ID ' . $r['user_id'])); ?></td>
                <td class="text-right"><?php echo (int)$r['cheque_count']; ?></td>
                <td class="text-right">
                  $ <?php echo number_format((float)$r['total'], 2, ',', '.'); ?>
                </td>
                <td class="text-center">
                  <a class="btn btn-xs btn-default" 
                     href="comprobante_rendicion.php?id=<?php echo (int)$r['id']; ?>" 
                     target="_blank">
                    <i class="fa fa-print"></i> Imprimir
                  </a>
                </td>
              </tr>
            <?php endforeach; ?>
          <?php endif; ?>
          </tbody>
        </table>
      </div>
    </div>

  </section>
</div>

<?php include __DIR__ . '/footer.php'; ?>
