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

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $u = sanitize_input($_POST['email']);
    $p = $_POST['password'];
    
    $stmt = $conn->prepare("SELECT id, password, role FROM dme_login WHERE username=?");
    $stmt->bind_param("s", $u);
    $stmt->execute();
    $stmt->bind_result($id, $hash, $role);
    
    if ($stmt->fetch()) {
        if (password_verify($p, $hash)) {
            $_SESSION['user_id'] = $id;
            $_SESSION['username'] = $u;
            $_SESSION['role'] = $role;
            header("Location: dashboard.php");
            exit();
        } else {
            $error = "Incorrect password. Please try again.";
        }
    } else {
        $error = "User not found. Please check your email.";
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>DME Portal Login | 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);
        }
    </style>
</head>
<body class="bg-slate-950 min-h-screen flex items-center justify-center p-4 selection:bg-indigo-500 selection:text-white">
    <div class="max-w-4xl w-full flex flex-col md:flex-row shadow-2xl rounded-3xl overflow-hidden glass-panel">
        <!-- Left Side: Official/Government Look -->
        <div class="md:w-1/2 bg-slate-900/50 p-12 text-white flex flex-col justify-center relative overflow-hidden text-center md:text-left border-r border-slate-700/50">
            <div class="relative z-10">
                <div class="mb-6">
                    <span class="bg-indigo-600/20 text-indigo-400 border border-indigo-500/30 px-4 py-1.5 rounded-full text-xs font-bold tracking-widest uppercase">Admin System</span>
                </div>
                <h1 class="text-4xl font-bold mb-6 italic text-white tracking-tighter">Work For <span class="text-indigo-400">Sri Lanka</span></h1>
                <h2 class="text-3xl font-semibold mb-4 text-slate-200">DME Officer Portal</h2>
                <p class="text-slate-400 text-lg mb-8 leading-relaxed">Official portal for Department of Manpower & Employment. Manage worker verifications, approvals, and system registrations.</p>
                
                <div class="mt-8 pt-8 border-t border-slate-700/50">
                    <p class="text-sm text-slate-500 italic">"Empowering the workforce of Sri Lanka through efficient management and verification."</p>
                </div>
            </div>
            <!-- Subtle background branding -->
            <div class="absolute top-0 right-0 p-4 opacity-5">
                <i data-lucide="shield" class="w-64 h-64 text-white"></i>
            </div>
        </div>

        <!-- Right Side: Secure Login Form -->
        <div class="md:w-1/2 p-10 md:p-14 bg-slate-900/30">
            <div class="mb-10">
                <h3 class="text-3xl font-bold text-white">Secure Login</h3>
                <p class="text-slate-400 mt-2 font-medium">Please enter your authorized credentials.</p>
            </div>

            <?php if($error): ?>
                <div class="bg-red-500/10 border border-red-500/20 text-red-400 px-4 py-3 rounded-xl mb-6 flex items-center text-sm">
                    <i data-lucide="alert-circle" class="w-5 h-5 mr-3 flex-shrink-0"></i>
                    <span><?=$error?></span>
                </div>
            <?php endif; ?>

            <form method="post" class="space-y-6">
                <div>
                    <label class="block text-sm font-semibold text-slate-400 mb-2 uppercase tracking-wide">Officer Email</label>
                    <div class="relative group">
                        <span class="absolute inset-y-0 left-0 pl-4 flex items-center text-slate-500 group-focus-within:text-indigo-400 transition-colors">
                            <i data-lucide="mail" class="w-5 h-5"></i>
                        </span>
                        <input type="email" name="email" required class="w-full pl-12 pr-4 py-4 rounded-2xl bg-slate-800/50 border border-slate-700 focus:ring-2 focus:ring-indigo-500 focus:border-transparent outline-none transition-all text-white placeholder-slate-600" placeholder="name@workforsl.com">
                    </div>
                </div>

                <div>
                    <label class="block text-sm font-semibold text-slate-400 mb-2 uppercase tracking-wide">Authorized Password</label>
                    <div class="relative group">
                        <span class="absolute inset-y-0 left-0 pl-4 flex items-center text-slate-500 group-focus-within:text-indigo-400 transition-colors">
                             <i data-lucide="lock" class="w-5 h-5"></i>
                        </span>
                        <input type="password" name="password" required class="w-full pl-12 pr-4 py-4 rounded-2xl bg-slate-800/50 border border-slate-700 focus:ring-2 focus:ring-indigo-500 focus:border-transparent outline-none transition-all text-white placeholder-slate-600" placeholder="Enter password">
                    </div>
                </div>

                <button type="submit" class="w-full bg-indigo-600 hover:bg-indigo-500 text-white font-bold py-4 rounded-2xl shadow-lg shadow-indigo-600/20 hover:shadow-indigo-600/40 transition-all transform active:scale-[0.98] uppercase tracking-widest text-sm flex items-center justify-center gap-2">
                    Access Portal <i data-lucide="arrow-right" class="w-4 h-4"></i>
                </button>
            </form>

            <div class="mt-6 text-center">
                <a href="change_password.php" class="text-indigo-400 hover:text-indigo-300 text-sm font-semibold transition-colors">
                    Change Password?
                </a>
            </div>

            <div class="mt-8 text-center">
                <a href="<?= BASE_URL ?>/index.php" class="text-slate-500 text-xs hover:text-slate-400 transition-colors font-medium flex items-center justify-center gap-2">
                    <i data-lucide="arrow-left" class="w-4 h-4"></i>
                    Return to Public Website
                </a>
            </div>
        </div>
    </div>
    <script>
        lucide.createIcons();
    </script>
</body>
</html>
