<?php
// /admin/editar-faq.php
require 'db.php';

if(!defined('VX_OK')||!VX_OK){header('HTTP/1.0 403 Forbidden');exit;}

if (!isset($_SESSION['admin_logado'])) {
    header("Location: login.php");
    exit;
}

$mensagem = '';

if (isset($_GET['excluir'])) {
    $pdo->prepare("DELETE FROM faqs WHERE id = ?")->execute([$_GET['excluir']]);
    $mensagem = "Pergunta excluída!";
}

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $pergunta = $_POST['pergunta'];
    $resposta = $_POST['resposta'];
    
    if (!empty($_POST['id'])) {
        $pdo->prepare("UPDATE faqs SET pergunta = ?, resposta = ? WHERE id = ?")->execute([$pergunta, $resposta, $_POST['id']]);
    } else {
        $pdo->prepare("INSERT INTO faqs (pergunta, resposta) VALUES (?, ?)")->execute([$pergunta, $resposta]);
    }
    $mensagem = "FAQ atualizado com sucesso!";
}

$faqs = $pdo->query("SELECT * FROM faqs ORDER BY id DESC")->fetchAll();
?>
<!DOCTYPE html>
<html lang="pt-BR">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Gerenciar FAQ | VoxCore CMS</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="admin.css?v=<?= time() ?>">


    <?php 
        // No admin, o caminho volta uma pasta para achar a imagem na raiz /img/
        $stmt_fav_adm = $pdo->query("SELECT valor FROM configs WHERE chave = 'favicon_admin'");
        $fav_adm_path = $stmt_fav_adm->fetchColumn() ?: 'img/favicon_admin.png';
    ?>
    
    <link rel="icon" href="../<?= $fav_adm_path ?>?v=<?= time() ?>" type="image/png">

</head>
<body>

<?php include 'sidebar.php'; ?>

<main class="col-md-9 ms-sm-auto col-lg-10 main-content">
    <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-4 border-bottom">
        <h1 class="h2"><i class="fas fa-question-circle text-primary"></i> Gerenciar FAQ</h1>
    </div>

    <?php if ($mensagem): ?>
        <div class="alert alert-success"><i class="fas fa-check-circle"></i> <?= $mensagem ?></div>
    <?php endif; ?>

    <div class="card p-4 shadow-sm mb-4 border-0" style="border-radius:15px;">
        <h4 class="text-secondary"><i class="fas fa-plus-circle text-primary"></i> Adicionar Nova Pergunta</h4>
        <form method="POST" class="mt-3">
            <input type="hidden" name="id" id="faq_id">
            <div class="mb-3">
                <label class="form-label">Pergunta:</label>
                <input type="text" name="pergunta" id="faq_pergunta" class="form-control" required>
            </div>
            <div class="mb-3">
                <label class="form-label">Resposta:</label>
                <textarea name="resposta" id="faq_resposta" class="form-control" rows="3" required></textarea>
            </div>
            <button type="submit" class="btn btn-success fw-bold"><i class="fas fa-save"></i> Salvar FAQ</button>
            <button type="reset" class="btn btn-link text-muted" onclick="document.getElementById('faq_id').value=''">Limpar Formulário</button>
        </form>
    </div>

    <div class="card p-4 shadow-sm border-0" style="border-radius:15px;">
        <h4 class="text-secondary mb-3">Perguntas Cadastradas</h4>
        <div class="table-responsive">
            <table class="table table-hover align-middle">
                <thead class="table-light">
                    <tr>
                        <th>Pergunta</th>
                        <th width="120" class="text-center">Ações</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($faqs as $f): ?>
                    <tr>
                        <td><?= htmlspecialchars($f['pergunta']) ?></td>
                        <td class="text-center">
                            <button class="btn btn-sm btn-primary" onclick="editar('<?= $f['id'] ?>', '<?= htmlspecialchars(addslashes($f['pergunta']), ENT_QUOTES) ?>', '<?= htmlspecialchars(addslashes($f['resposta']), ENT_QUOTES) ?>')"><i class="fas fa-edit"></i></button>
                            <a href="?excluir=<?= $f['id'] ?>" class="btn btn-sm btn-danger" onclick="return confirm('Apagar pergunta?')"><i class="fas fa-trash"></i></a>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
</main>

<?php include 'footer_admin.php'; ?>

<script>
    function editar(id, perg, resp) {
        document.getElementById('faq_id').value = id;
        document.getElementById('faq_pergunta').value = perg;
        document.getElementById('faq_resposta').value = resp;
        window.scrollTo(0,0);
    }
</script>
</body>
</html>