<?php
// faculty_docs.php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$pageTitle = 'تفاصيل التدريسي والوثائق';
include '../../includes/header.php';

if (session_status() == PHP_SESSION_NONE) { session_start(); }
checkLogin();

$db = getDB();

if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
    $_SESSION['error_message'] = 'رقم التدريسي غير صحيح.';
    header('Location: index.php');
    exit();
}

$teacher_id = (int)$_GET['id'];

try {
    // جلب بيانات التدريسي
    $faculty_sql = "SELECT * FROM faculty WHERE Teacher_ID = ? LIMIT 1";
    if (method_exists($db, 'select')) {
        $faculty_res = $db->select($faculty_sql, [$teacher_id]);
    } else {
        $stmt = $db->prepare($faculty_sql);
        $stmt->execute([$teacher_id]);
        $faculty_res = $stmt->fetchAll(PDO::FETCH_ASSOC);
    }

    if (empty($faculty_res)) {
        $_SESSION['error_message'] = 'التدريسي غير موجود.';
        header('Location: index.php');
        exit();
    }
    $faculty = $faculty_res[0];

    // جلب الوثائق
    $docs_sql = "SELECT * FROM faculty_docs WHERE Teacher_ID = ? LIMIT 1";
    if (method_exists($db, 'select')) {
        $docs_res = $db->select($docs_sql, [$teacher_id]);
    } else {
        $stmt = $db->prepare($docs_sql);
        $stmt->execute([$teacher_id]);
        $docs_res = $stmt->fetchAll(PDO::FETCH_ASSOC);
    }
    $docs = !empty($docs_res) ? $docs_res[0] : null;

} catch (Exception $e) {
    $_SESSION['error_message'] = 'حدث خطأ: ' . $e->getMessage();
    header('Location: index.php');
    exit();
}
?>

<style>
    .img-clickable {
        cursor: pointer;
        transition: transform 0.2s;
    }
    .img-clickable:hover {
        transform: scale(1.02);
        box-shadow: 0 4px 8px rgba(0,0,0,0.2);
    }
    #fullScreenModal .modal-dialog {
        max-width: 90%;
    }
    #fullScreenModal .modal-body {
        text-align: center;
        padding: 0;
    }
    #fullScreenImage {
        max-width: 100%;
        max-height: 85vh;
    }
</style>

