<?= $this->extend('layout/management_layout') ?>
<?= $this->section('content') ?>
<?php $isSchoolYearReadonly = (bool) ($isSchoolYearReadonly ?? false); ?>

<div class="container-fluid py-5">
    <div class="row">
        <div class="col-md-12">
            <?php if (session()->getFlashdata('success')): ?>
                <div class="alert alert-success alert-dismissible fade show" role="alert">
                    <?= session()->getFlashdata('success') ?>
                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                </div>
            <?php endif; ?>
            <?php if ($isSchoolYearReadonly): ?>
                <div class="alert alert-warning" role="alert">
                    This school year is read-only. Print request statuses cannot be changed.
                </div>
            <?php endif; ?>
            <?php if (session()->getFlashdata('error')): ?>
                <div class="alert alert-danger alert-dismissible fade show" role="alert">
                    <?= session()->getFlashdata('error') ?>
                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                </div>
            <?php endif; ?>

            <!-- Print Request History -->
            <div class="card my-4">
                <div class="card-header">
                    <h5 class="mb-0">Print Requests - Admin</h5>
                </div>
                <div class="card-body">
                    <table class="table table-bordered table-striped no-mgmt-sticky">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>Submitted By</th>
                                <th>Class</th>
                                <th>File</th>
                                <th>Pages</th>
                                <th>Copies</th>
                                <th>Required By</th>
                                <th>Pickup Method</th>
                                <th>Status</th>
                                <th>Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if (!empty($print_requests)): ?>
                                <?php foreach ($print_requests as $request): ?>
                                    <tr>
                                        <td><?= $request['id'] ?></td>
                                        <td><?= $request['firstname'] . ' ' . $request['lastname'] ?></td>
                                        <td><?= $request['class_section_name'] ?></td>
                                        <td>
                                            <?php if (!empty($request['file_path'])): ?>
                                                <a href="<?= site_url('print-requests/file/' . rawurlencode($request['file_path'])) ?>" target="_blank"><?= esc($request['file_path']) ?></a>
                                            <?php else: ?>
                                                <span class="text-muted">Physical copy request</span>
                                            <?php endif; ?>
                                        </td>
                                        <td>
                                            <?php if (!empty($request['page_selection'])): ?>
                                                <?= esc($request['page_selection']) ?>
                                            <?php else: ?>
                                                <span class="text-muted">ALL</span>
                                            <?php endif; ?>
                                        </td>
                                        <td><?= $request['num_copies'] ?></td>
                                        <td><?= $request['required_by'] ?></td>
                                        <td><?= $request['pickup_method'] ?></td>
                                        <td>
                                            <?php
                                                $status = $request['status'];
                                                $status_text = ucfirst(str_replace('_', ' ', $status));
                                                $badge_class = 'bg-light text-dark'; // default
                                                switch ($status) {
                                                    case 'not_assigned':
                                                        $badge_class = 'bg-secondary';
                                                        break;
                                                    case 'assigned':
                                                        $badge_class = 'bg-warning';
                                                        if (!empty($request['admin_firstname'])) {
                                                            $status_text = 'Assigned to ' . $request['admin_firstname'];
                                                        }
                                                        break;
                                                    case 'done':
                                                        $badge_class = 'bg-primary';
                                                        break;
                                                    case 'delivered':
                                                        $badge_class = 'bg-success';
                                                        break;
                                                }
                                            ?>
                                            <span class="badge <?= $badge_class ?>">
                                                <?= $status_text ?>
                                            </span>
                                        </td>
                                        <td>
                                            <?php if (!$isSchoolYearReadonly && $request['status'] != 'delivered'): ?>
                                                <form action="<?= site_url('print-requests/update/' . $request['id']) ?>" method="post" class="d-flex">
                                                    <?= csrf_field() ?>
                                                    <input type="hidden" name="admin_id" value="<?= session()->get('user_id') ?>">
                                                    <select name="status" class="form-control form-control-sm me-2">
                                                        <?php
                                                            $current_status = $request['status'];
                                                            if ($current_status == 'not_assigned') {
                                                                echo '<option value="not_assigned" selected>Not Assigned</option>';
                                                                echo '<option value="assigned">Assigned</option>';
                                                            } elseif ($current_status == 'assigned') {
                                                                echo '<option value="assigned" selected>Assigned</option>';
                                                                echo '<option value="not_assigned">Not Assigned</option>';
                                                                echo '<option value="done">Done</option>';
                                                            } elseif ($current_status == 'done') {
                                                                echo '<option value="done" selected>Done</option>';
                                                                echo '<option value="delivered">Delivered</option>';
                                                            }
                                                        ?>
                                                    </select>
                                                    <button type="submit" class="btn btn-primary btn-sm">Update</button>
                                                </form>
                                            <?php else: ?>
                                                <span>-</span>
                                            <?php endif; ?>
                                        </td>
                                    </tr>
                                <?php endforeach; ?>
                            <?php else: ?>
                                <tr>
                                    <td colspan="10" class="text-center">No print requests found.</td>
                                </tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<?= $this->endSection() ?>
