<?php
// Filename: admin_quotes.php
require 'config.php';
requireLogin();

// 1. PERMISSION CHECK
$uid = $_SESSION['user_id'];
$user_roles = explode(',', $_SESSION['role'] ?? '');
$allowed_roles = ['admin', 'principal', 'assistant'];

if (empty(array_intersect($user_roles, $allowed_roles))) {
    header("Location: dashboard.php"); exit;
}

$msg = ''; $msg_type = '';

// 2. ACTIONS
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['action']) && $_POST['action'] === 'save') {
        $id = $_POST['quote_id'] ?? '';
        $content = trim($_POST['content']);
        $author = trim($_POST['author']);

        if ($content) {
            try {
                if ($id) {
                    $stmt = $pdo->prepare("UPDATE quotes SET content=?, author=? WHERE id=?");
                    $stmt->execute([$content, $author, $id]);
                    $msg = "Quote updated.";
                } else {
                    $stmt = $pdo->prepare("INSERT INTO quotes (content, author) VALUES (?, ?)");
                    $stmt->execute([$content, $author]);
                    $msg = "Quote added.";
                }
                $msg_type = "success";
            } catch (Exception $e) {
                $msg = "Error: " . $e->getMessage();
                $msg_type = "danger";
            }
        }
    }

    if (isset($_POST['action']) && $_POST['action'] === 'delete') {
        $pdo->prepare("DELETE FROM quotes WHERE id=?")->execute([$_POST['delete_id']]);
        $msg = "Quote deleted."; $msg_type = "warning";
    }
}

