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

$res = $conn->query("SELECT ed.* FROM employee_details ed WHERE status='Pending'");
$emp_res = $conn->query("SELECT ed.*, el.username as email FROM employer_details ed JOIN employer_login el ON ed.user_id = el.id WHERE ed.status='Pending'");
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>DME Dashboard | Work For Sri Lanka</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);
        }
        @keyframes pop {
            0% { transform: scale(0.95); opacity: 0; }
            70% { transform: scale(1.05); opacity: 1; }
            100% { transform: scale(1); opacity: 1; }
        }
        .brand-pop {
            transition: transform 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
            display: inline-block;
        }
        .brand-pop:hover {
            transform: scale(1.15);
        }
    </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">
                <div class="flex-shrink-0 flex items-center gap-3">
                    <a href="<?= BASE_URL ?>/index.php" class="brand-pop block">
                        <img src="../assets/images/logo_banner.png" alt="Department of Manpower and Employment" class="h-10 md:h-12 object-contain">
                    </a>
                    <div class="h-8 w-px bg-slate-700 mx-2 hidden md:block"></div>
                    <a href="<?= BASE_URL ?>/index.php" class="text-xl md:text-2xl font-bold italic tracking-tighter text-white brand-pop block origin-left">
                        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">DME Panel</span>
                    </a>
                </div>
                <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-12 px-4 border-b border-slate-800 overflow-hidden">
        <div class="absolute top-0 left-0 w-[500px] h-[500px] bg-indigo-600/10 rounded-full blur-[100px] -mt-20 -ml-20 pointer-events-none"></div>

        <div class="max-w-6xl mx-auto flex flex-col md:flex-row justify-between items-center gap-6 relative z-10">
            <div>
                <h1 class="text-3xl font-bold mb-2 text-white">Divisional Management Dashboard</h1>
                <p class="text-slate-400">Oversee pending worker verifications and manage employee records.</p>
            </div>
            <div class="flex gap-4">
                 <form action="edit_employee.php" method="GET" class="flex gap-2 bg-slate-800 p-1 rounded-xl border border-slate-700">
                    <input type="text" name="nic" placeholder="Enter NIC to Edit" required class="bg-transparent text-white placeholder-slate-500 px-4 py-2 outline-none w-40 md:w-48 text-sm">
                    <button type="submit" class="bg-indigo-600 hover:bg-indigo-500 text-white px-4 py-2 rounded-lg font-bold transition shadow-lg flex items-center text-sm">
                        <i data-lucide="edit" class="w-4 h-4 mr-2"></i> Edit
                    </button>
                </form>
                <a href="add_employee.php" class="bg-emerald-600 hover:bg-emerald-500 text-white px-6 py-3 rounded-xl font-bold transition shadow-lg shadow-emerald-600/20 flex items-center transform hover:scale-105">
                    <i data-lucide="user-plus" class="w-5 h-5 mr-2"></i> Add New
                </a>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="max-w-6xl mx-auto px-4 pb-20 pt-8">
        
        <div class="glass-panel rounded-3xl shadow-xl overflow-hidden mb-8">
            <div class="p-8 border-b border-slate-700/50 bg-slate-800/30 flex justify-between items-center">
                <div>
                    <h2 class="text-xl font-bold text-white flex items-center">
                        <i data-lucide="clock" class="w-5 h-5 mr-3 text-orange-400"></i> Pending Worker Approvals
                    </h2>
                    <p class="text-sm text-slate-400 mt-1">Review and verify new worker registrations.</p>
                </div>
                <div class="text-orange-400 text-sm font-bold bg-orange-500/10 border border-orange-500/20 px-4 py-1.5 rounded-full">
                    <?=$res->num_rows?> Pending
                </div>
            </div>

            <div class="overflow-x-auto">
                <?php if($res->num_rows > 0): ?>
                <table class="w-full text-left text-sm text-slate-300">
                    <thead class="bg-slate-800/50 text-slate-400 uppercase font-semibold text-xs tracking-wider">
                        <tr>
                            <th class="px-6 py-4">Worker Info</th>
                            <th class="px-6 py-4">Qualification</th>
                            <th class="px-6 py-4">Job Category</th>
                            <th class="px-6 py-4">District</th>
                            <th class="px-6 py-4 text-center">Action</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-slate-700/50">
                        <?php while($r=$res->fetch_assoc()):?>
                        <tr class="hover:bg-slate-800/30 transition">
                            <td class="px-6 py-4">
                                <div class="flex items-center">
                                    <div class="h-10 w-10 flex-shrink-0 bg-indigo-500/20 rounded-full flex items-center justify-center text-indigo-400 font-bold mr-3 border border-indigo-500/30">
                                        <?= !empty($r['full_name']) ? strtoupper(substr($r['full_name'],0,1)) : '?' ?>
                                    </div>
                                    <div>
                                        <div class="font-bold text-white"><?= !empty($r['full_name']) ? $r['full_name'] : '<span class="italic text-slate-500">New Registrant</span>' ?></div>
                                        <div class="text-xs text-slate-500">NIC: <span class="font-mono text-slate-400"><?=$r['nic']?></span></div>
                                    </div>
                                </div>
                            </td>
                            <td class="px-6 py-4">
                                <span class="bg-slate-800 text-slate-300 px-2.5 py-1 rounded text-xs font-semibold border border-slate-700"><?= !empty($r['qualification']) ? $r['qualification'] : '-' ?></span>
                            </td>
                            <td class="px-6 py-4 font-medium text-emerald-400"><?= !empty($r['job_category']) ? $r['job_category'] : '-' ?></td>
                            <td class="px-6 py-4 text-slate-400"><?=$r['district'] ? $r['district'] : '-'?></td>
                            <td class="px-6 py-4 text-center">
                                <div class="flex items-center justify-center gap-2">
                                     <?php if(!empty($r['cv_file'])): ?>
                                        <a href="../uploads/cvs/<?=$r['cv_file']?>" target="_blank" class="bg-indigo-500/10 hover:bg-indigo-500/20 text-indigo-400 border border-indigo-500/20 px-3 py-1.5 rounded-lg text-xs font-bold transition flex items-center" title="Download CV">
                                            <i data-lucide="file-text" class="w-3 h-3 mr-1"></i> CV
                                        </a>
                                    <?php endif; ?>
                                    <a href="approve_reject.php?id=<?=$r['employee_nic']?>&act=approve" class="bg-emerald-500/10 hover:bg-emerald-500/20 text-emerald-400 border border-emerald-500/20 px-3 py-1.5 rounded-lg text-xs font-bold transition flex items-center">
                                        <i data-lucide="check" class="w-3 h-3 mr-1"></i> Approve
                                    </a>
                                    <a href="approve_reject.php?id=<?=$r['employee_nic']?>&act=reject" class="bg-red-500/10 hover:bg-red-500/20 text-red-400 border border-red-500/20 px-3 py-1.5 rounded-lg text-xs font-bold transition flex items-center" onclick="return confirm('Are you sure you want to reject this application?');">
                                        <i data-lucide="x" class="w-3 h-3 mr-1"></i> Reject
                                    </a>
                                    <a href="../worker_profile.php?id=<?=encrypt_id($r['employee_nic'])?>" target="_blank" class="bg-slate-800 hover:bg-slate-700 text-slate-400 px-2 py-1.5 rounded-lg text-xs transition" title="View Profile">
                                        <i data-lucide="eye" class="w-4 h-4"></i>
                                    </a>
                                </div>
                            </td>
                        </tr>
                        <?php endwhile;?>
                    </tbody>
                </table>
                <?php else: ?>
                    <div class="p-12 text-center">
                        <div class="inline-flex items-center justify-center w-16 h-16 rounded-full bg-slate-800/50 mb-4 border border-slate-700">
                            <i data-lucide="check-circle" class="w-8 h-8 text-emerald-500"></i>
                        </div>
                        <h3 class="text-lg font-bold text-white">All Caught Up!</h3>
                        <p class="text-slate-400 mt-2">There are no pending worker approvals at the moment.</p>
                    </div>
                <?php endif; ?>
            </div>
        </div>

        <div class="glass-panel rounded-3xl shadow-xl overflow-hidden mt-10">
            <div class="p-8 border-b border-slate-700/50 bg-slate-800/30 flex justify-between items-center">
                <div>
                    <h2 class="text-xl font-bold text-white flex items-center">
                        <i data-lucide="building-2" class="w-5 h-5 mr-3 text-blue-400"></i> Pending Employer Approvals
                    </h2>
                    <p class="text-sm text-slate-400 mt-1">Review and approve new company registrations.</p>
                </div>
                <div class="text-blue-400 text-sm font-bold bg-blue-500/10 border border-blue-500/20 px-4 py-1.5 rounded-full">
                    <?=$emp_res->num_rows?> Pending
                </div>
            </div>

            <div class="overflow-x-auto">
                <?php if($emp_res->num_rows > 0): ?>
                <table class="w-full text-left text-sm text-slate-300">
                    <thead class="bg-slate-800/50 text-slate-400 uppercase font-semibold text-xs tracking-wider">
                        <tr>
                            <th class="px-6 py-4">Company Info</th>
                            <th class="px-6 py-4">Contact</th>
                            <th class="px-6 py-4">Address</th>
                            <th class="px-6 py-4 text-center">Action</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-slate-700/50">
                        <?php while($e = $emp_res->fetch_assoc()):?>
                        <tr class="hover:bg-slate-800/30 transition">
                            <td class="px-6 py-4">
                                <div class="font-bold text-white"><?=$e['company_name']?></div>
                                <div class="text-xs text-slate-500"><?=$e['email']?></div>
                            </td>
                            <td class="px-6 py-4 text-slate-400"><?=$e['contact_no']?></td>
                            <td class="px-6 py-4 text-slate-400"><?=$e['address']?></td>
                            <td class="px-6 py-4 text-center">
                                <div class="flex items-center justify-center gap-2">
                                    <a href="approve_reject.php?emp_id=<?=$e['user_id']?>&act=approve" class="bg-emerald-500/10 hover:bg-emerald-500/20 text-emerald-400 border border-emerald-500/20 px-3 py-1.5 rounded-lg text-xs font-bold transition flex items-center">
                                        <i data-lucide="check" class="w-3 h-3 mr-1"></i> Approve
                                    </a>
                                    <a href="approve_reject.php?emp_id=<?=$e['user_id']?>&act=reject" class="bg-red-500/10 hover:bg-red-500/20 text-red-400 border border-red-500/20 px-3 py-1.5 rounded-lg text-xs font-bold transition flex items-center" onclick="return confirm('Reject this employer?');">
                                        <i data-lucide="x" class="w-3 h-3 mr-1"></i> Reject
                                    </a>
                                </div>
                            </td>
                        </tr>
                        <?php endwhile;?>
                    </tbody>
                </table>
                <?php else: ?>
                    <div class="p-12 text-center text-slate-500">
                        <p>No pending employer approvals.</p>
                    </div>
                <?php endif; ?>
            </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>
