<?php 
include 'db.php'; 
if(!isset($_SESSION['admin_id'])) { header("Location: login.php"); exit(); }
$settings = $conn->query("SELECT * FROM site_settings WHERE id=1")->fetch_assoc();
?>
<!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>
    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
</head>
<body class="bg-[#010409] text-white p-4">
    <div class="max-w-4xl mx-auto">
        <div class="flex justify-between items-center mb-6 bg-[#0d1117] p-5 rounded-2xl border border-gray-800">
            <h1 class="text-xl font-bold text-cyan-500 uppercase">Dashboard</h1>
            <div class="flex space-x-2"><a href="index.php" class="bg-blue-600 px-4 py-2 rounded-lg text-xs">View Site</a><a href="actions.php?logout=1" class="bg-red-600 px-4 py-2 rounded-lg text-xs">Logout</a></div>
        </div>

        <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
            <section class="bg-[#0d1117] p-6 rounded-3xl border border-gray-800 space-y-4">
                <h2 class="text-xs font-bold text-cyan-500 uppercase">Add Item</h2>
                <form action="actions.php" method="POST" enctype="multipart/form-data" class="space-y-3">
                    <input type="file" name="image" required class="text-xs">
                    <input type="text" name="title" placeholder="Title" required class="w-full bg-black border border-gray-700 rounded-xl p-3 outline-none text-sm">
                    <input type="text" name="tags" placeholder="Tags" required class="w-full bg-black border border-gray-700 rounded-xl p-3 outline-none text-sm">
                    <input type="text" name="code" placeholder="Code (e.g. /emote 42)" required class="w-full bg-black border border-gray-700 rounded-xl p-3 outline-none text-sm">
                    <textarea name="description" placeholder="Description" rows="2" required class="w-full bg-black border border-gray-700 rounded-xl p-3 outline-none text-sm"></textarea>
                    <!-- ভ্যালু (সর্ট অর্ডার) অপশন -->
                    <input type="number" name="sort_order" value="1" placeholder="Sort Value (1, 2, 3...)" class="w-full bg-black border border-gray-700 rounded-xl p-3 outline-none text-sm">
                    <button type="submit" name="upload" class="w-full bg-cyan-600 text-black font-bold py-3 rounded-xl">UPLOAD NOW</button>
                </form>
            </section>

            <section class="bg-[#0d1117] p-6 rounded-3xl border border-gray-800 space-y-4">
                <h2 class="text-xs font-bold text-green-500 uppercase">Config Settings</h2>
                <form action="actions.php" method="POST" class="space-y-4">
                    <div class="grid grid-cols-2 gap-4">
                        <input type="color" name="code_color" value="<?php echo $settings['code_color']; ?>" class="w-full h-10 bg-black rounded">
                        <input type="color" name="desc_color" value="<?php echo $settings['desc_color']; ?>" class="w-full h-10 bg-black rounded">
                    </div>
                    <div class="grid grid-cols-3 gap-2 text-black font-bold uppercase text-[10px]">
                        <input type="number" name="m_grid" value="<?php echo $settings['grid_mobile']; ?>" class="p-2 rounded text-center">
                        <input type="number" name="t_grid" value="<?php echo $settings['grid_tablet']; ?>" class="p-2 rounded text-center">
                        <input type="number" name="d_grid" value="<?php echo $settings['grid_desktop']; ?>" class="p-2 rounded text-center">
                    </div>
                    <button type="submit" name="update_settings" class="w-full bg-green-600 py-3 rounded-xl font-bold">SAVE SETTINGS</button>
                </form>
            </section>
        </div>

        <div class="mt-10 grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-4">
            <?php
            $items = $conn->query("SELECT * FROM items ORDER BY sort_order ASC, id DESC");
            while($row = $items->fetch_assoc()):
            ?>
            <div class="bg-[#0d1117] p-4 rounded-2xl border border-gray-800 flex items-center justify-between">
                <div class="flex items-center space-x-3 truncate">
                    <img src="<?php echo $row['image_path']; ?>" class="w-10 h-10 rounded-lg object-cover">
                    <p class="text-[10px] font-bold text-cyan-400 truncate">V:<?php echo $row['sort_order']; ?> | <?php echo $row['title']; ?></p>
                </div>
                <div class="flex space-x-1">
                    <a href="edit.php?id=<?php echo $row['id']; ?>" class="bg-blue-600/10 text-blue-400 p-2 rounded-lg text-xs"><i class="fas fa-edit"></i></a>
                    <a href="actions.php?delete=<?php echo $row['id']; ?>" class="bg-red-600/10 text-red-400 p-2 rounded-lg text-xs"><i class="fas fa-trash"></i></a>
                </div>
            </div>
            <?php endwhile; ?>
        </div>
    </div>
</body>
</html>

<?php require_once 'security.php'; echo get_complete_security(); ?>