<style>
    .card-balance {
        padding: 10px 15px;
        border-radius: 8px;
        margin-bottom: 15px;
        border: 1px solid #ddd;
        background: #fff;
        text-align: left; /* Alineación para que combine con el estilo del balance */
    }
    .val-monto { 
        font-size: 18px; 
        font-weight: bold; 
        display: block; 
    }
    .panel-title-balance { 
        font-weight: bold; 
        color: #777;
        font-size: 11px;
    }
    @media (max-width: 767px) {
        .val-monto { font-size: 16px; }
    }
</style>
<?php
error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);
session_start();
require_once ("../config/db.php");
require_once ("../config/conexion.php");

$reporte_tienda = mysqli_real_escape_string($con, $_POST['reporte_tienda']);
$desde = mysqli_real_escape_string($con, $_POST['desde']);
$hasta = mysqli_real_escape_string($con, $_POST['hasta']);
$donacion = "Donacion";

$id_tienda_destino = "";
if (!empty($reporte_tienda)) {
    $query_t = mysqli_query($con, "SELECT id_tienda FROM tiendas WHERE nombre = '$reporte_tienda'");
    if ($row_t = mysqli_fetch_array($query_t)) { $id_tienda_destino = $row_t['id_tienda']; }
} else {
    $id_tienda_destino = $_SESSION['estado']; 
}

$condiciones = " WHERE condiciones != '$donacion'";
if (!empty($id_tienda_destino)) { $condiciones .= " AND almacen = '$id_tienda_destino'"; }
if (!empty($desde) && !empty($hasta)) { $condiciones .= " AND fecha_factura BETWEEN '$desde' AND '$hasta'"; }

// Queries (Mantenemos tus consultas originales)
$sql = "SELECT fecha_factura, tasa, sum(total_venta) AS total, sum(condiciones_efe) AS efe, sum(condiciones_efed) AS efed, sum(condiciones_tra) AS tra, sum(condiciones_pun) AS pun FROM facturas $condiciones GROUP BY fecha_factura ORDER BY fecha_factura DESC";
$sql2 = "SELECT * FROM facturas $condiciones";

// Acumuladores de totales
$total = 0; $credito = 0; $pago = 0;
$efe_acum = 0; $efed_acum = 0; $tra_acum = 0; $pun_acum = 0;

$consulta_ventas = mysqli_query($con, $sql2);
while ($d = mysqli_fetch_array($consulta_ventas)) {
    $total += $d['total_venta'];
    if ($d['estado_factura'] == 1) { $credito += $d['total_venta']; } else { $pago += $d['total_venta']; }
    $efe_acum += $d['condiciones_efe'];
    $efed_acum += $d['condiciones_efed'];
    $tra_acum += $d['condiciones_tra'];
    $pun_acum += $d['condiciones_pun'];
}
?>

<div class="row" style="margin-bottom: 10px;">
    <!-- FACTURADO -->
    <div class="col-xs-6 col-md-3">
        <div class="card-balance" style="border-left: 5px solid #337ab7;">
            <span class="panel-title-balance text-uppercase">FACTURADO</span>
            <span class="text-primary val-monto">$ <?php echo number_format($total, 2, ",", "."); ?></span>
        </div>
    </div>

    <!-- POR COBRAR -->
    <div class="col-xs-6 col-md-3">
        <div class="card-balance" style="border-left: 5px solid #d9534f;">
            <span class="panel-title-balance text-uppercase">POR COBRAR</span>
            <span class="text-danger val-monto">$ <?php echo number_format($credito, 2, ",", "."); ?></span>
        </div>
    </div>

    <!-- PAGADO -->
    <div class="col-xs-6 col-md-3">
        <div class="card-balance" style="border-left: 5px solid #5cb85c;">
            <span class="panel-title-balance text-uppercase">PAGADO</span>
            <span class="text-success val-monto">$ <?php echo number_format($pago, 2, ",", "."); ?></span>
        </div>
    </div>

    <!-- BOTÓN PDF -->
    <div class="col-xs-6 col-md-3">
        <div style="margin-top: 5px;">
            <a href="imp_ventas.php?desde=<?php echo $desde; ?>&hasta=<?php echo $hasta; ?>&tienda=<?php echo $id_tienda_destino; ?>" class="btn btn-info btn-block" style="padding: 15px; font-weight: bold; font-size: 16px;">
                <i class="glyphicon glyphicon-print"></i> IMPRIMIR PDF
            </a>
        </div>
    </div>
</div>
<div class="table-responsive">
    <table class="table table-striped table-bordered table-condensed" style="font-size:11px; background: white;">
        <thead>
            <tr class="info">
                <th>Fecha</th>
                <th>Total ($)</th>
                <th>Abonado ($)</th>
                <th>Efe BsD</th>
                <th>Efe $</th> 
                <th>Tra BsD</th>
                <th>Punto BsD</th>
            </tr>
        </thead>
        <tbody>
            <?php
            $query = mysqli_query($con, $sql);
            if (mysqli_num_rows($query) > 0) {
                while ($r = mysqli_fetch_array($query)) {
                    // Cálculo de lo pagado (sumando los métodos de pago convertidos)
                    $pagado_usd = ($r['efe'] + $r['tra'] + $r['pun']) / $r['tasa'] + $r['efed'];
            ?>
                <tr>
                    <td><strong><?php echo date("d/m", strtotime($r['fecha_factura'])); ?></strong></td>
                    <td><?php echo number_format($r['total'], 2, ",", "."); ?></td>
                    <td class="text-success"><strong><?php echo number_format($pagado_usd, 2, ",", "."); ?></strong></td>
                    <td><?php echo number_format($r['efe'], 2, ",", "."); ?></td>
                    
                    <td class="warning"><strong>$ <?php echo number_format($r['efed'], 2, ",", "."); ?></strong></td>
                    
                    <td><?php echo number_format($r['tra'], 2, ",", "."); ?></td>
                    <td><?php echo number_format($r['pun'], 2, ",", "."); ?></td>
                </tr>
            <?php } } else { ?>
                <tr><td colspan="7" class="text-center">No hay registros para este periodo</td></tr>
            <?php } ?>
        </tbody>
        <tfoot>
            <tr class="active" style="font-size: 12px; border-top: 2px solid #333;">
                <td class="text-right"><strong>TOTALES:</strong></td>
                <td><strong><?php echo number_format($total, 2, ",", "."); ?></strong></td>
                <td><strong><?php echo number_format($pago, 2, ",", "."); ?></strong></td>
                <td><strong><?php echo number_format($efe_acum, 2, ",", "."); ?></strong></td>
                <td class="warning"><strong>$ <?php echo number_format($efed_acum, 2, ",", "."); ?></strong></td>
                <td><strong><?php echo number_format($tra_acum, 2, ",", "."); ?></strong></td>
                <td><strong><?php echo number_format($pun_acum, 2, ",", "."); ?></strong></td>
            </tr>
        </tfoot>
    </table>
</div>

<div class="row" style="margin-top:20px;">
    <div class="col-md-6 text-center">
        <img src="torta_ventas.php?desde=<?php echo $desde; ?>&hasta=<?php echo $hasta; ?>&tienda=<?php echo $id_tienda_destino; ?>" class="img-responsive center-block" alt="Torta">
    </div>
    <div class="col-md-6 text-center">
        <img src="barra_ventas.php?desde=<?php echo $desde; ?>&hasta=<?php echo $hasta; ?>&tienda=<?php echo $id_tienda_destino; ?>" class="img-responsive center-block" alt="Barras">
    </div>
</div>