<?php
session_start();
include '../db_config.php';
include '../functions.php';

check_login();
check_role('admin');

$msg = '';

// Handle Actions
if(isset($_POST['action'])) {
    $jid = intval($_POST['job_id']);
    $act = $_POST['action'];
    
    if($act == 'approve') {
        $stmt = $conn->prepare("UPDATE jobs SET status='Open' WHERE id=?");
        $stmt->bind_param("i", $jid);
        $stmt->execute();
        $stmt->close();
        $msg = "Job approved successfully.";
    } elseif($act == 'reject') {
        $stmt = $conn->prepare("UPDATE jobs SET status='Rejected' WHERE id=?");
        $stmt->bind_param("i", $jid);
        $stmt->execute();
        $stmt->close();
        $msg = "Job rejected.";
    } elseif($act == 'delete') {
        $stmt = $conn->prepare("DELETE FROM jobs WHERE id=?");
        $stmt->bind_param("i", $jid);
        $stmt->execute();
        $stmt->close();
        $msg = "Job deleted.";
    }
}

// Get Pending Jobs
$pending_jobs = $conn->query("SELECT j.*, e.company_name FROM jobs j JOIN employer_details e ON j.employer_id = e.user_id WHERE j.status='Pending' ORDER BY j.created_at ASC");

// Get All Jobs (for reference/management)
$all_jobs = $conn->query("SELECT j.*, e.company_name FROM jobs j JOIN employer_details e ON j.employer_id = e.user_id WHERE j.status!='Pending' ORDER BY j.created_at DESC LIMIT 50");
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Manage Jobs | Admin Panel</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <script src="https://unpkg.com/lucide@latest"></script>
    <style> 
        body { font-family: 'Inter', sans-serif; } 
        .glass-panel {
            background: rgba(30, 41, 59, 0.4);
            backdrop-filter: blur(12px);
            border: 1px solid rgba(255, 255, 255, 0.05);
        }
        .job-card {
            transition: all 0.3s ease;
        }
        .job-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 30px -10px rgba(0, 0, 0, 0.5);
            border-color: rgba(99, 102, 241, 0.3);
        }
    </style>
