<?php

require 'config.php';
session_start();
session_write_close();

$totalAssets = $conn->query("
SELECT COUNT(*) total
FROM assets
")->fetch_assoc()['total'];

$assignedAssets = $conn->query("
SELECT COUNT(*) total
FROM assets
WHERE status='Assigned'
")->fetch_assoc()['total'];

$availableAssets = $conn->query("
SELECT COUNT(*) total
FROM assets
WHERE status='Available'
")->fetch_assoc()['total'];

$returnedAssets = $conn->query("
SELECT COUNT(*) total
FROM assets
WHERE status='Returned'
")->fetch_assoc()['total'];

$res = $conn->query("
SELECT *
FROM assets
ORDER BY id DESC
LIMIT 10
");
?>

<!DOCTYPE html>
<html>
<head>

<title>Asset Dashboard</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-8">
Asset Management Dashboard
</h1>

<div class="grid md:grid-cols-4 gap-6 mb-8">

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

<p class="text-gray-500">
Total Assets
</p>

<h2 class="text-4xl font-bold text-blue-700 mt-2">
<?= number_format($totalAssets) ?>
</h2>

</div>

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

<p class="text-gray-500">
Assigned
</p>

<h2 class="text-4xl font-bold text-green-700 mt-2">
<?= number_format($assignedAssets) ?>
</h2>

</div>

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

<p class="text-gray-500">
Available
</p>

<h2 class="text-4xl font-bold text-indigo-700 mt-2">
<?= number_format($availableAssets) ?>
</h2>

</div>

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

<p class="text-gray-500">
Returned
</p>

<h2 class="text-4xl font-bold text-orange-700 mt-2">
<?= number_format($returnedAssets) ?>
</h2>

</div>

</div>

<div class="flex gap-3 mb-6">

<a
href="add_asset.php"
class="bg-blue-600 text-white px-5 py-3 rounded-xl">

Add Asset

</a>

<a
href="assign_asset.php"
class="bg-green-600 text-white px-5 py-3 rounded-xl">

Assign Asset

</a>

<a
href="asset_history.php"
class="bg-purple-600 text-white px-5 py-3 rounded-xl">

Asset History

</a>

</div>

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

<div class="p-5 border-b">

<h2 class="text-xl font-semibold">
Latest Assets
</h2>

</div>

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

<table class="w-full">

<thead>

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

<th class="p-4 text-left">Asset</th>
<th class="p-4 text-left">Tag</th>
<th class="p-4 text-left">Category</th>
<th class="p-4 text-left">Serial</th>
<th class="p-4 text-left">Status</th>

</tr>

</thead>

<tbody>

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

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

<td class="p-4">
<?= htmlspecialchars($row['asset_name']) ?>
</td>

<td class="p-4">
<?= htmlspecialchars($row['asset_tag']) ?>
</td>

<td class="p-4">
<?= htmlspecialchars($row['category']) ?>
</td>

<td class="p-4">
<?= htmlspecialchars($row['serial_number']) ?>
</td>

<td class="p-4">

<?php

$statusClass = 'bg-gray-100 text-gray-700';

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

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

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

?>

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

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

</span>

</td>

</tr>

<?php endwhile; ?>

</tbody>

</table>

</div>

</div>

</div>

</body>
</html>