<div class="content-area">
    <div class="page-header d-flex justify-content-between align-items-center mb-4">
        <div>
            <h2 class="page-title">تفاصيل التدريسي: <?php echo htmlspecialchars($faculty['Teacher']); ?></h2>
            <nav aria-label="breadcrumb">
                <ol class="breadcrumb">
                    <li class="breadcrumb-item"><a href="../../index.php">الرئيسية</a></li>
                    <li class="breadcrumb-item"><a href="index.php">التدريسيين</a></li>
                    <li class="breadcrumb-item active">عرض التفاصيل</li>
                </ol>
            </nav>
        </div>
        <div class="btn-group">
            <a href="edit_faculty.php?id=<?php echo $teacher_id; ?>" class="btn btn-warning"><i class="fas fa-edit me-2"></i>تعديل</a>
            <a href="index.php" class="btn btn-secondary"><i class="fas fa-arrow-right me-2"></i>عودة</a>
        </div>
    </div>

    <div class="row">
        <!-- المعلومات الشخصية والأكاديمية -->
        <div class="col-lg-4">
            <div class="card shadow-sm mb-4">
                <div class="card-body text-center">
                    <?php if (!empty($faculty['Teacher_pic'])): ?>
                        <img src="data:image/jpeg;base64,<?php echo base64_encode($faculty['Teacher_pic']); ?>" 
                             class="rounded-circle border mb-3 img-clickable" 
                             style="width: 150px; height: 150px; object-fit: cover;"
                             onclick="showFullScreen(this.src, 'صورة التدريسي')">
                    <?php else: ?>
                        <div class="rounded-circle bg-light border d-flex align-items-center justify-content-center text-secondary mx-auto mb-3" style="width: 150px; height: 150px;"><i class="fas fa-user fa-5x"></i></div>
                    <?php endif; ?>
                    <h4 class="fw-bold"><?php echo htmlspecialchars($faculty['Teacher']); ?></h4>
                    <p class="text-muted"><?php echo htmlspecialchars($faculty['Rank']); ?></p>
                    <hr>
                    <div class="text-start">
                        <p><strong><i class="fas fa-envelope me-2"></i>البريد:</strong> <?php echo htmlspecialchars($faculty['email'] ?: 'غير متوفر'); ?></p>
                        <p><strong><i class="fas fa-phone me-2"></i>الهاتف:</strong> <?php echo htmlspecialchars($faculty['phone'] ?: 'غير متوفر'); ?></p>
                        <p><strong><i class="fas fa-venus-mars me-2"></i>الجنس:</strong> <?php echo ($faculty['Gender'] == 'male' || $faculty['Gender'] == 'Male') ? 'ذكر' : 'أنثى'; ?></p>
                    </div>
                </div>
            </div>

            <div class="card shadow-sm mb-4">
                <div class="card-header bg-primary text-white">
                    <h5 class="mb-0">المعلومات الوظيفية</h5>
                </div>
                <div class="card-body">
                    <table class="table table-sm table-borderless">
                        <tr><th style="width: 40%;">الكلية:</th><td><?php echo htmlspecialchars($faculty['College']); ?></td></tr>
                        <tr><th>القسم:</th><td><?php echo htmlspecialchars($faculty['Department']); ?></td></tr>
                        <tr><th>الشهادة:</th><td><?php echo htmlspecialchars($faculty['Certificate']); ?></td></tr>
                        <tr><th>التخصص:</th><td><?php echo htmlspecialchars($faculty['specialization']); ?></td></tr>
                        <tr><th>تاريخ التعيين:</th><td><?php echo htmlspecialchars($faculty['Hiring_date'] ?: 'غير محدد'); ?></td></tr>
                        <tr><th>الراتب:</th><td><span class="text-success fw-bold"><?php echo number_format($faculty['Salary']); ?> د.ع</span></td></tr>
                    </table>
                </div>
            </div>
        </div>

        <!-- الوثائق والمستمسكات -->
        <div class="col-lg-8">
            <div class="card shadow-sm">
                <div class="card-header bg-dark text-white">
                    <h5 class="mb-0"><i class="fas fa-file-alt me-2"></i>الوثائق والمستمسكات</h5>
                </div>
                <div class="card-body">
                    <?php if ($docs): ?>
                        <div class="row g-4">
                            <div class="col-md-6">
                                <h6 class="fw-bold border-bottom pb-2">البطاقة الوطنية</h6>
                                <div class="border rounded p-2 bg-light text-center">
                                    <?php if (!empty($docs['National_ID_Card'])): ?>
                                        <img src="data:image/jpeg;base64,<?php echo base64_encode($docs['National_ID_Card']); ?>" 
                                             class="img-fluid rounded img-clickable" 
                                             style="max-height: 300px;"
                                             onclick="showFullScreen(this.src, 'البطاقة الوطنية')">
                                        <div class="mt-2">
                                            <a href="data:image/jpeg;base64,<?php echo base64_encode($docs['National_ID_Card']); ?>" download="National_ID_<?php echo $teacher_id; ?>.jpg" class="btn btn-sm btn-outline-primary"><i class="fas fa-download me-1"></i>تحميل</a>
                                        </div>
                                    <?php else: ?>
                                        <p class="text-muted py-4">لا توجد صورة</p>
                                    <?php endif; ?>
                                </div>
                            </div>

                            <div class="col-md-6">
                                <h6 class="fw-bold border-bottom pb-2">بطاقة السكن</h6>
                                <div class="border rounded p-2 bg-light text-center">
                                    <?php if (!empty($docs['Residential_Card'])): ?>
                                        <img src="data:image/jpeg;base64,<?php echo base64_encode($docs['Residential_Card']); ?>" 
                                             class="img-fluid rounded img-clickable" 
                                             style="max-height: 300px;"
                                             onclick="showFullScreen(this.src, 'بطاقة السكن')">
                                        <div class="mt-2">
                                            <a href="data:image/jpeg;base64,<?php echo base64_encode($docs['Residential_Card']); ?>" download="Residential_Card_<?php echo $teacher_id; ?>.jpg" class="btn btn-sm btn-outline-primary"><i class="fas fa-download me-1"></i>تحميل</a>
                                        </div>
                                    <?php else: ?>
                                        <p class="text-muted py-4">لا توجد صورة</p>
                                    <?php endif; ?>
                                </div>
                            </div>

                            <div class="col-md-12">
                                <h6 class="fw-bold border-bottom pb-2">نسخة الشهادة</h6>
                                <div class="border rounded p-2 bg-light text-center">
                                    <?php if (!empty($docs['Certificate_Pic'])): ?>
                                        <img src="data:image/jpeg;base64,<?php echo base64_encode($docs['Certificate_Pic']); ?>" 
                                             class="img-fluid rounded img-clickable" 
                                             style="max-height: 500px;"
                                             onclick="showFullScreen(this.src, 'نسخة الشهادة')">
                                        <div class="mt-2">
                                            <a href="data:image/jpeg;base64,<?php echo base64_encode($docs['Certificate_Pic']); ?>" download="Certificate_<?php echo $teacher_id; ?>.jpg" class="btn btn-sm btn-outline-primary"><i class="fas fa-download me-1"></i>تحميل</a>
                                        </div>
                                    <?php else: ?>
                                        <p class="text-muted py-4">لا توجد صورة</p>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>
                    <?php else: ?>
                        <div class="alert alert-info">لا توجد وثائق مرفوعة لهذا التدريسي.</div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Modal for Full Screen Image -->
<div class="modal fade" id="fullScreenModal" tabindex="-1" aria-labelledby="fullScreenModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="fullScreenModalLabel">عرض الصورة</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <img src="" id="fullScreenImage" alt="Full Screen">
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">إغلاق</button>
            </div>
        </div>
    </div>
</div>

<script>
    function showFullScreen(src, title) {
        document.getElementById('fullScreenImage').src = src;
        document.getElementById('fullScreenModalLabel').textContent = title;
        var myModal = new bootstrap.Modal(document.getElementById('fullScreenModal'));
        myModal.show();
    }
</script>

<?php include '../../includes/footer.php'; ?>