</head>
<body class="bg-slate-950 min-h-screen text-slate-200 selection:bg-indigo-500 selection:text-white">

    <!-- Navbar -->
    <nav class="bg-slate-900 border-b border-slate-800 sticky top-0 z-50">
        <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
            <div class="flex justify-between h-20 items-center">
                <a href="<?= BASE_URL ?>/index.php" class="text-2xl font-bold italic tracking-tighter text-white">
                    Work For <span class="text-indigo-400">Sri Lanka</span>
                    <span class="text-xs font-normal text-white bg-indigo-600 px-2 py-0.5 rounded ml-2 align-middle border border-indigo-500/50">Admin Panel</span>
                </a>
                <div class="flex items-center gap-6">
                    <span class="text-sm font-medium text-slate-400 hidden md:inline">Logged in as: <span class="text-indigo-400 font-mono"><?=$_SESSION['username']?></span></span>
                    <a href="../logout.php" class="text-slate-400 hover:text-red-400 font-medium transition text-sm flex items-center">
                        <i data-lucide="log-out" class="w-4 h-4 mr-2"></i> Logout
                    </a>
                </div>
            </div>
        </div>
    </nav>

    <!-- Header Section -->
    <div class="relative bg-slate-900 py-10 px-4 border-b border-slate-800">
        <div class="absolute top-0 right-0 w-[500px] h-[500px] bg-indigo-600/10 rounded-full blur-[100px] -mt-20 -mr-20 pointer-events-none"></div>
        
        <div class="max-w-7xl mx-auto flex flex-col md:flex-row justify-between items-center gap-4 relative z-10">
            <div>
                <h1 class="text-3xl font-bold mb-2 text-white">Manage Job Postings</h1>
                <p class="text-slate-400">Review and approve pending job posts from employers.</p>
            </div>
             <a href="dashboard.php" class="bg-slate-800 hover:bg-slate-700 text-white px-5 py-2.5 rounded-xl font-bold transition flex items-center border border-slate-700 hover:border-indigo-500 hover:text-indigo-400 shadow-sm text-sm group">
                <i data-lucide="arrow-left" class="w-4 h-4 mr-2 group-hover:-translate-x-1 transition-transform"></i> Back to Dashboard
            </a>
        </div>
    </div>

    <!-- Main Content -->
    <div class="max-w-7xl mx-auto px-4 pb-20 pt-8">
        
        <?php if($msg): ?>
            <div class="bg-emerald-500/10 border border-emerald-500/20 text-emerald-400 px-6 py-4 rounded-xl mb-6 flex items-center shadow-sm animate-fade-in-down">
                <i data-lucide="check-circle" class="w-5 h-5 mr-3 flex-shrink-0"></i>
                <span class="font-bold mr-2">Success!</span> <?=$msg?>
            </div>
        <?php endif; ?>

        <!-- Pending Jobs Section -->
        <div class="mb-12">
            <h2 class="text-xl font-bold text-white mb-6 flex items-center">
                <span class="bg-yellow-500/10 text-yellow-400 p-2 rounded-lg mr-3 border border-yellow-500/20"><i data-lucide="clock" class="w-5 h-5"></i></span>
                Pending Approval <span class="ml-3 bg-indigo-600 text-white text-xs px-2.5 py-1 rounded-full border border-indigo-500/50"><?=$pending_jobs->num_rows?></span>
            </h2>

            <?php if($pending_jobs->num_rows > 0): ?>
            <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
                <?php while($job = $pending_jobs->fetch_assoc()): ?>
                <div class="glass-panel rounded-2xl p-6 border border-slate-700/50 job-card flex flex-col h-full">
                    <div class="flex justify-between items-start mb-4">
                        <div>
                            <h3 class="font-bold text-lg text-white mb-1"><?=$job['title']?></h3>
                            <div class="text-sm text-indigo-400 font-medium flex items-center">
                                <i data-lucide="briefcase" class="w-3 h-3 mr-1"></i> <?=$job['company_name']?>
                            </div>
                        </div>
                        <span class="bg-slate-800 text-slate-300 text-xs font-bold px-2 py-1 rounded border border-slate-700"><?=$job['category']?></span>
                    </div>
                    
                    <div class="bg-slate-900/50 rounded-xl p-3 mb-4 border border-slate-800 flex-grow">
                        <p class="text-slate-400 text-sm line-clamp-4 leading-relaxed"><?=nl2br($job['description'])?></p>
                    </div>
                    
                    <div class="flex items-center text-xs text-slate-500 mb-6 gap-4 border-b border-slate-800 pb-4">
                        <span class="flex items-center"><i data-lucide="map-pin" class="w-3 h-3 mr-1 text-slate-400"></i> <?=$job['location']?></span>
                        <span class="flex items-center"><i data-lucide="calendar" class="w-3 h-3 mr-1 text-slate-400"></i> <?=date('M d, Y', strtotime($job['created_at']))?></span>
                    </div>

                    <div class="flex gap-3">
                        <form method="post" class="flex-1">
                            <input type="hidden" name="job_id" value="<?=$job['id']?>">
                            <input type="hidden" name="action" value="approve">
                            <button type="submit" class="w-full bg-emerald-600/20 hover:bg-emerald-600/30 text-emerald-400 border border-emerald-600/30 py-2.5 rounded-xl font-bold text-sm transition flex items-center justify-center">
                                <i data-lucide="check" class="w-4 h-4 mr-2"></i> Approve
                            </button>
                        </form>
                        <form method="post" class="flex-1">
                            <input type="hidden" name="job_id" value="<?=$job['id']?>">
                            <input type="hidden" name="action" value="reject">
                            <button type="submit" class="w-full bg-red-500/10 hover:bg-red-500/20 text-red-400 border border-red-500/20 py-2.5 rounded-xl font-bold text-sm transition flex items-center justify-center">
                                <i data-lucide="x" class="w-4 h-4 mr-2"></i> Reject
                            </button>
                        </form>
                    </div>
                </div>
                <?php endwhile; ?>
            </div>
            <?php else: ?>
                <div class="glass-panel rounded-2xl p-12 text-center border-dashed border-slate-700 flex flex-col items-center">
                    <div class="bg-slate-800 p-4 rounded-full mb-3 border border-slate-700">
                        <i data-lucide="check-circle" class="w-8 h-8 text-emerald-500"></i>
                    </div>
                    <p class="text-slate-400">No pending jobs to review. All caught up!</p>
                </div>
            <?php endif; ?>
        </div>

        <!-- Other Jobs History (Collapsed/Table) -->
        <div>
            <h2 class="text-xl font-bold text-white mb-6 flex items-center">
                <span class="bg-indigo-500/10 text-indigo-400 p-2 rounded-lg mr-3 border border-indigo-500/20"><i data-lucide="archive" class="w-5 h-5"></i></span>
                Job History (Last 50)
            </h2>
            
            <div class="glass-panel rounded-3xl shadow-xl overflow-hidden">
                <div class="overflow-x-auto">
                    <table class="w-full text-left text-sm text-slate-400">
                        <thead class="bg-slate-800/50 text-slate-400 uppercase font-semibold text-xs tracking-wider">
                            <tr>
                                <th class="px-6 py-4">Job Title</th>
                                <th class="px-6 py-4">Employer</th>
                                <th class="px-6 py-4">Status</th>
                                <th class="px-6 py-4">Date</th>
                                <th class="px-6 py-4 text-right">Actions</th>
                            </tr>
                        </thead>
                        <tbody class="divide-y divide-slate-700/50">
                            <?php while($j = $all_jobs->fetch_assoc()): ?>
                            <tr class="hover:bg-slate-800/30 transition group">
                                <td class="px-6 py-4 font-bold text-white"><?=$j['title']?></td>
                                <td class="px-6 py-4 text-slate-300"><?=$j['company_name']?></td>
                                <td class="px-6 py-4">
                                    <span class="px-2.5 py-1 rounded-full text-xs font-bold border 
                                        <?= $j['status'] == 'Open' ? 'bg-emerald-500/10 text-emerald-400 border-emerald-500/20' : 
                                           ($j['status'] == 'Rejected' ? 'bg-red-500/10 text-red-400 border-red-500/20' : 'bg-slate-700 text-slate-400 border-slate-600') ?>">
                                        <?=$j['status']?>
                                    </span>
                                </td>
                                <td class="px-6 py-4 text-slate-500"><?=date('M d', strtotime($j['created_at']))?></td>
                                <td class="px-6 py-4 text-right">
                                    <form method="post" onsubmit="return confirm('Are you sure you want to delete this job?');">
                                        <input type="hidden" name="job_id" value="<?=$j['id']?>">
                                        <input type="hidden" name="action" value="delete">
                                        <button type="submit" class="text-red-400 hover:text-red-300 transition p-2 hover:bg-red-500/10 rounded-lg">
                                            <i data-lucide="trash-2" class="w-4 h-4"></i>
                                        </button>
                                    </form>
                                </td>
                            </tr>
                            <?php endwhile; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

    </div>

    <!-- Simple Footer -->
    <footer class="bg-slate-900 border-t border-slate-800 py-8 text-center text-slate-500 text-sm mt-12">
        <p>&copy; <?=date('Y')?> Work For Sri Lanka. All rights reserved.</p>
    </footer>

    <script>
        lucide.createIcons();
    </script>
</body>
</html>
