<?php
include 'includes/auth.php';
include 'db.php';
include 'includes/header.php';

// Handle Approve / Reject actions
if (isset($_GET['action'], $_GET['id'])) {
    $id = intval($_GET['id']);
    if ($_GET['action'] === 'approve') {
        $stmt = $conn->prepare("UPDATE maintenance_requests SET status='Approved' WHERE id=?");
        $stmt->bind_param("i", $id);
        $stmt->execute();
        $stmt->close();
        echo "<script>alert('Request approved successfully'); window.location='maintenance_requests_approval.php';</script>";
        exit;
    } elseif ($_GET['action'] === 'reject') {
        $stmt = $conn->prepare("UPDATE maintenance_requests SET status='Rejected' WHERE id=?");
        $stmt->bind_param("i", $id);
        $stmt->execute();
        $stmt->close();
        echo "<script>alert('Request rejected'); window.location='maintenance_requests_approval.php';</script>";
        exit;
    }
}

// Fetch all pending maintenance requests
$requests_result = $conn->query("
    SELECT mr.id, r.room_number, mr.issue_description, mr.created_at
    FROM maintenance_requests mr
    JOIN rooms r ON mr.room_id = r.id
    WHERE mr.status='Pending'
    ORDER BY mr.created_at ASC
");
?>

<style>
.table-container {
    margin-top: 30px;
}
.table th, .table td {
    vertical-align: middle;
}
.action-btn {
    margin-right: 5px;
}
</style>

<div class="container mt-4">
     <button class="btn btn-secondary" onclick="history.back()">
            <i class="fa fa-arrow-left"></i> Back
        </button>
    <h3><i class="fa fa-screwdriver-wrench"></i>Pending Maintenance Requests</h3>
    <hr>
    <?php if ($requests_result->num_rows > 0): ?>
        <div class="table-responsive table-container">
            <table class="table table-bordered table-hover">
                <thead class="table-dark">
                    <tr>
                        <th>#</th>
                        <th>Room</th>
                        <th>Issue Description</th>
                        <th>Requested At</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php $i=1; while($row = $requests_result->fetch_assoc()): ?>
                        <tr>
                            <td><?= $i++ ?></td>
                            <td><?= htmlspecialchars($row['room_number']) ?></td>
                            <td><?= htmlspecialchars($row['issue_description']) ?></td>
                            <td><?= date("d-m-Y H:i", strtotime($row['created_at'])) ?></td>
                            <td>
                                <a href="?action=approve&id=<?= $row['id'] ?>" class="btn btn-sm btn-success action-btn">
                                    <i class="fa fa-check"></i> Approve
                                </a>
                                <a href="?action=reject&id=<?= $row['id'] ?>" class="btn btn-sm btn-danger action-btn">
                                    <i class="fa fa-times"></i> Reject
                                </a>
                            </td>
                        </tr>
                    <?php endwhile; ?>
                </tbody>
            </table>
        </div>
    <?php else: ?>
        <div class="alert alert-info">No pending maintenance requests.</div>
    <?php endif; ?>
</div>

<?php include 'includes/footer.php'; ?>