<?php
include('dbconnect.php');

// Handle interview form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $applicant_id = $_POST['applicant_id'];
    $interview_date = $_POST['interview_date'];
    $selection_status = $_POST['selection_status'];

    $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()) {
            echo "<script>alert('✅ Interview details updated: Applicant $selection_status');</script>";
        } else {
            echo "<script>alert('❌ Error: " . $stmt->error . "');</script>";
        }
        $stmt->close();
    } else {
        echo "<script>alert('❌ Error preparing query: " . $con->error . "');</script>";
    }
}

$applicants_sql = "SELECT * FROM job_applicants";
$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.0">

    <!-- Google Fonts -->
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600&display=swap" rel="stylesheet">

    <style>
        * {
            box-sizing: border-box;
        }

        body {
            font-family: 'Poppins', sans-serif;
            background: linear-gradient(to right, #6a11cb, #2575fc);
            margin: 0;
            padding: 0;
            min-height: 100vh;
        }

        header {
            background: rgba(0, 0, 0, 0.7);
            color: white;
            padding: 20px;
            text-align: center;
        }

        .logout-form {
            position: absolute;
            top: 20px;
            right: 20px;
        }

        .logout-btn {
            background: #e74c3c;
            border: none;
            color: white;
            padding: 8px 16px;
            border-radius: 20px;
            font-weight: bold;
            cursor: pointer;
            transition: background 0.3s;
        }

        .logout-btn:hover {
            background: #c0392b;
        }

        .form-container {
            max-width: 500px;
            margin: 40px auto;
            background: white;
            padding: 30px;
            border-radius: 12px;
            box-shadow: 0 10px 25px rgba(0, 0, 0, 0.15);
        }

        label {
            font-weight: 600;
            margin-top: 15px;
            display: block;
        }

        select, input[type="datetime-local"], input[type="submit"] {
            width: 100%;
            padding: 10px;
            margin-top: 6px;
            border: 1px solid #ccc;
            border-radius: 8px;
            font-size: 14px;
        }

        input[type="submit"] {
            background-color: #3498db;
            color: white;
            font-weight: bold;
            border: none;
            cursor: pointer;
            margin-top: 20px;
            transition: background 0.3s;
        }

        input[type="submit"]:hover {
            background-color: #2980b9;
        }

        @media (max-width: 600px) {
            .form-container {
                margin: 20px;
                padding: 20px;
            }

            .logout-form {
                position: static;
                text-align: right;
                margin: 10px;
            }
        }
    </style>
</head>
<body>

<header>
    <h2>Interview Panel</h2>
    <form class="logout-form" action="logout.php" method="POST">
        <button class="logout-btn">Logout</button>
    </form>
</header>

<div class="form-container">
    <form method="POST" action="selection.php">
        <label for="applicant_id">Select Applicant:</label>
        <select name="applicant_id" required>
            <option value="">-- Select Applicant --</option>
            <?php while ($row = $applicants_result->fetch_assoc()) { ?>
                <option value="<?php echo $row['id']; ?>">
                    <?php echo htmlspecialchars($row['name']); ?> (Passport: <?php echo htmlspecialchars($row['passport_no']); ?>)
                </option>
            <?php } ?>
        </select>

        <label for="interview_date">Interview Date:</label>
        <input type="datetime-local" name="interview_date" required>

        <label for="selection_status">Selection Status:</label>
        <select name="selection_status" required>
            <option value="">-- Choose Status --</option>
            <option value="Selected">✅ Selected</option>
            <option value="Rejected">❌ Rejected</option>
        </select>

        <input type="submit" value="Submit Interview Result">
    </form>
</div>

</body>
</html>
