<?php
session_start();
if (!isset($_SESSION['id'])) { header("Location: ../index/index.php"); exit(); }
include '../config.php';
include_once('../configuracao/modelo.php');
?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <title>Caixa - Lançamentos</title>
    <style>
        * { box-sizing: border-box; }
        body { background-color: #d4d0c8; font-family: "Tahoma", sans-serif; font-size: 11px; margin: 0; padding: 10px; }
        .window { border: 2px solid; border-color: #fff #808080 #808080 #fff; background: #d4d0c8; padding: 2px; }
        .title-bar { background: #000080; color: white; padding: 3px 6px; font-weight: bold; }
        .content { padding: 10px; }
        fieldset { border: 2px inset #fff; margin-bottom: 10px; padding: 10px; }
        legend { font-weight: bold; }
        .row { display: flex; gap: 10px; margin-bottom: 10px; }
        .field { display: flex; flex-direction: column; flex: 1; }
        input { border: 2px inset #fff; padding: 4px; font-family: "Tahoma"; font-size: 11px; background: #fff; }
        .input-bloqueado { background: #d4d0c8 !important; cursor: not-allowed; }
        .input-info { background: #e1e1e1 !important; font-weight: bold; color: #000080; }
        .input-yellow { background: #ffffcc !important; font-weight: bold; border: 2px inset #fff; }
        .btn-win { background: #d4d0c8; border: 2px solid; border-color: #fff #808080 #808080 #fff; cursor: pointer; font-family: "Tahoma"; font-size: 11px; padding: 3px 8px; }
        .btn-win:active { border-color: #808080 #fff #fff #808080; }
        #btnGravar { height: 35px; width: 150px; font-weight: bold; margin-top: 10px; background: #d4d0c8; }
    </style>
</head>
<body>

<div class="window">
    <div class="title-bar">Lançamento de Caixa Avulso / Participante</div>
    <form action="processar.php" method="POST" class="content">
        
        <fieldset>
            <legend>Identificação do Participante</legend>
            <div class="row">
                <div class="field" style="flex: 1;">
                    <label>Código Part.:</label>
                    <div style="display:flex; gap:2px;">
                        <input type="text" name="cod_participante" id="h_cod_part" class="input-yellow" readonly required>
                        <button type="button" class="btn-win" onclick="window.open('busca_participante.php','','width=750,height=500')">🔍</button>
                    </div>
                </div>
                <div class="field" style="flex: 3;">
                    <label>Nome / Razão Social:</label>
                    <input type="text" name="nome_participante" id="h_nome_part" class="input-info" readonly style="width:100%">
                </div>
            </div>
        </fieldset>

        <fieldset>
            <legend>Dados do Lançamento</legend>
            <div class="row">
                <div class="field">
                    <label>Natureza (C):</label>
                    <div style="display:flex; gap:2px;">
                        <input type="text" id="nat_descr" readonly style="width:100%" class="input-info">
                        <button type="button" class="btn-win" id="btnBuscaNat" onclick="window.open('busca_natureza.php','','width=550,height=400')">🔍</button>
                    </div>
                </div>
                <div class="field">
                    <label>Data Lançamento:</label>
                    <input type="date" name="data" id="data_lcto" value="<?= date('Y-m-d') ?>">
                </div>
            </div>
        </fieldset>

        <fieldset>
            <legend>Valores e Contabilidade</legend>
            <div class="row">
                <div class="field"><label>Valor Bruto:</label><input type="number" name="val_original" id="val_original" step="0.01" value="0.00" oninput="calc()"></div>
                <div class="field"><label>Desc.:</label><input type="number" name="v_desc" id="v_desc" step="0.01" value="0.00" oninput="calc()"></div>
                <div class="field"><label>Acre.:</label><input type="number" name="v_acre" id="v_acre" step="0.01" value="0.00" oninput="calc()"></div>
                <div class="field"><label>Vl. Líquido:</label><input type="number" name="valor_final" id="valor_final" class="input-yellow" readonly></div>
            </div>
            <div class="row">
                <div class="field"><label>Débito (Entrada):</label><input type="text" name="reg_i250_cta_deb" id="val_deb" readonly required class="input-info"></div>
                <div class="field"><label>Crédito (Saída):</label><input type="text" name="reg_i250_cta_cre" id="val_cre" readonly required class="input-info"></div>
            </div>
            <div class="field">
                <label>Histórico Contábil:</label>
                <input type="text" name="reg_i250_hist" id="val_hist" required style="width:100%" placeholder="Ex: RECEBIMENTO REFERENTE A...">
            </div>
        </fieldset>

        <div style="text-align:right;">
            <button type="submit" id="btnGravar" class="btn-win">GRAVAR LANÇAMENTO (F10)</button>
        </div>
    </form>
</div>

<script>
// Função chamada pela janela busca_participante.php
function definirParticipante(codigo, nome) {
    document.getElementById('h_cod_part').value = codigo;
    document.getElementById('h_nome_part').value = nome;
    
    // Sugestão automática de histórico
    document.getElementById('val_hist').value = "LCTO CAIXA - PART: " + codigo;
}

function definirNatureza(descr, deb, cre) {
    document.getElementById('nat_descr').value = descr;
    document.getElementById('val_deb').value = deb;
    document.getElementById('val_cre').value = cre;
}

function calc() {
    let n = parseFloat(document.getElementById('val_original').value) || 0;
    let d = parseFloat(document.getElementById('v_desc').value) || 0;
    let a = parseFloat(document.getElementById('v_acre').value) || 0;
    document.getElementById('valor_final').value = (n - d + a).toFixed(2);
}
</script>
</body>
</html>