<?php
session_start();
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'employee') {
    header("Location: login.php"); exit();
}
include 'config.php';
$employee_id = $_SESSION['user_id'];

$sql = "SELECT * FROM salary_advance_requests WHERE employee_id = ? ORDER BY created_at DESC";
$stmt = $conn->prepare($sql);
$stmt->bind_param("s", $employee_id);
$stmt->execute();
$result = $stmt->get_result();
?>
<!DOCTYPE html>
<html><head><title>Salary Advance History</title></head><body>
<h2>Salary Advance Requests</h2>
<table border="1"><tr><th>ID</th><th>Amount</th><th>Installments</th><th>Reason</th><th>Status</th><th>Date</th></tr>
<?php while($r=$result->fetch_assoc()): ?>
<tr>
    <td><?=$r['id']?></td>
    <td>₹<?=number_format($r['amount'],2)?></td>
    <td><?=$r['installments']?></td>
    <td><?=htmlspecialchars($r['reason'])?></td>
    <td><?=$r['status']?></td>
    <td><?=date("d M Y", strtotime($r['created_at']))?></td>
</tr>
<?php endwhile; ?>
</table>
</body></html>