<?php
include 'conexion2.php';

$customer_id = $_POST['customer_id'];
$desde = $_POST['desde'] ?? '';
$hasta = $_POST['hasta'] ?? '';

$sql = "SELECT sp.*, c.customer_name, si.created_at
        FROM selling_price sp
        JOIN selling_info si ON sp.invoice_id = si.invoice_id
        JOIN customers c ON si.customer_id = c.customer_id
        WHERE c.customer_id = ?";

$params = [$customer_id];

// Filtrar por fecha si se cargó
if (!empty($desde)) {
    $sql .= " AND si.created_at >= ?";
    $params[] = $desde . " 00:00:00";
}
if (!empty($hasta)) {
    $sql .= " AND si.created_at <= ?";
    $params[] = $hasta . " 23:59:59";
}

$sql .= " ORDER BY si.created_at DESC";

$stmt = $pdo->prepare($sql);
$stmt->execute($params);
$facturas = $stmt->fetchAll();

if (!$facturas) {
  echo "<div class='alert alert-warning'>No se encontraron facturas.</div>";
  exit;
}
?>

<table class="table table-bordered table-sm">
  <thead class="table-dark">
    <tr>
      <th>Factura</th>
      <th>Fecha</th>
      <th>Cliente</th>
      <th>Subtotal Pesos</th>
      <th>Descuento</th>
     
      <th>Cotizacion</th>
      <th>Imp- Dolares</th>
      <th>Pagado</th>
      <th>Debe</th>
      <th>Devolucion</th>
      <th>Acción</th>
    </tr>
  </thead>
  <tbody>
    <?php foreach ($facturas as $f): ?>
      <tr>
        <td><?= $f['invoice_id'] ?></td>
        <td><?= $f['created_at'] ?></td>
        <td><?= htmlspecialchars($f['customer_name']) ?></td>
        <td><input class="form-control form-control-sm" id="subtotal-<?= $f['price_id'] ?>" value="<?= $f['subtotal'] ?>"></td>
        <td><input class="form-control form-control-sm" id="descuento-<?= $f['price_id'] ?>" value="<?= $f['discount_amount'] ?>"></td>
        
        <td><input class="form-control form-control-sm" id="otros-<?= $f['price_id'] ?>" value="<?= $f['others_charge'] ?>"></td>
        <td><input class="form-control form-control-sm" id="total-<?= $f['price_id'] ?>" value="<?= $f['payable_amount'] ?>"></td>
        <td><input class="form-control form-control-sm" id="pagado-<?= $f['price_id'] ?>" value="<?= $f['paid_amount'] ?>"></td>
        <td><input class="form-control form-control-sm" id="debe-<?= $f['price_id'] ?>" value="<?= $f['due'] ?>"></td>
        <td><input class="form-control form-control-sm" id="return-<?= $f['price_id'] ?>" value="<?= $f['return_amount'] ?>"></td>
        <td>
          <button class="btn btn-sm btn-success" onclick="guardarFactura(<?= $f['price_id'] ?>)">Guardar</button>
        </td>
      </tr>
    <?php endforeach; ?>
  </tbody>
</table>

<script>
function guardarFactura(id) {
  const datos = {
    price_id: id,
    subtotal: $('#subtotal-' + id).val(),
    discount_amount: $('#descuento-' + id).val(),
  
 
   
    others_charge: $('#otros-' + id).val(),
    payable_amount: $('#total-' + id).val(),
    paid_amount: $('#pagado-' + id).val(),
    due: $('#debe-' + id).val(),
    return_amount: $('#return-' + id).val()
  };

  $.post('guardar_factura.php', datos, function(resp){
    alert(resp);
  });
}
</script>
