<?php
    $title = "dashboard";
    $MODULO = $title;
    require_once "../../config/init.php";
?>

<!DOCTYPE html>
<html lang="es">
<head>
    <?php include("head.php"); ?> 
    <title><?= $title ?></title>
    <?php include("styles.php"); ?> 
</head>
<body>

    <?php include("navbar.php"); ?>
    <div class="container">

    <!-- ══ ENCABEZADO ══ <h5>Dashboard</h5>
    <div class="page-header">
        <div class="page-header-icon">
            <i class="fas fa-user-tie"></i>
        </div>
        <div>
            <small id="frase"></small>
            <small id="autor"></small>
        </div>
    </div> -->

    <!-- Tarjeta Filtros -->
    <?php include("body_tienda.php"); ?>

    <!-- TARJETAS  + PIE CHART -->
    <div class="row mb-4">
        <!-- Total Facturado -->
        <div class="col-md-3 col-sm-6 mb-3">
            <div class="card shadow-sm h-100">
                <div class="card-body text-center">
                    <h6 class="text-success">🛒 Total Ventas:</h6>
                    <h3 id="cardTotal" class="text-primary">S/ 0.00</h3>
                    <h6>💳 Créditos otorgados:</h6>
                    <h5 id="cardCreditos">S/ 0.00</h5>
                </div>
            </div>
        </div>

        <!-- Comprobantes -->
        <div class="col-md-3 col-sm-6 mb-3">
            <div class="card shadow-sm h-100">
                <div class="card-body text-center">
                    <h6 class="text-success">💰 Cobranzas realizadas:</h6>
                    <h3 id="cardCobranzas" class="text-primary">S/ 0.00</h3>
                    <h6>🧾 Comprobantes emitidos:</h6>
                    <h5 id="cardComprobantes">0</h5>
                </div>
            </div>
        </div>

        <!-- Donut Crédito vs Contado -->
        <div class="col-md-3 col-sm-6 mb-3">
            <div class="card shadow-sm h-100">
                <div class="card-body p-2 chart-box-sm text-center">
                    <h6 class="fw-bold mb-1" style="font-size:13px;">💳 Credito vs. el contado</h6>
                    <canvas id="pieVentas"></canvas>
                </div>
            </div>
        </div>
    </div>


    <!-- GRAFICO DE BARRAS-->
    <div class="card shadow-sm mt-4">
        <div class="card-body">
            <canvas id="graficoFacturas" height="150"></canvas>
        </div>
    </div>

    <!-- CLIENTES Y PRODUCTOS -->
    <div class="row mt-3">
        <!-- Top Clientes -->
        <div class="col-md-6 mb-3">
            <div class="card shadow-sm card-clientes h-100">
                <div class="card-body p-2">
                    <h6 class="mb-2 text-center fw-bold">👤 Top 5 Mejores Clientes</h6>
                    <div class="table-responsive compact-table">
                        <table class="table table-sm table-borderless">
                            <thead>
                                <tr class="bg-light text-center">
                                    <th style="width:10%;">RUC/DNI</th>
                                    <th style="width:60%;">Nombre/Razón Social</th>
                                    <th class="text-end" style="width:30%;">Importe</th>
                                </tr>
                            </thead>
                            <tbody id="tablatopClientes">
                                <tr><td colspan="3" class="text-center">Cargando...</td></tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>

        <!-- Top Productos -->
        <div class="col-md-6 mb-3">
            <div class="card shadow-sm card-productos h-100">
                <div class="card-body p-2">
                    <h6 class="mb-2 text-center fw-bold">📦 Top 5 Productos Más Vendidos</h6>
                    <div class="table-responsive compact-table">
                        <table class="table table-sm table-borderless align-middle">
                            <thead>
                                <tr class="bg-light text-center">
                                    <th style="width:10%;">Código</th>
                                    <th style="width:40%;">Nombre</th>
                                    <th class="text-end" style="width:15%;">Cantidad</th>
                                    <th class="text-end" style="width:35%;">Importe</th>
                                </tr>
                            </thead>
                            <tbody id="tablatopProductos">
                                <tr><td colspan="4" class="text-center">Cargando...</td></tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>

</div>

    <!-- Librerías locales -->
    <script src="../../public/assets/js/chart.js"></script>
    <script src="../../public/assets/js/jquery-3.6.0.min.js"></script>
    <script src="../views/scripts/constante.js"></script>
    <script src="../views/scripts/venta.js?v=<?= time() ?>"></script>
</body>
</html>
