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

$res = $conn->query("
SELECT

aa.*,

a.asset_name,
a.asset_type,
a.serial_number,

e.name employee_name,

d.name department,

u.username assigned_by_user

FROM asset_assignments aa

LEFT JOIN assets_master a
ON a.id = aa.asset_id

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

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

LEFT JOIN users u
ON u.id = aa.assigned_by

ORDER BY aa.id DESC
");
?>

<!DOCTYPE html>
<html>
<head>

<title>Asset History</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">

<!-- HEADER -->

<div class="flex justify-between items-center mb-8">

    <div>

        <h1 class="text-3xl font-bold text-gray-800">

            Asset Assignment History

        </h1>

        <p class="text-gray-500 mt-1">

            Complete audit trail of company assets

        </p>

    </div>

</div>

<!-- TABLE -->

<div class="bg-white rounded-2xl shadow overflow-hidden">

<div class="overflow-x-auto">

<table class="w-full">

<thead>

<tr class="bg-gray-100 text-gray-700">

    <th class="p-4 text-left">
        Asset
    </th>

    <th class="p-4 text-left">
        Type
    </th>

    <th class="p-4 text-left">
        Serial No.
    </th>

    <th class="p-4 text-left">
        Employee
    </th>

    <th class="p-4 text-left">
        Department
    </th>

    <th class="p-4 text-center">
        Assigned Date
    </th>

    <th class="p-4 text-center">
        Returned Date
    </th>

    <th class="p-4 text-center">
        Status
    </th>

    <th class="p-4 text-center">
        Assigned By
    </th>

</tr>

</thead>

<tbody>

<?php while($row = $res->fetch_assoc()): ?>

<tr class="border-b hover:bg-gray-50">

    <td class="p-4 font-medium">

        <?= htmlspecialchars($row['asset_name']) ?>

    </td>

    <td class="p-4">

        <?= htmlspecialchars($row['asset_type']) ?>

    </td>

    <td class="p-4">

        <?= htmlspecialchars($row['serial_number']) ?>

    </td>

    <td class="p-4">

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

    </td>

    <td class="p-4">

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

    </td>

    <td class="p-4 text-center">

        <?= !empty($row['assigned_date'])
            ? date(
                'd M Y',
                strtotime($row['assigned_date'])
            )
            : '-'
        ?>

    </td>

    <td class="p-4 text-center">

        <?= !empty($row['returned_date'])
            ? date(
                'd M Y',
                strtotime($row['returned_date'])
            )
            : '-'
        ?>

    </td>

    <td class="p-4 text-center">

        <?php

        $badge =
        'bg-yellow-100 text-yellow-700';

        if($row['status']=='Assigned'){
            $badge =
            'bg-blue-100 text-blue-700';
        }

        if($row['status']=='Returned'){
            $badge =
            'bg-green-100 text-green-700';
        }

        ?>

        <span class="<?= $badge ?> px-3 py-1 rounded-full text-sm">

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

        </span>

    </td>

    <td class="p-4 text-center">

        <?= htmlspecialchars(
            $row['assigned_by_user'] ?? 'System'
        ) ?>

    </td>

</tr>

<?php endwhile; ?>

</tbody>

</table>

</div>

</div>

</div>

</body>
</html>