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

/*
====================================
RETURN ASSET
====================================
*/

if(isset($_GET['return'])){

    $assignmentId = intval($_GET['return']);

    /*
    GET ASSET ID
    */

    $asset = $conn->query("
    SELECT asset_id
    FROM asset_assignments
    WHERE id=$assignmentId
    ");

    if($asset->num_rows){

        $row = $asset->fetch_assoc();

        $assetId = intval($row['asset_id']);

        /*
        UPDATE ASSIGNMENT
        */

        $conn->query("
        UPDATE asset_assignments
        SET
            status='Returned',
            returned_date=CURDATE()
        WHERE id=$assignmentId
        ");

        /*
        UPDATE ASSET
        */

        $conn->query("
        UPDATE assets_master
        SET asset_status='Available'
        WHERE id=$assetId
        ");
    }

    header("Location: asset_return.php?success=1");
    exit;
}

/*
====================================
LOAD ASSIGNED ASSETS
====================================
*/

$res = $conn->query("
SELECT

aa.id assignment_id,
aa.assigned_date,

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

e.name employee_name,
d.name department

FROM asset_assignments aa

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

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

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

WHERE aa.status='Assigned'

ORDER BY aa.assigned_date DESC
");
?>

<!DOCTYPE html>
<html>
<head>

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

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

    <div>

        <h1 class="text-3xl font-bold">
            Asset Returns
        </h1>

        <p class="text-gray-500 mt-1">
            Return assets assigned to employees
        </p>

    </div>

</div>

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

<div class="bg-green-100 border border-green-300 text-green-700 p-4 rounded-xl mb-6">

    Asset returned successfully.

</div>

<?php endif; ?>

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

<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">
Type
</th>

<th class="p-4 text-left">
Serial Number
</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">
Action
</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">

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

</td>

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

<a
href="?return=<?= $row['assignment_id'] ?>"
onclick="return confirm('Return this asset?')"
class="bg-green-600 hover:bg-green-700 text-white px-4 py-2 rounded-lg">

Return Asset

</a>

</td>

</tr>

<?php endwhile; ?>

</tbody>

</table>

</div>

</div>

</div>

</body>
</html>