<?php
session_start();
require_once __DIR__ . "/../config/db.php";

if (
    !isset($_SESSION['user_id']) ||
    !in_array($_SESSION['role'], ['hod','staff'])
) {
    header("Location: ../login.php");
    exit();
}

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

if($request_id <= 0){
    die("Invalid request ID.");
}

/* ===============================
   FETCH REQUEST
================================= */

$sql = "
SELECT 
    m.*,
    u.full_name,
    e.equipment_name

FROM maintenance_request m

JOIN users u
ON m.requested_by = u.id

JOIN equipment e
ON m.equipment_id = e.id

WHERE m.id = ?
LIMIT 1
";

$stmt = mysqli_prepare($conn, $sql);

mysqli_stmt_bind_param(
    $stmt,
    "i",
    $request_id
);

mysqli_stmt_execute($stmt);

$result = mysqli_stmt_get_result($stmt);

$request = mysqli_fetch_assoc($result);

if(!$request){
    die("Request not found.");
}

/* ===============================
   STATUS BADGE
================================= */

switch($request['status']){

    case 'Reported':
        $status_class='status-reported';
        break;

    case 'Approved':
        $status_class='status-approved';
        break;

    case 'Rejected':
        $status_class='status-rejected';
        break;

    case 'HOD Review':
        $status_class='status-review';
        break;

    case 'Completed':
        $status_class='status-completed';
        break;

    default:
        $status_class='status-reported';
        break;
}

/* ===============================
   HIDE ACTIONS?
================================= */

$hide_actions = in_array(
    $request['status'],
    ['HOD Review', 'Completed']
);

$hod_department_id =
$_SESSION['department_id'] ?? 0;

?>

<!DOCTYPE html>
<html>

<head>

<meta charset="UTF-8">

<title>Maintenance Details</title>

<link rel="stylesheet" href="../assets/style.css">
<link rel="stylesheet" href="../assets/maintenance_details.css">

<style>

/* ===============================
   MODAL DESIGN
================================= */

.modal-overlay {

    position: fixed;
    top: 0;
    left: 0;

    width: 100%;
    height: 100%;

    background: rgba(0,0,0,0.45);

    display: none;
    justify-content: center;
    align-items: center;

    z-index: 999;
}

.modal-card {

    background: #fff;
    width: 420px;

    border-radius: 14px;

    padding: 20px 25px;

    box-shadow: 0 10px 35px rgba(0,0,0,0.2);

    animation: fadeIn 0.25s ease;
}

.modal-header {

    display: flex;
    justify-content: space-between;
    align-items: center;

    margin-bottom: 20px;
}

.modal-header h3 {

    margin: 0;
    font-size: 18px;
}

.close-btn {

    cursor: pointer;
    font-size: 22px;
    color: #888;
}

.close-btn:hover {

    color: #000;
}

.form-group {

    margin-bottom: 15px;
}

.form-group label {

    font-weight: 600;
    display: block;
    margin-bottom: 6px;
}

.form-group select,
.form-group input {

    width: 100%;

    padding: 10px;

    border-radius: 8px;
    border: 1px solid #ccc;

    font-size: 14px;
}

.form-group select:focus,
.form-group input:focus {

    border-color: #4CAF50;
    outline: none;
}

.modal-actions {

    display: flex;
    justify-content: flex-end;
    gap: 10px;

    margin-top: 20px;
}

.btn {

    padding: 10px 14px;

    border-radius: 8px;
    border: none;

    cursor: pointer;
}

.btn.primary {

    background: #4CAF50;
    color: white;
}

.btn.primary:hover {

    background: #43a047;
}

.btn.cancel {

    background: #ddd;
}

.btn.cancel:hover {

    background: #bbb;
}

/* ===============================
   STATUS COLORS
================================= */

.status-review{
    background:#f39c12 !important;
    color:#fff;
}

.status-completed{
    background:#27ae60 !important;
    color:#fff;
}

/* ===============================
   ANIMATION
================================= */

