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

<div class="container mx-auto px-4 py-8 max-w-5xl">
    <h1 class="text-2xl font-bold text-gray-800 mb-6">Submit Daily Report</h1>

    <?php if (empty($template)): ?>
    <div class="bg-amber-50 border border-amber-200 text-amber-800 rounded-lg p-4 mb-6">
        No report template has been linked to this project yet. Ask your project manager to set one up under Report Templates.
    </div>
    <?php endif; ?>
    <form action="<?= APP_URL ?>/reports/daily/create" method="POST" enctype="multipart/form-data" class="bg-white shadow rounded-lg p-6">
        <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($_SESSION[CSRF_TOKEN_NAME] ?? '') ?>">
        <input type="hidden" name="project_id" value="<?= (int)$projectId ?>">

        <div class="grid grid-cols-1 md:grid-cols-3 gap-6 mb-8 border-b pb-6">
            <div>
                <label class="block text-gray-700 text-sm font-bold mb-2">Date</label>
                <input type="date" name="date" value="<?= date('Y-m-d') ?>" class="border rounded w-full py-2 px-3" required>
            </div>
            <div>
                <label class="block text-gray-700 text-sm font-bold mb-2">Weather</label>
                <select name="weather" class="border rounded w-full py-2 px-3">
                    <option value="Sunny">Sunny</option>
                    <option value="Cloudy">Cloudy</option>
                    <option value="Rainy">Rainy</option>
                    <option value="Snow">Snow</option>
                </select>
            </div>
            <div>
                <label class="block text-gray-700 text-sm font-bold mb-2">Manpower Count</label>
                <input type="number" name="manpower" class="border rounded w-full py-2 px-3" min="0">
            </div>
        </div>

        <h2 class="text-xl font-semibold mb-4">Workload Progress</h2>
        <div class="overflow-x-auto mb-8 border-b pb-6">
            <table class="w-full text-left border-collapse">
                <thead>
                    <tr>
                        <th class="border-b p-2">Activity</th>
                        <th class="border-b p-2">Unit</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 ($workloadItems as $wl): ?>
                    <tr>
                        <td class="border-b p-2"><?= htmlspecialchars($wl['activity_name']) ?></td>
                        <td class="border-b p-2"><?= htmlspecialchars($wl['unit']) ?></td>
                        <td class="border-b p-2"><?= htmlspecialchars($wl['project_scope'] ?? '') ?></td>
                        <td class="border-b p-2">
                            <input type="number" step="0.01" name="workload[<?= $wl['id'] ?>][planned]" class="border rounded w-20 p-1">
                        </td>
                        <td class="border-b p-2">
                            <input type="number" step="0.01" name="workload[<?= $wl['id'] ?>][achieved]" class="border rounded w-24 p-1">
                        </td>
                        <td class="border-b p-2">
                            <input type="number" step="0.01" name="workload[<?= $wl['id'] ?>][cumulative]" class="border rounded w-24 p-1 bg-gray-50" readonly>
                        </td>
                        <td class="border-b p-2">
                            <input type="text" name="workload[<?= $wl['id'] ?>][notes]" class="border rounded w-full p-1">
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>

        <h2 class="text-xl font-semibold mb-4">Report Details</h2>
        <div class="space-y-6 mb-8 border-b pb-6">
            <?php foreach ($sections as $sec): ?>
            <div>
                <label class="block text-gray-700 font-bold mb-2">
                    <?= htmlspecialchars($sec['title']) ?> <?= $sec['is_required'] ? '<span class="text-red-500">*</span>' : '' ?>
                </label>
                <?php if ($sec['type'] == 'text'): ?>
                    <textarea name="sections[<?= $sec['id'] ?>]" class="border rounded w-full py-2 px-3" rows="3" <?= $sec['is_required'] ? 'required' : '' ?>></textarea>
                <?php elseif ($sec['type'] == 'photo' || $sec['type'] == 'file'): ?>
                    <input type="file" name="files[<?= $sec['id'] ?>][]" multiple class="border rounded w-full py-2 px-3" <?= $sec['is_required'] ? 'required' : '' ?> <?= $sec['type'] == 'photo' ? 'accept="image/*"' : '' ?>>
                <?php endif; ?>
            </div>
            <?php endforeach; ?>
        </div>

        <div class="mb-8">
            <label class="block text-gray-700 font-bold mb-2">General Site Notes</label>
            <textarea name="site_notes" class="border rounded w-full py-2 px-3" rows="4"></textarea>
        </div>

        <div class="flex justify-end gap-4">
            <button type="submit" name="draft" value="1" class="bg-gray-400 hover:bg-gray-500 text-white font-bold py-2 px-6 rounded shadow">Save as Draft</button>
            <button type="submit" name="submit" value="1" class="bg-blue-600 hover:bg-blue-700 text-white font-bold py-2 px-6 rounded shadow">Submit Report</button>
        </div>
    </form>
</div>

