<?php
session_start();


// Check if the user is logged in
if (!isset($_SESSION['email'])) {
    header("Location: login");
    exit();
}
// Check if seeker_id and employer id exists in the session
$seeker_id = isset($_SESSION['seeker_id']);
$employer_id = isset($_SESSION['employer_id']);

include 'connect.php';

// Get job ID from URL
$job_id = isset($_GET['id']) ? intval($_GET['id']) : 0;

if ($job_id > 0) {
    // Query to get job details by job_id
    $sql = "SELECT * FROM job_postings WHERE job_id = ?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("i", $job_id);
    $stmt->execute();
    $result = $stmt->get_result();

    // Check if the job exists
    if ($result->num_rows > 0) {
        $job = $result->fetch_assoc();
        $is_employer = ($_SESSION['user_type'] === 'Employer');
        $is_job_owner = ($is_employer && isset($_SESSION['employer_id']) && $_SESSION['employer_id'] == $job['employer_id']);

    } else {
        echo "Job not found.";
        exit();
    }
} else {
    echo "Invalid job ID.";
    exit();
}

// Check if the logged-in user is a job seeker,employer and is the one who posted the job
$is_job_seeker = ($_SESSION['user_type'] === 'Job seeker');
$is_employer = ($_SESSION['user_type'] === 'Employer');
$_isemployer_id = ($_SESSION['employer_id'] ==='employer_id');
// Handle job application
if (isset($_POST['apply']) && $is_job_seeker) {
    // Get seeker_id from session
    $seeker_id = $_SESSION['seeker_id'];
    $application_date = date('Y-m-d H:i:s'); // Current date and time
    $status = 'Pending'; // Default status

    // Insert the application into the applications table
    $sql_apply = "INSERT INTO applications (job_id, seeker_id, application_date, status) VALUES (?, ?, ?, ?)";
    $stmt_apply = $conn->prepare($sql_apply);
    $stmt_apply->bind_param("iiss", $job_id, $seeker_id, $application_date, $status);

    if ($stmt_apply->execute()) {
        $application_message = "You have successfully applied for this job!";
    } else {
        $application_message = "There was an error while applying. Please try again later.";
    }
}
// Handle job deletion (for employers only)
if (isset($_POST['remove']) && $is_employer && $is_job_owner ) {
    $conn->begin_transaction(); // Start a transaction

    try {
        // Delete related applications
        $sql_delete_applications = "DELETE FROM applications WHERE job_id = ?";
        $stmt_delete_applications = $conn->prepare($sql_delete_applications);
        $stmt_delete_applications->bind_param("i", $job_id);
        $stmt_delete_applications->execute();

        // Delete the job posting
        $sql_delete_job = "DELETE FROM job_postings WHERE job_id = ?";
        $stmt_delete_job = $conn->prepare($sql_delete_job);
        $stmt_delete_job->bind_param("i", $job_id);
        $stmt_delete_job->execute();

        $conn->commit(); // Commit the transaction
        echo '<script>
            alert("Job deleted successfully!");
            window.location.href = "viewjobs.php";
          </script>';
        exit();
    } catch (Exception $e) {
        $conn->rollback(); // Rollback the transaction on error
        $delete_message = "There was an error while deleting the job. Please try again later.";
    }
}


?>

<!DOCTYPE html>
<html lang="en" data-theme="light">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Job Details</title>
    <link href="https://cdn.jsdelivr.net/npm/daisyui@4.7.2/dist/full.min.css" rel="stylesheet" type="text/css" />
    <script src="https://cdn.tailwindcss.com"></script>
</head>

