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

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

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

$fornecedores = $pdo->query("SELECT id, nome FROM fornecedores 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 Pagar</h2>
        <div class="subtitle">Preencha os dados da despesa</div>
    </div>
    <a href="contas_pagar.php" class="btn btn-secondary">Voltar</a>
</div>

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

<div class="grid-3">
    <div class="form-group">
        <label>Fornecedor</label>
        <select name="fornecedor_id">
            <option value="">Selecione</option>
            <?php foreach ($fornecedores as $f): ?>
            <option value="<?= $f['id'] ?>" <?= $conta['fornecedor_id']==$f['id']?'selected':'' ?>><?= sanitize($f['nome']) ?></option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="form-group">
        <label>Descricao</label>
        <input name="descricao" value="<?= sanitize($conta['descricao']) ?>" required placeholder="Ex: Conta de Luz Abril">
    </div>
    <div class="form-group">
        <label>Numero/NF</label>
        <input name="numero" value="<?= sanitize($conta['numero'] ?? '') ?>" placeholder="Numero da nota">
    </div>
</div>

<div class="grid-3">
    <div class="form-group">
        <label>Valor (R$)</label>
        <input type="number" step="0.01" name="valor" value="<?= $conta['valor'] ?>" required>
    </div>
    <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>

<div class="grid-3">
    <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 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 / Total</label>
        <div class="d-flex gap-10">
            <input type="number" name="parcela" value="<?= intval($conta['parcela']) ?>" min="1" style="width:50%">
            <input type="number" name="total_parcelas" value="<?= intval($conta['total_parcelas']) ?>" min="1" style="width:50%">
        </div>
    </div>
</div>

<div class="form-group">
    <label>Observacao</label>
    <textarea name="observacao"><?= 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_pagar.php" class="btn btn-secondary">Cancelar</a>
</div>
</form>
</div>

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