<?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 an employer
$is_employer = ($_SESSION['user_type'] === 'Employer');
if (!$is_employer) {
    echo "Access denied. Only employers can view applications.";
    exit();
}

// Get all applications for the employer's job postings
$employer_id = $_SESSION['employer_id'];
$sql = "SELECT 
            a.*,
            j.job_title,
            j.location,
            j.salary,
            u.firstname,
            u.lastname,
            u.email,
            js.skills,
            u.phone
        FROM applications a 
        JOIN job_postings j ON a.job_id = j.job_id 
        JOIN job_seekers js ON a.seeker_id = js.seeker_id
        JOIN users u ON js.user_id = u.user_id
        WHERE j.employer_id = ?
        ORDER BY a.application_date DESC";

$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $employer_id);
$stmt->execute();
$result = $stmt->get_result();

// Handle status updates if posted
if (isset($_POST['update_status']) && isset($_POST['application_id']) && isset($_POST['new_status'])) {
    $application_id = intval($_POST['application_id']);
    $new_status = $_POST['new_status'];
    $allowed_statuses = ['Pending', 'Accepted', 'Rejected'];
    
    if (in_array($new_status, $allowed_statuses)) {
        $update_sql = "UPDATE applications SET status = ? WHERE application_id = ?";
        $update_stmt = $conn->prepare($update_sql);
        $update_stmt->bind_param("si", $new_status, $application_id);
        $update_stmt->execute();
        
        // Refresh the page to show updated status
        header("Location: ".$_SERVER['PHP_SELF']);
        exit();
    }
}
?>
<!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>View 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>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
</head>
<body class="min-h-screen flex flex-col bg-base-100">
    <?php include 'navbar.php';?>
    
    <main class="flex-grow container mx-auto px-4 py-8">
        <!-- Breadcrumbs with icons -->
        <div class="text-sm breadcrumbs mb-6">
            <ul class="flex items-center space-x-2">
                <li><a href="index.php" class="flex items-center text-gray-600 hover:text-primary">
                    <i class="fas fa-home mr-2"></i> Home
                </a></li>
                <li class="flex items-center text-primary">
                    <i class="fas fa-file-alt mr-2"></i> Applications Received
                </li>
            </ul>
        </div>

        <div class="card bg-base-100 shadow-xl">
            <div class="card-body">
                <div class="flex items-center justify-between mb-6">
                    <h1 class="text-3xl font-bold text-base-content">
                        <i class="fas fa-inbox mr-3 text-primary"></i>Applications Received
                    </h1>
                </div>
            
                <?php if ($result->num_rows > 0): ?>
                    <div class="overflow-x-auto">
                        <table class="table table-zebra w-full">
                            <thead>
                                <tr class="bg-base-200">
                                    <th class="text-base-content font-bold">
                                        <i class="fas fa-briefcase mr-2"></i>Job Title
                                    </th>
                                    <th class="text-base-content font-bold">
                                        <i class="fas fa-user mr-2"></i>Applicant
                                    </th>
                                    <th class="text-base-content font-bold">
                                        <i class="fas fa-address-card mr-2"></i>Contact
                                    </th>
                                    <th class="text-base-content font-bold">
                                        <i class="fas fa-graduation-cap mr-2"></i>Skills
                                    </th>
                                    <th class="text-base-content font-bold">
                                        <i class="fas fa-calendar mr-2"></i>Date
                                    </th>
                                    <th class="text-base-content font-bold">
                                        <i class="fas fa-tasks mr-2"></i>Status
                                    </th>
                                    <th class="text-base-content font-bold">
                                        <i class="fas fa-cog mr-2"></i>Actions
                                    </th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php while ($application = $result->fetch_assoc()): ?>
                                    <tr class="hover">
                                        <td class="font-medium">
                                            <div class="flex items-center space-x-2">
                                                <span class="text-primary">
                                                    <?= htmlspecialchars($application['job_title']) ?>
                                                </span>
                                            </div>
                                        </td>
                                        <td>
                                            <div class="font-medium">
                                                <?= htmlspecialchars($application['firstname'] . ' ' . $application['lastname']) ?>
                                            </div>
                                        </td>
                                        <td>
                                            <div class="space-y-1">
                                                <div class="flex items-center text-sm">
                                                    <i class="fas fa-envelope mr-2 text-gray-400"></i>
                                                    <?= htmlspecialchars($application['email']) ?>
                                                </div>
                                                <div class="flex items-center text-sm">
                                                    <i class="fas fa-phone mr-2 text-gray-400"></i>
                                                    <?= htmlspecialchars($application['phone']) ?>
                                                </div>
                                                <div class="flex items-center text-sm">
                                                    <i class="fas fa-location-dot mr-2 text-gray-400"></i>
                                                    <?= htmlspecialchars($application['location']) ?>
                                                </div>
                                            </div>
                                        </td>
                                        <td>
                                            <div class="font-medium">
                                                <?= htmlspecialchars($application['skills']) ?>
                                            </div>
                                        </td>
                                        <td>
                                            <div class="flex items-center">
                                                <span class="text-sm">
                                                    <?= date('F j, Y', strtotime($application['application_date'])) ?>
                                                </span>
                                            </div>
                                        </td>
                                        <td>
                                            <?php
                                            $statusClass = match($application['status']) {
                                                'Pending' => 'badge-warning',
                                                'Accepted' => 'badge-success',
                                                'Rejected' => 'badge-error',
                                                default => 'badge-ghost'
                                            };
                                            ?>
                                            <div class="badge <?= $statusClass ?> badge-lg gap-2">
                                                <?php if ($application['status'] === 'Pending'): ?>
                                                    <i class="fas fa-clock"></i>
                                                <?php elseif ($application['status'] === 'Accepted'): ?>
                                                    <i class="fas fa-check"></i>
                                                <?php elseif ($application['status'] === 'Rejected'): ?>
                                                    <i class="fas fa-times"></i>
                                                <?php endif; ?>
                                                <?= htmlspecialchars($application['status']) ?>
                                            </div>
                                        </td>
                                        <td>
                                            <form method="POST" class="flex items-center gap-2">
                                                <input type="hidden" name="application_id" value="<?= $application['application_id'] ?>">
                                                <select name="new_status" class="select select-bordered select-sm">
                                                    <option value="Pending" <?= $application['status'] === 'Pending' ? 'selected' : '' ?>>Pending</option>
                                                    <option value="Accepted" <?= $application['status'] === 'Accepted' ? 'selected' : '' ?>>Accept</option>
                                                    <option value="Rejected" <?= $application['status'] === 'Rejected' ? 'selected' : '' ?>>Reject</option>
                                                </select>
                                                <button type="submit" name="update_status" class="btn btn-primary btn-sm">
                                                    <i class="fas fa-save mr-1"></i> Update
                                                </button>
                                            </form>
                                        </td>
                                    </tr>
                                <?php endwhile; ?>
                            </tbody>
                        </table>
                    </div>
                <?php else: ?>
                    <div class="alert alert-info shadow-lg">
                        <div>
                            <i class="fas fa-info-circle text-2xl"></i>
                            <div>
                                <h3 class="font-bold">No Applications Yet</h3>
                                <div class="text-sm">No applications have been received yet for your job postings.</div>
                            </div>
                        </div>
                    </div>
                <?php endif; ?>
            </div>
        </div>
    </main>

    <footer class="mt-auto">
        <?php include 'footer.php';?>
    </footer>
</body>
</html>