@keyframes fadeIn {

    from {
        transform: translateY(-10px);
        opacity: 0;
    }

    to {
        transform: translateY(0);
        opacity: 1;
    }
}

</style>

</head>

<body>

<?php include "../includes/sidebar.php"; ?>

<div class="main">

<div class="card">

<h3>Maintenance Request Details</h3>

<a href="javascript:history.back()"
class="btn-back">

    ← Back

</a>

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

<div
style="
background:#d4edda;
padding:10px;
border-radius:8px;
margin-bottom:15px;
">

    Technician assigned successfully.

</div>

<?php endif; ?>

<!-- EQUIPMENT -->

<div class="field-row">

<label>Equipment</label>

<div>

    <?= htmlspecialchars($request['equipment_name']) ?>

</div>

</div>

<!-- REPORTED BY -->

<div class="field-row">

<label>Reported By</label>

<div>

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

</div>

</div>

<!-- DESCRIPTION -->

<div class="field-row">

<label>Issue Description</label>

<div>

<?= nl2br(
    htmlspecialchars($request['issue_description'])
) ?>

</div>

</div>

<!-- URGENCY -->

<div class="field-row">

<label>Urgency</label>

<div>

    <?= htmlspecialchars($request['urgency']) ?>

</div>

</div>

<!-- STATUS -->

<div class="field-row">

<label>Status</label>

<div>

<span class="badge <?= $status_class ?>">

    <?= htmlspecialchars($request['status']) ?>

</span>

</div>

</div>

<!-- DATE -->

<div class="field-row">

<label>Date</label>

<div>

<?= date(
    "d M Y",
    strtotime($request['created_at'])
) ?>

</div>

</div>

<!-- ACTIONS -->

<?php if(
    $_SESSION['role']=='hod'
    && $request['status']=='Reported'
    && !$hide_actions
): ?>

<div class="actions">

<!-- REJECT -->

<form method="POST"
action="reject_request.php">

<input type="hidden"
name="request_id"
value="<?= $request['id'] ?>">

<button type="submit"
class="btn-sm reject">

    Reject

</button>

</form>

<!-- APPROVE -->

<button type="button"
class="btn-sm approve"
onclick="openModal()">

    Assign & Approve

</button>

</div>

<?php endif; ?>

</div>

</div>

<!-- ===============================
     MODAL
================================= -->

<div class="modal-overlay"
id="assignModal">

<div class="modal-card">

<div class="modal-header">

<h3>Assign Technician</h3>

<span class="close-btn"
onclick="closeModal()">

    ×

</span>

</div>

<form method="POST"
action="assign_technician.php">

<input type="hidden"
name="request_id"
value="<?= $request['id'] ?>">

<!-- TECHNICIAN -->

<div class="form-group">

<label>Select Technician</label>

<select name="technician_id" required>

<option value="">
    -- Select Technician --
</option>

<?php

$techs = mysqli_prepare(
$conn,
"SELECT id, full_name 
FROM users 
WHERE role='technician' 
AND department_id=?"
);

mysqli_stmt_bind_param(
    $techs,
    "i",
    $hod_department_id
);

mysqli_stmt_execute($techs);

$tech_res = mysqli_stmt_get_result($techs);

while($tech=mysqli_fetch_assoc($tech_res)){

echo "

<option value='{$tech['id']}'>

".htmlspecialchars($tech['full_name'])."

</option>

";
}

?>

</select>

</div>

<!-- DATE -->

<div class="form-group">

<label>Maintenance Due</label>

<input type="datetime-local"
name="maintenance_date"
min="<?= date('Y-m-d\TH:i') ?>"
required>

</div>

<!-- ACTIONS -->

<div class="modal-actions">

<button type="submit"
class="btn primary">

    Confirm Assign

</button>

<button type="button"
class="btn cancel"
onclick="closeModal()">

    Cancel

</button>

</div>

</form>

</div>

</div>

<script>

function openModal(){

    document.getElementById(
        'assignModal'
    ).style.display = 'flex';
}

function closeModal(){

    document.getElementById(
        'assignModal'
    ).style.display = 'none';
}

</script>

</body>
</html>