<?php
// Lógica de Processamento Back-end
$mensagem = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $numero = $_POST['numero_nf'];
    $valor_final = $_POST['valor_total_final'];
    $mensagem = "<div class='success-alert'>✅ Documento nº <strong>$numero</strong> salvo! Valor Total: <strong>R$ $valor_final</strong>.</div>";
}
?>

<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <title>Entrada Fiscal Pro</title>
    <style>
        body { font-family: 'Segoe UI', Tahoma, sans-serif; background-color: #f0f2f5; padding: 20px; }
        .container { max-width: 1000px; margin: auto; background: white; padding: 30px; border-radius: 12px; box-shadow: 0 8px 24px rgba(0,0,0,0.1); }
        h2 { color: #1a73e8; border-bottom: 2px solid #1a73e8; padding-bottom: 10px; margin-top: 0; }
        h3 { font-size: 11px; color: #64748b; background: #f8fafc; padding: 8px; margin-top: 20px; text-transform: uppercase; border-radius: 4px; border: 1px solid #e2e8f0; }
        
        .grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; }
        .col-2 { grid-column: span 2; }
        .col-4 { grid-column: span 4; }

        .form-group { display: flex; flex-direction: column; }
        label { font-size: 10px; font-weight: bold; margin-bottom: 4px; color: #475569; }
        input, select { padding: 10px; border: 1px solid #cbd5e1; border-radius: 6px; font-size: 13px; }
        input:focus { border-color: #1a73e8; outline: none; }
        
        /* Campos de destaque */
        .bg-subtrativo { background-color: #fff1f2; border-color: #fca5a5; } /* Vermelho suave para desconto */
        .bg-aditivo { background-color: #f0fdf4; border-color: #86efac; }    /* Verde suave para valores */
        
        .total-box { background: #0f172a; color: white; padding: 25px; border-radius: 8px; margin-top: 30px; display: flex; align-items: center; justify-content: space-between; }
        .total-info { display: flex; flex-direction: column; }
        .total-info span { font-size: 12px; color: #94a3b8; }
        
        .input-total-final { 
            background: transparent; 
            border: 2px solid #22c55e; 
            color: #22c55e; 
            font-size: 28px; 
            font-weight: bold; 
            text-align: right; 
            width: 220px;
            padding: 5px;
        }
        
        .btn-calc { background: #334155; color: white; border: none; padding: 8px 15px; border-radius: 5px; cursor: pointer; font-size: 12px; transition: 0.2s; }
        .btn-calc:hover { background: #1e293b; border: 1px solid #22c55e; }

        .btn-save { background: #1a73e8; color: white; border: none; padding: 18px; border-radius: 8px; cursor: pointer; font-size: 16px; font-weight: bold; width: 100%; margin-top: 20px; }
        .success-alert { background: #dcfce7; color: #166534; padding: 15px; border-radius: 8px; margin-bottom: 20px; text-align: center; border: 1px solid #bbf7d0; }
    </style>
</head>
<body>

<div class="container">
    <h2>📄 Gestão de Entrada Fiscal</h2>
    <?php echo $mensagem; ?>

    <form method="POST">
        <div class="grid">
            <div class="form-group col-2">
                <label>TIPO DE DOCUMENTO</label>
                <select name="tipo_doc">
                    <option value="NF-e">NF-e (Nota de Produto)</option>
                    <option value="CT-e">CT-e (Transporte)</option>
                    <option value="NFS-e">NFS-e (Serviço)</option>
                </select>
            </div>
            <div class="form-group col-2">
                <label>CHAVE DE ACESSO</label>
                <input type="text" name="chave_acesso" maxlength="44" placeholder="0000...">
            </div>
        </div>

        <h3>1. Cabeçalho e Logística</h3>
        <div class="grid">
            <div class="form-group"><label>Número</label><input type="text" name="numero_nf" required></div>
            <div class="form-group"><label>Série</label><input type="text" name="serie"></div>
            <div class="form-group"><label>Data Emissão</label><input type="date" name="data_emissao" required></div>
            <div class="form-group"><label>CEP Destino</label><input type="text" name="cep"></div>
            <div class="form-group col-4"><label>Endereço de Entrega</label><input type="text" name="endereco"></div>
        </div>

        <h3>2. Impostos (PIS / COFINS / ICMS)</h3>
        <div class="grid">
            <div class="form-group"><label>Base PIS</label><input type="number" step="0.01" id="b_pis"></div>
            <div class="form-group"><label>Valor PIS</label><input type="number" step="0.01" id="v_pis"></div>
            <div class="form-group"><label>Base COFINS</label><input type="number" step="0.01" id="b_cof"></div>
            <div class="form-group"><label>Valor COFINS</label><input type="number" step="0.01" id="v_cof"></div>
            
            <div class="form-group"><label>Base ICMS</label><input type="number" step="0.01"></div>
            <div class="form-group"><label>Valor ICMS</label><input type="number" step="0.01"></div>
            <div class="form-group"><label>Base ICMS ST</label><input type="number" step="0.01"></div>
            <div class="form-group"><label>Valor ICMS ST</label><input type="number" step="0.01" class="soma" id="v_st"></div>
        </div>

        <h3>3. Fechamento de Valores</h3>
        <div class="grid">
            <div class="form-group col-2">
                <label>VALOR TOTAL DAS MERCADORIAS</label>
                <input type="number" step="0.01" class="soma bg-aditivo" id="v_merc" placeholder="0.00">
            </div>
            <div class="form-group col-2">
                <label>DESCONTO (-)</label>
                <input type="number" step="0.01" class="subtrai bg-subtrativo" id="v_desc" placeholder="0.00">
            </div>
            <div class="form-group"><label>Frete (+)</label><input type="number" step="0.01" class="soma" id="v_frete"></div>
            <div class="form-group"><label>Seguro (+)</label><input type="number" step="0.01" class="soma" id="v_seguro"></div>
            <div class="form-group"><label>Outras Despesas (+)</label><input type="number" step="0.01" class="soma" id="v_outros"></div>
        </div>

        <div class="total-box">
            <div class="total-info">
                <span>Conferência de Cálculos</span>
                <button type="button" class="btn-calc" onclick="calcularSugerido()">Calcular Total Sugerido</button>
            </div>
            <div style="text-align: right;">
                <label>VALOR TOTAL DA NOTA FISCAL</label><br>
                <input type="number" step="0.01" name="valor_total_final" id="total_final" class="input-total-final" required>
            </div>
        </div>

        <button type="submit" class="btn-save">GRAVAR NO BANCO DE DADOS</button>
    </form>
</div>

<script>
    function calcularSugerido() {
        let aditivos = 0;
        let subtrativos = 0;

        // Soma Mercadorias, ST, Frete, Seguro e Outros
        document.querySelectorAll('.soma').forEach(el => {
            aditivos += parseFloat(el.value) || 0;
        });

        // Pega o Desconto
        document.querySelectorAll('.subtrai').forEach(el => {
            subtrativos += parseFloat(el.value) || 0;
        });

        const resultado = aditivos - subtrativos;
        document.getElementById('total_final').value = resultado.toFixed(2);
    }

    // Lógica rápida para cálculo de PIS/COFINS (1.65% e 7.6%)
    document.getElementById('b_pis').addEventListener('input', function() {
        document.getElementById('v_pis').value = (parseFloat(this.value || 0) * 0.0165).toFixed(2);
    });
    document.getElementById('b_cof').addEventListener('input', function() {
        document.getElementById('v_cof').value = (parseFloat(this.value || 0) * 0.076).toFixed(2);
    });
</script>

</body>
</html>