<?php
require_once __DIR__ . '/bootstrap_public.php';

$pdo = getPDO();

$rid = (int)($_GET['rid'] ?? 0);
if ($rid <= 0) { http_response_code(404); exit('Not found'); }

$st = $pdo->prepare("SELECT * FROM sponsor_access_requests WHERE id=?");
$st->execute([$rid]);
$req = $st->fetch(PDO::FETCH_ASSOC);
if (!$req) { http_response_code(404); exit('Not found'); }

// fetch orphan info
$bid = (int)$req['beneficiary_id'];
$bst = $pdo->prepare("SELECT id, full_name, file_number, beneficiary_type_id FROM beneficiaries WHERE id=? LIMIT 1");
$bst->execute([$bid]);
$ben = $bst->fetch(PDO::FETCH_ASSOC);

if (!$ben || (int)$ben['beneficiary_type_id'] !== 2) {
    http_response_code(404);
    exit('Not found');
}

// shareable documents
$dst = $pdo->prepare("SELECT id, doc_type, doc_side, original_name, mime_type, size_bytes, created_at FROM beneficiary_documents WHERE beneficiary_id = ? AND is_shareable = 1 ORDER BY id DESC LIMIT 200");
$dst->execute([$bid]);
$docs = $dst->fetchAll(PDO::FETCH_ASSOC);

