<?php
require_once 'auth_check.php';
require_once 'db.php';
$conn = $GLOBALS['conn'] ?? $conn ?? null;

$message = '';$error = '';

// Form Submission Logic
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_policy'])) {
    $title = trim($_POST['title'] ?? '');
    $description = trim($_POST['description'] ?? '');

    if (!empty($title)) {
        $stmt = mysqli_prepare($conn, "INSERT INTO `guidelines` (title, description, status) VALUES (?, ?, 'active')");
        if ($stmt) {
            mysqli_stmt_bind_param($stmt, "ss", $title,$description);
            if (mysqli_stmt_execute($stmt)) {$message = "Guideline successfully add ho gayi!";
            } else {
                $error = "Database Execution Error: " . mysqli_stmt_error($stmt);
            }
            mysqli_stmt_close($stmt);
        } else {
            $error = "Database Prepare Error: " . mysqli_error($conn);
        }
    } else {
        $error = "Title field zaroori hai.";
    }
}

// Delete Logic
if (isset($_GET['del'])) {
    $id = intval($_GET['del']);
    $del_stmt = mysqli_prepare($conn, "DELETE FROM `guidelines` WHERE id = ?");
    if ($del_stmt) {
        mysqli_stmt_bind_param($del_stmt, "i", $id);
        mysqli_stmt_execute($del_stmt);
        mysqli_stmt_close($del_stmt);
    }
    header("Location: manage-guidelines.php?msg=deleted");
    exit();
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Manage Guidelines | Wings Admin</title>
    
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.2/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600;700&display=swap" rel="stylesheet">

    <style>
        :root {
            --brand-green: #12521f;
            --brand-saffron: #D4A359;
            --dark-navy: #0F172A;
            --sidebar-width: 260px;
            --bg-light: #F8FAFC;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Poppins', sans-serif;
        }

        body {
            background-color: var(--bg-light);
            display: flex;
            min-height: 100vh;
        }

        /* SIDEBAR STYLING */
        .admin-sidebar {
            width: var(--sidebar-width);
            background-color: var(--dark-navy);
            color: #ffffff;
            position: fixed;
            top: 0;
            bottom: 0;
            left: 0;
            display: flex;
            flex-direction: column;
            z-index: 100;
            box-shadow: 4px 0 15px rgba(0, 0, 0, 0.05);
        }

        .sidebar-brand {
            padding: 24px 20px;
            background: rgba(0, 0, 0, 0.25);
            text-align: center;
            border-bottom: 1px solid rgba(255, 255, 255, 0.05);
        }

        .sidebar-brand h2 {
            font-size: 22px;
            color: var(--brand-saffron);
            letter-spacing: 1.5px;
            font-weight: 700;
        }

        .sidebar-brand p {
            font-size: 11px;
            color: #94A3B8;
            margin-top: 2px;
        }

        .sidebar-menu {
            list-style: none;
            padding: 20px 0;
            flex: 1;
            overflow-y: auto;
        }

        .sidebar-menu li a {
            display: flex;
            align-items: center;
            gap: 14px;
            padding: 13px 24px;
            color: #CBD5E1;
            text-decoration: none;
            font-size: 14px;
            font-weight: 500;
            transition: all 0.25s ease;
        }

        .sidebar-menu li a:hover, 
        .sidebar-menu li.active a {
            background: rgba(255, 255, 255, 0.08);
            color: #ffffff;
            border-left: 4px solid var(--brand-saffron);
        }

        .sidebar-menu li a i {
            font-size: 16px;
            width: 20px;
            text-align: center;
        }

        /* MAIN WRAPPER */
        .admin-main {
            margin-left: var(--sidebar-width);
            flex: 1;
            display: flex;
            flex-direction: column;
            width: calc(100% - var(--sidebar-width));
        }

        .admin-topbar {
            background: #ffffff;
            height: 70px;
            padding: 0 35px;
            display: flex;
            align-items: center;
            justify-content: space-between;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.03);
            position: sticky;
            top: 0;
            z-index: 99;
        }

        .topbar-title {
            font-size: 15px;
            color: var(--dark-navy);
            font-weight: 600;
        }

        .btn-logout {
            padding: 8px 16px;
            background: #FEF2F2;
            color: #EF4444;
            border-radius: 8px;
            text-decoration: none;
            font-size: 13px;
            font-weight: 600;
            display: inline-flex;
            align-items: center;
            gap: 6px;
            transition: all 0.2s ease;
            border: 1px solid #FCA5A5;
        }

        .btn-logout:hover {
            background: #EF4444;
            color: #ffffff;
        }

        .admin-content {
            padding: 35px;
        }

        .page-title {
            font-size: 22px;
            color: var(--dark-navy);
            margin-bottom: 25px;
            font-weight: 700;
        }

        .card {
            background: #ffffff;
            padding: 24px;
            border-radius: 14px;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.03);
            border: 1px solid #E2E8F0;
            margin-bottom: 30px;
        }

        .form-group {
            margin-bottom: 18px;
        }

        .form-group label {
            display: block;
            font-size: 13px;
            font-weight: 600;
            color: var(--dark-navy);
            margin-bottom: 8px;
        }

        .form-group input, 
        .form-group textarea {
            width: 100%;
            padding: 12px 14px;
            border: 1.5px solid #CBD5E1;
            border-radius: 10px;
            font-size: 14px;
            outline: none;
            transition: all 0.25s ease;
            background-color: #fff;
        }

        .form-group input:focus, 
        .form-group textarea:focus {
            border-color: var(--brand-green);
            box-shadow: 0 0 0 3px rgba(18, 82, 31, 0.1);
        }

        .btn-submit {
            background: var(--brand-green);
            color: #ffffff;
            padding: 12px 22px;
            border: none;
            border-radius: 10px;
            font-weight: 600;
            cursor: pointer;
            font-size: 14px;
            transition: background 0.2s ease;
            box-shadow: 0 4px 12px rgba(18, 82, 31, 0.15);
            display: inline-flex;
            align-items: center;
            gap: 8px;
        }

        .btn-submit:hover {
            background: #0d3d17;
        }

        .table-container {
            background: #ffffff;
            border-radius: 14px;
            border: 1px solid #E2E8F0;
            overflow-x: auto;
            margin-bottom: 30px;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.03);
        }

        table {
            width: 100%;
            border-collapse: collapse;
            font-size: 13.5px;
            text-align: left;
            white-space: nowrap;
        }

        th {
            background: #F1F5F9;
            color: var(--dark-navy);
            font-weight: 600;
            padding: 14px 18px;
            border-bottom: 1px solid #E2E8F0;
        }

        td {
            padding: 14px 18px;
            border-bottom: 1px solid #E2E8F0;
            color: #334155;
            vertical-align: middle;
        }

        tr:hover {
            background-color: #F8FAFC;
        }

        .btn-del {
            color: #EF4444;
            background: #FEF2F2;
            padding: 6px 12px;
            border-radius: 6px;
            text-decoration: none;
            font-size: 12px;
            font-weight: 600;
            transition: all 0.2s ease;
            display: inline-flex;
            align-items: center;
            gap: 5px;
        }

        .btn-del:hover {
            background: #EF4444;
            color: #ffffff;
        }

        .alert-success {
            padding: 12px 16px;
            background: #DCFCE7;
            color: #166534;
            border-radius: 8px;
            font-size: 13px;
            margin-bottom: 20px;
            border: 1px solid #BBF7D0;
        }

        .alert-error {
            padding: 12px 16px;
            background: #FEF2F2;
            color: #991B1B;
            border-radius: 8px;
            font-size: 13px;
            margin-bottom: 20px;
            border: 1px solid #FCA5A5;
        }

        /* RESPONSIVE DESIGN */
        @media (max-width: 768px) {
            .admin-sidebar {
                width: 70px;
            }
            .sidebar-brand h2,
            .sidebar-brand p,
            .sidebar-menu li a span {
                display: none;
            }
            .admin-main {
                margin-left: 70px;
                width: calc(100% - 70px);
            }
            .sidebar-menu li a {
                justify-content: center;
                padding: 16px;
            }
            .admin-content {
                padding: 20px;
            }
            .admin-topbar {
                padding: 0 20px;
            }
        }

        /* PREMIUM MOBILE RESPONSIVE OVERRIDES */
        html, body { overflow-x: hidden; }
        .admin-main { min-width: 0; }
        .admin-content { min-width: 0; }
        .card, .table-container { width: 100%; max-width: 100%; }
        .form-group input, .form-group textarea, .btn-submit, .btn-del, .btn-logout {
            -webkit-tap-highlight-color: transparent;
        }
        .form-group input, .form-group textarea { min-height: 46px; }
        .form-group textarea { resize: vertical; min-height: 120px; }
        .btn-submit, .btn-del, .btn-logout { min-height: 44px; }
        table { min-width: 650px; }
        .table-container { -webkit-overflow-scrolling: touch; scrollbar-width: thin; }

        @media (max-width: 992px) {
            .admin-sidebar {
                transform: translateX(-100%);
                width: 260px;
                transition: transform .3s ease;
            }
            .admin-sidebar.sidebar-open { transform: translateX(0); }
            .admin-main {
                margin-left: 0;
                width: 100%;
            }
            .mobile-nav-toggle {
                display: inline-flex;
                align-items: center;
                justify-content: center;
                width: 42px;
                height: 42px;
                border: 0;
                border-radius: 10px;
                background: var(--dark-navy);
                color: #fff;
                cursor: pointer;
            }
            .sidebar-backdrop {
                display: none;
                position: fixed;
                inset: 0;
                background: rgba(15,23,42,.58);
                z-index: 99;
                backdrop-filter: blur(2px);
            }
            .sidebar-backdrop.show { display: block; }
            .admin-topbar {
                height: 64px;
                padding: 0 16px;
            }
            .admin-content { padding: 22px 16px; }
        }

        @media (max-width: 768px) {
            .topbar-title {
                font-size: 13px;
                line-height: 1.25;
                max-width: calc(100vw - 120px);
            }
            .topbar-title i { margin-right: 4px !important; }
            .btn-logout {
                padding: 9px 10px;
                font-size: 0;
                gap: 0;
                min-width: 42px;
                justify-content: center;
            }
            .btn-logout i { font-size: 15px; }
            .page-title {
                font-size: 20px;
                margin-bottom: 18px;
                line-height: 1.3;
            }
            .card {
                padding: 17px;
                border-radius: 13px;
                margin-bottom: 20px;
            }
            .card h3, .admin-content > h3 {
                line-height: 1.35;
            }
            .form-group { margin-bottom: 15px; }
            .form-group label { font-size: 12px; margin-bottom: 6px; }
            .form-group input, .form-group textarea {
                font-size: 13px;
                padding: 11px 12px;
            }
            .btn-submit {
                width: 100%;
                justify-content: center;
                padding: 12px 16px;
            }
            .table-container {
                border-radius: 12px;
                margin-bottom: 20px;
            }
            table { font-size: 12px; min-width: 600px; }
            th, td { padding: 11px 12px; }
            .btn-del {
                padding: 8px 10px;
                font-size: 11px;
                white-space: nowrap;
            }
            .alert-success, .alert-error {
                padding: 11px 12px;
                font-size: 12px;
                line-height: 1.5;
                border-radius: 9px;
            }
        }

        @media (max-width: 380px) {
            .admin-topbar { padding: 0 11px; }
            .admin-content { padding: 16px 10px; }
            .topbar-title {
                font-size: 11.5px;
                max-width: calc(100vw - 100px);
            }
            .page-title { font-size: 18px; }
            .card { padding: 14px; }
            .card h3, .admin-content > h3 { font-size: 14px !important; }
            .form-group input, .form-group textarea { font-size: 12.5px; }
            .btn-submit { font-size: 13px; }
            table { min-width: 570px; }
            th, td { padding: 10px; }
        }

        @media (prefers-reduced-motion: reduce) {
            *, *::before, *::after { scroll-behavior: auto !important; transition: none !important; }
        }
    </style>
