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

$id = isset($_GET['id']) ? $_GET['id'] : null;

$pagamento = [
    'nome' => '',
    'status' => 'ativo'
];

if($id){
    $sql = "SELECT * FROM pagamentos WHERE id = ?";
    $stmt = $pdo->prepare($sql);
    $stmt->execute([$id]);
    $result = $stmt->fetch(PDO::FETCH_ASSOC);

    if($result){
        $pagamento = $result;
    }
}
?>

<style>
.topo-form {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 25px;
}

.card-premium {
    background: linear-gradient(145deg,#0d1b2a,#1b263b);
    border-radius: 15px;
    border: none;
}

.input-dark {
    background: #0d1b2a;
    border: 1px solid #415a77;
    color: #fff;
    border-radius: 8px;
}

.input-dark:focus {
    background: #0d1b2a;
    color: #fff;
    border-color: #778da9;
    box-shadow: none;
}

.btn-salvar {
    background: linear-gradient(45deg,#f7971e,#ffd200);
    border: none;
    padding: 10px 25px;
    border-radius: 8px;
    font-weight: 600;
    color: #000;
}

.btn-voltar {
    background: #6c757d;
    padding: 10px 20px;
    border-radius: 8px;
    color: #fff;
}
</style>

<div class="container-fluid mt-4">

    <div class="topo-form">
        <h3 class="text-white">
            <?= $id ? "✏️ Editar Forma de Pagamento" : "💳 Nova Forma de Pagamento" ?>
        </h3>

        <a href="pagamentos.php" class="btn btn-voltar">
            ← Voltar
        </a>
    </div>

    <div class="card card-premium shadow-lg">
        <div class="card-body p-4">

            <form method="POST" action="pagamento_salvar.php">

                <input type="hidden" name="id" value="<?= $id ?>">

                <div class="mb-3">
                    <label class="text-white">Nome</label>
                    <input type="text" name="nome"
                           class="form-control input-dark"
                           value="<?= htmlspecialchars($pagamento['nome']) ?>"
                           required>
                </div>

                <div class="mb-3">
                    <label class="text-white">Status</label>
                    <select name="status" class="form-control input-dark">
                        <option value="ativo" <?= $pagamento['status']=='ativo' ? 'selected' : '' ?>>Ativo</option>
                        <option value="inativo" <?= $pagamento['status']=='inativo' ? 'selected' : '' ?>>Inativo</option>
                    </select>
                </div>

                <div class="mt-4 text-end">
                    <button type="submit" class="btn btn-salvar">
                        💾 Salvar Forma
                    </button>
                </div>

            </form>

        </div>
    </div>

</div>

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