<?php
include('dbconnect.php');

// Handle POST form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $applicant_id = $_POST['applicant_id'];
    $interview_date = $_POST['interview_date'];
    $selection_status = $_POST['selection_status']; // Selected or Rejected

    $sql = "INSERT INTO candidate_selection (applicant_id, interview_status, selection_status, interview_date, selection_date)
            VALUES (?, 'Completed', ?, ?, NOW())
            ON DUPLICATE KEY UPDATE interview_status = 'Completed', selection_status = ?, interview_date = ?, selection_date = NOW()";

    if ($stmt = $con->prepare($sql)) {
        $stmt->bind_param("isssss", $applicant_id, $selection_status, $interview_date, $selection_status, $interview_date);
        if ($stmt->execute()) {
            $message = "✅ Interview details updated and applicant <strong>" . htmlspecialchars($selection_status) . "</strong>.";
        } else {
            $message = "❌ Error: " . $stmt->error;
        }
        $stmt->close();
    } else {
        $message = "❌ Error preparing query: " . $con->error;
    }
}

$applicants_sql = "SELECT * FROM job_applicants ORDER BY name ASC";
$applicants_result = $con->query($applicants_sql);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>Interview Panel</title>
    <meta name="viewport" content="width=device-width, initial-scale=1" />

    <!-- Bootstrap 5 CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet" />

    <!-- Font Awesome -->
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet" />

    <style>
        body {
            background: linear-gradient(135deg, #667eea, #764ba2);
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            min-height: 100vh;
            display: flex;
            flex-direction: column;
        }

        header {
            background-color: rgba(0, 0, 0, 0.5);
            padding: 15px 30px;
            color: white;
            display: flex;
            justify-content: space-between;
            align-items: center;
            flex-wrap: wrap;
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.4);
        }

        header h2 {
            font-weight: 700;
            font-size: 1.8rem;
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .logout-btn {
            background-color: #e74c3c;
            border: none;
            padding: 10px 22px;
            border-radius: 50px;
            font-weight: 600;
            color: white;
            cursor: pointer;
            display: flex;
            align-items: center;
            gap: 8px;
            transition: background-color 0.3s ease;
            box-shadow: 0 2px 10px rgba(231, 76, 60, 0.6);
        }

        .logout-btn:hover {
            background-color: #c0392b;
            box-shadow: 0 4px 15px rgba(192, 57, 43, 0.8);
        }

        main {
            flex-grow: 1;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 40px 15px;
        }

        form {
            background: white;
            border-radius: 16px;
            padding: 30px 40px;
            max-width: 480px;
            width: 100%;
            box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
            transition: box-shadow 0.3s ease;
        }

        form:hover {
            box-shadow: 0 12px 40px rgba(0, 0, 0, 0.25);
        }

        form label {
            font-weight: 600;
            margin-top: 15px;
            display: flex;
            align-items: center;
            gap: 8px;
            color: #333;
        }

        select, input[type="datetime-local"], input[type="submit"] {
            margin-top: 6px;
            padding: 12px 14px;
            border-radius: 10px;
            border: 1.8px solid #ddd;
            font-size: 1rem;
            width: 100%;
            transition: border-color 0.3s ease;
        }

        select:focus, input[type="datetime-local"]:focus {
            outline: none;
            border-color: #667eea;
            box-shadow: 0 0 6px rgba(102, 126, 234, 0.6);
        }

        input[type="submit"] {
            background: #667eea;
            border: none;
            color: white;
            font-weight: 700;
            cursor: pointer;
            margin-top: 25px;
            font-size: 1.1rem;
            display: flex;
            justify-content: center;
            align-items: center;
            gap: 10px;
            transition: background-color 0.3s ease;
        }

        input[type="submit"]:hover {
            background: #5a6dd9;
        }

        /* Responsive */
        @media (max-width: 576px) {
            form {
                padding: 25px 20px;
            }

            header {
                justify-content: center;
                gap: 15px;
            }
        }

        /* Message */
        .message {
            max-width: 480px;
            margin: 0 auto 20px auto;
            padding: 14px 18px;
            border-radius: 10px;
            font-weight: 600;
            text-align: center;
            box-shadow: 0 3px 15px rgba(0,0,0,0.1);
        }

        .message.success {
            background-color: #d4edda;
            color: #155724;
            border: 1.8px solid #c3e6cb;
        }

        .message.error {
            background-color: #f8d7da;
            color: #721c24;
            border: 1.8px solid #f5c6cb;
        }
    </style>
</head>
<body>

<header>
    <h2><i class="fas fa-comments-medical"></i> Interview Panel</h2>
    <form action="logout.php" method="POST">
        <button class="logout-btn" type="submit"><i class="fas fa-sign-out-alt"></i> Logout</button>
    </form>
</header>

<main>
    <div class="container">
        <?php if (!empty($message)): ?>
            <div class="message <?= strpos($message, '✅') === 0 ? 'success' : 'error' ?>">
                <?= $message ?>
            </div>
        <?php endif; ?>

        <form method="POST" action="selection.php" novalidate>
            <label for="applicant_id"><i class="fas fa-user"></i> Select Applicant:</label>
            <select name="applicant_id" id="applicant_id" required>
                <option value="" disabled selected>-- Choose Applicant --</option>
                <?php while ($row = $applicants_result->fetch_assoc()) : ?>
                    <option value="<?= htmlspecialchars($row['id']) ?>">
                        <?= htmlspecialchars($row['name']) ?> (Passport: <?= htmlspecialchars($row['passport_no']) ?>)
                    </option>
                <?php endwhile; ?>
            </select>

            <label for="interview_date"><i class="fas fa-calendar-check"></i> Interview Date & Time:</label>
            <input type="datetime-local" id="interview_date" name="interview_date" required>

            <label for="selection_status"><i class="fas fa-check-circle"></i> Selection Status:</label>
            <select name="selection_status" id="selection_status" required>
                <option value="" disabled selected>-- Choose Status --</option>
                <option value="Selected">Selected</option>
                <option value="Rejected">Rejected</option>
            </select>

            <input type="submit" value="Submit Interview Result" />
        </form>
    </div>
</main>

<!-- Bootstrap 5 JS Bundle (with Popper) -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>

</body>
</html>
