<?php
date_default_timezone_set($_SESSION["zona_horaria"]);
setlocale(LC_ALL,"es_ES@euro","es_ES","esp");
$fecha = date("d-m-Y");
$fechaa = date("m-Y");
?>
<input type="hidden" id="moneda" value="<?php echo Session::get('moneda'); ?>"/>
<style>
/* === VARIABLES CSS === */
:root {
    --primary-color: #5664d2;
    --success-color: #1bc943;
    --warning-color: #f4a100;
    --danger-color: #e74c3c;
    --info-color: #3498db;
    --dark-color: #2c3e50;
    --light-bg: #f8f9fa;
    --card-shadow: 0 4px 20px rgba(0,0,0,0.05);
    --border-radius: 12px;
    --transition: all 0.3s ease;
}

/* === HEADER MODERN === */
.page-titles-modern {
    background: linear-gradient(135deg, var(--primary-color) 0%, #667eea 100%);
    padding: 1.5rem 2rem;
    border-radius: var(--border-radius);
    margin-bottom: 2rem;
    color: white;
    box-shadow: var(--card-shadow);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.page-titles-modern h4 {
    color: white;
    font-weight: 600;
    margin-bottom: 0.5rem;
    font-size: 1.5rem;
}

.breadcrumb-modern {
    background: rgba(255, 255, 255, 0.2);
    padding: 0.4rem 1rem;
    border-radius: 20px;
    display: inline-block;
    font-size: 0.9rem;
}

.breadcrumb-modern .breadcrumb-item, .breadcrumb-modern a {
    color: rgba(255, 255, 255, 0.9);
    text-decoration: none;
    transition: color 0.2s;
}

.breadcrumb-modern a:hover {
    color: white;
    text-decoration: underline;
}

.breadcrumb-modern .breadcrumb-item.active {
    color: white;
    font-weight: 500;
}

/* === STATS CONTAINER (GRID) === */
.stats-container {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
    gap: 1.5rem;
    margin-bottom: 2rem;
}

.stat-card {
    background: white;
    border-radius: var(--border-radius);
    padding: 1.5rem;
    box-shadow: var(--card-shadow);
    display: flex;
    align-items: center;
    border-bottom: 4px solid transparent;
    transition: var(--transition);
}

.stat-card:hover {
    transform: translateY(-5px);
    box-shadow: 0 8px 15px rgba(0,0,0,0.1);
}

.stat-icon-wrapper {
    width: 60px;
    height: 60px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.5rem;
    margin-right: 1.5rem;
    flex-shrink: 0;
}

.stat-content h2 {
    font-size: 1.8rem;
    font-weight: 700;
    margin-bottom: 0;
    color: #333;
}

.stat-content h6 {
    color: #8898aa;
    font-size: 0.85rem;
    margin-bottom: 0;
    font-weight: 500;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

/* Variantes de Stats */
.stat-warning { border-color: var(--warning-color); }
.stat-warning .stat-icon-wrapper { background: rgba(244, 161, 0, 0.1); color: var(--warning-color); }

.stat-success { border-color: var(--success-color); }
.stat-success .stat-icon-wrapper { background: rgba(27, 201, 67, 0.1); color: var(--success-color); }

.stat-info { border-color: var(--info-color); }
.stat-info .stat-icon-wrapper { background: rgba(52, 152, 219, 0.1); color: var(--info-color); }

.stat-danger { border-color: var(--danger-color); }
.stat-danger .stat-icon-wrapper { background: rgba(231, 76, 60, 0.1); color: var(--danger-color); }

/* === FILTERS CARD === */
.filters-card {
    background: white;
    border-radius: var(--border-radius);
    padding: 1.5rem;
    box-shadow: var(--card-shadow);
    margin-bottom: 2rem;
}

.form-group-modern label {
    font-size: 0.85rem;
    font-weight: 600;
    color: #555;
    margin-bottom: 0.5rem;
    display: block;
}

.form-control-modern {
    border: 1px solid #e0e0e0;
    border-radius: 8px;
    padding: 0.6rem 1rem;
    font-size: 0.95rem;
    height: 45px;
    display: flex;
    align-items: center;
}

/* === TABLE MODERN === */
.table-card {
    background: white;
    border-radius: var(--border-radius);
    box-shadow: var(--card-shadow);
    overflow: hidden;
}

.table-responsive-modern {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

.table-modern {
    margin-bottom: 0;
}

.table-modern thead {
    background: linear-gradient(135deg, #667eea 0%, var(--primary-color) 100%);
    color: white;
}

.table-modern thead th {
    border: none;
    font-weight: 600;
    text-transform: uppercase;
    font-size: 0.75rem;
    letter-spacing: 0.5px;
    padding: 1rem;
    white-space: nowrap;
}

.table-modern tbody tr {
    transition: var(--transition);
    border-bottom: 1px solid #f1f3f5;
}

.table-modern tbody tr:hover {
    background-color: #f8f9fa;
    transform: scale(1.01);
}

.table-modern tbody td {
    padding: 1rem;
    vertical-align: middle;
}

/* === SCROLLBAR PERSONALIZADO === */
.table-responsive-modern::-webkit-scrollbar {
    height: 8px;
}

.table-responsive-modern::-webkit-scrollbar-track {
    background: #f1f1f1;
    border-radius: 10px;
}

.table-responsive-modern::-webkit-scrollbar-thumb {
    background: var(--primary-color);
    border-radius: 10px;
}

.table-responsive-modern::-webkit-scrollbar-thumb:hover {
    background: #667eea;
}

.animate-in { animation: fadeInUp 0.6s ease; }
@keyframes fadeInUp {
    from { opacity: 0; transform: translateY(20px); }
    to { opacity: 1; transform: translateY(0); }
}

/* === RESPONSIVE STATS (MOBILE) === */
@media (max-width: 767px) {
    .stats-container {
        grid-template-columns: repeat(2, 1fr);
        gap: 10px;
    }
    .stat-card {
        padding: 15px;
        flex-direction: column;
        text-align: center;
        align-items: center;
        justify-content: center;
    }
    .stat-icon-wrapper {
        margin-right: 0;
        margin-bottom: 10px;
        width: 45px; 
        height: 45px;
        font-size: 1.1rem;
    }
    .stat-content h2 {
        font-size: 1.1rem;
    }
    .stat-content h6 {
        font-size: 0.7rem;
    }
    
    /* Header Mobile Improvements */
    .page-titles-modern {
        flex-direction: column;
        text-align: center;
        padding: 1.5rem 1rem;
    }
    .page-titles-modern > div {
        width: 100%;
        margin-bottom: 1rem;
    }
    .page-titles-modern .btn-group {
        width: 100%;
        margin-bottom: 0;
    }
    .page-titles-modern .btn {
        flex: 1;
    }
}
</style>

<!-- HEADER -->
<div class="page-titles-modern animate-in">
    <div>
        <h4><i class="fas fa-motorcycle mr-2"></i>Ventas Delivery</h4>
        <div class="breadcrumb-modern">
            <a href="<?php echo URL; ?>informe" class="breadcrumb-item"><i class="fas fa-home mr-1"></i>Informe</a>
            <span class="breadcrumb-item">/</span>
            <span class="breadcrumb-item">Ventas</span>
            <span class="breadcrumb-item active">Delivery</span>
        </div>
    </div>
    <div class="btn-group">
        <button type="button" class="btn btn-light dropdown-toggle font-weight-bold shadow-sm" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
            <i class="fas fa-download mr-1"></i> Exportar
        </button>
        <div class="dropdown-menu dropdown-menu-right">
            <a class="dropdown-item" href="javascript:void(0);" id="excel"><i class="fas fa-file-excel text-success mr-2"></i> Excel</a>
            <a class="dropdown-item" href="javascript:void(0);" id="pdf"><i class="fas fa-file-pdf text-danger mr-2"></i> PDF</a>
        </div>
    </div>
</div>

<div class="wrapper wrapper-content animated fadeIn">
    <!-- STATS GRID -->
    <div class="stats-container animate-in">
        <!-- N° Operaciones -->
        <div class="stat-card stat-info">
            <div class="stat-icon-wrapper">
                <i class="fas fa-clipboard-check"></i>
            </div>
            <div class="stat-content">
                <h2 class="ventas-operaciones">0</h2>
                <h6>N° Operaciones</h6>
            </div>
        </div>
        <!-- Total Comisiones -->
        <div class="stat-card stat-warning">
            <div class="stat-icon-wrapper">
                <i class="fas fa-percent"></i>
            </div>
            <div class="stat-content">
                <h2 class="ventas-comision">0.00</h2>
                <h6>Total Comisiones</h6>
            </div>
        </div>
        <!-- Total Ventas -->
        <div class="stat-card stat-success">
            <div class="stat-icon-wrapper">
                <i class="fas fa-dollar-sign"></i>
            </div>
            <div class="stat-content">
                <h2 class="ventas-total">0.00</h2>
                <h6>Total Ventas</h6>
            </div>
        </div>
    </div>

    <!-- FILTERS -->
    <div class="filters-card animate-in">
        <div class="row align-items-end">
            <!-- Rango Fechas -->
            <div class="col-lg-3 col-md-6 mb-3">
                <div class="form-group-modern">
                    <label>Rango de Fechas</label>
                    <div class="input-group">
                        <input type="text" class="form-control form-control-modern text-center" name="start" id="start" value="<?php echo '01-'.$fechaa; ?>" autocomplete="off" placeholder="Inicio"/>
                        <div class="input-group-append">
                             <span class="input-group-text bg-light border-0">al</span>
                        </div>
                        <input type="text" class="form-control form-control-modern text-center" name="end" id="end" value="<?php echo $fecha; ?>" autocomplete="off" placeholder="Fin"/>
                    </div>
                </div>
            </div>
            <!-- Tipo Entrega -->
            <div class="col-lg-3 col-md-6 mb-3">
                 <div class="form-group-modern">
                    <label>Tipo Entrega</label>
                    <select class="selectpicker form-control" name="filtro_tipo_entrega" id="filtro_tipo_entrega" data-style="btn-white border" data-live-search="true" autocomplete="off" data-size="5" title="Mostrar Todo">
                        <option value="%" selected>Mostrar Todo</option>
                        <optgroup>
                            <option value="1">A DOMICILIO</option>
                            <option value="2">POR RECOGER</option>
                        </optgroup>
                    </select>
                </div>
            </div>
             <!-- Repartidor -->
             <div class="col-lg-3 col-md-6 mb-3">
                 <div class="form-group-modern">
                    <label>Repartidor</label>
                    <select class="selectpicker form-control" name="filtro_repartidor" id="filtro_repartidor" data-style="btn-white border" data-live-search="true" autocomplete="off" data-size="5" title="Mostrar Todo">
                        <option value="%" active>Mostrar Todo</option>
                        <optgroup>
                            <?php foreach($this->Repartidor as $key => $value): ?>
                                <option value="<?php echo $value['id_usu']; ?>"><?php echo $value['ape_paterno'].' '.$value['ape_materno'].' '.$value['nombres']; ?></option>
                            <?php endforeach; ?>
                        </optgroup>
                        <?php if(Session::get('opc_01') == 1) { ?>
                        <optgroup>
                            <option value="2222">DIDI FOOD</option>
                            <!-- <option value="3333">UBER</option> -->
                            <option value="4444">PEDIDOS YA</option>
                        </optgroup>
                        <?php } ?>
                    </select>
                </div>
            </div>
        </div>
    </div>

    <!-- TABLE -->
    <div class="table-card animate-in">
        <div class="table-responsive-modern">
            <table id="table" class="table table-modern" width="100%">
                 <thead>
                    <tr>
                        <th width="10%">Fecha</th>
                        <th width="10%">Caja</th>
                        <th width="20%">Cliente</th>
                        <th width="20%">Repartidor</th>
                        <th width="15%">Documento</th>                               
                        <th class="text-right" width="10%">Entrega</th>
                        <th class="text-right" width="10%">Comisión</th>
                        <th class="text-right" width="10%">Total</th>
                        <th class="text-right" width="5%">Opciones</th>
                    </tr>
                </thead>
                <tbody class="tb-st"></tbody>
            </table>
        </div>
    </div>
</div>
<div class="modal inmodal" id="detalle" tabindex="-1" role="dialog" aria-hidden="true" data-backdrop="static" data-keyboard="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content animated bounceInRight">
            <div class="modal-header" style="background: linear-gradient(135deg, var(--primary-color) 0%, #667eea 100%); color: white; border-radius: 12px 12px 0 0;">
                <h5 class="modal-title title-d" style="font-weight: 600;">Detalle</h5>
                <button type="button" class="close text-white" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Cerrar</span></button>
            </div>
            <div class="modal-body p-0">
                <div class="table-responsive">
                    <table class="table table-modern m-b-0">
                        <thead class="table-head">
                            <tr>
                                <th width="10%">Cant.</th>
                                <th width="60%">Producto</th>
                                <th width="15%">P.U.</th>
                                <th width="15%" class="text-right">Total</th>
                            </tr>
                        </thead>
                    </table>
                    <div class="table-responsive scroll_detalle">
                        <table class="table table-modern table-hover">
                            <tbody class="tb-st" id="lista_pedidos"></tbody>
                        </table>
                    </div>
                    <table class="table m-b-0">
                        <tfooter>
                            <tr>
                                <td width="85%" class="text-right">Total consumido:</td>
                                <td class="total-consumido text-right"></td>
                            </tr>
                                <td width="85%" class="text-right text-danger">Descuento:</td>
                                <td class="total-descuento text-right"></td></tr>
                            <tr>
                                <td width="80%" class="text-right text-success">Total facturado:</td>
                                <td class="total-facturado text-right"></td></tr>
                        </tfooter>
                    </table>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Cerrar</button>
            </div>
        </div>
    </div>
</div>
