<?php $__env->startSection('title', 'Dashboard — SINAGEM'); ?>

<?php $__env->startSection('page-title', 'Dashboard'); ?>
<?php $__env->startSection('breadcrumb'); ?>
    <span class="bc-item">Dashboard</span>
    <i class="fas fa-chevron-right"></i>
    <span class="bc-current">Principal</span>
<?php $__env->stopSection(); ?>

<?php $__env->startPush('styles'); ?>
<style>
    /* Chart.js canvas */
    .chart-canvas {
        display: block;
        width: 100% !important;
        max-height: 280px;
    }

    /* Animações */
    @keyframes fadeInUp {
        from {
            opacity: 0;
            transform: translateY(20px);
        }
        to {
            opacity: 1;
            transform: translateY(0);
        }
    }

    .fade-in-up {
        animation: fadeInUp 0.4s ease forwards;
        opacity: 0;
    }

    .delay-1 { animation-delay: 0.05s; }
    .delay-2 { animation-delay: 0.1s; }
    .delay-3 { animation-delay: 0.15s; }
    .delay-4 { animation-delay: 0.2s; }
</style>
<?php $__env->stopPush(); ?>

<?php $__env->startSection('content'); ?>
<div class="sinagem-main">

    
    <div class="kpi-grid">
        <div class="kpi-card green fade-in-up delay-1">
            <div class="kpi-info">
                <h3>Receita de Hoje</h3>
                <div class="kpi-value">KZ <?php echo e(number_format($receitaHoje ?? 0, 2, ',', '.')); ?></div>
                <small class="text-muted">Últimas 24 horas</small>
            </div>
            <div class="kpi-icon">
                <i class="fas fa-money-bill-wave"></i>
            </div>
        </div>

        <div class="kpi-card dark fade-in-up delay-2">
            <div class="kpi-info">
                <h3>Taxa de Ocupação</h3>
                <div class="kpi-value"><?php echo e($taxaOcupacao ?? '82'); ?>%</div>
                <small class="text-muted">Bancadas ocupadas</small>
            </div>
            <div class="kpi-icon">
                <i class="fas fa-chart-line"></i>
            </div>
        </div>

        <div class="kpi-card orange fade-in-up delay-3">
            <div class="kpi-info">
                <h3>Taxa de Inadimplência</h3>
                <div class="kpi-value"><?php echo e($taxaInadimplencia ?? '35'); ?>%</div>
                <small class="text-muted">Pagamentos em atraso</small>
            </div>
            <div class="kpi-icon">
                <i class="fas fa-exclamation-triangle"></i>
            </div>
        </div>

        <div class="kpi-card yellow fade-in-up delay-4">
            <div class="kpi-info">
                <h3>Fiscais Activos</h3>
                <div class="kpi-value"><?php echo e($fiscaisActivos ?? 12); ?></div>
                <small class="text-muted">Em serviço</small>
            </div>
            <div class="kpi-icon">
                <i class="fas fa-user-shield"></i>
            </div>
        </div>
    </div>

    
    <div class="row g-3 mb-4">
        
        <div class="col-12 col-lg-6">
            <div class="s-card fade-in-up delay-2">
                <div class="s-card-header">
                    <div class="s-card-title">
                        <i class="fas fa-chart-pie"></i>
                        Operacional
                    </div>
                </div>
                <div class="s-card-body">
                    <h6 class="fw-600 mb-3 text-secondary">Ocupação por Sector</h6>
                    <div class="chart-container" style="height: 280px;">
                        <canvas id="chartOcupacaoSector" class="chart-canvas"></canvas>
                    </div>
                </div>
            </div>
        </div>

        
        <div class="col-12 col-lg-6">
            <div class="s-card fade-in-up delay-3">
                <div class="s-card-header">
                    <div class="s-card-title">
                        <i class="fas fa-chart-bar"></i>
                        Financeiro
                    </div>
                    <div class="d-flex align-items-center gap-2">
                        <input type="date" id="date-selector-input"
                               class="form-control form-control-sm"
                               style="width: auto; display: none;">
                        <button id="date-selector-btn" class="date-filter">
                            <i class="fas fa-calendar-alt"></i>
                            Selecione a data
                        </button>
                    </div>
                </div>
                <div class="s-card-body">
                    <h6 class="fw-600 mb-3 text-secondary">Arrecadação Semanal (Previsto vs Real)</h6>
                    <div class="chart-container" style="height: 280px;">
                        <canvas id="chartArrecadacaoSemanal" class="chart-canvas"></canvas>
                    </div>
                </div>
            </div>
        </div>
    </div>

    
    <div class="row g-3">
        
        <div class="col-12 col-lg-6">
            <div class="s-card fade-in-up delay-4">
                <div class="s-card-header">
                    <div class="s-card-title">
                        <i class="fas fa-ship"></i>
                        Últimos Desembarques
                    </div>
                    <a href="<?php echo e(route('pesca.desembarques.index')); ?>" class="btn btn-outline-primary btn-sm">
                        <i class="fas fa-eye"></i> Ver todos
                    </a>
                </div>
                <div class="s-card-body p-0">
                    <div class="s-table-wrapper">
                        <table class="s-table">
                            <thead>
                                <tr>
                                    <th>Designação</th>
                                    <th>Responsável</th>
                                    <th>Pescado</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php $__empty_1 = true; $__currentLoopData = $ultimosDesembarques ?? []; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $d): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                                    <tr data-href="<?php echo e(route('pesca.desembarques.show', $d->id)); ?>" style="cursor: pointer;">
                                        <td><?php echo e($d->designacao ?? 'N/A'); ?></td>
                                        <td><?php echo e($d->responsavel ?? 'N/A'); ?></td>
                                        <td><?php echo e($d->pescado ?? 'N/A'); ?></td>
                                    </tr>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                                    <?php $__currentLoopData = [
                                        ['B/001', 'João Silva', 'Carapau (450kg)'],
                                        ['B/002', 'Manuel Costa', 'Sardinha (320kg)'],
                                        ['B/003', 'António Mendes', 'Garopa (180kg)'],
                                        ['B/004', 'Francisco Neto', 'Polvo (95kg)'],
                                        ['B/005', 'Carlos Alberto', 'Carapau (520kg)'],
                                        ['B/006', 'Paulo Jorge', 'Sardinha (280kg)'],
                                        ['B/007', 'Luis Fernando', 'Polvo (110kg)'],
                                        ['B/008', 'José Augusto', 'Carapau (390kg)'],
                                    ]; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $row): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                        <tr>
                                            <td><?php echo e($row[0]); ?></td>
                                            <td><?php echo e($row[1]); ?></td>
                                            <td><?php echo e($row[2]); ?></td>
                                        </tr>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                <?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>

        
        <div class="col-12 col-lg-6">
            <div class="s-card fade-in-up delay-5">
                <div class="s-card-header">
                    <div class="s-card-title">
                        <i class="fas fa-exclamation-triangle"></i>
                        Alertas de Maiores Bancadas Devedoras
                    </div>
                    <a href="<?php echo e(route('financas.pagamentos.index')); ?>" class="btn btn-outline-primary btn-sm">
                        <i class="fas fa-eye"></i> Ver todos
                    </a>
                </div>
                <div class="s-card-body p-0">
                    <div class="s-table-wrapper">
                        <table class="s-table">
                            <thead>
                                <tr>
                                    <th>Nome</th>
                                    <th>Status</th>
                                    <th class="text-end">Valor (AOA)</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php $__empty_1 = true; $__currentLoopData = $bancadasDevedoras ?? []; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $b): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                                    <tr>
                                        <td><?php echo e($b->nome ?? 'N/A'); ?></td>
                                        <td><span class="badge badge-danger">Atrasado</span></td>
                                        <td class="text-end fw-600 text-danger"><?php echo e(number_format($b->valor ?? 0, 2, ',', '.')); ?></td>
                                    </tr>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                                    <?php $__currentLoopData = [
                                        ['Bancada A-001', 158.500,00],
                                        ['Bancada B-023', 124.300,00],
                                        ['Bancada C-045', 98.750,00],
                                        ['Bancada D-012', 87.200,00],
                                        ['Bancada E-078', 76.400,00],
                                        ['Bancada F-034', 65.900,00],
                                        ['Bancada G-056', 54.300,00],
                                        ['Bancada H-089', 43.800,00],
                                    ]; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $b): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                        <tr>
                                            <td><?php echo e($b[0]); ?></td>
                                            <td><span class="badge badge-danger"><i class="fas fa-clock"></i> Atrasado</span></td>
                                            <td class="text-end fw-600 text-danger">KZ <?php echo e(number_format($b[1], 2, ',', '.')); ?></td>
                                        </tr>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                <?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>

