<?php

require '../config.php';
session_start();
session_write_close();

if(!isset($_SESSION['user_id'])){
    header("Location: ../login.php");
    exit;
}

$userId = intval($_SESSION['user_id']);
$username = $_SESSION['username'] ?? 'HR Admin';
$role = $_SESSION['role'] ?? 'Administrator';

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

    $title = trim($_POST['title']);
    $departmentId = intval($_POST['department_id']);
    $description = trim($_POST['description']);
    $requirements = trim($_POST['requirements']);
    $employmentType = trim($_POST['employment_type']);
    $vacancies = intval($_POST['vacancies']);

    $stmt = $conn->prepare("
    INSERT INTO jobs
    (
        title,
        department_id,
        description,
        requirements,
        employment_type,
        vacancies,
        created_by
    )
    VALUES
    (
        ?,?,?,?,?,?,?
    )
    ");

    $stmt->bind_param(
        "sisssii",
        $title,
        $departmentId,
        $description,
        $requirements,
        $employmentType,
        $vacancies,
        $userId
    );

    $stmt->execute();

    header("Location: job_positions.php?saved=1");
    exit;
}

$departments = $conn->query("
SELECT *
FROM departments
ORDER BY name
");
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Create Vacancy - HR System</title>
    
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Mulish:ital,wght=0,200..1000;1,200..1000&display=swap" rel="stylesheet">
    
    <script src="https://cdn.tailwindcss.com"></script>
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    fontFamily: {
                        sans: ['Mulish', 'sans-serif'],
                    }
                }
            }
        }
    </script>
    <link rel="stylesheet" href="../components/sidebar.css">