$docTypes = [
    'id_card' => 'هوية شخصية',
    'birth_cert' => 'شهادة ميلاد',
    'father_death_cert' => 'شهادة وفاة الأب',
];
$sides = ['front' => 'الوجه الأمامي', 'back' => 'الوجه الخلفي'];
?>
<!doctype html>
<html lang="ar" dir="rtl">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>بيانات الحالة | <?= e((string)$ben['full_name']) ?></title>
    
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.rtl.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Cairo:wght@400;600;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.5/font/bootstrap-icons.css">

    <style>
        body {
            font-family: 'Cairo', sans-serif;
            background-color: #f4f7f9;
            color: #333;
        }
        .main-card {
            border: none;
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.05);
            overflow: hidden;
        }
        .card-header-custom {
            background: linear-gradient(45deg, #2c3e50, #3498db);
            color: white;
            padding: 2rem;
            text-align: center;
        }
        .info-badge {
            background: rgba(255,255,255,0.1);
            padding: 10px 20px;
            border-radius: 50px;
            display: inline-block;
            margin-top: 10px;
        }
        .doc-icon {
            font-size: 1.5rem;
            color: #3498db;
        }
        .btn-download {
            border-radius: 8px;
            transition: all 0.3s;
        }
        .btn-download:hover {
            transform: translateY(-2px);
        }
        /* تحسين الجدول للموبايل */
        @media (max-width: 768px) {
            .table-responsive-custom thead { display: none; }
            .table-responsive-custom tr { 
                display: block; 
                margin-bottom: 1rem; 
                border: 1px solid #eee; 
                border-radius: 10px; 
                background: #fff;
                padding: 10px;
            }
            .table-responsive-custom td { 
                display: flex; 
                justify-content: space-between; 
                align-items: center; 
                border: none; 
                padding: 0.5rem;
            }
            .table-responsive-custom td::before {
                content: attr(data-label);
                font-weight: bold;
                color: #666;
            }
        }
    </style>
</head>
<body>

<div class="container my-5">
    <div class="row justify-content-center">
        <div class="col-lg-10">
            
            <div class="card main-card">
                <div class="card-header-custom">
                    <i class="bi bi-patch-check-fill fs-1"></i>
                    <h2 class="mt-3">تم التحقق من البيانات بنجاح</h2>
                    <div class="info-badge">
                        <i class="bi bi-shield-lock-fill me-2"></i> وصول آمن ومصرح به
                    </div>
                </div>

                <div class="card-body p-4 p-md-5">
                    <div class="row g-4 mb-5">
                        <div class="col-md-6">
                            <div class="p-3 border rounded-3 bg-light">
                                <small class="text-muted d-block mb-1">اسم اليتيم</small>
                                <h4 class="mb-0 text-primary"><?= e((string)$ben['full_name']) ?></h4>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="p-3 border rounded-3 bg-light">
                                <small class="text-muted d-block mb-1">رقم الملف</small>
                                <h4 class="mb-0"><?= (int)$ben['file_number'] ?></h4>
                            </div>
                        </div>
                    </div>

                    <hr class="my-4">

                    <div class="d-flex align-items-center mb-4">
                        <i class="bi bi-file-earmark-pdf-fill fs-3 text-danger me-2"></i>
                        <h3 class="mb-0">الوثائق الثبوتية</h3>
                    </div>
                    
                    <div class="alert alert-info border-0 shadow-sm mb-4">
                        <i class="bi bi-info-circle-fill me-2"></i>
                        ملاحظة: تظهر هنا فقط الوثائق التي تمت مراجعتها والموافقة على مشاركتها من قبل الإدارة.
                    </div>

                    <div class="table-responsive">
                        <table class="table table-hover align-middle table-responsive-custom">
                            <thead class="table-light">
                                <tr>
                                    <th>نوع الوثيقة</th>
                                    <th>الجهة</th>
                                    <th class="d-none d-md-table-cell">الحجم</th>
                                    <th class="d-none d-md-table-cell">تاريخ الرفع</th>
                                    <th class="text-center">الإجراء</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach ($docs as $d): ?>
                                    <tr>
                                        <td data-label="الوثيقة">
                                            <div class="d-flex align-items-center">
                                                <i class="bi bi-file-earmark-text me-2 doc-icon"></i>
                                                <div>
                                                    <div class="fw-bold"><?= e($docTypes[$d['doc_type']] ?? $d['doc_type']) ?></div>
                                                    <small class="text-muted d-md-none"><?= number_format($d['size_bytes'] / 1024, 1) ?> KB</small>
                                                </div>
                                            </div>
                                        </td>
                                        <td data-label="الجهة"><?= e($sides[$d['doc_side']] ?? '—') ?></td>
                                        <td data-label="الحجم" class="d-none d-md-table-cell text-muted">
                                            <?= number_format($d['size_bytes'] / 1024, 1) ?> KB
                                        </td>
                                        <td data-label="تاريخ الرفع" class="d-none d-md-table-cell text-muted">
                                            <?= date('Y/m/d', strtotime($d['created_at'])) ?>
                                        </td>
                                        <td class="text-center">
                                            <a class="btn btn-outline-primary btn-sm btn-download px-3" 
                                               target="_blank" 
                                               href="document_download.php?id=<?= (int)$d['id'] ?>">
                                                <i class="bi bi-eye-fill me-1"></i> عرض / تحميل
                                            </a>
                                        </td>
                                    </tr>
                                <?php endforeach; ?>
                                
                                <?php if (!$docs): ?>
                                    <tr>
                                        <td colspan="5" class="text-center py-5 text-muted">
                                            <i class="bi bi-folder-x fs-1 d-block mb-3"></i>
                                            لا توجد وثائق متاحة للمشاركة حالياً.
                                        </td>
                                    </tr>
                                <?php endif; ?>
                            </tbody>
                        </table>
                    </div>

                    <div class="mt-5 d-grid d-md-flex justify-content-md-start gap-2">
                        <a class="btn btn-secondary px-4 py-2" href="<?= ORPHAN_PUBLIC_BASE ?>/index.php">
                            <i class="bi bi-house-door me-2"></i> العودة للرئيسية
                        </a>
                    </div>
                </div>
            </div>

            <footer class="text-center mt-4 text-muted">
                <small>&copy; <?= date('Y') ?> نظام كفالة الأيتام - جميع الحقوق محفوظة</small>
            </footer>

        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>