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

$id = intval($_GET['id'] ?? 0);
$f = ['nome'=>'','cnpj'=>'','telefone'=>'','email'=>'','endereco'=>'','cidade'=>'','estado'=>'','cep'=>'','contato'=>''];

if ($id > 0) {
    $stmt = $pdo->prepare("SELECT * FROM fornecedores WHERE id = :id");
    $stmt->execute(['id' => $id]);
    $f = $stmt->fetch() ?: $f;
}
$titulo = $id > 0 ? 'Editar Fornecedor' : 'Novo Fornecedor';
?>

<div class="page-header">
    <div><h2><?= $titulo ?></h2><div class="subtitle">Preencha os dados do fornecedor</div></div>
    <div><a href="fornecedores.php" class="btn btn-secondary">Voltar</a></div>
</div>

<form method="POST" action="fornecedor_salvar.php">
<input type="hidden" name="id" value="<?= $id ?>">

<div class="card-premium" style="padding:25px;margin-bottom:20px;">
    <h3 style="color:var(--accent);margin-bottom:15px;border-bottom:1px solid var(--border);padding-bottom:10px;">Dados do Fornecedor</h3>
    <div style="display:grid;grid-template-columns:repeat(auto-fit,minmax(220px,1fr));gap:15px;">
        <div class="form-group"><label>Nome / Razao Social *</label><input name="nome" value="<?= sanitize($f['nome']) ?>" required></div>
        <div class="form-group"><label>CNPJ</label><input name="cnpj" value="<?= sanitize($f['cnpj']) ?>"></div>
        <div class="form-group"><label>Telefone</label><input name="telefone" value="<?= sanitize($f['telefone']) ?>"></div>
        <div class="form-group"><label>Email</label><input type="email" name="email" value="<?= sanitize($f['email']) ?>"></div>
        <div class="form-group"><label>Pessoa de Contato</label><input name="contato" value="<?= sanitize($f['contato']) ?>"></div>
    </div>
</div>

<div class="card-premium" style="padding:25px;margin-bottom:20px;">
    <h3 style="color:var(--accent);margin-bottom:15px;border-bottom:1px solid var(--border);padding-bottom:10px;">Endereco</h3>
    <div style="display:grid;grid-template-columns:repeat(auto-fit,minmax(220px,1fr));gap:15px;">
        <div class="form-group" style="grid-column:span 2;"><label>Endereco</label><input name="endereco" value="<?= sanitize($f['endereco']) ?>"></div>
        <div class="form-group"><label>CEP</label><input name="cep" value="<?= sanitize($f['cep']) ?>"></div>
        <div class="form-group"><label>Cidade</label><input name="cidade" value="<?= sanitize($f['cidade']) ?>"></div>
        <div class="form-group"><label>Estado</label><input name="estado" value="<?= sanitize($f['estado']) ?>"></div>
    </div>
</div>

<div style="display:flex;gap:10px;justify-content:flex-end;margin-bottom:30px;">
    <a href="fornecedores.php" class="btn btn-secondary">Cancelar</a>
    <button type="submit" class="btn btn-novo">Salvar Fornecedor</button>
</div>
</form>

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