<?php $this->layout('layouts/main', ['title' => 'Report Templates']) ?>

<div class="container mx-auto px-4 py-8">
    <div class="flex justify-between items-center mb-6">
        <h1 class="text-2xl font-bold text-gray-800">Report Templates</h1>
        <a href="<?= APP_URL ?>/reports/templates/create" class="bg-blue-600 hover:bg-blue-700 text-white font-semibold py-2 px-4 rounded shadow">
            Create Template
        </a>
    </div>

    <div class="bg-white shadow rounded-lg overflow-hidden">
        <div class="overflow-x-auto">
        <table class="min-w-full leading-normal">
            <thead>
                <tr>
                    <th class="px-5 py-3 border-b-2 border-gray-200 bg-gray-100 text-left text-xs font-semibold text-gray-600 uppercase tracking-wider">
                        Template Name
                    </th>
                    <?php if (!empty($isPlatformView)): ?>
                    <th class="px-5 py-3 border-b-2 border-gray-200 bg-gray-100 text-left text-xs font-semibold text-gray-600 uppercase tracking-wider">
                        Organization
                    </th>
                    <?php endif; ?>
                    <th class="px-5 py-3 border-b-2 border-gray-200 bg-gray-100 text-left text-xs font-semibold text-gray-600 uppercase tracking-wider">
                        Sections
                    </th>
                    <th class="px-5 py-3 border-b-2 border-gray-200 bg-gray-100 text-left text-xs font-semibold text-gray-600 uppercase tracking-wider">
                        Workload Items
                    </th>
                    <th class="px-5 py-3 border-b-2 border-gray-200 bg-gray-100 text-left text-xs font-semibold text-gray-600 uppercase tracking-wider">
                        Linked Projects
                    </th>
                    <th class="px-5 py-3 border-b-2 border-gray-200 bg-gray-100 text-left text-xs font-semibold text-gray-600 uppercase tracking-wider">
                        Actions
                    </th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($templates as $t): ?>
                <tr>
                    <td class="px-5 py-5 border-b border-gray-200 bg-white text-sm">
                        <p class="text-gray-900 whitespace-no-wrap font-medium"><?= htmlspecialchars($t['name']) ?></p>
                        <p class="text-gray-500 text-xs"><?= htmlspecialchars(substr($t['description'] ?? '', 0, 50)) ?></p>
                    </td>
                    <?php if (!empty($isPlatformView)): ?>
                    <td class="px-5 py-5 border-b border-gray-200 bg-white text-sm"><?= htmlspecialchars($t['org_name'] ?? '-') ?></td>
                    <?php endif; ?>
                    <td class="px-5 py-5 border-b border-gray-200 bg-white text-sm">
                        <p class="text-gray-900 whitespace-no-wrap"><?= $t['sections_count'] ?></p>
                    </td>
                    <td class="px-5 py-5 border-b border-gray-200 bg-white text-sm">
                        <p class="text-gray-900 whitespace-no-wrap"><?= $t['workload_items_count'] ?></p>
                    </td>
                    <td class="px-5 py-5 border-b border-gray-200 bg-white text-sm">
                        <p class="text-gray-900 whitespace-no-wrap"><?= $t['linked_projects_count'] ?></p>
                    </td>
                    <td class="px-5 py-5 border-b border-gray-200 bg-white text-sm">
                        <a href="<?= APP_URL ?>/reports/templates/<?= $t['id'] ?>/edit" class="text-blue-600 hover:text-blue-900 mr-3">Edit</a>
                        <form action="<?= APP_URL ?>/reports/templates/<?= $t['id'] ?>/delete" method="POST" class="inline" onsubmit="return confirm('Are you sure?');">
                            <input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?>">
                            <button type="submit" class="text-red-600 hover:text-red-900">Delete</button>
                        </form>
                    </td>
                </tr>
                <?php endforeach; ?>
                <?php if (empty($templates)): ?>
                <tr>
                    <td colspan="<?= !empty($isPlatformView) ? 6 : 5 ?>" class="px-5 py-5 border-b border-gray-200 bg-white text-sm text-center text-gray-500">
                        No templates found.
                    </td>
                </tr>
                <?php endif; ?>
            </tbody>
        </table>
        </div>
    </div>
</div>

