<?php
date_default_timezone_set('America/Sao_Paulo');
include_once('../config.php'); 
include_once('../header.php');

if (isset($_POST['salvar_despesa'])) {
    $descricao = mysqli_real_escape_string($conn, $_POST['descricao']);
    $observacao = mysqli_real_escape_string($conn, $_POST['observacao']);
    $dono = mysqli_real_escape_string($conn, $_POST['dono']);
    $dia_vencimento = intval($_POST['dia_vencimento']);
    
    $meses_db = ["jan", "fev", "mar", "abr", "mai", "jun", "jul", "ago", "setem", "outu", "nov", "dez"];
    $valores = [];
    foreach($meses_db as $m) {
        $valores[$m] = floatval(str_replace(',', '.', str_replace('.', '', $_POST[$m])));
    }

    $query = "INSERT INTO cartao_credito (descricao, observacao, dono, dia_vencimento, jan, fev, mar, abr, mai, jun, jul, ago, setem, outu, nov, dez) 
              VALUES ('$descricao', '$observacao', '$dono', '$dia_vencimento', 
              '{$valores['jan']}', '{$valores['fev']}', '{$valores['mar']}', '{$valores['abr']}', 
              '{$valores['mai']}', '{$valores['jun']}', '{$valores['jul']}', '{$valores['ago']}', 
              '{$valores['setem']}', '{$valores['outu']}', '{$valores['nov']}', '{$valores['dez']}')";

    if ($conn->query($query)) {
        echo "<script>alert('Lançamento Realizado!'); window.location.href='exibir_cartao.php';</script>";
    }
}
?>

<style>
    .container-form { max-width: 900px; margin: 20px auto; font-family: 'Segoe UI', sans-serif; text-transform: uppercase; }
    .card-form { background: white; border: 4px solid #00569e; border-radius: 8px; padding: 20px; }
    .row-inputs { display: flex; gap: 15px; margin-bottom: 20px; }
    .campo-flex { flex: 1; }
    label { display: block; font-size: 10px; font-weight: bold; color: #217346; margin-bottom: 5px; }
    .input-padrao { width: 100%; padding: 8px; border: 1px solid #ccc; border-radius: 4px; font-weight: bold; }
    .quadro-meses { background: #f4f4f4; padding: 15px; border-radius: 6px; border: 1px solid #ddd; display: grid; grid-template-columns: repeat(4, 1fr); gap: 15px; }
    .mes-item { display: flex; align-items: center; gap: 10px; background: white; padding: 5px 10px; border: 1px solid #ccc; border-radius: 4px; }
    .input-mes { width: 100%; border: none; text-align: right; font-weight: bold; outline: none; }
    .btn-submit { background: #217346; color: white; border: none; padding: 12px; width: 100%; border-radius: 4px; font-weight: bold; cursor: pointer; margin-top: 20px; }
</style>

<div class="container-form">
    <div class="card-form">
        <h2 style="text-align:center; color:#217346; font-size:14px;">📊 LANÇAMENTO ANUAL - CARTÃO</h2>
        <form method="POST">
            <div class="row-inputs">
                <div class="campo-flex">
                    <label>DESCRIÇÃO</label>
                    <input type="text" name="descricao" class="input-padrao" required>
                </div>
                <div class="campo-flex">
                    <label>OBSERVAÇÃO</label>
                    <input type="text" name="observacao" class="input-padrao">
                </div>
                <div style="width: 150px;">
                    <label>DONO(A)</label>
                    <select name="dono" class="input-padrao">
                        <option value="ALEXANDRE">ALEXANDRE</option>
                        <option value="FRAN">FRAN</option>
                    </select>
                </div>
                <div style="width: 80px;">
                    <label>DIA VENC.</label>
                    <input type="number" name="dia_vencimento" class="input-padrao" value="10" required>
                </div>
            </div>

            <div class="quadro-meses">
                <?php 
                $meses = ["jan"=>"JAN", "fev"=>"FEV", "mar"=>"MAR", "abr"=>"ABR", "mai"=>"MAI", "jun"=>"JUN", "jul"=>"JUL", "ago"=>"AGO", "setem"=>"SET", "outu"=>"OUT", "nov"=>"NOV", "dez"=>"DEZ"];
                foreach($meses as $key => $nome): ?>
                    <div class="mes-item">
                        <span style="font-size:10px; font-weight:bold;"><?php echo $nome; ?></span>
                        <input type="text" name="<?php echo $key; ?>" class="input-mes" value="0,00" onInput="mascaraMoeda(this)">
                    </div>
                <?php endforeach; ?>
            </div>
            <button type="submit" name="salvar_despesa" class="btn-submit">SALVAR LANÇAMENTO</button>
        </form>
    </div>
</div>

<script>
function mascaraMoeda(i) {
    var v = i.value.replace(/\D/g,'');
    v = (v/100).toFixed(2) + '';
    v = v.replace(".", ",");
    v = v.replace(/(\d)(\d{3})(\d{3}),/g, "$1.$2.$3,");
    v = v.replace(/(\d)(\d{3}),/g, "$1.$2,");
    i.value = v;
}
</script>