// 3. FETCH DATA
$quotes = $pdo->query("SELECT * FROM quotes ORDER BY id DESC")->fetchAll();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>Daily Quotes</title>
    <link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;500;600;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.1/font/bootstrap-icons.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
    <style>
        :root { --primary-grad: <?= getHeaderTheme() ?>; ... }
		body { font-family: 'Plus Jakarta Sans', sans-serif; background-color: var(--bg-color); padding-bottom: 90px; }
        .app-header { background: var(--primary-grad); padding: 30px 20px 80px 20px; border-radius: 0 0 35px 35px; color: white; text-align: center; }
        .floating-card { background: white; margin: -60px 20px 20px 20px; border-radius: 24px; padding: 20px; box-shadow: 0 15px 35px rgba(0,0,0,0.08); position: relative; z-index: 10; }
        
        .quote-item { 
            background: white; border-radius: 16px; padding: 20px; margin-bottom: 15px; 
            border: 1px solid #f1f5f9; position: relative; transition: transform 0.2s;
        }
        .quote-item:active { transform: scale(0.98); background: #f8fafc; }
        .quote-text { font-size: 1rem; font-style: italic; color: #334155; margin-bottom: 8px; line-height: 1.5; }
        .quote-author { font-size: 0.8rem; font-weight: 700; color: #64748b; text-transform: uppercase; letter-spacing: 0.5px; }
        .quote-icon { position: absolute; top: 15px; right: 20px; font-size: 2rem; color: #f1f5f9; }

        .btn-action { width: 35px; height: 35px; border-radius: 10px; display: flex; align-items: center; justify-content: center; font-size: 0.9rem; border: none; }
        .btn-edit { background: #e0f2fe; color: #0ea5e9; }
        .btn-del { background: #fee2e2; color: #ef4444; }

        .form-control { border-radius: 12px; padding: 12px; border: 1px solid #e2e8f0; background: #f8fafc; }
        .form-control:focus { border-color: #8B5CF6; background: white; box-shadow: 0 0 0 3px rgba(139, 92, 246, 0.1); }
        
        .bottom-nav { position: fixed; bottom: 0; left: 0; right: 0; background: white; padding: 10px 30px 18px 30px; display: flex; justify-content: space-between; border-radius: 30px 30px 0 0; box-shadow: 0 -5px 20px rgba(0,0,0,0.05); z-index: 100; }
        .nav-btn { display: flex; flex-direction: column; align-items: center; color: #94a3b8; text-decoration: none; font-size: 0.7rem; font-weight: 600; }
        .nav-btn.active { color: #8B5CF6; }
        .nav-btn i { font-size: 1.4rem; margin-bottom: 4px; }
    </style>
</head>
<body>

    <div class="app-header">
        <div class="d-flex justify-content-between align-items-center mb-3">
            <a href="admin_dashboard.php" class="text-white"><i class="bi bi-arrow-left fs-4"></i></a>
            <div class="fw-bold fs-5">Daily Quotes</div>
            <div style="width: 24px;"></div>
        </div>
        <div class="opacity-75 small">Inspiration for Dashboard</div>
    </div>

    <div class="floating-card">
        <?php if($msg): ?><div class="alert alert-<?= $msg_type ?> border-0 shadow-sm rounded-3 text-center small mb-3"><?= $msg ?></div><?php endif; ?>
        
        <div class="d-flex justify-content-between align-items-center mb-2">
            <h6 class="fw-bold text-dark m-0">Library (<?= count($quotes) ?>)</h6>
            <button class="btn btn-primary btn-sm rounded-pill fw-bold px-3 shadow-sm" onclick="openModal()">
                <i class="bi bi-plus-lg me-1"></i> Add New
            </button>
        </div>
    </div>

    <div class="px-3 pb-4">
        <?php if(empty($quotes)): ?>
            <div class="text-center py-5 text-muted small opacity-50"><i class="bi bi-chat-quote display-1"></i><br>No quotes added yet.</div>
        <?php else: ?>
            <div class="d-flex flex-column gap-3">
                <?php foreach($quotes as $q): ?>
                <div class="quote-item">
                    <i class="bi bi-quote quote-icon"></i>
                    <div class="quote-text">"<?= htmlspecialchars($q['content']) ?>"</div>
                    <div class="d-flex justify-content-between align-items-end">
                        <div class="quote-author">&mdash; <?= htmlspecialchars($q['author'] ?: 'Unknown') ?></div>
                        <div class="d-flex gap-2">
                            <button class="btn-action btn-edit" onclick='editQuote(<?= json_encode($q) ?>)'><i class="bi bi-pencil-fill"></i></button>
                            <form method="POST" onsubmit="return confirm('Delete quote?')" style="margin:0;">
                                <input type="hidden" name="action" value="delete">
                                <input type="hidden" name="delete_id" value="<?= $q['id'] ?>">
                                <button class="btn-action btn-del"><i class="bi bi-trash-fill"></i></button>
                            </form>
                        </div>
                    </div>
                </div>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>
    </div>

    <div class="modal fade" id="quoteModal" tabindex="-1">
        <div class="modal-dialog modal-dialog-centered">
            <div class="modal-content border-0 shadow rounded-4">
                <div class="modal-header border-0 bg-primary text-white">
                    <h6 class="modal-title fw-bold" id="modalTitle">Add Quote</h6>
                    <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
                </div>
                <form method="POST">
                    <input type="hidden" name="action" value="save">
                    <input type="hidden" name="quote_id" id="inp_id">
                    <div class="modal-body p-4">
                        <div class="mb-3">
                            <label class="form-label small fw-bold text-muted">Quote Text</label>
                            <textarea name="content" id="inp_content" class="form-control" rows="3" placeholder="Type quote here..." required></textarea>
                        </div>
                        <div class="mb-2">
                            <label class="form-label small fw-bold text-muted">Author</label>
                            <input type="text" name="author" id="inp_author" class="form-control" placeholder="Optional">
                        </div>
                    </div>
                    <div class="modal-footer border-0 pt-0">
                        <button type="button" class="btn btn-light text-muted fw-bold rounded-pill" data-bs-dismiss="modal">Cancel</button>
                        <button type="submit" class="btn btn-primary fw-bold rounded-pill px-4 flex-grow-1">Save</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

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

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        const modal = new bootstrap.Modal(document.getElementById('quoteModal'));

        function openModal() {
            document.getElementById('modalTitle').innerText = "Add Quote";
            document.getElementById('inp_id').value = "";
            document.getElementById('inp_content').value = "";
            document.getElementById('inp_author').value = "";
            modal.show();
        }

        function editQuote(q) {
            document.getElementById('modalTitle').innerText = "Edit Quote";
            document.getElementById('inp_id').value = q.id;
            document.getElementById('inp_content').value = q.content;
            document.getElementById('inp_author').value = q.author;
            modal.show();
        }
    </script>
</body>
</html>