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

<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">Daily Reports<?= !empty($project) ? ' — ' . htmlspecialchars($project['name']) : '' ?></h1>
        <?php if (!empty($projectId)): ?>
        <div>
            <a href="<?= APP_URL ?>/reports/daily/progress?project_id=<?= $projectId ?>" class="bg-indigo-600 hover:bg-indigo-700 text-white font-semibold py-2 px-4 rounded shadow mr-2">
                Dashboard
            </a>
            <a href="<?= APP_URL ?>/reports/daily/create?project_id=<?= $projectId ?>" class="bg-blue-600 hover:bg-blue-700 text-white font-semibold py-2 px-4 rounded shadow">
                Submit Report
            </a>
        </div>
        <?php endif; ?>
    </div>

    <?php if (empty($projectId)): ?>
    <div class="bg-white shadow rounded-lg p-6 text-gray-500">No projects available for daily reports.</div>
    <?php else: ?>

    <!-- Project switcher -->
    <?php if (count($projects) > 1): ?>
    <form method="GET" class="mb-4">
        <select name="project_id" onchange="this.form.submit()" class="border rounded p-2">
            <?php foreach ($projects as $p): ?>
            <option value="<?= $p['id'] ?>" <?= (int)$p['id'] === (int)$projectId ? 'selected' : '' ?>><?= htmlspecialchars($p['name']) ?></option>
            <?php endforeach; ?>
        </select>
    </form>
    <?php endif; ?>

    <!-- Filters -->
    <form method="GET" class="mb-6 bg-white p-4 rounded shadow flex gap-4 items-end">
        <input type="hidden" name="project_id" value="<?= $projectId ?>">
        <div>
            <label class="block text-xs text-gray-600 mb-1">From Date</label>
            <input type="date" name="date_from" value="<?= htmlspecialchars($filters['date_from'] ?? '') ?>" class="border rounded p-2">
        </div>
        <div>
            <label class="block text-xs text-gray-600 mb-1">To Date</label>
            <input type="date" name="date_to" value="<?= htmlspecialchars($filters['date_to'] ?? '') ?>" class="border rounded p-2">
        </div>
        <div>
            <label class="block text-xs text-gray-600 mb-1">Status</label>
            <select name="status" class="border rounded p-2">
                <option value="">All</option>
                <option value="draft" <?= ($filters['status']??'')=='draft'?'selected':'' ?>>Draft</option>
                <option value="submitted" <?= ($filters['status']??'')=='submitted'?'selected':'' ?>>Submitted</option>
                <option value="reviewed" <?= ($filters['status']??'')=='reviewed'?'selected':'' ?>>Reviewed</option>
                <option value="approved" <?= ($filters['status']??'')=='approved'?'selected':'' ?>>Approved</option>
            </select>
        </div>
        <button type="submit" class="bg-gray-200 hover:bg-gray-300 px-4 py-2 rounded">Filter</button>
    </form>

    <div class="bg-white shadow rounded-lg overflow-hidden">
        <table class="min-w-full leading-normal">
            <thead>
                <tr>
                    <th class="px-5 py-3 border-b border-gray-200 bg-gray-100 text-left text-xs font-semibold text-gray-600 uppercase">Date</th>
                    <th class="px-5 py-3 border-b border-gray-200 bg-gray-100 text-left text-xs font-semibold text-gray-600 uppercase">Contractor</th>
                    <th class="px-5 py-3 border-b border-gray-200 bg-gray-100 text-left text-xs font-semibold text-gray-600 uppercase">Sections Filled</th>
                    <th class="px-5 py-3 border-b border-gray-200 bg-gray-100 text-left text-xs font-semibold text-gray-600 uppercase">Photos</th>
                    <th class="px-5 py-3 border-b border-gray-200 bg-gray-100 text-left text-xs font-semibold text-gray-600 uppercase">Status</th>
                    <th class="px-5 py-3 border-b border-gray-200 bg-gray-100 text-left text-xs font-semibold text-gray-600 uppercase">Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($reports as $r): ?>
                <tr>
                    <td class="px-5 py-5 border-b border-gray-200 bg-white text-sm"><?= htmlspecialchars($r['date']) ?></td>
                    <td class="px-5 py-5 border-b border-gray-200 bg-white text-sm"><?= htmlspecialchars($r['contractor_name']) ?></td>
                    <td class="px-5 py-5 border-b border-gray-200 bg-white text-sm"><?= $r['sections_filled'] ?></td>
                    <td class="px-5 py-5 border-b border-gray-200 bg-white text-sm"><?= $r['photos_count'] ?></td>
                    <td class="px-5 py-5 border-b border-gray-200 bg-white text-sm">
                        <span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-<?= $r['status'] == 'approved' ? 'green' : ($r['status'] == 'submitted' ? 'blue' : 'gray') ?>-100 text-<?= $r['status'] == 'approved' ? 'green' : ($r['status'] == 'submitted' ? 'blue' : 'gray') ?>-800">
                            <?= ucfirst($r['status']) ?>
                        </span>
                    </td>
                    <td class="px-5 py-5 border-b border-gray-200 bg-white text-sm">
                        <a href="<?= APP_URL ?>/reports/daily/<?= $r['id'] ?>" class="text-blue-600 hover:text-blue-900 mr-2">View</a>
                    </td>
                </tr>
                <?php endforeach; ?>
                <?php if (empty($reports)): ?>
                <tr>
                    <td colspan="6" class="px-5 py-5 text-center text-gray-500">No daily reports found.</td>
                </tr>
                <?php endif; ?>
            </tbody>
        </table>
        <div class="p-4 border-t border-gray-200">
            <?php @include ROOT_PATH . '/modules/shared/views/partials/pagination.php'; ?>
        </div>
    </div>
    <?php endif; ?>
</div>

