<?php
include '../db_config.php';
include '../functions.php';
$msg = '';
$error = '';
$show_login = isset($_GET['mode']) && $_GET['mode'] == 'login';
$email_val = '';

if ($_SERVER["REQUEST_METHOD"] == "POST") {

    // LOGIN LOGIC
    if(isset($_POST['login_action'])) {
        $u = sanitize_input($_POST['email']);
        $p = $_POST['password'];
        $stmt = $conn->prepare("SELECT id, password, role FROM employer_login WHERE username=?");
        $stmt->bind_param("s", $u);
        $stmt->execute();
        $stmt->bind_result($id, $hash, $role);
        
        if($stmt->fetch() && password_verify($p, $hash)) {
            $stmt->close(); 
            // Check status
            $chk = $conn->query("SELECT status FROM employer_details WHERE user_id=$id")->fetch_assoc();
            if($chk['status'] == 'Pending') {
                $error = "Your account is pending approval. Please wait for an admin to verify your details.";
                $show_login = true;
                $email_val = $u;
            } elseif ($chk['status'] == 'Rejected') {
                $error = "Your account has been rejected. Please contact support.";
                $show_login = true;
                $email_val = $u;
            } else {
                session_start();
                $_SESSION['user_id'] = $id;
                $_SESSION['username'] = $u;
                $_SESSION['role'] = $role;
                header("Location: dashboard.php");
                exit();
            }
        } else {
            $error = "Invalid credential. Please try again.";
            $show_login = true;
            $email_val = $u;
        }
    }
    // REGISTER LOGIC
    else {
        $email = sanitize_input($_POST['email']);
        $password = password_hash($_POST['password'], PASSWORD_DEFAULT);
        $company = sanitize_input($_POST['company']);
        $br_no = sanitize_input($_POST['br_no']);
        $phone = sanitize_input($_POST['phone']);
        $address = sanitize_input($_POST['address']);
        $email_val = $email;

        // Check if email already exists
        $check = $conn->query("SELECT id FROM employer_login WHERE username = '$email'");
        if($check->num_rows > 0) {
            $error = "This email is already registered. Please login below.";
            $show_login = true;
        } else {
            if($conn->query("INSERT INTO employer_login (username, password, role) VALUES ('$email', '$password', 'employer')")) {
                $last_id = $conn->insert_id;
                $conn->query("INSERT INTO employer_details (user_id, company_name, business_reg_no, contact_no, address, status) VALUES ($last_id, '$company', '$br_no', '$phone', '$address', 'Pending')");
                $msg = "Success! Account created. Please wait for Admin/DME approval before logging in.";
                $show_login = true;
            } else {
                $error = "Something went wrong. Please try again.";
            }
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Employer Registration | 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">
    <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);
        }
        .form-input {
            background: rgba(15, 23, 42, 0.6);
            border: 1px solid rgba(51, 65, 85, 1);
            color: white;
            transition: all 0.3s ease;
        }
        .form-input:focus {
            background: rgba(15, 23, 42, 0.9);
            border-color: #6366f1;
            box-shadow: 0 0 0 4px rgba(99, 102, 241, 0.1);
        }
        @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 flex items-center justify-center p-4">
    <!-- Navbar (Simplified) -->
    <nav class="absolute top-0 left-0 w-full p-6">
        <a href="<?= BASE_URL ?>/index.php" class="text-2xl font-bold italic tracking-tighter text-white brand-pop block origin-left">
            Work For <span class="text-indigo-400">Sri Lanka</span>
        </a>
    </nav>

    <div class="absolute inset-0 overflow-hidden pointer-events-none">
        <div class="absolute top-0 right-0 w-[500px] h-[500px] bg-indigo-600/10 rounded-full blur-[100px] -mt-20 -mr-20"></div>
        <div class="absolute bottom-0 left-0 w-[500px] h-[500px] bg-purple-600/10 rounded-full blur-[100px] -mb-20 -ml-20"></div>
    </div>

    <div class="glass-panel w-full max-w-4xl grid md:grid-cols-2 rounded-3xl overflow-hidden shadow-2xl relative z-10">
        
        <!-- Left Side: Branding/Info -->
        <div class="p-12 bg-slate-900/50 flex flex-col justify-center relative overflow-hidden border-r border-slate-700/50">
             <div class="absolute inset-0 bg-gradient-to-br from-indigo-900/40 to-slate-900/40 z-0"></div>
            <div class="relative z-10">
                <h2 class="text-3xl font-bold mb-4 text-white">Hire the Best Talent</h2>
                <p class="text-slate-400 text-lg mb-8 leading-relaxed">Access Sri Lanka's largest pool of verified skilled workers. Post jobs, review profiles, and hire with confidence.</p>
                
                <div class="space-y-4">
                    <div class="flex items-center space-x-3 text-slate-300">
                        <div class="bg-indigo-500/20 p-2 rounded-lg text-indigo-400 border border-indigo-500/30">
                            <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"></path></svg>
                        </div>
                        <span>Verified Worker Profiles</span>
                    </div>
                     <div class="flex items-center space-x-3 text-slate-300">
                        <div class="bg-indigo-500/20 p-2 rounded-lg text-indigo-400 border border-indigo-500/30">
                            <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 13.255A23.931 23.931 0 0112 15c-3.183 0-6.22-.62-9-1.745M16 6V4a2 2 0 00-2-2h-4a2 2 0 00-2 2v2m4 6h.01M5 20h14a2 2 0 002-2V8a2 2 0 00-2-2H5a2 2 0 00-2 2v10a2 2 0 002 2z"></path></svg>
                        </div>
                        <span>Post & Manage Jobs Easily</span>
                    </div>
                </div>
            </div>
        </div>

        <!-- Right Side: Form -->
        <div class="p-8 md:p-12 bg-slate-900/30">
            <div class="mb-8">
                <?php if($show_login): ?>
                    <h3 class="text-2xl font-bold text-white mb-2">Employer Login</h3>
                    <p class="text-slate-400 text-sm">Welcome back! Manage your jobs and applicants.</p>
                <?php else: ?>
                    <h3 class="text-2xl font-bold text-white mb-2">Create Account</h3>
                    <p class="text-slate-400 text-sm">Fill in your business details to get started.</p>
                <?php endif; ?>
            </div>

            <?php if($msg): ?>
                <div class="bg-emerald-500/10 border border-emerald-500/20 p-4 mb-6 rounded-xl flex items-start">
                    <svg class="h-5 w-5 text-emerald-400 mt-0.5 mr-3 flex-shrink-0" viewBox="0 0 20 20" fill="currentColor">
                        <path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zm3.707-9.293a1 1 0 00-1.414-1.414L9 10.586 7.707 9.293a1 1 0 00-1.414 1.414l2 2a1 1 0 001.414 0l4-4z" clip-rule="evenodd" />
                    </svg>
                    <p class="text-sm font-medium text-emerald-400"><?=$msg?></p>
                </div>
            <?php endif; ?>

            <?php if($error): ?>
                <div class="bg-red-500/10 border border-red-500/20 p-4 mb-6 rounded-xl flex items-start">
                    <svg class="h-5 w-5 text-red-400 mt-0.5 mr-3 flex-shrink-0" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
                    </svg>
                    <p class="text-sm font-medium text-red-400"><?=$error?></p>
                </div>
            <?php endif; ?>

            <?php if($show_login): ?>
                <!-- LOGIN FORM -->
                <form method="post" class="space-y-4">
                    <input type="hidden" name="login_action" value="1">
                    <div>
                        <label class="block text-xs font-bold text-slate-400 uppercase tracking-wide mb-1">Email Address</label>
                        <div class="relative">
                            <span class="absolute left-4 top-3.5 text-slate-500"><svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 8l7.89 5.26a2 2 0 002.22 0L21 8M5 19h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v10a2 2 0 002 2z"></path></svg></span>
                            <input type="email" name="email" required value="<?=$email_val?>" class="w-full pl-11 pr-4 py-3 rounded-xl form-input outline-none" placeholder="name@company.com">
                        </div>
                    </div>
                    <div>
                        <label class="block text-xs font-bold text-slate-400 uppercase tracking-wide mb-1">Password</label>
                        <div class="relative">
                            <span class="absolute left-4 top-3.5 text-slate-500"><svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 15v2m-6 4h12a2 2 0 002-2v-6a2 2 0 00-2-2H6a2 2 0 00-2 2v6a2 2 0 002 2zm10-10V7a4 4 0 00-8 0v4h8z"></path></svg></span>
                            <input type="password" name="password" required class="w-full pl-11 pr-4 py-3 rounded-xl form-input outline-none" placeholder="Min 6 characters">
                        </div>
                    </div>
                    <div class="pt-2">
                        <button type="submit" class="w-full bg-indigo-600 hover:bg-indigo-500 text-white font-bold py-3.5 rounded-xl shadow-lg shadow-indigo-600/20 hover:shadow-indigo-600/40 transition transform active:scale-[0.98]">
                            Login to Dashboard
                        </button>
                    </div>
                    <div class="text-center mt-6">
                        <a href="register.php" class="text-indigo-400 font-medium text-sm hover:text-indigo-300 transition">Create an account instead</a>
                    </div>
                </form>
            <?php else: ?>
                <!-- REGISTER FORM -->
                <form method="post" class="space-y-4">
                    <div class="grid grid-cols-1 gap-4">
                        <div>
                            <label class="block text-xs font-bold text-slate-400 uppercase tracking-wide mb-1">Company Name</label>
                            <input type="text" name="company" required class="w-full px-4 py-3 rounded-xl form-input outline-none" placeholder="Abans / Keells / Own Name">
                        </div>
                        <div>
                            <label class="block text-xs font-bold text-slate-400 uppercase tracking-wide mb-1">BR Number</label>
                            <input type="text" name="br_no" required class="w-full px-4 py-3 rounded-xl form-input outline-none" placeholder="PV 12345">
                        </div>
                        <div>
                            <label class="block text-xs font-bold text-slate-400 uppercase tracking-wide mb-1">Email Address</label>
                            <input type="email" name="email" required value="<?=$email_val?>" class="w-full px-4 py-3 rounded-xl form-input outline-none" placeholder="name@company.com">
                        </div>
                         <div class="grid grid-cols-2 gap-4">
                             <div>
                                <label class="block text-xs font-bold text-slate-400 uppercase tracking-wide mb-1">Password</label>
                                <input type="password" name="password" required class="w-full px-4 py-3 rounded-xl form-input outline-none" placeholder="******">
                            </div>
                            <div>
                                <label class="block text-xs font-bold text-slate-400 uppercase tracking-wide mb-1">Contact No</label>
                                <input type="text" name="phone" required class="w-full px-4 py-3 rounded-xl form-input outline-none" placeholder="07xxxxxxx">
                            </div>
                        </div>
                        <div>
                            <label class="block text-xs font-bold text-slate-400 uppercase tracking-wide mb-1">Business Address</label>
                            <textarea name="address" required rows="2" class="w-full px-4 py-3 rounded-xl form-input outline-none" placeholder="City, District..."></textarea>
                        </div>
                    </div>

                    <div class="pt-4">
                        <button type="submit" class="w-full bg-indigo-600 hover:bg-indigo-500 text-white font-bold py-3.5 rounded-xl shadow-lg shadow-indigo-600/20 hover:shadow-indigo-600/40 transition transform active:scale-[0.98]">
                            Create Employer Account
                        </button>
                    </div>
                    <div class="mt-6 text-center text-sm text-slate-400">
                        Already have an account? <a href="register.php?mode=login" class="text-indigo-400 font-medium hover:text-indigo-300 transition">Login here</a>
                    </div>
                </form>
            <?php endif; ?>
        </div>
    </div>
</body>
</html>
