<?php
// data_user/my_permissions.php
// This page shows the user a list of all their data access requests and their statuses.

error_reporting(E_ALL);
ini_set('display_errors', 1);

session_start();
require_once '../config.php';
// We'll assume these files exist based on your original code
require_once '../includes/auth.php';
require_once '../classes/Permission.php';
require_once '../classes/User.php';

// Redirect if not logged in
if (!isset($_SESSION['user_id'])) {
    header('Location: ../index.php?page=login');
    exit;
}

$user_id = $_SESSION['user_id'];
$person_id = $_SESSION['person_id'];
$username = $_SESSION['username'];

$currentPage = basename($_SERVER['PHP_SELF']);

// Instantiate classes
$permissionManager = new Permission($pdo);
$userManager = new User($pdo);

// Get user details
$currentUserDetails = $userManager->getUserDetails($user_id);

// Fetch all permission requests for the logged-in user
$permissionRequests = [];
try {
    $permissionRequests = $permissionManager->getPermissionsByPersonId($person_id);
} catch (Exception $e) {
    set_message('Error retrieving permission requests: ' . $e->getMessage(), 'danger');
}
?>
<!DOCTYPE html>
<html lang="en">
<!-- Header -->
<?php
// Include header file for admin pages
include_once("../includes/header_user.php");
?>
<body>
    <div class="wrapper">
        <!-- Sidebar -->
        <?php
        // Include header file for admin pages
        include_once("../includes/nav_user.php");
        ?>
        <!-- Page Content -->
        <div class="main-content">
            <nav class="navbar navbar-expand-lg navbar-light bg-light mb-4 rounded-3">
                <div class="container-fluid">
                    <a class="navbar-brand" href="#"> My Permissions</a>
                    <div class="d-flex">
                        <span class="navbar-text me-3">
                            Welcome, <?php echo htmlspecialchars($_SESSION['username']); ?>!
                        </span>
                    </div>
                </div>
            </nav>         

            <?php if (isset($_SESSION['message'])): ?>
                <div class="alert alert-<?= $_SESSION['message_type'] ?> alert-dismissible fade show rounded" role="alert">
                    <?= $_SESSION['message'] ?>
                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                </div>
                <?php
                unset($_SESSION['message']);
                unset($_SESSION['message_type']);
                ?>
            <?php endif; ?>

            <div class="card shadow-sm rounded-lg">
                <div class="card-body p-4">
                    <?php if (!empty($permissionRequests)): ?>
                        <div class="table-responsive">
                            <table class="table table-hover table-borderless align-middle">
                                <thead class="table-light">
                                    <tr>
                                        <th scope="col">Data Source</th>
                                        <th scope="col">Requested For</th>
                                        <th scope="col">Date Submitted</th>
                                        <th scope="col">Proof</th>
                                        <th scope="col">Status</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php foreach ($permissionRequests as $request): ?>
                                    <tr>
                                        <td class="fw-bold"><?= htmlspecialchars($request['ds_title']) ?></td>
                                        <td><?= htmlspecialchars($request['dspspr_permission_type']) ?></td>
                                        <td><?= htmlspecialchars(date('M d, Y', strtotime($request['dspspr_request_date']))) ?></td>
                                        <td>
                                            <?php if (!empty($request['dspspr_proof_path'])): ?>
                                                <a href="../uploads/<?= htmlspecialchars($request['dspspr_proof_path']) ?>" class="btn btn-sm btn-outline-primary rounded-pill" target="_blank" rel="noopener">
                                                    <i class="fas fa-file-pdf me-1"></i> View
                                                </a>
                                            <?php else: ?>
                                                <span class="text-muted">N/A</span>
                                            <?php endif; ?>
                                        </td>
                                        <td>
                                            <?php
                                            $status_class = '';
                                            switch ($request['dspspr_status']) {
                                                case 'Approved':
                                                    $status_class = 'bg-success badge text-white';
                                                    break;
                                                case 'Pending':
                                                    $status_class = 'bg-warning badge text-dark';
                                                    break;
                                                case 'Denied':
                                                    $status_class = 'bg-danger badge text-white';
                                                    break;
                                                default:
                                                    $status_class = 'bg-secondary badge text-white';
                                                    break;
                                            }
                                            ?>
                                            <span class="status-badge <?= $status_class ?>"><?= htmlspecialchars($request['dspspr_status']) ?></span>
                                        </td>
                                    </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                    <?php else: ?>
                        <div class="alert alert-info text-center rounded m-0">
                            You have not submitted any permission requests yet. <a href="browse_datasources.php" class="alert-link">Browse data sources</a> to get started.
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>

    <!-- Footer -->
    <?php
    // Include Footer file for owner pages
    include_once("../includes/footer_user.php");
    ?>  
</body>
</html>
