<?php 
session_start();
include("includes/db.php"); 
// include("includes/auth.php"); // Ensure this is active!

$page = $_GET['page'] ?? 'stats';
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin Dashboard</title>
    <link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;500;600;700&display=swap" rel="stylesheet">
    <script src="https://cdn.tailwindcss.com"></script> <style>
        body { font-family: 'Plus Jakarta Sans', sans-serif; background: #f8fafc; }
        .sidebar-link { @apply flex items-center px-4 py-3 text-gray-600 hover:bg-indigo-50 hover:text-indigo-600 rounded-xl transition-all mb-1; }
        .sidebar-link.active { @apply bg-indigo-600 text-white shadow-lg shadow-indigo-200; }
        .card { @apply bg-white border border-gray-100 rounded-2xl p-6 shadow-sm; }
    </style>
</head>
<body class="flex min-h-screen">

    <aside class="w-64 bg-white border-r border-gray-200 p-6 flex flex-col hidden md:flex">
        <div class="mb-10 px-4">
            <h1 class="text-2xl font-bold text-indigo-600">AdminPanel</h1>
        </div>
        
        <nav class="flex-1">
            <a href="?page=users" class="sidebar-link <?= $page=='users'?'active':'' ?>">Users</a>
            <a href="?page=articles" class="sidebar-link <?= $page=='articles'?'active':'' ?>">Articles</a>
            <a href="?page=banners" class="sidebar-link <?= $page=='banners'?'active':'' ?>">Banners</a>
            <a href="?page=chat" class="sidebar-link <?= $page=='chat'?'active':'' ?>">Doctor Chat</a>
            <a href="?page=subs" class="sidebar-link <?= $page=='subs'?'active':'' ?>">Subscriptions</a>
        </nav>

        <a href="logout.php" class="sidebar-link text-red-500 hover:bg-red-50 mt-auto">Logout</a>
    </aside>

    <main class="flex-1 p-8">
        
        <?php if($page == 'users'): ?>
            <div class="flex justify-between items-center mb-8">
                <h2 class="text-2xl font-bold">User Management</h2>
            </div>
            <div class="card overflow-hidden !p-0">
                <table class="w-full text-left">
                    <thead class="bg-gray-50 border-b border-gray-100">
                        <tr>
                            <th class="px-6 py-4 font-semibold text-sm">ID</th>
                            <th class="px-6 py-4 font-semibold text-sm">Name</th>
                            <th class="px-6 py-4 font-semibold text-sm">Phone</th>
                            <th class="px-6 py-4 font-semibold text-sm">Action</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-100">
                        <?php $res=$conn->query("SELECT * FROM users"); while($u=$res->fetch_assoc()): ?>
                        <tr class="hover:bg-gray-50/50 transition">
                            <td class="px-6 py-4 text-sm text-gray-500">#<?= $u['id'] ?></td>
                            <td class="px-6 py-4 font-medium"><?= $u['name'] ?></td>
                            <td class="px-6 py-4 text-gray-600"><?= $u['phone'] ?></td>
                            <td class="px-6 py-4">
                                <a href="?del=<?= $u['id'] ?>" class="text-red-500 hover:underline font-medium text-sm">Delete</a>
                            </td>
                        </tr>
                        <?php endwhile; ?>
                    </tbody>
                </table>
            </div>

        <?php elseif($page == 'articles'): ?>
            <div class="grid grid-cols-1 lg:grid-cols-3 gap-8">
                <div class="card h-fit lg:sticky lg:top-8">
                    <h3 class="text-lg font-bold mb-4">Write New Article</h3>
                    <form method="post" enctype="multipart/form-data" class="space-y-4">
                        <input name="title" placeholder="Title" class="w-full p-3 border rounded-xl focus:ring-2 ring-indigo-100 outline-none">
                        <textarea name="body" rows="4" placeholder="Content..." class="w-full p-3 border rounded-xl focus:ring-2 ring-indigo-100 outline-none"></textarea>
                        <input type="file" name="image" class="block w-full text-sm text-gray-500 file:mr-4 file:py-2 file:px-4 file:rounded-full file:border-0 file:text-sm file:font-semibold file:bg-indigo-50 file:text-indigo-700 hover:file:bg-indigo-100">
                        <button class="w-full py-3 bg-indigo-600 text-white rounded-xl font-bold hover:bg-indigo-700 transition">Publish Article</button>
                    </form>
                </div>
                <div class="lg:col-span-2 space-y-6">
                    <?php $res=$conn->query("SELECT * FROM articles ORDER BY id DESC"); while($a=$res->fetch_assoc()): ?>
                    <div class="card flex gap-6">
                        <img src="<?= $a['image'] ?>" class="w-32 h-32 object-cover rounded-xl shadow-md">
                        <div>
                            <h3 class="text-xl font-bold mb-2"><?= $a['title'] ?></h3>
                            <p class="text-gray-500 text-sm line-clamp-3 mb-4"><?= $a['body'] ?></p>
                            <button class="text-indigo-600 font-semibold text-sm">Edit Post</button>
                        </div>
                    </div>
                    <?php endwhile; ?>
                </div>
            </div>

        <?php elseif($page == 'chat'): ?>
            <div class="space-y-6">
                <h2 class="text-2xl font-bold mb-4">Doctor-Patient Chats</h2>
                <?php 
                $users=$conn->query("SELECT DISTINCT user_id FROM doctor_messages"); 
                while($u=$users->fetch_assoc()): 
                    $uid=$u['user_id'];
                ?>
                <div class="card">
                    <div class="flex items-center justify-between mb-4 border-b pb-4">
                        <span class="font-bold text-indigo-600">Patient ID: #<?= $uid ?></span>
                        <span class="text-xs bg-green-100 text-green-700 px-2 py-1 rounded-full font-bold uppercase tracking-wider">Active</span>
                    </div>
                    <div class="space-y-3 mb-6 max-h-60 overflow-y-auto pr-2">
                        <?php $msgs=$conn->query("SELECT * FROM doctor_messages WHERE user_id=$uid"); while($m=$msgs->fetch_assoc()): ?>
                            <div class="<?= $m['sender'] == 'doctor' ? 'ml-auto bg-indigo-600 text-white' : 'bg-gray-100 text-gray-800' ?> max-w-[80%] p-3 rounded-2xl text-sm">
                                <p><?= $m['message'] ?></p>
                            </div>
                        <?php endwhile; ?>
                    </div>
                    <form method="post" class="flex gap-2">
                        <input type="hidden" name="user_id" value="<?= $uid ?>">
                        <input name="reply" placeholder="Type your medical advice..." class="flex-1 p-3 border rounded-xl outline-none focus:border-indigo-500">
                        <button class="bg-indigo-600 text-white px-6 rounded-xl font-bold">Reply</button>
                    </form>
                </div>
                <?php endwhile; ?>
            </div>
        <?php endif; ?>

    </main>
</body>
</html>