<?php
session_start();
if (!isset($_SESSION['admin_logged_in'])) {
    header("Location: login.php");
    exit;
}
include '../includes/db.php';

// Handle Delete
if (isset($_GET['delete'])) {
    $id = intval($_GET['delete']);
    $conn->query("DELETE FROM services WHERE id=$id");
    header("Location: manage_services.php");
    exit;
}

// Fetch Services
$result = $conn->query("SELECT * FROM services ORDER BY created_at DESC");
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Manage Services</title>
    <link rel="shortcut icon" href="/bm/BM LOGO.jpg" type="image/jpeg">
    <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">
    <style>body{ display: flex; } .sidebar{ width: 280px; background: #343a40; min-height: 100vh; color: white; } .sidebar a{ color: white; text-decoration: none; display: block; padding: 10px; } .sidebar a:hover{ background: #495057; } .content{ flex: 1; padding: 20px; }</style>
</head>
<body>
    <div class="sidebar p-3">
        
        <a href="dashboard.php"  style="width: 200px;" aria-live="#5118e0ff" class="d-flex align-items-center mb-3 mb-md-0 me-md-auto text-white text-decoration-none">
                <span class="fs-4">Admin Panel</span>
                
                
            </a>

        <ul class="nav nav-pills flex-column mb-auto">
                <li class="nav-item">
                    <a href="dashboard.php" class="active"><i class="fas fa-tachometer-alt me-2"></i> Dashboard</a>
                </li>
                <li>
                    <a href="manage_services.php"><i class="fas fa-tools me-2"></i> Manage Services</a>
                </li>
                <li>
                    <a href="manage_pages.php"><i class="fas fa-file-alt me-2"></i> Manage Content</a>
                </li>
                <li>
                    <a href="manage_blogs.php"><i class="fas fa-blog me-2"></i> Manage Blogs</a>
                </li>
                <li>
                    <a href="job_applications.php"><i class="fas fa-briefcase me-2"></i> Job Applications</a>
                </li>
                <li>
                    <a href="contact_messages.php"><i class="fas fa-envelope me-2"></i> Messages</a>
                </li>
                <li>
                    <a href="wp/post.php"><i class="fas fa-pen me-2"></i> Add New Post</a>
                </li>
                <li>
                    <a href="#"><i class="fas fa-newspaper me-2"></i> Blog</a>
                </li>
                <li>
                    <a href="logout.php" class="text-danger"><i class="fas fa-sign-out-alt me-2"></i> Logout</a>
                </li>
            </ul>
    </div>

    <div class="content">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h2>Manage Services</h2>
            <a href="service_form.php" class="btn btn-primary"><i class="fas fa-plus"></i> Add New Service</a>
        </div>

        <table class="table table-bordered table-striped">
            <thead class="table-dark">
                <tr>
                    <th>ID</th>
                    <th>Image</th>
                    <th>Title</th>
                    <th>Description</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php while($row = $result->fetch_assoc()): ?>
                <tr>
                    <td><?php echo $row['id']; ?></td>
                    <td><img src="../<?php echo $row['image_url']; ?>" alt="" width="50"></td>
                    <td><?php echo $row['title']; ?></td>
                    <td><?php echo substr($row['description'], 0, 50) . '...'; ?></td>
                    <td>
                        <a href="service_form.php?id=<?php echo $row['id']; ?>" class="btn btn-sm btn-warning"><i class="fas fa-edit"></i></a>
                        <a href="manage_services.php?delete=<?php echo $row['id']; ?>" class="btn btn-sm btn-danger" onclick="return confirm('Are you sure?')"><i class="fas fa-trash"></i></a>
                    </td>
                </tr>
                <?php endwhile; ?>
            </tbody>
        </table>
    </div>
</body>
</html>
