<?php
session_start();
require_once ("../config/db.php");
require_once ("../config/conexion.php");

// Consulta de Tasa (Mantenida)
$con_tasa = mysqli_query($con, "select * from tasa where id_tasa='1'");
while ($dato_tasa = mysqli_fetch_array($con_tasa)) {
    $tasa_actual = $dato_tasa['tasa'];
}

$desde = $_POST['desde'];
$hasta = $_POST['hasta'];
$sTable = "compras ";
$sWhere = " ";

// Lógica de Filtros (Exactamente igual a la tuya)
if($_POST['desde']!="" && $_POST['hasta']!="") {
    $sWhere.= "WHERE fecha BETWEEN '$desde' AND '$hasta'";
} else if($_POST['desde']!="" && $_POST['hasta']=="") {
    $sWhere.= "WHERE fecha = '$desde'";
} else if($_POST['desde']=="" && $_POST['hasta']!="") {
    $sWhere.= "WHERE fecha = '$hasta'";
}

$sql = "SELECT * FROM $sTable $sWhere ORDER BY id_compra DESC";
$query = mysqli_query($con, $sql);

$deuda = 0;
$pagos = 0;
$total = 0;

while($d = mysqli_fetch_array($query)) {
    $total = $total + $d['total'];
    $deuda = $deuda + $d['por_pagar'];  
    $pagos = $pagos + $d['abono'];      
}

// Guardar en sesión para los gráficos
$_SESSION["deuda"] = $deuda;
$_SESSION["pagos"] = $pagos;
$_SESSION["total"] = $total;

// Evitar error matemático si el total es 0
$porcentaje_deuda = ($total > 0) ? ($deuda / $total) * 100 : 0;
$porcentaje_pagos = ($total > 0) ? ($pagos / $total) * 100 : 0;
?>

<div class="row">
    <div class="col-md-4">
        <div class="well text-center" style="background: #f8f9fa; border-left: 5px solid #31708f;">
            <p style="margin-bottom:0; color:#777;">TOTAL FACTURADO</p>
            <h3 style="margin-top:5px; font-weight:bold;">$ <?php echo number_format($total, 2, ",", "."); ?></h3>
            <span class="label label-primary">100%</span>
        </div>
    </div>
    
    <div class="col-md-4">
        <div class="well text-center" style="background: #f8f9fa; border-left: 5px solid #a94442;">
            <p style="margin-bottom:0; color:#777;">POR PAGAR (DEUDA)</p>
            <h3 style="margin-top:5px; font-weight:bold; color:#a94442;">$ <?php echo number_format($deuda, 2, ",", "."); ?></h3>
            <span class="label label-danger"><?php echo number_format($porcentaje_deuda, 2, ",", "."); ?>%</span>
        </div>
    </div>

    <div class="col-md-4">
        <div class="well text-center" style="background: #f8f9fa; border-left: 5px solid #3c763d;">
            <p style="margin-bottom:0; color:#777;">PAGADO (ABONOS)</p>
            <h3 style="margin-top:5px; font-weight:bold; color:#3c763d;">$ <?php echo number_format($pagos, 2, ",", "."); ?></h3>
            <span class="label label-success"><?php echo number_format($porcentaje_pagos, 2, ",", "."); ?>%</span>
        </div>
    </div>
</div>

<hr>

<div class="row">
    <div class="col-md-6 text-center">
        <div class="thumbnail">
            <p><strong>Distribución de Deuda vs Pagos</strong></p>
            <img src="torta_compras.php?desde=<?php echo $desde; ?>&hasta=<?php echo $hasta; ?>&t=<?php echo time(); ?>" class="img-responsive" style="margin:0 auto;">
        </div>
    </div>
    <div class="col-md-6 text-center">
        <div class="thumbnail">
            <p><strong>Comparativa de Montos</strong></p>
            <img src="barra_compras.php?desde=<?php echo $desde; ?>&hasta=<?php echo $hasta; ?>&t=<?php echo time(); ?>" class="img-responsive" style="margin:0 auto;">
        </div>
    </div>
</div>