<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>إدارة النماذج<?php $__template->endSection(); ?>
<?php $__template->section('page_actions'); ?>
<div style="display:flex;gap:10px;">
    <?php foreach ($schemas as $s): ?>
        <a href="/forms/render/<?= e($s['form_code']) ?>" class="btn btn-sm btn-outline"><?= e($s['name_ar']) ?></a>
    <?php endforeach; ?>
</div>
<?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>

<div class="card" style="margin-bottom:20px;padding:15px;">
    <form method="GET" action="/forms/submissions" style="display:flex;gap:10px;flex-wrap:wrap;align-items:end;">
        <div>
            <label class="form-label" style="font-size:12px;">بحث</label>
            <input type="text" name="q" value="<?= e($filters['search'] ?? '') ?>" placeholder="رقم النموذج..." class="form-input" style="min-width:150px;">
        </div>
        <div>
            <label class="form-label" style="font-size:12px;">نوع النموذج</label>
            <select name="form_code" class="form-select" style="min-width:150px;">
                <option value="">الكل</option>
                <?php foreach ($schemas as $s): ?>
                    <option value="<?= e($s['form_code']) ?>" <?= ($filters['form_code'] ?? '') === $s['form_code'] ? 'selected' : '' ?>><?= e($s['name_ar']) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div>
            <label class="form-label" style="font-size:12px;">الحالة</label>
            <select name="status" class="form-select">
                <option value="">الكل</option>
                <option value="draft" <?= ($filters['status'] ?? '') === 'draft' ? 'selected' : '' ?>>مسودة</option>
                <option value="submitted" <?= ($filters['status'] ?? '') === 'submitted' ? 'selected' : '' ?>>مُقدّم</option>
                <option value="under_review" <?= ($filters['status'] ?? '') === 'under_review' ? 'selected' : '' ?>>تحت المراجعة</option>
                <option value="approved" <?= ($filters['status'] ?? '') === 'approved' ? 'selected' : '' ?>>مقبول</option>
                <option value="rejected" <?= ($filters['status'] ?? '') === 'rejected' ? 'selected' : '' ?>>مرفوض</option>
                <option value="expired" <?= ($filters['status'] ?? '') === 'expired' ? 'selected' : '' ?>>منتهي</option>
            </select>
        </div>
        <div>
            <label class="form-label" style="font-size:12px;">من</label>
            <input type="date" name="date_from" value="<?= e($filters['date_from'] ?? '') ?>" class="form-input">
        </div>
        <div>
            <label class="form-label" style="font-size:12px;">إلى</label>
            <input type="date" name="date_to" value="<?= e($filters['date_to'] ?? '') ?>" class="form-input">
        </div>
        <button type="submit" class="btn btn-outline">بحث</button>
        <a href="/forms/submissions" class="btn btn-sm btn-outline" style="color:#6B7280;">مسح</a>
    </form>
</div>

<div class="card">
    <div class="table-responsive">
        <table class="data-table">
            <thead>
                <tr>
                    <th>رقم النموذج</th>
                    <th>نوع النموذج</th>
                    <th>الحالة</th>
                    <th>بواسطة</th>
                    <th>التاريخ</th>
                    <th>انتهاء الصلاحية</th>
                    <th>الإجراءات</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($rows as $r): ?>
                <tr>
                    <td style="font-weight:600;direction:ltr;text-align:right;"><?= e($r['form_number']) ?></td>
                    <td><?= e($r['schema_name_ar'] ?? $r['form_code']) ?></td>
                    <td>
                        <?php
                        $statusColors = [
                            'draft' => '#6B7280', 'submitted' => '#0284C7', 'under_review' => '#D97706',
                            'approved' => '#059669', 'rejected' => '#DC2626', 'expired' => '#9CA3AF',
                        ];
                        $statusLabels = [
                            'draft' => 'مسودة', 'submitted' => 'مُقدّم', 'under_review' => 'تحت المراجعة',
                            'approved' => 'مقبول', 'rejected' => 'مرفوض', 'expired' => 'منتهي',
                        ];
                        $color = $statusColors[$r['status']] ?? '#6B7280';
                        $label = $statusLabels[$r['status']] ?? $r['status'];
                        ?>
                        <span style="color:<?= $color ?>;font-weight:600;"><?= e($label) ?></span>
                    </td>
                    <td style="font-size:13px;"><?= e($r['employee_name'] ?? '—') ?></td>
                    <td style="font-size:12px;"><?= e(substr($r['created_at'], 0, 10)) ?></td>
                    <td style="font-size:12px;"><?= $r['expires_at'] ? e(substr($r['expires_at'], 0, 10)) : '—' ?></td>
                    <td>
                        <div style="display:flex;gap:5px;">
                            <a href="/forms/submissions/<?= (int) $r['id'] ?>" class="btn btn-sm btn-outline">عرض</a>
                            <a href="/forms/print/<?= (int) $r['id'] ?>" class="btn btn-sm btn-outline" target="_blank">طباعة</a>
                        </div>
                    </td>
                </tr>
                <?php endforeach; ?>
                <?php if (empty($rows)): ?>
                <tr><td colspan="7" style="text-align:center;padding:40px;color:#6B7280;">لا توجد نماذج مقدمة</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
<?php $__template->endSection(); ?>