<?php

error_reporting(E_ALL);
ini_set('display_errors',1);

require '../config.php';
session_start();
session_write_close();

if(!isset($_SESSION['user_id'])){
    header("Location: ../login.php");
    exit;
}

/*
|--------------------------------------------------------------------------
| COMPLETE INTERVIEW
|--------------------------------------------------------------------------
*/

if(isset($_POST['complete'])){

    $interviewId =
    intval($_POST['interview_id']);

    $score =
    intval($_POST['score']);

    $feedback =
    trim($_POST['feedback']);

    $stmt = $conn->prepare("
    UPDATE interviews
    SET
        score=?,
        feedback=?,
        status='Completed'
    WHERE id=?
    ");

    $stmt->bind_param(
        "isi",
        $score,
        $feedback,
        $interviewId
    );

    $stmt->execute();

    header(
    "Location: interview_calendar.php?saved=1"
    );
    exit;
}

/*
|--------------------------------------------------------------------------
| LOAD INTERVIEWS
|--------------------------------------------------------------------------
*/

$interviews = $conn->query("
SELECT

i.*,

a.full_name,

j.title job_title,

u.name interviewer_name

FROM interviews i

LEFT JOIN applicants a
ON a.id=i.applicant_id

LEFT JOIN jobs j
ON j.id=a.job_id

LEFT JOIN users u
ON u.id=i.interviewer

ORDER BY i.interview_date ASC
");
?>

<!DOCTYPE html>
<html>
<head>

<title>Interview Calendar</title>

<script src="https://cdn.tailwindcss.com"></script>

</head>

<body class="flex bg-gray-100 min-h-screen">

<?php include '../sidebar.php'; ?>

<div class="flex-1 p-8" id="main-content">

<h1 class="text-3xl font-bold mb-6">

Interview Calendar

</h1>

<?php if(isset($_GET['saved'])): ?>

<div class="bg-green-100 border border-green-300 text-green-700 p-4 rounded-xl mb-6">

Interview completed successfully.

</div>

<?php endif; ?>

<div class="bg-white rounded-xl shadow overflow-x-auto">

<table class="w-full">

<thead class="bg-gray-100">

<tr>

<th class="p-4 text-left">
Candidate
</th>

<th class="p-4 text-left">
Position
</th>

<th class="p-4 text-left">
Interviewer
</th>

<th class="p-4 text-left">
Date
</th>

<th class="p-4">
Status
</th>

<th class="p-4">
Action
</th>

</tr>

</thead>

<tbody>

<?php while($row=$interviews->fetch_assoc()): ?>

<tr class="border-b hover:bg-gray-50">

<td class="p-4">

<?= htmlspecialchars(
$row['full_name']
) ?>

</td>

<td class="p-4">

<?= htmlspecialchars(
$row['job_title']
) ?>

</td>

<td class="p-4">

<?= htmlspecialchars(
$row['interviewer_name']
) ?>

</td>

<td class="p-4">

<?= date(
'd M Y H:i',
strtotime(
$row['interview_date']
)
) ?>

</td>

<td class="p-4 text-center">

<?php if($row['status']=='Scheduled'): ?>

<span class="bg-yellow-100 text-yellow-700 px-3 py-1 rounded-full">

Scheduled

</span>

<?php else: ?>

<span class="bg-green-100 text-green-700 px-3 py-1 rounded-full">

Completed

</span>

<?php endif; ?>

</td>

<td class="p-4">

<?php if($row['status']=='Scheduled'): ?>

<button
onclick="document.getElementById('modal<?= $row['id'] ?>').classList.remove('hidden')"
class="bg-blue-600 text-white px-4 py-2 rounded">

Complete

</button>

<?php else: ?>

<a
href="applicant_profile.php?id=<?= $row['applicant_id'] ?>"
class="bg-gray-600 text-white px-4 py-2 rounded">

View

</a>

<?php endif; ?>

</td>

</tr>

<!-- MODAL -->

<div
id="modal<?= $row['id'] ?>"
class="hidden fixed inset-0 bg-black/50 flex items-center justify-center z-50">

<div class="bg-white rounded-xl shadow-lg w-full max-w-xl p-6">

<h2 class="text-xl font-bold mb-4">

Complete Interview

</h2>

<form method="POST">

<input
type="hidden"
name="interview_id"
value="<?= $row['id'] ?>">

<div class="mb-4">

<label class="block mb-2">

Score (1 - 100)

</label>

<input
type="number"
name="score"
min="1"
max="100"
required
class="w-full border p-3 rounded-lg">

</div>

<div class="mb-4">

<label class="block mb-2">

Feedback

</label>

<textarea
name="feedback"
rows="5"
required
class="w-full border p-3 rounded-lg"></textarea>

</div>

<div class="flex gap-3">

<button
name="complete"
class="bg-green-600 text-white px-5 py-3 rounded-lg">

Save Interview

</button>

<button
type="button"
onclick="document.getElementById('modal<?= $row['id'] ?>').classList.add('hidden')"
class="bg-gray-500 text-white px-5 py-3 rounded-lg">

Close

</button>

</div>

</form>

</div>

</div>

<?php endwhile; ?>

</tbody>

</table>

</div>

</div>

</body>
</html>