<?php
require_once __DIR__ . '/../includes/header.php';

$id = intval($_GET['id'] ?? 0);
$conta = ['aluno_id'=>'','descricao'=>'','valor'=>'','data_vencimento'=>'','forma_pagamento_id'=>'','conta_bancaria_id'=>'','centro_custo_id'=>'','parcela'=>1,'total_parcelas'=>1,'observacao'=>'','status'=>'pendente'];

if ($id > 0) {
    $stmt = $pdo->prepare("SELECT * FROM contas_receber WHERE id = :id");
    $stmt->execute(['id' => $id]);
    $conta = $stmt->fetch() ?: $conta;
}

// Listas para selects
$alunos = $pdo->query("SELECT id, nome FROM alunos WHERE status='Ativo' ORDER BY nome")->fetchAll();
$formas = $pdo->query("SELECT id, nome FROM formas_pagamento WHERE status='ativo' ORDER BY nome")->fetchAll();
$bancos = $pdo->query("SELECT id, nome FROM contas_bancarias WHERE status='ativo' ORDER BY nome")->fetchAll();
$centros = $pdo->query("SELECT id, nome FROM centros_custo WHERE status='ativo' ORDER BY nome")->fetchAll();
?>

<div class="page-header">
    <div>
        <h2><?= $id ? 'Editar' : 'Nova' ?> Conta a Receber</h2>
        <div class="subtitle">Preencha os dados abaixo</div>
    </div>
    <a href="contas_receber.php" class="btn btn-secondary">Voltar</a>
</div>

<div class="card-premium">
<form method="POST" action="conta_receber_salvar.php">
<input type="hidden" name="id" value="<?= $id ?>">

<div class="grid-3">
    <div class="form-group">
        <label>Aluno</label>
        <select name="aluno_id" required>
            <option value="">Selecione</option>
            <?php foreach ($alunos as $a): ?>
            <option value="<?= $a['id'] ?>" <?= $conta['aluno_id']==$a['id']?'selected':'' ?>><?= sanitize($a['nome']) ?></option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="form-group">
        <label>Descricao</label>
        <input name="descricao" value="<?= sanitize($conta['descricao']) ?>" required placeholder="Ex: Mensalidade Abril/2026">
    </div>
    <div class="form-group">
        <label>Valor (R$)</label>
        <input type="number" step="0.01" name="valor" value="<?= $conta['valor'] ?>" required>
    </div>
</div>

<div class="grid-3">
    <div class="form-group">
        <label>Data Vencimento</label>
        <input type="date" name="data_vencimento" value="<?= $conta['data_vencimento'] ?>" required>
    </div>
    <div class="form-group">
        <label>Forma de Pagamento</label>
        <select name="forma_pagamento_id">
            <option value="">Selecione</option>
            <?php foreach ($formas as $f): ?>
            <option value="<?= $f['id'] ?>" <?= $conta['forma_pagamento_id']==$f['id']?'selected':'' ?>><?= sanitize($f['nome']) ?></option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="form-group">
        <label>Conta Bancaria</label>
        <select name="conta_bancaria_id">
            <option value="">Selecione</option>
            <?php foreach ($bancos as $b): ?>
            <option value="<?= $b['id'] ?>" <?= $conta['conta_bancaria_id']==$b['id']?'selected':'' ?>><?= sanitize($b['nome']) ?></option>
            <?php endforeach; ?>
        </select>
    </div>
</div>

<div class="grid-3">
    <div class="form-group">
        <label>Centro de Custo</label>
        <select name="centro_custo_id">
            <option value="">Selecione</option>
            <?php foreach ($centros as $cc): ?>
            <option value="<?= $cc['id'] ?>" <?= $conta['centro_custo_id']==$cc['id']?'selected':'' ?>><?= sanitize($cc['nome']) ?></option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="form-group">
        <label>Parcela</label>
        <input type="number" name="parcela" value="<?= intval($conta['parcela']) ?>" min="1">
    </div>
    <div class="form-group">
        <label>Total Parcelas</label>
        <input type="number" name="total_parcelas" value="<?= intval($conta['total_parcelas']) ?>" min="1">
    </div>
</div>

<div class="form-group">
    <label>Observacao</label>
    <textarea name="observacao" placeholder="Observacoes adicionais..."><?= sanitize($conta['observacao'] ?? '') ?></textarea>
</div>

<div class="d-flex gap-10 mt-2">
    <button type="submit" class="btn btn-primary">Salvar</button>
    <a href="contas_receber.php" class="btn btn-secondary">Cancelar</a>
</div>
</form>
</div>

<?php require_once __DIR__ . '/../includes/footer.php'; ?>