<body class="min-h-screen flex flex-col bg-base-200">
    <?php include 'navbar.php'; ?>

    <main class="flex-grow container mx-auto px-4 py-8">
        <!-- Breadcrumb -->
        <div class="text-sm breadcrumbs mb-6">
            <ul>
                <li><a href="index.php" class="text-primary hover:text-primary-focus">Home</a></li>
                <li class="text-base-content">Job Details</li>
            </ul>
        </div>

        <div class="card bg-base-100 shadow-xl">
            <div class="card-body">
                <h1 class="card-title text-3xl mb-6"><?= htmlspecialchars($job['job_title']) ?></h1>

                <!-- Display Application Message -->
                <?php if (isset($application_message)): ?>
                    <div class="alert alert-success mb-4">
                        <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24"
                            class="stroke-current shrink-0 w-6 h-6">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                                d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path>
                        </svg>
                        <span><?= htmlspecialchars($application_message) ?></span>
                    </div>
                <?php endif; ?>

                <div class="grid grid-cols-1 md:grid-cols-2 gap-6 mb-6">
                    <div class="flex items-center gap-2">
                        <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 text-primary" fill="none"
                            viewBox="0 0 24 24" stroke="currentColor">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                                d="M17.657 16.657L13.414 20.9a1.998 1.998 0 01-2.827 0l-4.244-4.243a8 8 0 1111.314 0z" />
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                                d="M15 11a3 3 0 11-6 0 3 3 0 016 0z" />
                        </svg>
                        <div>
                            <div class="text-sm opacity-70">Location</div>
                            <div class="font-medium"><?= htmlspecialchars($job['location']) ?></div>
                        </div>
                    </div>

                    <div class="flex items-center gap-2">
                        <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 text-primary" fill="none"
                            viewBox="0 0 24 24" stroke="currentColor">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                                d="M12 8c-1.657 0-3 .895-3 2s1.343 2 3 2 3 .895 3 2-1.343 2-3 2m0-8c1.11 0 2.08.402 2.599 1M12 8V7m0 1v8m0 0v1m0-1c-1.11 0-2.08-.402-2.599-1M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
                        </svg>
                        <div>
                            <div class="text-sm opacity-70">Salary</div>
                            <div class="font-medium"><?= htmlspecialchars($job['salary']) ?></div>
                        </div>
                    </div>

                    <div class="flex items-center gap-2">
                        <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 text-primary" fill="none"
                            viewBox="0 0 24 24" stroke="currentColor">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                                d="M12 8c-1.657 0-3 .895-3 2s1.343 2 3 2 3 .895 3 2-1.343 2-3 2m0-8c1.11 0 2.08.402 2.599 1M12 8V7m0 1v8m0 0v1m0-1c-1.11 0-2.08-.402-2.599-1M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
                        </svg>
                        <div>
                            <div class="text-sm opacity-70">Salary Type</div>
                            <div class="font-medium"><?= htmlspecialchars($job['salary_type']) ?></div>
                        </div>
                    </div>


                    <div class="flex items-center gap-2">
                    <i class="fas fa-briefcase text-primary mr-3"></i>
                        <div>
                            <div class="text-sm opacity-70">job Type</div>
                            <div class="font-medium"><?= htmlspecialchars($job['job_type']) ?></div>
                        </div>
                    </div>


                    <div class="flex items-center gap-2">
                    <i class="fa-solid fa-graduation-cap text-primary mr-3"></i>
                        <div>
                            <div class="text-sm opacity-70">Skill Level</div>
                            <div class="font-medium"><?= htmlspecialchars($job['skills_level']) ?></div>
                        </div>
                    </div>
                </div>

                <div class="divider"></div>

                <div class="prose max-w-none">
                    <h2 class="text-2xl font-semibold mb-4">Job Description</h2>
                    <p class="whitespace-pre-line"><?= nl2br(htmlspecialchars($job['job_description'])) ?></p>
                </div>

                <div class="divider"></div>

                <div class="flex justify-between items-center mt-6">
                    <div class="text-sm">
                        Posted on: <?= date('F j, Y', strtotime($job['posted_date'])) ?>
                    </div>

                    <!-- Apply Button for Job Seekers -->
                    <?php if ($is_job_seeker): ?>
                        <form method="POST">
                            <button type="submit" name="apply" class="btn btn-primary">
                                <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 mr-2" fill="none" viewBox="0 0 24 24"
                                    stroke="currentColor">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                                        d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z" />
                                </svg>
                                Apply for Job
                            </button>
                        </form>
                    <?php else: ?>
                        <button class="btn btn-disabled" disabled>
                            <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 mr-2" fill="none" viewBox="0 0 24 24"
                                stroke="currentColor">
                                <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" />
                            </svg>
                            You must be a job seeker to apply
                        </button>
                    <?php endif; ?>

                    <?php if ($is_employer && $is_job_owner): ?>
                        <form method="POST">
                            <button type="submit" name="remove" class="btn btn-error">
                                <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 mr-2" fill="none" viewBox="0 0 24 24"
                                    stroke="currentColor">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                                        d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z" />
                                </svg>
                                Remove Job Posting
                            </button>
                        </form>
                    <?php else: ?>
                        <button class="btn btn-disabled" disabled>
                            <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 mr-2" fill="none" viewBox="0 0 24 24"
                                stroke="currentColor">
                                <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" />
                            </svg>
                            You must be a Employer and be the one who posted the job to remove
                        </button>
                    <?php endif; ?>


                </div>
            </div>
        </div>
    </main>

    <?php include 'footer.php'; ?>
</body>

</html>

<?php
$conn->close();
?>