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

// Verifica se o usuário está logado
if (!isset($_SESSION['user_id'])) {
    header("Location: login.php");
    exit;
}

$user_id = $_SESSION['user_id'];

// Busca nome do usuário
$stmt = $pdo->prepare("SELECT name FROM users WHERE id = ?");
$stmt->execute([$user_id]);
$user = $stmt->fetch();

// Contagem de clientes cadastrados
$stmt = $pdo->prepare("SELECT COUNT(*) FROM client_customers WHERE user_id = ?");
$stmt->execute([$user_id]);
$total_clientes = $stmt->fetchColumn();

// Contagem de fiados cadastrados
$stmt = $pdo->prepare("SELECT COUNT(*) FROM fiados WHERE user_id = ?");
$stmt->execute([$user_id]);
$total_fiados = $stmt->fetchColumn();

// Valor total a receber (parcelas em aberto)
// --- CORREÇÃO APLICADA AQUI ---
// A consulta agora verifica se a coluna 'paid_at' é NULA para considerar uma parcela como "em aberto".
$stmt = $pdo->prepare("
    SELECT SUM(amount)
    FROM installments i
    JOIN fiados f ON f.id = i.fiado_id
    WHERE f.user_id = ? AND i.paid_at IS NULL
");
$stmt->execute([$user_id]);
$total_receber = $stmt->fetchColumn();
if ($total_receber === null) $total_receber = 0.00;
?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <title>Painel - Fiado Fácil</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- Google Fonts -->
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&display=swap" rel="stylesheet">
    <!-- Font Awesome -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/all.min.css">
    <style>
        :root {
            --primary-color: #1876F2;
            --primary-dark: #1356b3;
            --primary-light: #eaf4ff;
            --secondary-color: #1bbf5c;
            --background-color: #f8fafc;
            --surface-color: #ffffff;
            --text-primary: #1e293b;
            --text-secondary: #64748b;
            --border-color: #e2e8f0;
            --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05 );
            --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
            --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1);
            --shadow-xl: 0 20px 25px -5px rgb(0 0 0 / 0.1), 0 8px 10px -6px rgb(0 0 0 / 0.1);
            --gradient-primary: linear-gradient(135deg, #1876F2 0%, #1356b3 100%);
            --gradient-success: linear-gradient(135deg, #1bbf5c 0%, #16a34a 100%);
            --gradient-background: linear-gradient(135deg, #f8fafc 0%, #e2e8f0 100%);
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            background: var(--gradient-background);
            color: var(--text-primary);
            line-height: 1.6;
            min-height: 100vh;
        }

        .sidebar {
            position: fixed;
            left: 0;
            top: 0;
            bottom: 0;
            width: 280px;
            background: var(--surface-color);
            backdrop-filter: blur(20px);
            border-right: 1px solid var(--border-color);
            display: flex;
            flex-direction: column;
            z-index: 100;
            box-shadow: var(--shadow-xl);
            transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        }

        .sidebar::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 120px;
            background: var(--gradient-primary);
            opacity: 0.05;
            border-radius: 0 0 24px 0;
        }

        .sidebar .logo {
            position: relative;
            padding: 32px 24px;
            font-size: 1.75rem;
            font-weight: 800;
            color: var(--primary-color);
            display: flex;
            align-items: center;
            gap: 12px;
            border-bottom: 1px solid var(--border-color);
        }

        .sidebar .logo i {
            font-size: 2rem;
            background: var(--gradient-primary);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }

        .sidebar nav {
            flex: 1;
            padding: 24px 16px;
        }

        .sidebar nav a {
            display: flex;
            align-items: center;
            gap: 16px;
            color: var(--text-secondary);
            text-decoration: none;
            font-size: 0.95rem;
            font-weight: 500;
            padding: 14px 20px;
            margin: 4px 0;
            border-radius: 12px;
            transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
            position: relative;
            overflow: hidden;
        }

        .sidebar nav a::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: var(--gradient-primary);
            opacity: 0;
            transition: opacity 0.3s ease;
            border-radius: 12px;
        }

        .sidebar nav a i {
            font-size: 1.1rem;
            width: 20px;
            text-align: center;
            position: relative;
            z-index: 1;
        }

        .sidebar nav a span {
            position: relative;
            z-index: 1;
        }

        .sidebar nav a.active,
        .sidebar nav a:hover {
            color: white;
            transform: translateX(4px);
            box-shadow: var(--shadow-md);
        }

        .sidebar nav a.active::before,
        .sidebar nav a:hover::before {
            opacity: 1;
        }

        .sidebar .bottom {
            padding: 24px;
            border-top: 1px solid var(--border-color);
        }

        .sidebar .logout-link {
            display: flex;
            align-items: center;
            gap: 12px;
            color: #ef4444;
            font-weight: 600;
            font-size: 0.95rem;
            text-decoration: none;
            padding: 12px 16px;
            border-radius: 12px;
            transition: all 0.3s ease;
        }

        .sidebar .logout-link:hover {
            background: #fef2f2;
            transform: translateX(4px);
        }

        .main-content {
            margin-left: 280px;
            padding: 32px;
            min-height: 100vh;
            animation: fadeIn 0.6s ease-out;
        }

        @keyframes fadeIn {
            from {
                opacity: 0;
                transform: translateY(20px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        .dashboard-header {
            display: flex;
            justify-content: space-between;
            align-items: flex-start;
            gap: 24px;
            margin-bottom: 32px;
            padding-bottom: 24px;
            border-bottom: 1px solid var(--border-color);
        }

        .greeting {
            font-size: 2rem;
            font-weight: 700;
            color: var(--text-primary);
            display: flex;
            align-items: center;
            gap: 12px;
        }

        .greeting i {
            color: var(--primary-color);
            font-size: 1.75rem;
        }

        .top-actions {
            display: flex;
            align-items: center;
            gap: 16px;
        }

        .support-dropdown {
            position: relative;
            display: inline-block;
        }

        .support-btn, .video-btn {
            display: flex;
            align-items: center;
            gap: 8px;
            background: var(--surface-color);
            color: var(--primary-color);
            font-weight: 600;
            font-size: 0.95rem;
            border: 2px solid var(--border-color);
            border-radius: 12px;
            padding: 12px 20px;
            text-decoration: none;
            cursor: pointer;
            transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
            box-shadow: var(--shadow-sm);
            position: relative;
            overflow: hidden;
        }

        .support-btn::before, .video-btn::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: var(--gradient-primary);
            opacity: 0;
            transition: opacity 0.3s ease;
        }

        .support-btn:hover, .support-btn:focus,
        .video-btn:hover, .video-btn:focus {
            color: white;
            border-color: var(--primary-color);
            transform: translateY(-2px);
            box-shadow: var(--shadow-lg);
        }

        .support-btn:hover::before, .support-btn:focus::before,
        .video-btn:hover::before, .video-btn:focus::before {
            opacity: 1;
        }

        .support-btn > *, .video-btn > * {
            position: relative;
            z-index: 1;
        }

        .support-dropdown-content {
            display: none;
            position: absolute;
            right: 0;
            background: var(--surface-color);
            min-width: 200px;
            box-shadow: var(--shadow-xl);
            border-radius: 16px;
            z-index: 15;
            overflow: hidden;
            margin-top: 8px;
            border: 1px solid var(--border-color);
            animation: slideDown 0.3s ease-out;
        }

        @keyframes slideDown {
            from {
                opacity: 0;
                transform: translateY(-10px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        .support-dropdown-content a {
            color: var(--text-primary);
            padding: 16px 20px;
            text-decoration: none;
            display: flex;
            align-items: center;
            gap: 12px;
            font-weight: 500;
            font-size: 0.95rem;
            transition: all 0.3s ease;
        }

        .support-dropdown-content a:hover {
            background: var(--primary-light);
            color: var(--primary-color);
        }

        .support-dropdown:focus-within .support-dropdown-content,
        .support-dropdown.open .support-dropdown-content {
            display: block;
        }

        .cards-row {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
            gap: 24px;
            margin-bottom: 40px;
        }

        .card {
            background: var(--surface-color);
            border-radius: 20px;
            box-shadow: var(--shadow-md);
            padding: 32px;
            display: flex;
            align-items: center;
            gap: 24px;
            transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
            border: 1px solid var(--border-color);
            position: relative;
            overflow: hidden;
            animation: slideUp 0.6s ease-out;
            animation-fill-mode: both;
        }

        .card:nth-child(1) { animation-delay: 0.1s; }
        .card:nth-child(2) { animation-delay: 0.2s; }
        .card:nth-child(3) { animation-delay: 0.3s; }

        @keyframes slideUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        .card::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 4px;
            background: var(--gradient-primary);
        }

        .card:hover {
            transform: translateY(-8px);
            box-shadow: var(--shadow-xl);
        }

        .card .icon-wrapper {
            width: 64px;
            height: 64px;
            border-radius: 16px;
            display: flex;
            align-items: center;
            justify-content: center;
            background: var(--primary-light);
            flex-shrink: 0;
        }

        .card i {
            font-size: 1.75rem;
            color: var(--primary-color);
        }

        .card:last-child .icon-wrapper {
            background: #f0fdf4;
        }

        .card:last-child i {
            color: var(--secondary-color);
        }

        .card-content {
            flex: 1;
        }

        .card-title {
            font-size: 0.95rem;
            font-weight: 600;
            color: var(--text-secondary);
            margin-bottom: 8px;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }

        .card-value {
            font-size: 2rem;
            font-weight: 800;
            color: var(--text-primary);
            line-height: 1.2;
        }

        .card-value.valor {
            color: var(--secondary-color);
            font-size: 2.25rem;
        }

        .actions-row {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 20px;
            margin-bottom: 32px;
        }

        .action-btn {
            background: var(--gradient-primary);
            color: white;
            font-weight: 600;
            font-size: 1rem;
            border: none;
            border-radius: 16px;
            padding: 20px 32px;
            text-decoration: none;
            text-align: center;
            transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
            box-shadow: var(--shadow-md);
            cursor: pointer;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 12px;
            position: relative;
            overflow: hidden;
        }

        .action-btn::before {
            content: '';
            position: absolute;
            top: 0;
            left: -100%;
            width: 100%;
            height: 100%;
            background: linear-gradient(90deg, transparent, rgba(255,255,255,0.2), transparent);
            transition: left 0.5s;
        }

        .action-btn:hover {
            transform: translateY(-4px);
            box-shadow: var(--shadow-xl);
        }

        .action-btn:hover::before {
            left: 100%;
        }

        .action-btn i {
            font-size: 1.1rem;
        }

        /* Responsividade melhorada */
        @media (max-width: 1200px) {
            .main-content {
                padding: 24px;
            }
            
            .cards-row {
                grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
            }
        }

        @media (max-width: 768px) {
            .sidebar {
                position: fixed;
                transform: translateX(-100%);
                width: 100%;
                max-width: 320px;
                z-index: 1000;
            }

            .sidebar.open {
                transform: translateX(0);
            }

            .main-content {
                margin-left: 0;
                padding: 20px 16px;
            }

            .dashboard-header {
                flex-direction: column;
                align-items: stretch;
                gap: 20px;
            }

            .greeting {
                font-size: 1.5rem;
            }

            .top-actions {
                flex-wrap: wrap;
                justify-content: center;
            }

            .cards-row {
                grid-template-columns: 1fr;
                gap: 16px;
            }

            .card {
                padding: 24px;
            }

            .actions-row {
                grid-template-columns: 1fr;
                gap: 16px;
            }

            .action-btn {
                padding: 16px 24px;
            }
        }

        /* Menu toggle para mobile */
        .menu-toggle {
            display: none;
            position: fixed;
            top: 20px;
            left: 20px;
            z-index: 1001;
            background: var(--surface-color);
            border: 2px solid var(--border-color);
            border-radius: 12px;
            padding: 12px;
            cursor: pointer;
            box-shadow: var(--shadow-md);
            color: var(--primary-color);
            font-size: 1.2rem;
        }

        @media (max-width: 768px) {
            .menu-toggle {
                display: block;
            }
        }

        /* Overlay para mobile */
        .overlay {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: rgba(0, 0, 0, 0.5);
            z-index: 999;
        }

        @media (max-width: 768px) {
            .overlay.active {
                display: block;
            }
        }
    </style>
</head>
<body>
    <button class="menu-toggle" id="menuToggle">
        <i class="fa-solid fa-bars"></i>
    </button>
    
    <div class="overlay" id="overlay"></div>
    
    <aside class="sidebar" id="sidebar">
        <div class="logo">
            <i class="fa-solid fa-wallet"></i>
            <span>Fiado Fácil</span>
			
        </div>
        <nav>
            <a href="dashboard.php" class="active">
                <i class="fa-solid fa-house"></i>
                <span>Painel</span>
            </a>
            <a href="clientes.php">
                <i class="fa-solid fa-users"></i>
                <span>Clientes</span>
            </a>
            <a href="fiados.php">
                <i class="fa-solid fa-file-invoice-dollar"></i>
                <span>Fiados</span>
            </a>
            <a href="carne.php">
                <i class="fa-solid fa-file-pdf"></i>
                <span>Carnê PDF</span>
            </a>
            <a href="relatorios.php">
                <i class="fa-solid fa-chart-line"></i>
                <span>Relatórios</span>
            </a>
            <a href="configuracao.php">
                <i class="fa-solid fa-cog"></i>
                <span>Configuração</span>
            </a>
        </nav>
        <div class="bottom">
            <a href="logout.php" class="logout-link">
                <i class="fa-solid fa-right-from-bracket"></i>
                <span>Sair</span>
            </a>
        </div>
    </aside>
    
    <div class="main-content">
        <div class="dashboard-header">
            <div class="greeting">
                <i class="fa-solid fa-user"></i>
                Bem-vindo, <?=htmlspecialchars($user['name'])?> ao Zorion Sistemas
            </div>
            <div class="top-actions">
                <div class="support-dropdown" tabindex="0">
                    <button class="support-btn" id="supportBtn" type="button">
                        <i class="fa-solid fa-headset"></i>
                        <span>Suporte</span>
                        <i class="fa-solid fa-angle-down" style="font-size:0.85em;"></i>
                    </button>
                    <div class="support-dropdown-content" id="supportDropdown">
                        <a href="https://wa.me/5511999999999?text=Ol%C3%A1%2C%20preciso%20de%20suporte%20no%20Fiado%20F%C3%A1cil" target="_blank">
                            <i class="fa-brands fa-whatsapp" style="color:#25d366;"></i>
                            WhatsApp
                        </a>
                        <a href="mailto:suporte@seudominio.com?subject=Suporte%20Fiado%20F%C3%A1cil">
                            <i class="fa-solid fa-envelope" style="color:#1876F2;"></i>
                            E-mail
                        </a>
                    </div>
                </div>
                <a href="https://www.youtube.com/watch?v=U2l5dJvCn6w" class="video-btn" target="_blank" title="Assistir vídeo aula">
                    <i class="fa-brands fa-youtube"></i>
                    <span>Vídeo Aula</span>
                </a>
            </div>
        </div>
        
        <div class="cards-row">
            <div class="card">
                <div class="icon-wrapper">
                    <i class="fa-solid fa-users"></i>
                </div>
                <div class="card-content">
                    <div class="card-title">Meus Clientes</div>
                    <div class="card-value"><?= $total_clientes ?></div>
                </div>
            </div>
            <div class="card">
                <div class="icon-wrapper">
                    <i class="fa-solid fa-file-invoice-dollar"></i>
                </div>
                <div class="card-content">
                    <div class="card-title">Fiados Registrados</div>
                    <div class="card-value"><?= $total_fiados ?></div>
                </div>
            </div>
            <div class="card">
                <div class="icon-wrapper">
                    <i class="fa-solid fa-money-bill-wave"></i>
                </div>
                <div class="card-content">
                    <div class="card-title">Valor Total a Receber</div>
                    <div class="card-value valor">R$ <?= number_format($total_receber,2,',','.' ) ?></div>
                </div>
            </div>
        </div>
        
        <div class="actions-row">
            <a href="clientes.php" class="action-btn">
                <i class="fa-solid fa-users"></i>
                Gerenciar Clientes
            </a>
            <a href="fiados.php" class="action-btn">
                <i class="fa-solid fa-file-invoice-dollar"></i>
                Gerenciar Fiados
            </a>
            <a href="carne.php" class="action-btn">
                <i class="fa-solid fa-file-pdf"></i>
                Gerar Carnê PDF
            </a>
            <a href="relatorios.php" class="action-btn">
                <i class="fa-solid fa-chart-line"></i>
                Relatórios
            </a>
        </div>
    </div>
    
    <script>
        // Suporte Dropdown
        document.addEventListener('click', function(event) {
            var btn = document.getElementById('supportBtn');
            var dd = document.getElementById('supportDropdown');
            if (btn && dd) {
                if (btn.contains(event.target)) {
                    dd.parentElement.classList.toggle('open');
                } else if (!dd.contains(event.target)) {
                    dd.parentElement.classList.remove('open');
                }
            }
        });

        document.addEventListener('keydown', function(event) {
            if (event.key === 'Escape') {
                var dd = document.getElementById('supportDropdown');
                if (dd) dd.parentElement.classList.remove('open');
            }
        });

        // Menu mobile
        const menuToggle = document.getElementById('menuToggle');
        const sidebar = document.getElementById('sidebar');
        const overlay = document.getElementById('overlay');

        menuToggle.addEventListener('click', function() {
            sidebar.classList.toggle('open');
            overlay.classList.toggle('active');
        });

        overlay.addEventListener('click', function() {
            sidebar.classList.remove('open');
            overlay.classList.remove('active');
        });

        // Fechar menu ao clicar em um link (mobile)
        const sidebarLinks = sidebar.querySelectorAll('nav a, .logout-link');
        sidebarLinks.forEach(link => {
            link.addEventListener('click', function() {
                if (window.innerWidth <= 768) {
                    sidebar.classList.remove('open');
                    overlay.classList.remove('active');
                }
            });
        });

        // Animações de entrada
        window.addEventListener('load', function() {
            document.body.style.opacity = '1';
        });
    </script>
</body>
</html>
