<?php
// Include the database connection
include('dbconnect.php');

// Function to conduct interview and update status
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Get data from the form
    $applicant_id = $_POST['applicant_id'];
    $interview_date = $_POST['interview_date'];
    $selection_status = $_POST['selection_status'];

    // SQL query to insert or update interview and 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)) {
        // Bind 6 parameters (1 int + 5 strings)
        $stmt->bind_param("isssss", $applicant_id, $selection_status, $interview_date, $selection_status, $interview_date);
        if ($stmt->execute()) {
            header("Location: " . $_SERVER['PHP_SELF']);
            exit();
        } else {
            echo "❌ Error: " . $stmt->error;
        }
        $stmt->close();
    } else {
        echo "❌ Error preparing query: " . $con->error;
    }
}

// Fetch applicants
$applicants_sql = "SELECT * FROM job_applicants";
$applicants_result = $con->query($applicants_sql);
?>

<!-- HTML with Responsive Form and Logout -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Interview Status Update</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style>
        body {
            font-family: 'Segoe UI', sans-serif;
            background-color: #ecf0f1;
            margin: 0;
            padding: 0;
        }

        header {
            background-color: #2c3e50;
            color: white;
            padding: 15px 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        header h2 {
            margin: 0;
            font-size: 22px;
        }

        .logout-btn {
            background-color: #e74c3c;
            color: white;
            border: none;
            padding: 10px 18px;
            border-radius: 5px;
            cursor: pointer;
            font-size: 14px;
            font-weight: bold;
        }

        .logout-btn:hover {
            background-color: #c0392b;
        }

        .container {
            max-width: 600px;
            margin: 40px auto;
            padding: 25px;
            background-color: #fff;
            border-radius: 8px;
            box-shadow: 0 5px 12px rgba(0, 0, 0, 0.1);
        }

        h3 {
            text-align: center;
            margin-bottom: 25px;
            color: #333;
        }

        label {
            font-weight: 500;
            display: block;
            margin: 10px 0 6px;
            color: #555;
        }

        select,
        input[type="datetime-local"],
        input[type="submit"] {
            width: 100%;
            padding: 12px;
            margin-bottom: 15px;
            border: 1px solid #ccc;
            border-radius: 6px;
            font-size: 16px;
            box-sizing: border-box;
        }

        input[type="submit"] {
            background-color: #3498db;
            color: white;
            font-weight: bold;
            cursor: pointer;
            transition: background 0.3s;
        }

        input[type="submit"]:hover {
            background-color: #2980b9;
        }

        @media screen and (max-width: 768px) {
            .container {
                width: 90%;
                margin: 20px auto;
                padding: 15px;
            }

            header {
                flex-direction: column;
                align-items: flex-start;
            }

            .logout-btn {
                margin-top: 10px;
                width: 100%;
            }
        }
    </style>
</head>
<body>

<header>
    <h2>Interview Panel</h2>
    <form action="logout.php" method="post">
        <button class="logout-btn">Logout</button>
    </form>
</header>

<div class="container">
    <h3>Update Interview Status</h3>
    <form method="POST" action="interview.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']); ?>) -
                    (Position: <?php echo htmlspecialchars($row['position']); ?>) -
                    (Job Country: <?php echo htmlspecialchars($row['job_country']); ?>) -
                    (Reference: <?php echo htmlspecialchars($row['reference']); ?>)
                </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="Selected">Selected</option>
            <option value="Rejected">Rejected</option>
        </select>

        <input type="submit" value="Update Interview Status">
    </form>
</div>

</body>
</html>
