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

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

/* ================= GET ID ================= */
if (!isset($_GET['id'])) {
    die("Invalid request.");
}

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

/* ================= FETCH DATA ================= */
$sql = "
SELECT 
    mr.*,
    e.equipment_name,
    u.full_name AS technician_name
FROM maintenance_record mr
JOIN equipment e ON mr.equipment_id = e.id
JOIN users u ON mr.technician_id = u.id
WHERE mr.id = ?
";

$stmt = mysqli_prepare($conn, $sql);
mysqli_stmt_bind_param($stmt, "i", $id);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);

if (!$row = mysqli_fetch_assoc($result)) {
    die("Record not found.");
}

/* ================= HANDLE ACTION ================= */
if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    $comment = trim($_POST['hod_comment']);
    $action = $_POST['action'];
    $now = date("Y-m-d H:i:s");

    if ($action == "approve") {
        $status = "Completed";
    } elseif ($action == "reject") {
        $status = "HOD Rejected";
    } else {
        die("Invalid action.");
    }

    $update = "
        UPDATE maintenance_record
        SET status = ?, hod_comment = ?, updated_at = ?
        WHERE id = ?
    ";

    $stmt2 = mysqli_prepare($conn, $update);
    mysqli_stmt_bind_param($stmt2, "sssi", $status, $comment, $now, $id);

    if (mysqli_stmt_execute($stmt2)) {
        header("Location: maintenance.php?hod=success");
        exit();
    } else {
        echo "Failed to update record.";
    }
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>HOD Review</title>
    <link rel="stylesheet" href="../assets/style.css">

    <style>
        .box {
            max-width:700px;
            margin:30px auto;
            background:#fff;
            padding:25px;
            border-radius:10px;
        }

        textarea {
            width:100%;
            padding:10px;
            margin-top:10px;
        }

        .btn {
            padding:10px 15px;
            border:none;
            cursor:pointer;
            margin-right:10px;
            border-radius:6px;
            color:#fff;
        }

        .approve { background:green; }
        .reject { background:red; }
    </style>
</head>

<body>

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

<div class="main">

<div class="box">

    <h2>HOD Review</h2>

    <p><b>Equipment:</b> <?= htmlspecialchars($row['equipment_name']) ?></p>
    <p><b>Technician:</b> <?= htmlspecialchars($row['technician_name']) ?></p>
    <p><b>Description:</b> <?= htmlspecialchars($row['description']) ?></p>
    <p><b>Status:</b> <?= htmlspecialchars($row['status']) ?></p>

    <hr>

    <form method="POST">

        <label>HOD Comment</label>
        <textarea name="hod_comment" required></textarea>

        <br><br>

        <button type="submit" name="action" value="approve" class="btn approve">
            Approve
        </button>

        <button type="submit" name="action" value="reject" class="btn reject">
            Reject
        </button>

    </form>

</div>

</div>

</body>
</html>