<?php

declare(strict_types=1);

date_default_timezone_set('Asia/Colombo');

require_once __DIR__ . '/../includes/auth.php';
require_once __DIR__ . '/../config/app.php';
require_once __DIR__ . '/../config/db.php';
require_once __DIR__ . '/../includes/role_filter.php';
require_once __DIR__ . '/../includes/report_helper.php';

if (!isset($_SESSION['role']) || !in_array($_SESSION['role'], ['admin', 'zonal_director'], true)) {
    http_response_code(403);
    exit('Access denied.');
}

$dataset = buildReportDataset($conn, $_GET);
$fields = $dataset['fields'];
$equipment = $dataset['equipment'];
$surveys = $dataset['surveys'];
$equipmentTotals = $dataset['equipment_totals'];
$columnCount = count($fields) + (count($equipment) * 3);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Full Survey Report</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.2/css/all.min.css" rel="stylesheet">
<link href="<?= htmlspecialchars(BASE_URL . 'assets/css/style.css?v=' . urlencode(SYSTEM_VERSION), ENT_QUOTES, 'UTF-8') ?>" rel="stylesheet">
</head>
<body class="full-report-page">
<div class="container-fluid py-3 no-print">
<div class="d-flex justify-content-between">
<a href="<?= htmlspecialchars(BASE_URL . 'admin/export_options.php', ENT_QUOTES, 'UTF-8') ?>" class="btn btn-secondary"><i class="fa-solid fa-arrow-left me-1"></i>Back</a>
<button type="button" class="btn btn-primary" onclick="window.print()"><i class="fa-solid fa-print me-1"></i>Print</button>
</div>
</div>
<main class="container-fluid pb-4">
<h2 class="text-center mb-3">Sabaragamuwa Province School Computer Hardware Inventory Report</h2>
<div class="table-responsive">
<table class="table table-bordered table-sm align-middle report-data-table">
<thead class="table-light"><tr>
<?php foreach ($fields as $field): ?>
<th><?= htmlspecialchars(ucwords(str_replace('_', ' ', $field)), ENT_QUOTES, 'UTF-8') ?></th>
<?php endforeach; ?>
<?php foreach ($equipment as $equipmentName): ?>
<th><?= htmlspecialchars($equipmentName, ENT_QUOTES, 'UTF-8') ?> Working</th>
<th><?= htmlspecialchars($equipmentName, ENT_QUOTES, 'UTF-8') ?> Not Working</th>
<th><?= htmlspecialchars($equipmentName, ENT_QUOTES, 'UTF-8') ?> Repair</th>
<?php endforeach; ?>
</tr></thead>
<tbody>
<?php if ($surveys === []): ?>
<tr><td colspan="<?= max(1, $columnCount) ?>" class="text-center">No matching survey records found.</td></tr>
<?php else: ?>
<?php foreach ($surveys as $survey): ?>
<tr>
<?php foreach ($fields as $field): ?>
<td><?= htmlspecialchars((string)($survey[$field] ?? ''), ENT_QUOTES, 'UTF-8') ?></td>
<?php endforeach; ?>
<?php $surveyId = (int)$survey['id']; ?>
<?php foreach ($equipment as $equipmentId => $equipmentName): ?>
<?php $totals = $equipmentTotals[$surveyId][$equipmentId] ?? ['working'=>0,'not_working'=>0,'repair'=>0]; ?>
<td><?= $totals['working'] ?></td>
<td><?= $totals['not_working'] ?></td>
<td><?= $totals['repair'] ?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
</main>
</body>
</html>