</div>
<?php $__env->stopSection(); ?>

<?php $__env->startPush('scripts'); ?>
<script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.3/dist/chart.umd.min.js"></script>
<script>
(function() {
    'use strict';

    // =====================================================
    // Gráfico de Pizza: Ocupação por Sector
    // =====================================================
    const pieCtx = document.getElementById('chartOcupacaoSector');
    if (pieCtx) {
        new Chart(pieCtx, {
            type: 'pie',
            data: {
                labels: ['Comerciantes', 'Pescadores', 'Ambulantes'],
                datasets: [{
                    data: [62.5, 25, 12.5],
                    backgroundColor: ['#27ae60', '#f39c12', '#e74c3c'],
                    borderWidth: 2,
                    borderColor: '#fff',
                    hoverOffset: 8,
                }],
            },
            options: {
                responsive: true,
                maintainAspectRatio: false,
                plugins: {
                    legend: {
                        position: 'bottom',
                        labels: {
                            font: { size: 12, family: "'Inter', sans-serif" },
                            padding: 14,
                            usePointStyle: true,
                        },
                    },
                    tooltip: {
                        callbacks: {
                            label: (ctx) => {
                                const label = ctx.label || '';
                                const value = ctx.parsed || 0;
                                return `${label}: ${value}%`;
                            },
                        },
                    },
                },
            },
        });
    }

    // =====================================================
    // Gráfico de Barras: Arrecadação Semanal
    // =====================================================
    const barCtx = document.getElementById('chartArrecadacaoSemanal');
    if (barCtx) {
        const labels = ['Segunda', 'Terça', 'Quarta', 'Quinta', 'Sexta', 'Sábado'];
        const previsto = [108000, 95000, 112000, 89000, 125000, 98000];
        const real = [87500, 82000, 94500, 76000, 108000, 85000];

        new Chart(barCtx, {
            type: 'bar',
            data: {
                labels: labels,
                datasets: [
                    {
                        label: 'Previsto (KZ)',
                        data: previsto,
                        backgroundColor: 'rgba(39, 174, 96, 0.15)',
                        borderColor: '#27ae60',
                        borderWidth: 1.5,
                        borderRadius: 6,
                    },
                    {
                        label: 'Real (KZ)',
                        data: real,
                        backgroundColor: '#e03030',
                        borderColor: '#c02020',
                        borderWidth: 0,
                        borderRadius: 6,
                    },
                ],
            },
            options: {
                responsive: true,
                maintainAspectRatio: false,
                scales: {
                    x: {
                        grid: { display: false },
                        ticks: { font: { size: 11, family: "'Inter', sans-serif" } },
                    },
                    y: {
                        beginAtZero: true,
                        grid: { color: '#e5e7eb' },
                        ticks: {
                            font: { size: 11, family: "'Inter', sans-serif" },
                            callback: (value) => 'KZ ' + value.toLocaleString('pt-AO'),
                        },
                    },
                },
                plugins: {
                    legend: {
                        position: 'top',
                        align: 'end',
                        labels: {
                            font: { size: 11, family: "'Inter', sans-serif" },
                            boxWidth: 12,
                            padding: 16,
                            usePointStyle: true,
                        },
                    },
                    tooltip: {
                        callbacks: {
                            label: (ctx) => {
                                const label = ctx.dataset.label || '';
                                const value = ctx.parsed.y || 0;
                                return `${label}: KZ ${value.toLocaleString('pt-AO')}`;
                            },
                        },
                    },
                },
            },
        });
    }

    // =====================================================
    // Date Selector
    // =====================================================
    const btn = document.getElementById('date-selector-btn');
    const input = document.getElementById('date-selector-input');

    if (btn && input) {
        btn.addEventListener('click', function(e) {
            e.preventDefault();
            input.style.display = 'inline-block';
            input.focus();
            try {
                input.showPicker();
            } catch(err) {
                // Fallback para navegadores que não suportam showPicker
            }
        });

        input.addEventListener('change', function() {
            if (input.value) {
                const d = new Date(input.value);
                const formatted = d.toLocaleDateString('pt-AO', {
                    day: '2-digit',
                    month: 'short',
                    year: 'numeric'
                });
                btn.innerHTML = `<i class="fas fa-calendar-alt"></i> ${formatted}`;

                // Disparar evento para recarregar dados
                document.dispatchEvent(new CustomEvent('date-changed', {
                    detail: { date: input.value }
                }));
            }
            input.style.display = 'none';
        });

        input.addEventListener('blur', function() {
            setTimeout(() => {
                input.style.display = 'none';
            }, 200);
        });
    }

    // =====================================================
    // Table Row Click (data-href)
    // =====================================================
    document.querySelectorAll('tr[data-href]').forEach(row => {
        row.addEventListener('click', function(e) {
            if (!e.target.closest('a') && !e.target.closest('button')) {
                window.location.href = this.dataset.href;
            }
        });
    });

})();
</script>
<?php $__env->stopPush(); ?>

<?php echo $__env->make('layouts.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH /var/www/html/resources/views/admin/index.blade.php ENDPATH**/ ?>