<?php
session_start();
if (!isset($_SESSION['admin_logged_in'])) { header("Location: login.php"); exit(); }
include '../db_connect.php';

// Handle Add Service
if (isset($_POST['add_service'])) {
    $cat = $_POST['category'];
    $title = $_POST['title'];
    $items = $_POST['items']; // Stored as comma separated text
    
    $stmt = $conn->prepare("INSERT INTO services (category, title, items) VALUES (?, ?, ?)");
    $stmt->bind_param("sss", $cat, $title, $items);
    $stmt->execute();
    header("Location: services.php");
}

// Handle Delete
if (isset($_GET['delete'])) {
    $id = $_GET['delete'];
    $conn->query("DELETE FROM services WHERE id=$id");
    header("Location: services.php");
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Manage Services</title>
    <script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-gray-50 p-6">
    <nav class="mb-6 flex gap-4">
        <a href="dashboard.php" class="text-blue-600 font-bold">← Back to Dashboard</a>
    </nav>

    <div class="grid md:grid-cols-3 gap-8">
        <div class="bg-white p-6 rounded shadow-md h-fit">
            <h2 class="text-xl font-bold mb-4">Add Treatment Category</h2>
            <form method="POST" class="space-y-4">
                <div>
                    <label class="block text-sm text-gray-600">Department</label>
                    <select name="category" class="w-full border p-2 rounded">
                        <option value="Surgical">Surgical & Laparoscopy</option>
                        <option value="Cosmetology">Skin & Cosmetology</option>
                    </select>
                </div>
                <div>
                    <label class="block text-sm text-gray-600">Title (e.g. Urology)</label>
                    <input type="text" name="title" required class="w-full border p-2 rounded">
                </div>
                <div>
                    <label class="block text-sm text-gray-600">Treatments (Comma separated)</label>
                    <textarea name="items" rows="3" placeholder="e.g. Kidney Stones, Prostate, Stricture" required class="w-full border p-2 rounded"></textarea>
                </div>
                <button type="submit" name="add_service" class="w-full bg-teal-600 text-white py-2 rounded hover:bg-teal-700">Add Service</button>
            </form>
        </div>

        <div class="md:col-span-2 space-y-4">
            <h2 class="text-xl font-bold">Current Services</h2>
            <?php
            $result = $conn->query("SELECT * FROM services ORDER BY category DESC");
            while($row = $result->fetch_assoc()) {
                $color = ($row['category'] == 'Surgical') ? 'border-teal-500' : 'border-pink-500';
                echo '<div class="bg-white p-4 rounded shadow border-l-4 ' . $color . ' flex justify-between items-start">';
                echo '<div>';
                echo '<span class="text-xs font-bold uppercase text-gray-400">' . $row['category'] . '</span>';
                echo '<h3 class="font-bold text-lg text-gray-800">' . $row['title'] . '</h3>';
                echo '<p class="text-gray-600 mt-1">' . $row['items'] . '</p>';
                echo '</div>';
                echo '<a href="services.php?delete=' . $row['id'] . '" class="text-red-500 text-sm hover:underline" onclick="return confirm(\'Delete this category?\')">Delete</a>';
                echo '</div>';
            }
            ?>
        </div>
    </div>
</body>
</html>