</head>
<body>
    <div class="sidebar-backdrop" id="sidebarBackdrop" onclick="toggleSidebar()"></div>

    <?php include 'sidebar.php'; ?>

    <div class="admin-main">
        <header class="admin-topbar">
            <div style="display:flex;align-items:center;gap:10px;">
                <button type="button" class="mobile-nav-toggle" onclick="toggleSidebar()" aria-label="Open navigation">
                    <i class="fas fa-bars"></i>
                </button>
                <div class="topbar-title">
                <i class="fas fa-clipboard-list" style="color: var(--brand-green); margin-right: 6px;"></i> Passenger Guidelines Management
                </div>
            </div>
            <a href="logout.php" class="btn-logout"><i class="fas fa-power-off"></i> Logout</a>
        </header>

        <main class="admin-content">
            <h1 class="page-title">Manage Passenger Guidelines</h1>

            <?php if (!empty($message) || (isset($_GET['msg']) &&$_GET['msg'] === 'deleted')): ?>
                <div class="alert-success"><?php echo !empty($message) ?$message : 'Record successfully delete ho gaya!'; ?></div>
            <?php endif; ?>
            <?php if (!empty($error)): ?>
                <div class="alert-error"><?php echo $error; ?></div>
            <?php endif; ?>

            <!-- ADD FORM -->
            <div class="card">
                <h3 style="margin-bottom: 20px; font-size: 16px; color: var(--dark-navy); font-weight: 600;">Add New Guideline Item</h3>
                <form action="" method="POST">
                    <div class="form-group">
                        <label>Title / Heading</label>
                        <input type="text" name="title" placeholder="e.g. Yatra Registration Requirements" required>
                    </div>
                    <div class="form-group">
                        <label>Description / Details</label>
                        <textarea name="description" rows="4" placeholder="Enter full guideline details here..."></textarea>
                    </div>
                    <button type="submit" name="add_policy" class="btn-submit"><i class="fas fa-plus"></i> Save Guideline</button>
                </form>
            </div>

            <!-- DISPLAY TABLE -->
            <h3 style="margin: 25px 0 12px; font-size: 16px; color: var(--dark-navy); font-weight: 600;">All Guidelines List</h3>
            <div class="table-container">
                <table>
                    <thead>
                        <tr>
                            <th>#ID</th>
                            <th>Title</th>
                            <th>Description</th>
                            <th>Action</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php 
                        $data = mysqli_query($conn, "SELECT id, title, description FROM `guidelines` ORDER BY id DESC");
                        if ($data && mysqli_num_rows($data) > 0): 
                            while ($r = mysqli_fetch_assoc($data)): 
                        ?>
                                <tr>
                                    <td>#<?php echo $r['id']; ?></td>
                                    <td><strong><?php echo htmlspecialchars($r['title']); ?></strong></td>
                                    <td><?php echo htmlspecialchars($r['description'] ?? ''); ?></td>
                                    <td>
                                        <a href="manage-guidelines.php?del=<?php echo $r['id']; ?>" class="btn-del" onclick="return confirm('Delete this entry?')"><i class="fas fa-trash"></i> Delete</a>
                                    </td>
                                </tr>
                            <?php endwhile; 
                        else: ?>
                            <tr><td colspan="4" style="text-align:center; color:#94A3B8; padding: 25px;">No entries found.</td></tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>

        </main>
    </div>


    <script>
        function toggleSidebar() {
            const sidebar = document.querySelector('.admin-sidebar');
            const backdrop = document.getElementById('sidebarBackdrop');
            if (!sidebar) return;
            sidebar.classList.toggle('sidebar-open');
            if (backdrop) backdrop.classList.toggle('show');
        }

        document.addEventListener('click', function(e) {
            if (window.innerWidth > 992) return;
            const sidebar = document.querySelector('.admin-sidebar');
            const toggle = document.querySelector('.mobile-nav-toggle');
            const backdrop = document.getElementById('sidebarBackdrop');
            if (!sidebar || !backdrop) return;
            if (sidebar.classList.contains('sidebar-open') &&
                !sidebar.contains(e.target) && !toggle.contains(e.target)) {
                sidebar.classList.remove('sidebar-open');
                backdrop.classList.remove('show');
            }
        });

        window.addEventListener('resize', function() {
            if (window.innerWidth > 992) {
                const sidebar = document.querySelector('.admin-sidebar');
                const backdrop = document.getElementById('sidebarBackdrop');
                if (sidebar) sidebar.classList.remove('sidebar-open');
                if (backdrop) backdrop.classList.remove('show');
            }
        });
    </script>
</body>
</html>