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

$userRole = strtolower($_SESSION['role'] ?? '');

if(
    !in_array(
        $userRole,
        ['hr','admin','superadmin','manager']
    )
){
    die("Access Denied");
}

/*
|--------------------------------------------------------------------------
| SAVE DISCIPLINARY CASE
|--------------------------------------------------------------------------
*/

if($_SERVER['REQUEST_METHOD'] == 'POST'){

    $employeeId =
    intval($_POST['employee_id']);

    $caseType =
    trim($_POST['case_type']);

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

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

    $issuedBy =
    intval($_SESSION['user_id']);

    $stmt = $conn->prepare("
        INSERT INTO disciplinary_cases
        (
            employee_id,
            case_type,
            subject,
            description,
            issued_by,
            issue_date,
            status
        )
        VALUES
        (
            ?,?,?,?,?,CURDATE(),'Open'
        )
    ");

    $stmt->bind_param(
        "isssi",
        $employeeId,
        $caseType,
        $subject,
        $description,
        $issuedBy
    );

    $stmt->execute();

    header(
        "Location: issue_query.php?success=1"
    );
    exit;
}

/*
|--------------------------------------------------------------------------
| LOAD EMPLOYEES
|--------------------------------------------------------------------------
*/

$employees = $conn->query("
SELECT

e.id,
e.name,
d.name department

FROM employees e

LEFT JOIN departments d
ON d.id=e.department_id

ORDER BY e.name
");

?>

<!DOCTYPE html>
<html>
<head>

<title>Issue Query / Warning</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">

Issue Disciplinary Case

</h1>

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

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

Disciplinary case created successfully.

</div>

<?php endif; ?>

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

<form method="POST">

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

<!-- EMPLOYEE -->

<div>

<label class="block font-medium mb-2">

Employee

</label>

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

<option value="">

Select Employee

</option>

<?php while($emp=$employees->fetch_assoc()): ?>

<option value="<?= $emp['id'] ?>">

<?= htmlspecialchars($emp['name']) ?>

(
<?= htmlspecialchars($emp['department']) ?>
)

</option>

<?php endwhile; ?>

</select>

</div>

<!-- CASE TYPE -->

<div>

<label class="block font-medium mb-2">

Case Type

</label>

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

<option value="Query">

Query

</option>

<option value="Warning">

Warning

</option>

<option value="Suspension">

Suspension

</option>

<option value="Termination">

Termination

</option>

</select>

</div>

</div>

<!-- SUBJECT -->

<div class="mt-6">

<label class="block font-medium mb-2">

Subject

</label>

<input
type="text"
name="subject"
required
class="w-full border rounded-lg p-3">

</div>

<!-- DESCRIPTION -->

<div class="mt-6">

<label class="block font-medium mb-2">

Description

</label>

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

</div>

<button
type="submit"
class="mt-6 bg-red-600 hover:bg-red-700 text-white px-8 py-3 rounded-lg">

Create Case

</button>

</form>

</div>

</div>

</body>
</html>