<?php
include 'verifica_login.php'; // Verificar se está logado
include 'conexao.php';
$conn = conectar_banco();

// Lógica para Inserir ou Alterar Status
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $nome = $_POST['nome_status'];
    $salvarvazio = $_POST['salvarvazio'] ?? 'N'; // 'S' ou 'N'
    $id_edit = $_POST['id_status_edit'] ?? null;

    if ($id_edit) {
        $stmt = $conn->prepare("UPDATE statusdoquestionario SET nome_status = ?, salvarvazio = ? WHERE id_status = ?");
        $stmt->bind_param("ssi", $nome, $salvarvazio, $id_edit);
    } else {
        $stmt = $conn->prepare("INSERT INTO statusdoquestionario (nome_status, salvarvazio) VALUES (?, ?)");
        $stmt->bind_param("ss", $nome, $salvarvazio);
    }
    if (!$stmt->execute()) {
        echo "Erro ao salvar status: " . $conn->error;
        exit;
    }
    header("Location: gerenciar_status.php");
    exit;
}

// Lógica para buscar dados de edição
$edit_data = null;
if (isset($_GET['edit'])) {
    $res = $conn->query("SELECT * FROM statusdoquestionario WHERE id_status = " . (int)$_GET['edit']);
    $edit_data = $res->fetch_assoc();
}

$status_list = $conn->query("SELECT * FROM statusdoquestionario ORDER BY id_status");
?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <title>Gerenciar Status - Belgo Arames</title>
    <style>
        body { font-family: Arial; padding: 20px; background: #f4f4f4; }
        .card { background: white; padding: 20px; border-radius: 8px; max-width: 500px; margin: auto; box-shadow: 0 2px 5px rgba(0,0,0,0.1); }
        table { width: 100%; border-collapse: collapse; margin-top: 20px; }
        th, td { border: 1px solid #ddd; padding: 10px; text-align: left; }
        input[type="text"] { width: 70%; padding: 8px; }
        .btn { padding: 8px 12px; cursor: pointer; border-radius: 4px; border: none; color: white; text-decoration: none; }
        .btn-save { background: #2e7d32; }
        .btn-edit { background: #f57c00; font-size: 0.8em; }
        .btn-new { background: #1976d2; margin-right:6px; }
    </style>
</head>
<body>
<div class="card">
    <h2><?= $edit_data ? 'Editar' : 'Novo' ?> Status</h2>
    <form method="POST">
        <input type="hidden" name="id_status_edit" value="<?= $edit_data['id_status'] ?? '' ?>">
        <input type="text" name="nome_status" value="<?= $edit_data['nome_status'] ?? '' ?>" required placeholder="Nome do Status">
        <div style="margin-top:10px;">
            <label style="font-weight:bold; display:block; margin-bottom:6px;">Pode salvar vazio?</label>
            <label style="margin-right:10px;"><input type="radio" name="salvarvazio" value="S" <?= (isset($edit_data['salvarvazio']) && $edit_data['salvarvazio'] == 'S') ? 'checked' : '' ?>> Sim</label>
            <label><input type="radio" name="salvarvazio" value="N" <?= (!isset($edit_data['salvarvazio']) || $edit_data['salvarvazio'] == 'N') ? 'checked' : '' ?>> Não</label>
        </div>
        <div style="margin-top:10px; display:flex; gap:8px;">
            <a href="gerenciar_status.php" class="btn btn-new">Novo</a>
            <button type="submit" class="btn btn-save">Salvar</button>
        </div>
    </form>

    <table>
        <thead><tr><th>ID</th><th>Nome</th><th>Pode salvar vazio?</th><th>Ação</th></tr></thead>
        <tbody>
            <?php while($row = $status_list->fetch_assoc()): ?>
            <tr>
                <td><?= $row['id_status'] ?></td>
                <td><?= $row['nome_status'] ?></td>
                <td><?= ($row['salvarvazio'] ?? 'N') == 'S' ? 'Sim' : 'Não' ?></td>
                <td><a href="?edit=<?= $row['id_status'] ?>" class="btn btn-edit">Editar</a></td>
            </tr>
            <?php endwhile; ?>
        </tbody>
    </table>
    <br><a href="principal.php">Voltar para Principal</a>
</div>
</body>
</html>