<?php

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

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

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

    $conn->query("
    UPDATE salary_advances
    SET hr_status='Approved'
    WHERE id=$id
    ");

    header("Location: salary_advance_hr.php");
    exit;
}

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

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

    $conn->query("
    UPDATE salary_advances
    SET hr_status='Rejected'
    WHERE id=$id
    ");

    header("Location: salary_advance_hr.php");
    exit;
}

$res=$conn->query("

SELECT
s.*,
e.name

FROM salary_advances s

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

WHERE manager_status='Approved'
AND hr_status='Pending'

ORDER BY s.id DESC

");
?>

<!DOCTYPE html>
<html>
<head>

<title>HR Salary Advance Approval</title>

<script src="https://cdn.tailwindcss.com"></script>

</head>

<body class="flex bg-gray-100">

<?php include '../sidebar.php'; ?>

<div class="flex-1 p-8" id="main-content">

<h1 class="text-3xl font-bold mb-6">
HR Salary Advance Approval
</h1>

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

<table class="w-full">

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

<tr>

<th class="p-4 text-left">Employee</th>
<th class="p-4">Amount</th>
<th class="p-4">Reason</th>
<th class="p-4">Date</th>
<th class="p-4">Action</th>

</tr>

</thead>

<tbody>

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

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

<td class="p-4">

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

</td>

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

₦<?= number_format($row['amount'],2) ?>

</td>

<td class="p-4">

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

</td>

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

<?= $row['request_date'] ?>

</td>

<td class="p-4">

<div class="flex gap-2 justify-center">

<a
href="?approve=<?= $row['id'] ?>"
class="bg-green-600 text-white px-4 py-2 rounded-lg">

Approve

</a>

<a
href="?reject=<?= $row['id'] ?>"
class="bg-red-600 text-white px-4 py-2 rounded-lg">

Reject

</a>

</div>

</td>

</tr>

<?php endwhile; ?>

</tbody>

</table>

</div>

</div>

</body>
</html>