<?php 
session_start();


// Check if the user is logged in
if (!isset($_SESSION['email'])) {
    header("Location: login.php");
    exit();
}

include 'connect.php';

// Check if the logged-in user is a job seeker
$is_job_seeker = ($_SESSION['user_type'] === 'Job seeker');
if (!$is_job_seeker) {
    echo "Access denied. Only job seekers can view applications.";
    exit();
}

// Get all applications for the current seeker
$seeker_id = $_SESSION['seeker_id'];
$sql = "SELECT a.*, j.job_title, j.location, j.salary, j.posted_date,j.job_type,j.skills_level,j.salary_type
        FROM applications a 
        JOIN job_postings j ON a.job_id = j.job_id 
        WHERE a.seeker_id = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $seeker_id);
$stmt->execute();
$result = $stmt->get_result();
?>

<!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>My Job Applications</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">My Job Applications</li>
            </ul>
        </div>

        <div class="card bg-base-100 shadow-xl">
            <div class="card-body">
                <h1 class="card-title text-3xl mb-6">My Job Applications</h1>
                
                <?php if ($result->num_rows > 0): ?>
                    <div class="overflow-x-auto">
                        <table class="table table-zebra w-full">
                            <thead>
                                <tr>
                                    <th class="bg-base-200">Job Title</th>
                                    <th class="bg-base-200">Location</th>
                                    <th class="bg-base-200">Salary</th>
                                    <th class="bg-base-200">Salary Type</th>
                                    <th class="bg-base-200">Job type</th>
                                    <th class="bg-base-200">Skill Level</th>
                                    <th class="bg-base-200">Application Date</th>
                                    <th class="bg-base-200">Status</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php while ($application = $result->fetch_assoc()): ?>
                                    <tr class="hover">
                                        <td>
                                            <div class="font-medium">
                                                <?= htmlspecialchars($application['job_title']) ?>
                                            </div>
                                        </td>
                                        <td>
                                            <?= htmlspecialchars($application['location']) ?>
                                        </td>
                                        <td>
                                            <?= htmlspecialchars($application['salary']) ?>
                                        </td>
                                        <td>
                                            <?= htmlspecialchars($application['salary_type']) ?>
                                        </td>
                                        <td>
                                            <?= htmlspecialchars($application['job_type']) ?>
                                        </td>
                                        <td>
                                            <?= htmlspecialchars($application['skills_level']) ?>
                                        </td>
                                        <td>
                                            <?= date('F j, Y', strtotime($application['application_date'])) ?>
                                        </td>
                                        <td>
                                            <?php
                                            $statusClass = match($application['status']) {
                                                'Pending' => 'badge badge-warning',
                                                'Accepted' => 'badge badge-success',
                                                default => 'badge badge-error'
                                            };
                                            ?>
                                            <span class="<?= $statusClass ?>">
                                                <?= htmlspecialchars($application['status']) ?>
                                            </span>
                                        </td>
                                    </tr>
                                <?php endwhile; ?>
                            </tbody>
                        </table>
                    </div>
                <?php else: ?>
                    <div class="alert alert-info">
                        <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>You haven't applied to any jobs yet.</span>
                    </div>
                <?php endif; ?>
            </div>
        </div>
    </main>

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