<?php
require 'auth_check.php';
require '../config/db.php';

$msg = "";
$msgClass = "";

// 1. ADD / UPDATE FAQ
if (isset($_POST['save_faq'])) {
    $question = trim($_POST['question']);
    $answer = trim($_POST['answer']);
    $order = (int)$_POST['sort_order'];
    
    if (!empty($_POST['faq_id'])) {
        // Update
        $stmt = $pdo->prepare("UPDATE faqs SET question=?, answer=?, sort_order=? WHERE id=?");
        $stmt->execute([$question, $answer, $order, $_POST['faq_id']]);
        $msg = "FAQ updated successfully!";
    } else {
        // Insert
        $stmt = $pdo->prepare("INSERT INTO faqs (question, answer, sort_order) VALUES (?, ?, ?)");
        $stmt->execute([$question, $answer, $order]);
        $msg = "FAQ added successfully!";
    }
    $msgClass = "success";
}

// 2. DELETE FAQ
if (isset($_GET['delete'])) {
    $pdo->prepare("DELETE FROM faqs WHERE id=?")->execute([$_GET['delete']]);
    header("Location: faqs.php?msg=deleted"); exit();
}

// 3. FETCH
$edit_faq = null;
if (isset($_GET['edit'])) {
    $stmt = $pdo->prepare("SELECT * FROM faqs WHERE id=?");
    $stmt->execute([$_GET['edit']]);
    $edit_faq = $stmt->fetch();
}
$faqs = $pdo->query("SELECT * FROM faqs ORDER BY sort_order ASC")->fetchAll();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Manage FAQs</title>
    <link rel="stylesheet" href="admin_style.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        .msg-box { padding: 10px; border-radius: 5px; margin-bottom: 20px; text-align: center; }
        .success { background: #d4edda; color: #155724; border: 1px solid #c3e6cb; }
        
        .form-card { background: white; padding: 20px; border-radius: 8px; box-shadow: 0 2px 5px rgba(0,0,0,0.05); margin-bottom: 30px; }
        .faq-item { background: white; padding: 15px; border-bottom: 1px solid #eee; display: flex; justify-content: space-between; align-items: center; }
        .faq-item:last-child { border-bottom: none; }
    </style>
</head>
<body>

    <div class="sidebar">
        <?php include 'sidebar.php'; ?>
    </div>

    <div class="content">
        <h1>FAQ Manager</h1>
        <?php if($msg): ?><div class="msg-box <?php echo $msgClass; ?>"><?php echo $msg; ?></div><?php endif; ?>

        <div class="form-card">
            <h3><?php echo $edit_faq ? "Edit Question" : "Add New Question"; ?></h3>
            <form method="POST">
                <input type="hidden" name="faq_id" value="<?php echo $edit_faq['id'] ?? ''; ?>">
                
                <div style="margin-bottom: 15px;">
                    <label style="font-weight:bold;">Question</label>
                    <input type="text" name="question" class="form-control" required value="<?php echo htmlspecialchars($edit_faq['question'] ?? ''); ?>" style="width:100%; padding:10px; border:1px solid #ddd; border-radius:4px;">
                </div>
                
                <div style="margin-bottom: 15px;">
                    <label style="font-weight:bold;">Answer</label>
                    <textarea name="answer" rows="3" class="form-control" required style="width:100%; padding:10px; border:1px solid #ddd; border-radius:4px;"><?php echo htmlspecialchars($edit_faq['answer'] ?? ''); ?></textarea>
                </div>

                <div style="margin-bottom: 15px;">
                    <label style="font-weight:bold;">Sort Order</label>
                    <input type="number" name="sort_order" value="<?php echo $edit_faq['sort_order'] ?? 0; ?>" style="width:80px; padding:8px; border:1px solid #ddd; border-radius:4px;">
                </div>

                <button type="submit" name="save_faq" class="btn btn-primary" style="background:#2c3e50; color:white; border:none; padding:10px 20px; cursor:pointer; border-radius:4px;">
                    <?php echo $edit_faq ? "Update FAQ" : "Add FAQ"; ?>
                </button>
                <?php if($edit_faq): ?>
                    <a href="faqs.php" style="color:red; margin-left:10px;">Cancel</a>
                <?php endif; ?>
            </form>
        </div>

        <div style="background: white; border-radius: 8px; overflow: hidden; box-shadow: 0 2px 5px rgba(0,0,0,0.05);">
            <?php foreach($faqs as $f): ?>
                <div class="faq-item">
                    <div>
                        <strong style="color:#2c3e50;">Q: <?php echo htmlspecialchars($f['question']); ?></strong>
                        <p style="margin:5px 0 0; color:#666; font-size:14px;"><?php echo htmlspecialchars($f['answer']); ?></p>
                    </div>
                    <div style="min-width: 120px; text-align: right;">
                        <small style="background:#eee; padding:2px 6px; border-radius:4px;">Order: <?php echo $f['sort_order']; ?></small>
                        <a href="faqs.php?edit=<?php echo $f['id']; ?>" style="color:blue; margin-left:10px;"><i class="fas fa-edit"></i></a>
                        <a href="faqs.php?delete=<?php echo $f['id']; ?>" style="color:red; margin-left:10px;" onclick="return confirm('Delete?')"><i class="fas fa-trash"></i></a>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>

    </div>

</body>
</html>