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

$erro = '';
$fiado = null;
$parcelas = [];
$fiado_id = intval($_GET['fiado_id'] ?? 0);

if (!$fiado_id) {
    $erro = "Carnê não encontrado.";
} else {
    $stmt = $pdo->prepare("SELECT f.*, c.name as cliente_nome FROM fiados f JOIN client_customers c ON c.id=f.customer_id WHERE f.id=?");
    $stmt->execute([$fiado_id]);
    $fiado = $stmt->fetch();

    if (!$fiado) {
        $erro = "Carnê não encontrado.";
    } else {
        // Supondo que o campo para status de pagamento é 'paid' (1 = pago, 0 = não pago)
        $stmt = $pdo->prepare("SELECT * FROM installments WHERE fiado_id = ? ORDER BY due_date ASC");
        $stmt->execute([$fiado_id]);
        $parcelas = $stmt->fetchAll();
    }
}
?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <title>Carnê - 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;
            --success-color: #10b981;
            --warning-color: #f59e0b;
            --danger-color: #ef4444;
            --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, #10b981 0%, #059669 100%);
            --gradient-warning: linear-gradient(135deg, #f59e0b 0%, #d97706 100%);
            --gradient-danger: linear-gradient(135deg, #ef4444 0%, #dc2626 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);
            }
        }

        .page-header {
            display: flex;
            align-items: center;
            gap: 16px;
            margin-bottom: 32px;
            padding-bottom: 24px;
            border-bottom: 1px solid var(--border-color);
        }

        .page-header .icon-wrapper {
            width: 56px;
            height: 56px;
            border-radius: 16px;
            display: flex;
            align-items: center;
            justify-content: center;
            background: var(--primary-light);
        }

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

        .page-header h2 {
            font-size: 2rem;
            font-weight: 700;
            color: var(--text-primary);
            margin: 0;
        }

        .error-card {
            background: var(--surface-color);
            border-radius: 20px;
            box-shadow: var(--shadow-md);
            padding: 32px;
            border: 1px solid var(--border-color);
            position: relative;
            overflow: hidden;
            animation: slideUp 0.6s ease-out;
            text-align: center;
            max-width: 600px;
        }

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

        .error-card .error-icon {
            width: 80px;
            height: 80px;
            border-radius: 50%;
            background: #fef2f2;
            display: flex;
            align-items: center;
            justify-content: center;
            margin: 0 auto 24px;
        }

        .error-card .error-icon i {
            font-size: 2rem;
            color: var(--danger-color);
        }

        .error-card h3 {
            font-size: 1.5rem;
            font-weight: 700;
            color: var(--danger-color);
            margin-bottom: 16px;
        }

        .error-card p {
            color: var(--text-secondary);
            font-size: 1rem;
            line-height: 1.6;
        }

        .error-card a {
            color: var(--primary-color);
            font-weight: 600;
            text-decoration: none;
        }

        .error-card a:hover {
            text-decoration: underline;
        }

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

        .carne-info {
            background: var(--surface-color);
            border-radius: 20px;
            box-shadow: var(--shadow-md);
            padding: 32px;
            border: 1px solid var(--border-color);
            position: relative;
            overflow: hidden;
            animation: slideUp 0.6s ease-out;
            animation-delay: 0.1s;
            animation-fill-mode: both;
            margin-bottom: 32px;
        }

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

        .carne-info h3 {
            font-size: 1.25rem;
            font-weight: 700;
            color: var(--text-primary);
            margin-bottom: 24px;
            display: flex;
            align-items: center;
            gap: 12px;
        }

        .carne-info h3 i {
            color: var(--primary-color);
        }

        .info-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 20px;
        }

        .info-item {
            display: flex;
            flex-direction: column;
            gap: 8px;
        }

        .info-label {
            font-size: 0.9rem;
            font-weight: 600;
            color: var(--text-secondary);
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }

        .info-value {
            font-size: 1.1rem;
            font-weight: 600;
            color: var(--text-primary);
        }

        .parcelas-section {
            background: var(--surface-color);
            border-radius: 20px;
            box-shadow: var(--shadow-md);
            border: 1px solid var(--border-color);
            overflow: hidden;
            animation: slideUp 0.6s ease-out;
            animation-delay: 0.2s;
            animation-fill-mode: both;
            position: relative;
            margin-bottom: 32px;
        }

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

        .section-header {
            padding: 24px 32px;
            border-bottom: 1px solid var(--border-color);
            background: var(--primary-light);
        }

        .section-header h3 {
            font-size: 1.25rem;
            font-weight: 700;
            color: var(--primary-color);
            margin: 0;
            display: flex;
            align-items: center;
            gap: 12px;
        }

        .parcelas-list {
            padding: 24px 32px;
        }

        .parcela-item {
            display: flex;
            align-items: center;
            justify-content: space-between;
            padding: 20px;
            margin-bottom: 16px;
            background: var(--background-color);
            border-radius: 12px;
            border: 1px solid var(--border-color);
            transition: all 0.3s ease;
            position: relative;
            overflow: hidden;
        }

        .parcela-item:hover {
            transform: translateY(-2px);
            box-shadow: var(--shadow-md);
        }

        .parcela-item:last-child {
            margin-bottom: 0;
        }

        .parcela-info {
            display: flex;
            align-items: center;
            gap: 16px;
            flex: 1;
        }

        .parcela-icon {
            width: 48px;
            height: 48px;
            border-radius: 12px;
            background: var(--primary-light);
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .parcela-icon i {
            font-size: 1.2rem;
            color: var(--primary-color);
        }

        .parcela-details {
            flex: 1;
        }

        .parcela-date {
            font-size: 1rem;
            font-weight: 600;
            color: var(--text-primary);
            margin-bottom: 4px;
        }

        .parcela-value {
            font-size: 0.9rem;
            color: var(--text-secondary);
        }

        .parcela-status {
            padding: 8px 16px;
            border-radius: 20px;
            font-size: 0.85rem;
            font-weight: 600;
            text-transform: uppercase;
            letter-spacing: 0.5px;
            display: flex;
            align-items: center;
            gap: 6px;
        }

        .status-pago {
            background: #dcfce7;
            color: var(--success-color);
        }

        .status-pendente {
            background: #fef3c7;
            color: var(--warning-color);
        }

        .carne-actions {
            animation: slideUp 0.6s ease-out;
            animation-delay: 0.3s;
            animation-fill-mode: both;
        }

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

        .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(-2px);
            box-shadow: var(--shadow-lg);
        }

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

        /* 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;
        }

        /* 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;
        }

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

            .overlay.active {
                display: block;
            }

            .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;
            }

            .page-header {
                flex-direction: column;
                align-items: flex-start;
                gap: 12px;
            }

            .page-header h2 {
                font-size: 1.5rem;
            }

            .carne-info,
            .parcelas-section {
                padding: 24px;
            }

            .section-header {
                padding: 20px 24px;
            }

            .parcelas-list {
                padding: 20px 24px;
            }

            .info-grid {
                grid-template-columns: 1fr;
            }

            .parcela-item {
                flex-direction: column;
                align-items: flex-start;
                gap: 16px;
            }

            .parcela-info {
                width: 100%;
            }

            .parcela-status {
                align-self: flex-end;
            }
        }
    </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">
                <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" class="active">
                <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">
        <?php if ($erro): ?>
            <div class="page-header">
                <div class="icon-wrapper">
                    <i class="fa-solid fa-exclamation-triangle"></i>
                </div>
                <h2>Erro ao Carregar Carnê</h2>
            </div>
            
            <div class="error-card">
                <div class="error-icon">
                    <i class="fa-solid fa-file-circle-xmark"></i>
                </div>
                <h3><?=htmlspecialchars($erro)?></h3>
                <p>
                    Verifique se você selecionou um fiado válido.<br>
                    Volte para <a href="fiados.php">Fiados</a> para gerar o carnê corretamente.
                </p>
            </div>
        <?php else: ?>
            <div class="page-header">
                <div class="icon-wrapper">
                    <i class="fa-solid fa-file-pdf"></i>
                </div>
                <h2>Carnê de <?=htmlspecialchars($fiado['cliente_nome'])?></h2>
            </div>
            
            <div class="carne-info">
                <h3>
                    <i class="fa-solid fa-info-circle"></i>
                    Informações do Fiado
                </h3>
                
                <div class="info-grid">
                    <div class="info-item">
                        <div class="info-label">Descrição</div>
                        <div class="info-value"><?=htmlspecialchars($fiado['description'])?></div>
                    </div>
                    <div class="info-item">
                        <div class="info-label">Valor Total</div>
                        <div class="info-value">R$ <?=number_format($fiado['total_amount'],2,',','.')?></div>
                    </div>
                    <div class="info-item">
                        <div class="info-label">Número de Parcelas</div>
                        <div class="info-value"><?=count($parcelas)?></div>
                    </div>
                    <div class="info-item">
                        <div class="info-label">Cliente</div>
                        <div class="info-value"><?=htmlspecialchars($fiado['cliente_nome'])?></div>
                    </div>
                </div>
            </div>
            
            <div class="parcelas-section">
                <div class="section-header">
                    <h3>
                        <i class="fa-solid fa-calendar-check"></i>
                        Parcelas do Carnê
                    </h3>
                </div>
                
                <div class="parcelas-list">
                    <?php if (count($parcelas) > 0): ?>
                        <?php foreach ($parcelas as $p): ?>
                        <div class="parcela-item">
                            <div class="parcela-info">
                                <div class="parcela-icon">
                                    <i class="fa-solid fa-calendar-days"></i>
                                </div>
                                <div class="parcela-details">
                                    <div class="parcela-date">
                                        Vencimento: <?=date('d/m/Y', strtotime($p['due_date']))?>
                                    </div>
                                    <div class="parcela-value">
                                        Valor: R$ <?=number_format($p['amount'],2,',','.')?>
                                    </div>
                                </div>
                            </div>
                            <div class="parcela-status <?php echo (isset($p['paid']) && $p['paid']) ? 'status-pago' : 'status-pendente'; ?>">
                                <?php if (isset($p['paid']) && $p['paid']): ?>
                                    <i class="fa-solid fa-check-circle"></i>
                                    Pago
                                <?php else: ?>
                                    <i class="fa-solid fa-clock"></i>
                                    Pendente
                                <?php endif; ?>
                            </div>
                        </div>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <div class="parcela-item" style="justify-content: center; text-align: center;">
                            <div style="color: var(--text-secondary);">
                                <i class="fa-solid fa-calendar-xmark" style="font-size: 2rem; margin-bottom: 16px; display: block;"></i>
                                Nenhuma parcela encontrada para este fiado.
                            </div>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
            
            <div class="carne-actions">
                <a href="gerar_pdf.php?fiado_id=<?=$fiado_id?>" target="_blank" class="action-btn">
                    <i class="fa-solid fa-file-pdf"></i>
                    Gerar PDF do Carnê
                </a>
            </div>
        <?php endif; ?>
    </div>
    
    <script>
        // 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>