</head>
<body class="bg-stone-100/60 min-h-screen text-slate-800 antialiased selection:bg-indigo-600 selection:text-white font-sans">

    <div class="flex">
        
        <?php include '../components/sidebar.php'; ?>

        <div id="sidebar-overlay" class="fixed inset-0 bg-slate-900/40 hidden z-40 transition-opacity duration-300" onclick="toggleSidebar()"></div>

        <div id="main-content" class="flex-1 min-w-0 md:pl-60 transition-all duration-300 min-h-screen flex flex-col">
            
            <header class="sticky top-0 bg-white/90 backdrop-blur-md border-b border-slate-300/80 h-14 flex items-center justify-between px-4 sm:px-6 z-30 shadow-xs">
                <div class="flex items-center gap-3">
                    <button onclick="toggleSidebar()" class="p-1.5 rounded-lg text-slate-600 hover:bg-slate-200/80 transition active:scale-95" aria-label="Toggle Side Area Menu">
                        <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24" stroke-width="2">
                            <path stroke-linecap="round" stroke-linejoin="round" d="M4 6h16M4 12h16M4 18h16"></path>
                        </svg>
                    </button>
                    <div>
                        <h1 class="text-sm font-black tracking-tight text-slate-950">Recruitment Portal</h1>
                        <p class="text-[10px] text-indigo-700 font-bold uppercase tracking-wider">Create Job Requisition</p>
                    </div>
                </div>
                
                <div class="flex items-center gap-4">
                    <div class="hidden sm:flex flex-col text-right">
                        <span class="text-[11px] font-extrabold text-slate-950 leading-tight"><?= htmlspecialchars($username); ?></span>
                        <span class="text-[9px] text-indigo-700 uppercase font-black tracking-wider leading-none mt-0.5"><?= htmlspecialchars($role ?: 'Staff'); ?></span>
                    </div>
                    <div class="w-7 h-7 bg-slate-200 border border-slate-300 rounded-lg flex items-center justify-center font-black text-xs text-slate-700 select-none uppercase">
                        <?= strtoupper(substr($username, 0, 1)); ?>
                    </div>
                </div>
            </header>

            <main class="flex-1 p-4 sm:p-6 max-w-5xl w-full mx-auto space-y-6">

                <div class="flex items-center justify-between gap-4 bg-white border border-slate-300/80 rounded-xl p-4 shadow-sm">
                    <div class="flex items-center gap-3">
                        <a href="job_positions.php" class="p-1.5 rounded-lg text-slate-600 hover:bg-slate-200/80 transition active:scale-95" aria-label="Back to Jobs">
                            <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24" stroke-width="2">
                                <path stroke-linecap="round" stroke-linejoin="round" d="M10.5 19.5L3 12m0 0l7.5-7.5M3 12h18"></path>
                            </svg>
                        </a>
                        <div>
                            <h2 class="text-base font-black text-slate-950 uppercase tracking-tight">New Vacancy Specification</h2>
                            <p class="text-[10px] text-slate-600 font-semibold mt-0.5">Publish a new open role to the career portal and recruitment database.</p>
                        </div>
                    </div>
                    <span class="bg-indigo-50 text-indigo-800 border border-indigo-200 px-2.5 py-1 rounded-md font-extrabold text-[10px] font-mono shadow-xs">
                        Drafting
                    </span>
                </div>

                <div class="bg-white border border-slate-300/80 rounded-xl p-5 sm:p-6 shadow-sm space-y-6">
                    <div class="border-b border-slate-200/80 pb-3 flex items-center justify-between">
                        <div>
                            <h3 class="text-xs font-black text-slate-950 uppercase tracking-tight">Requisition Attributes</h3>
                            <p class="text-[10px] text-slate-600 font-semibold mt-0.5">All fields marked with an asterisk (<span class="text-rose-500">*</span>) are required.</p>
                        </div>
                    </div>

                    <form method="POST" class="space-y-6">
                        <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                            <div class="space-y-1">
                                <label class="text-[10px] font-extrabold uppercase text-slate-600 tracking-wider">Job Title <span class="text-rose-500">*</span></label>
                                <input type="text" name="title" required placeholder="e.g. Senior Software Engineer" class="w-full border border-slate-300 bg-stone-50 rounded-lg px-3 py-2 focus:ring-2 focus:ring-indigo-100 focus:border-indigo-500 outline-none text-xs text-slate-800 font-bold placeholder-slate-400">
                            </div>

                            <div class="space-y-1">
                                <label class="text-[10px] font-extrabold uppercase text-slate-600 tracking-wider">Department <span class="text-rose-500">*</span></label>
                                <select name="department_id" required class="w-full border border-slate-300 bg-stone-50 rounded-lg px-3 py-2 focus:ring-2 focus:ring-indigo-100 focus:border-indigo-500 outline-none text-xs text-slate-800 font-bold">
                                    <option value="">Select Department</option>
                                    <?php while($dept = $departments->fetch_assoc()): ?>
                                        <option value="<?= $dept['id'] ?>">
                                            <?= htmlspecialchars($dept['name']) ?>
                                        </option>
                                    <?php endwhile; ?>
                                </select>
                            </div>

                            <div class="space-y-1">
                                <label class="text-[10px] font-extrabold uppercase text-slate-600 tracking-wider">Employment Type <span class="text-rose-500">*</span></label>
                                <select name="employment_type" required class="w-full border border-slate-300 bg-stone-50 rounded-lg px-3 py-2 focus:ring-2 focus:ring-indigo-100 focus:border-indigo-500 outline-none text-xs text-slate-800 font-bold">
                                    <option value="Full Time">Full Time</option>
                                    <option value="Part Time">Part Time</option>
                                    <option value="Contract">Contract</option>
                                    <option value="Internship">Internship</option>
                                </select>
                            </div>

                            <div class="space-y-1">
                                <label class="text-[10px] font-extrabold uppercase text-slate-600 tracking-wider">Number of Vacancies <span class="text-rose-500">*</span></label>
                                <input type="number" name="vacancies" value="1" min="1" required class="w-full border border-slate-300 bg-stone-50 rounded-lg px-3 py-2 focus:ring-2 focus:ring-indigo-100 focus:border-indigo-500 outline-none text-xs text-slate-800 font-mono font-bold">
                            </div>
                        </div>

                        <div class="space-y-1">
                            <label class="text-[10px] font-extrabold uppercase text-slate-600 tracking-wider">Job Description <span class="text-rose-500">*</span></label>
                            <textarea name="description" rows="6" required placeholder="Provide a detailed overview of the role, key responsibilities, and team structure..." class="w-full border border-slate-300 bg-stone-50 rounded-lg p-3 focus:ring-2 focus:ring-indigo-100 focus:border-indigo-500 outline-none text-xs text-slate-800 font-semibold placeholder-slate-400"></textarea>
                        </div>

                        <div class="space-y-1">
                            <label class="text-[10px] font-extrabold uppercase text-slate-600 tracking-wider">Requirements & Skills <span class="text-rose-500">*</span></label>
                            <textarea name="requirements" rows="6" required placeholder="List qualifications, required skills, degree level, years of experience..." class="w-full border border-slate-300 bg-stone-50 rounded-lg p-3 focus:ring-2 focus:ring-indigo-100 focus:border-indigo-500 outline-none text-xs text-slate-800 font-semibold placeholder-slate-400"></textarea>
                        </div>

                        <div class="pt-3 border-t border-slate-200/80 flex items-center justify-end gap-3">
                            <a href="job_positions.php" class="px-4 py-2 rounded-lg text-xs font-bold text-slate-600 hover:bg-slate-200/70 transition">
                                Cancel
                            </a>
                            <button type="submit" class="bg-slate-900 hover:bg-slate-800 active:scale-[0.98] transition text-white px-5 py-2 rounded-lg font-bold text-xs shadow-xs border border-slate-950 flex items-center gap-2">
                                <span>Publish Vacancy</span>
                                <svg class="w-3.5 h-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24" stroke-width="2.5">
                                    <path stroke-linecap="round" stroke-linejoin="round" d="M12 4.5v15m7.5-7.5h-15"></path>
                                </svg>
                            </button>
                        </div>
                    </form>
                </div>

            </main>

            <footer class="mt-auto border-t border-slate-300 bg-white px-6 py-4 text-center">
                <p class="text-[9px] text-slate-500 font-mono tracking-wider">&copy; 2026 Core HR Engine System Platform • Dynamic Secure Architecture Protocol.</p>
            </footer>
        </div>
    </div>

    <script src="../components/sidebar.js"></script>
</body>
</html>