<?php 


include '../config.php';
require_once "../configuracao/modelo.php"; 

// Filtro por Status (p = Pendente, f = Fechada, m = Emitida)
$filtro = isset($_GET['status']) ? $_GET['status'] : '';

$sql = "SELECT * FROM nf_cab";
if ($filtro !== '') {
    $statusSeguro = $mysqli->real_escape_string($filtro);
    $sql .= " WHERE nf_cab_status = '$statusSeguro'";
}
$sql .= " ORDER BY codigo DESC";
$resultado = $mysqli->query($sql);
?>

<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <title>Gestão de Notas Fiscais</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        :root { --pendente: #ffc107; --fechada: #6c757d; --emitida: #198754; }
        body { background-color: #f8f9fa; }
        .card-nf { border: none; border-left: 6px solid #ccc; transition: 0.2s; }
        .status-p, .status-P { border-left-color: var(--pendente); }
        .status-f, .status-F { border-left-color: var(--fechada); }
        .status-m, .status-M { border-left-color: var(--emitida); }
    </style>
</head>
<body>

<div class="container py-5">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2 class="fw-bold">📑 Painel de Notas</h2>
        <div class="btn-group">
            <a href="index.php" class="btn btn-outline-dark">Todas</a>
            <a href="index.php?status=P" class="btn btn-outline-warning">Pendentes</a>
            <a href="index.php?status=F" class="btn btn-outline-secondary">Fechadas</a>
        </div>
    </div>

    <div class="row g-4">
        <?php while($nota = $resultado->fetch_assoc()): ?>
            <div class="col-md-4">
                <div class="card h-100 shadow-sm card-nf status-<?= $nota['nf_cab_status'] ?>">
                    <div class="card-body">
                        <div class="d-flex justify-content-between">
                            <span class="badge bg-dark"><?= $nota['nf_cab_status'] == 'P' ? 'PENDENTE' : 'FECHADA' ?></span>
                            <small>#<?= $nota['codigo'] ?></small>
                        </div>
                        <h5 class="mt-3 fw-bold"><?= htmlspecialchars($nota['nf_cab_nome']) ?></h5>
                        <p class="text-muted mb-1">Tipo: <?= $nota['nf_cab_c_v'] == 'S' ? 'Saída' : 'Entrada' ?></p>
                        <h4 class="text-primary">R$ <?= number_format((float)$nota['nf_cab_tot_nf'], 2, ',', '.') ?></h4>
                    </div>
                    <div class="card-footer bg-white border-0 d-grid gap-2">
                        <button class="btn btn-sm btn-outline-primary" onclick="verItens(<?= $nota['codigo'] ?>, '<?= $nota['nf_cab_nome'] ?>')">🔍 Itens</button>
                        <?php if($nota['nf_cab_status'] == 'P'): ?>
                            <button class="btn btn-sm btn-success" onclick="fecharNota(<?= $nota['codigo'] ?>)">🔒 Fechar Nota</button>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        <?php endwhile; ?>
    </div>
</div>

<div class="modal fade" id="modalItens" tabindex="-1">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header bg-dark text-white"><h5 class="modal-title" id="m_titulo"></h5></div>
            <div class="modal-body p-0" id="m_corpo"></div>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script>
function verItens(id, nome) {
    const m = new bootstrap.Modal(document.getElementById('modalItens'));
    document.getElementById('m_titulo').innerText = "Itens: " + nome;
    fetch('buscar_produtos.php?id=' + id)
        .then(r => r.text())
        .then(h => { document.getElementById('m_corpo').innerHTML = h; m.show(); });
}

function fecharNota(id) {
    if(confirm("Confirmar fechamento e movimentar estoque?")) {
        fetch('processar.php?id=' + id + '&status=P')
            .then(r => r.json())
            .then(j => { alert(j.message); if(j.status=='sucesso') location.reload(); });
    }
}
</script>
</body>
</html>