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

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

$user_id = $_SESSION['user_id'];
$role = $_SESSION['role'];

if (!isset($_GET['id'])) {
    die("Invalid request.");
}

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

// Ambil detail maintenance_record + equipment
$sql = "
SELECT 
    mr.id AS record_id,
    mr.description AS maintenance_desc,
    mr.maintenance_date,
    mr.status AS maintenance_status,
    mr.created_at AS request_date,
    u.full_name AS technician_name,
    e.id AS equipment_id,
    e.equipment_name,
    e.category,
    e.serial_number,
    e.location,
    e.status AS equipment_status,
    e.qr_code,
    e.image
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", $record_id);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);

if (!$row = mysqli_fetch_assoc($result)) {
    die("Maintenance record not found.");
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Maintenance Record Details - QRCareTrack</title>

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

    <style>
        .card-details { 
            max-width:700px; 
            margin:20px auto; 
            background:#fff; 
            padding:30px; 
            border-radius:12px; 
            box-shadow:0 10px 25px rgba(0,0,0,0.1); 
        }

        .card-details h2 { margin-bottom:20px; }

        .detail-row { 
            display:flex; 
            justify-content:space-between; 
            margin-bottom:10px; 
        }

        .detail-row strong { 
            width:150px; 
            display:inline-block; 
        }

        .btn { 
            display:inline-block; 
            padding:10px 20px; 
            background:#2bbbad; 
            color:#fff; 
            text-decoration:none; 
            border-radius:6px; 
            margin-top:15px; 
        }

        .btn:hover { background:#239c92; }

        .btn-warning { background:#f39c12; }
        .btn-warning:hover { background:#d58512; }

        .btn-secondary { background:#e74c3c; }
        .btn-secondary:hover { background:#c0392b; }

        .equipment-image { 
            max-width:150px; 
            max-height:150px; 
            object-fit:cover; 
            border-radius:6px; 
        }

        .status-badge{
            display:inline-block;
            padding:4px 10px;
            border-radius:6px;
            background:#f39c12;
            color:#fff;
            font-size:13px;
        }
    </style>
</head>

<body>

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

<div class="main">

    <div class="card-details">

        <h2>Maintenance Record Details</h2>

        <div class="detail-row">
            <strong>Equipment Name:</strong>
            <?= htmlspecialchars($row['equipment_name']) ?>
        </div>

        <div class="detail-row">
            <strong>Category:</strong>
            <?= htmlspecialchars($row['category']) ?>
        </div>

        <div class="detail-row">
            <strong>Serial Number:</strong>
            <?= htmlspecialchars($row['serial_number']) ?>
        </div>

        <div class="detail-row">
            <strong>Location:</strong>
            <?= htmlspecialchars($row['location']) ?>
        </div>

        <div class="detail-row">
            <strong>Maintenance Description:</strong>
            <?= nl2br(htmlspecialchars($row['maintenance_desc'])) ?>
        </div>

        <div class="detail-row">
            <strong>Technician:</strong>
            <?= htmlspecialchars($row['technician_name']) ?>
        </div>

        <div class="detail-row">
            <strong>Status:</strong>
            <span class="status-badge">
                <?= htmlspecialchars($row['maintenance_status']) ?>
            </span>
        </div>

        <div class="detail-row">
            <strong>Maintenance Date:</strong>
            <?= $row['maintenance_date']
                ? date("d/m/Y", strtotime($row['maintenance_date']))
                : '-' ?>
        </div>

        <div class="detail-row">
            <strong>Request Date:</strong>
            <?= $row['request_date']
                ? date("d/m/Y H:i", strtotime($row['request_date']))
                : '-' ?>
        </div>

        <?php if (!empty($row['image'])): ?>
            <div class="detail-row">
                <strong>Equipment Image:</strong><br>
                <img src="../uploads/<?= htmlspecialchars($row['image']) ?>"
                     class="equipment-image">
            </div>
        <?php endif; ?>

        <div style="margin-top:15px;">

            <a href="../equipment/equipment_details.php?id=<?= $row['equipment_id'] ?>"
               class="btn">
                View Equipment Details
            </a>

            <?php if (
                $role == 'technician' &&
                $row['maintenance_status'] != 'HOD Review' &&
                $row['maintenance_status'] != 'Completed'
            ): ?>

                <a href="edit_records.php?id=<?= $row['record_id'] ?>"
                   class="btn btn-warning">
                    Edit Record
                </a>

            <?php endif; ?>

            <?php if (
                $role == 'hod' &&
                $row['maintenance_status'] == 'HOD Review'
            ): ?>

                <a href="HOD_review_Status.php?id=<?= $row['record_id'] ?>"
                   class="btn"
                   style="background:#2563eb; margin-left:10px;">
                    Review (HOD)
                </a>

            <?php endif; ?>

            <a href="../maintenance.php"
               class="btn btn-secondary">
                Back to Maintenance
            </a>

        </div>

    </div>

</div>

</body>
</html>