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

<div class="container mx-auto px-4 py-8 max-w-5xl">
    <div class="flex justify-between items-center mb-6">
        <h1 class="text-2xl font-bold text-gray-800">Daily Report - <?= htmlspecialchars($report['date']) ?></h1>
        <span class="px-3 py-1 rounded-full text-sm font-semibold uppercase bg-<?= $report['status'] == 'approved' ? 'green' : ($report['status'] == 'submitted' ? 'blue' : 'gray') ?>-100 text-<?= $report['status'] == 'approved' ? 'green' : ($report['status'] == 'submitted' ? 'blue' : 'gray') ?>-800">
            <?= htmlspecialchars($report['status']) ?>
        </span>
    </div>

    <div class="bg-white shadow rounded-lg p-6 mb-6">
        <div class="grid grid-cols-3 gap-4 text-sm text-gray-600">
            <p><strong>Contractor:</strong> <?= htmlspecialchars($report['contractor_name']) ?></p>
            <p><strong>Weather:</strong> <?= htmlspecialchars($report['weather']) ?></p>
            <p><strong>Manpower:</strong> <?= htmlspecialchars($report['manpower']) ?></p>
        </div>
        <?php if (!empty($report['site_notes'])): ?>
            <div class="mt-4 pt-4 border-t">
                <p><strong>Site Notes:</strong><br><?= nl2br(htmlspecialchars($report['site_notes'])) ?></p>
            </div>
        <?php endif; ?>
    </div>

    <h2 class="text-xl font-semibold mb-4">Workload Progress</h2>
    <div class="bg-white shadow rounded-lg overflow-x-auto mb-6 p-4">
        <table class="w-full text-left border-collapse">
            <thead>
                <tr>
                    <th class="border-b p-2">Activity</th>
                    <th class="border-b p-2">Project Scope</th>
                    <th class="border-b p-2">Planned Qty</th>
                    <th class="border-b p-2">Achieved Today</th>
                    <th class="border-b p-2">Cumulative</th>
                    <th class="border-b p-2">Notes</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($workloadEntries as $wl): ?>
                <tr>
                    <td class="border-b p-2"><?= htmlspecialchars(($wl['activity_name'] ?? '') . (!empty($wl['unit']) ? ' (' . $wl['unit'] . ')' : '')) ?></td>
                    <td class="border-b p-2"><?= htmlspecialchars($wl['project_scope'] ?? '') ?></td>
                    <td class="border-b p-2"><?= htmlspecialchars($wl['planned_qty']) ?></td>
                    <td class="border-b p-2 font-bold"><?= htmlspecialchars($wl['achieved_qty']) ?></td>
                    <td class="border-b p-2"><?= htmlspecialchars($wl['cumulative']) ?></td>
                    <td class="border-b p-2"><?= htmlspecialchars($wl['notes']) ?></td>
                </tr>
                <?php endforeach; ?>
                <?php if (empty($workloadEntries)): ?>
                    <tr><td colspan="6" class="p-4 text-center text-gray-500">No workload items recorded.</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>

    <h2 class="text-xl font-semibold mb-4">Sections Details</h2>
    <div class="bg-white shadow rounded-lg p-6 mb-6 space-y-4">
        <?php foreach ($sectionEntries as $entry): ?>
            <div class="border-b pb-4">
                <p class="font-bold text-gray-700"><?= htmlspecialchars($entry['title'] ?? ('Section ' . $entry['section_id'])) ?></p>
                <p class="mt-1 text-gray-600"><?= nl2br(htmlspecialchars($entry['content'] ?? '')) ?></p>
            </div>
        <?php endforeach; ?>
        <?php if (empty($sectionEntries)): ?>
            <p class="text-gray-500">No section details provided.</p>
        <?php endif; ?>
    </div>

    <h2 class="text-xl font-semibold mb-4">Attachments & Photos</h2>
    <?php if (empty($attachments)): ?>
    <div class="bg-white shadow rounded-lg p-6 mb-6">
        <p class="text-gray-500">No photos or files attached.</p>
    </div>
    <?php else: ?>
        <?php
            $groupedAttachments = [];
            foreach ($attachments as $att) {
                $groupedAttachments[$att['section_title'] ?? 'General'][] = $att;
            }
        ?>
        <?php foreach ($groupedAttachments as $sectionLabel => $items): ?>
        <div class="bg-white shadow rounded-lg p-6 mb-6">
            <h3 class="text-xs font-bold text-gray-500 uppercase tracking-wide mb-4">
                <?= htmlspecialchars($sectionLabel) ?>
                <span class="text-gray-400 font-normal normal-case">(<?= count($items) ?>)</span>
            </h3>
            <div class="grid grid-cols-2 md:grid-cols-4 gap-4">
                <?php foreach ($items as $att): ?>
                    <?php $isImage = str_starts_with($att['mime_type'] ?? '', 'image/') || preg_match('/\.(jpe?g|png|gif|webp|bmp)$/i', $att['file_path'] ?? ''); ?>
                    <a href="<?= APP_URL ?>/uploads/<?= htmlspecialchars($att['file_path']) ?>" target="_blank" class="border rounded p-1 block hover:opacity-80 transition" title="<?= htmlspecialchars($att['file_name'] ?? '') ?>">
                        <?php if ($isImage): ?>
                            <img src="<?= APP_URL ?>/uploads/<?= htmlspecialchars($att['file_path']) ?>" alt="<?= htmlspecialchars($att['file_name'] ?? 'Photo') ?>" class="w-full h-28 object-cover rounded">
                        <?php else: ?>
                            <div class="w-full h-28 flex flex-col items-center justify-center bg-gray-50 rounded text-gray-500">
                                <svg width="28" height="28" fill="none" stroke="currentColor" stroke-width="1.5" viewBox="0 0 24 24"><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/><polyline points="14 2 14 8 20 8"/></svg>
                                <span class="text-xs mt-2 px-2 truncate w-full text-center"><?= htmlspecialchars($att['file_name'] ?? 'File') ?></span>
                            </div>
                        <?php endif; ?>
                    </a>
                <?php endforeach; ?>
            </div>
        </div>
        <?php endforeach; ?>
    <?php endif; ?>

    <!-- PM Review Section -->
    <?php if ($report['status'] == 'submitted'): ?>
    <div class="bg-indigo-50 border border-indigo-200 rounded-lg p-6">
        <h3 class="text-lg font-bold text-indigo-900 mb-4">Project Manager Review</h3>
        <form action="<?= APP_URL ?>/reports/daily/<?= $report['id'] ?>/review" method="POST">
            <input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?>">
            <div class="mb-4">
                <label class="block text-sm font-bold text-indigo-800 mb-2">Review Notes</label>
                <textarea name="pm_notes" rows="3" class="w-full border-indigo-300 rounded p-2" placeholder="Leave comments for the contractor if changes are needed..."></textarea>
            </div>
            <div class="flex gap-4">
                <button type="submit" name="action" value="approve" class="bg-green-600 hover:bg-green-700 text-white font-bold py-2 px-6 rounded shadow">Approve Report</button>
                <button type="submit" name="action" value="request_changes" class="bg-yellow-500 hover:bg-yellow-600 text-white font-bold py-2 px-6 rounded shadow">Request Changes</button>
            </div>
        </form>
    </div>
    <?php endif; ?>
</div>

