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

$id = intval($_GET['id'] ?? 0);
$conta = ['nome'=>'','banco'=>'','agencia'=>'','numero_conta'=>'','tipo'=>'corrente','saldo_inicial'=>0,'status'=>'ativo'];

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

<div class="page-header">
    <div>
        <h2><?= $id ? 'Editar' : 'Nova' ?> Conta Bancaria</h2>
    </div>
    <a href="contas_bancarias.php" class="btn btn-secondary">Voltar</a>
</div>

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

<div class="grid-3">
    <div class="form-group">
        <label>Nome da Conta</label>
        <input name="nome" value="<?= sanitize($conta['nome']) ?>" required placeholder="Ex: Conta Itau">
    </div>
    <div class="form-group">
        <label>Banco</label>
        <input name="banco" value="<?= sanitize($conta['banco']) ?>" placeholder="Ex: Itau, Bradesco">
    </div>
    <div class="form-group">
        <label>Tipo</label>
        <select name="tipo">
            <option value="corrente" <?= $conta['tipo']==='corrente'?'selected':'' ?>>Corrente</option>
            <option value="poupanca" <?= $conta['tipo']==='poupanca'?'selected':'' ?>>Poupanca</option>
            <option value="caixa_fisico" <?= $conta['tipo']==='caixa_fisico'?'selected':'' ?>>Caixa Fisico</option>
            <option value="pix" <?= $conta['tipo']==='pix'?'selected':'' ?>>PIX</option>
        </select>
    </div>
</div>

<div class="grid-3">
    <div class="form-group">
        <label>Agencia</label>
        <input name="agencia" value="<?= sanitize($conta['agencia']) ?>" placeholder="0001">
    </div>
    <div class="form-group">
        <label>Numero da Conta</label>
        <input name="numero_conta" value="<?= sanitize($conta['numero_conta']) ?>" placeholder="12345-6">
    </div>
    <div class="form-group">
        <label>Saldo Inicial (R$)</label>
        <input type="number" step="0.01" name="saldo_inicial" value="<?= $conta['saldo_inicial'] ?>">
    </div>
</div>

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

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