<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>رفع مستندات — <?= e($member['full_name_ar']) ?><?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>
<div style="display:grid;grid-template-columns:1fr 1fr;gap:20px;">
    <div>
        <form method="POST" action="/documents/upload/<?= (int) $member['id'] ?>" enctype="multipart/form-data">
            <?= csrf_field() ?>
            <div class="card" style="padding:20px;">
                <h4 style="color:#0D7377;margin-bottom:15px;">رفع مستند جديد</h4>
                <div style="display:grid;gap:15px;">
                    <div class="form-group">
                        <label class="form-label">نوع المستند <span style="color:#DC2626;">*</span></label>
                        <select name="document_type" class="form-select" required>
                            <option value="">-- اختر --</option>
                            <?php foreach ($docTypes as $code => $label): ?>
                                <option value="<?= e($code) ?>"><?= e($label) ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="form-group">
                        <label class="form-label">الملف <span style="color:#DC2626;">*</span></label>
                        <input type="file" name="document_file" class="form-input" required accept=".pdf,.jpg,.jpeg,.png">
                        <small style="color:#6B7280;display:block;margin-top:4px;">PDF, JPG, PNG — الحد الأقصى 10 ميجا</small>
                    </div>
                    <div class="form-group">
                        <label class="form-label">وصف (اختياري)</label>
                        <input type="text" name="description" class="form-input" placeholder="وصف مختصر للمستند">
                    </div>
                </div>
            </div>
            <button type="submit" class="btn btn-primary" style="margin-top:15px;">رفع المستند</button>
            <a href="/members/<?= (int) $member['id'] ?>" class="btn btn-outline" style="margin-top:15px;">العودة للعضو</a>
        </form>
    </div>

    <div>
        <div class="card">
            <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;">
                <h4 style="margin:0;color:#0D7377;">المستندات المرفوعة (<?= count($documents) ?>)</h4>
            </div>
            <div style="padding:15px;">
                <?php if (empty($documents)): ?>
                    <div style="text-align:center;color:#6B7280;padding:20px;">لا توجد مستندات</div>
                <?php else: ?>
                    <?php foreach ($documents as $doc): ?>
                    <div style="display:flex;justify-content:space-between;align-items:center;padding:10px 0;border-bottom:1px solid #F3F4F6;">
                        <div>
                            <div style="font-weight:600;font-size:13px;"><?= e(\App\Modules\Documents\Models\Document::getTypeLabel($doc['document_type'])) ?></div>
                            <div style="font-size:12px;color:#6B7280;"><?= e($doc['original_filename']) ?> — <?= round(($doc['file_size'] ?? 0) / 1024) ?> كيلوبايت</div>
                            <div style="font-size:11px;color:#9CA3AF;"><?= e(substr($doc['uploaded_at'], 0, 16)) ?> <?= $doc['uploaded_by_name'] ? '— ' . e($doc['uploaded_by_name']) : '' ?></div>
                        </div>
                        <div style="display:flex;gap:5px;">
                            <a href="/documents/<?= (int) $doc['id'] ?>/preview" class="btn btn-sm btn-outline" target="_blank">معاينة</a>
                            <a href="/documents/<?= (int) $doc['id'] ?>/download" class="btn btn-sm btn-outline">تحميل</a>
                            <form method="POST" action="/documents/<?= (int) $doc['id'] ?>/archive" style="display:inline;">
                                <?= csrf_field() ?>
                                <input type="hidden" name="redirect_url" value="/documents/upload/<?= (int) $member['id'] ?>">
                                <button type="submit" class="btn btn-sm btn-outline" style="color:#DC2626;" onclick="return confirm('أرشفة المستند؟')">حذف</button>
                            </form>
                        </div>
                    </div>
                    <?php endforeach; ?>
                <?php endif; ?>
            </div>
        </div>

        <!-- Required Documents Checklist -->
        <div class="card" style="margin-top:20px;">
            <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;">
                <h4 style="margin:0;color:#0D7377;">قائمة المستندات المطلوبة</h4>
            </div>
            <div style="padding:15px;">
                <?php
                $uploadedTypes = array_column($documents, 'document_type');
                $requiredDocs = [
                    'national_id' => 'صورة الرقم القومي',
                    'photo' => 'صورة شخصية',
                    'form_scan' => 'نسخة الاستمارة',
                ];
                if (($member['marital_status'] ?? '') === 'married') {
                    $requiredDocs['marriage_certificate'] = 'عقد زواج';
                }
                ?>
                <?php foreach ($requiredDocs as $typeCode => $typeLabel): ?>
                    <div style="display:flex;align-items:center;gap:8px;padding:6px 0;">
                        <?php if (in_array($typeCode, $uploadedTypes)): ?>
                            <span style="color:#059669;font-size:16px;">✓</span>
                            <span style="color:#059669;"><?= e($typeLabel) ?></span>
                        <?php else: ?>
                            <span style="color:#DC2626;font-size:16px;">✗</span>
                            <span style="color:#DC2626;"><?= e($typeLabel) ?> — <strong>مطلوب</strong></span>
                        <?php endif; ?>
                    </div>
                <?php endforeach; ?>
            </div>
        </div>
    </div>
</div>
<?php $__template->endSection(); ?>