<?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;
}

$id = intval($_GET['id'] ?? 0);

if(!$id){
    die("Invalid case.");
}

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

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

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

    $stmt = $conn->prepare("
        UPDATE disciplinary_cases
        SET
            status=?,
            hr_comments=?
        WHERE id=?
    ");

    $stmt->bind_param(
        "ssi",
        $status,
        $comments,
        $id
    );

    $stmt->execute();

    header(
        "Location: disciplinary_review.php?id=$id&saved=1"
    );

    exit;
}

$stmt = $conn->prepare("
SELECT

dc.*,
e.name employee_name,
u.username issued_by_name

FROM disciplinary_cases dc

LEFT JOIN employees e
ON e.id=dc.employee_id

LEFT JOIN users u
ON u.id=dc.issued_by

WHERE dc.id=?
");

$stmt->bind_param("i",$id);
$stmt->execute();

$case =
$stmt->get_result()->fetch_assoc();

if(!$case){
    die("Case not found.");
}

?>

<!DOCTYPE html>
<html>
<head>

<title>Review Case</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">

Disciplinary Review

</h1>

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

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

Case updated successfully.

</div>

<?php endif; ?>

<div class="bg-white rounded-xl shadow p-6">

<div class="grid md:grid-cols-2 gap-6 mb-6">

<div>

<strong>Employee:</strong><br>

<?= htmlspecialchars($case['employee_name']) ?>

</div>

<div>

<strong>Case Type:</strong><br>

<?= htmlspecialchars($case['case_type']) ?>

</div>

<div>

<strong>Issued By:</strong><br>

<?= htmlspecialchars($case['issued_by_name']) ?>

</div>

<div>

<strong>Issue Date:</strong><br>

<?= date(
'd M Y',
strtotime($case['issue_date'])
) ?>

</div>

</div>

<div class="mb-6">

<h2 class="font-bold mb-2">

Subject

</h2>

<div class="bg-gray-50 border rounded-lg p-4">

<?= nl2br(
htmlspecialchars($case['subject'])
) ?>

</div>

</div>

<div class="mb-6">

<h2 class="font-bold mb-2">

Description

</h2>

<div class="bg-gray-50 border rounded-lg p-4">

<?= nl2br(
htmlspecialchars($case['description'])
) ?>

</div>

</div>

<form method="POST">

<div class="mb-5">

<label class="block mb-2">

Status

</label>

<select
name="status"
class="border p-3 rounded-lg w-full">

<option
value="Open"
<?= $case['status']=='Open'?'selected':'' ?>>

Open

</option>

<option
value="Under Review"
<?= $case['status']=='Under Review'?'selected':'' ?>>

Under Review

</option>

<option
value="Resolved"
<?= $case['status']=='Resolved'?'selected':'' ?>>

Resolved

</option>

<option
value="Closed"
<?= $case['status']=='Closed'?'selected':'' ?>>

Closed

</option>

</select>

</div>

<div class="mb-5">

<label class="block mb-2">

HR Comments

</label>

<textarea
name="comments"
rows="6"
class="w-full border p-3 rounded-lg"><?= htmlspecialchars($case['hr_comments']) ?></textarea>

</div>

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

Save Review

</button>

<a
href="disciplinary_cases.php"
class="bg-gray-600 text-white px-8 py-3 rounded-lg ml-2">

Back

</a>

</form>

</div>

</div>

</body>
</html>