<?php
/**
 * Prescription Detail View — Premium SaaS Design
 * Shows full prescription with linked medications & their lifecycle status
 */
$doctorIcons = [
    'باطنة' => 'fa-stomach', 'نظر' => 'fa-eye', 'صدر' => 'fa-lungs',
    'أنف وأذن' => 'fa-ear-listen', 'جلدية' => 'fa-hand-dots', 'عظام' => 'fa-bone',
    'أسنان' => 'fa-tooth', 'قلب' => 'fa-heart-pulse', 'مسالك' => 'fa-kidneys',
    'نفسية' => 'fa-brain', 'عام' => 'fa-stethoscope'
];
$icon = $doctorIcons[$prescription->doctor_type] ?? 'fa-user-doctor';
?>

<style>
.rx-detail-card {
    border-radius: 20px; border: none; box-shadow: 0 10px 30px rgba(0,0,0,0.04);
    transition: all 0.3s ease; background: #ffffff; overflow: hidden;
}
.rx-detail-card:hover { transform: translateY(-4px); box-shadow: 0 15px 40px rgba(0,0,0,0.07); }
.rx-med-item { transition: all 0.3s ease; }
.rx-med-item:hover { transform: translateX(-4px); }
.rx-timeline { position: relative; padding-right: 2rem; }
.rx-timeline::before { content: ''; position: absolute; right: 0; top: 0; bottom: 0; width: 3px; background: linear-gradient(to bottom, #10b981, #3b82f6, #94a3b8); border-radius: 5px; }
</style>

<div class="container-fluid py-4" style="font-family: 'Cairo', sans-serif;">

    <!-- Breadcrumb -->
    <nav class="mb-4 d-flex justify-content-between align-items-center">
        <a href="<?= BASE_URL ?>/health?tab=prescriptions" class="text-decoration-none text-primary fw-bold">
            <i class="fa-solid fa-arrow-right me-1"></i> العودة للروشتات
        </a>
        <a href="<?= BASE_URL ?>/health/deletePrescription/<?= $prescription->id ?>" onclick="return confirm('حذف هذه الروشتة وجميع أدويتها نهائياً؟')" class="btn btn-sm btn-outline-danger">
            <i class="fa-solid fa-trash me-1"></i> حذف الروشتة
        </a>
    </nav>

    <!-- Prescription Header Card -->
    <div class="rx-detail-card p-5 mb-5 border-start border-4 border-primary position-relative">
        <div class="row align-items-center">
            <div class="col-md-1 text-center">
                <div class="bg-primary-subtle text-primary rounded-circle d-flex align-items-center justify-content-center mx-auto" style="width: 70px; height: 70px;">
                    <i class="fa-solid <?= $icon ?> fs-2"></i>
                </div>
            </div>
            <div class="col-md-7">
                <h2 class="fw-bold text-dark mb-1"><?= htmlspecialchars($prescription->doctor_type) ?></h2>
                <?php if($prescription->clinic_name): ?>
                    <p class="text-secondary fw-semibold mb-0"><i class="fa-solid fa-hospital me-1"></i> <?= htmlspecialchars($prescription->clinic_name) ?></p>
                <?php endif; ?>
            </div>
            <div class="col-md-4 text-md-end mt-3 mt-md-0">
                <div class="bg-light rounded-4 p-3 d-inline-block">
                    <span class="d-block text-muted fw-bold small">تاريخ الزيارة</span>
                    <span class="fw-bold fs-5 text-dark"><i class="fa-regular fa-calendar me-1"></i> <?= date('d M Y', strtotime($prescription->visit_date)) ?></span>
                </div>
            </div>
        </div>
        <?php if($prescription->notes): ?>
        <div class="mt-4 p-3 bg-light rounded-3 border-start border-3 border-info">
            <span class="fw-bold text-info d-block mb-1"><i class="fa-solid fa-note-sticky me-1"></i> ملاحظات الطبيب:</span>
            <p class="text-dark mb-0"><?= nl2br(htmlspecialchars($prescription->notes)) ?></p>
        </div>
        <?php endif; ?>
    </div>

    <!-- Medications List -->
    <h4 class="fw-bold text-dark mb-4"><i class="fa-solid fa-pills text-primary me-2"></i> الأدوية الموصوفة <span class="badge bg-primary-subtle text-primary rounded-pill"><?= count($meds) ?></span></h4>

    <?php if(empty($meds)): ?>
    <div class="text-center py-5 bg-white rounded-4 shadow-sm">
        <i class="fa-solid fa-capsules fa-3x text-muted opacity-25 mb-3"></i>
        <h5 class="text-muted">لا توجد أدوية مسجلة لهذه الروشتة.</h5>
    </div>
    <?php else: ?>
    <div class="rx-timeline">
        <div class="row g-4">
        <?php foreach($meds as $med): 
            $expired = (bool)$med->is_expired;
            $daysLeft = null;
            if($med->end_date && !$expired) {
                $daysLeft = (int)((strtotime($med->end_date) - time()) / 86400);
            }
        ?>
        <div class="col-md-6 col-xl-4">
            <div class="rx-detail-card rx-med-item p-4 h-100 border-start border-4 <?= $expired ? 'border-secondary' : 'border-success' ?>">
                <div class="d-flex justify-content-between align-items-start mb-3">
                    <div>
                        <h5 class="fw-bold <?= $expired ? 'text-muted text-decoration-line-through' : 'text-dark' ?> mb-1">
                            <i class="fa-solid fa-capsules <?= $expired ? 'text-secondary' : 'text-primary' ?> me-2"></i>
                            <?= htmlspecialchars($med->med_name) ?>
                        </h5>
                        <span class="text-secondary"><i class="fa-solid fa-syringe me-1"></i> <?= htmlspecialchars($med->dose) ?></span>
                    </div>
                    <?php if($expired): ?>
                        <span class="badge bg-secondary-subtle text-secondary rounded-pill px-3 py-2"><i class="fa-solid fa-clock-rotate-left me-1"></i> منتهي</span>
                    <?php else: ?>
                        <span class="badge bg-success-subtle text-success rounded-pill px-3 py-2"><i class="fa-solid fa-circle-check me-1"></i> نشط</span>
                    <?php endif; ?>
                </div>

                <div class="d-flex flex-wrap gap-2 mb-3">
                    <span class="badge bg-light text-dark border fw-semibold"><i class="fa-solid fa-repeat me-1"></i> <?= htmlspecialchars($med->frequency) ?></span>
                    <?php if($med->schedule): ?>
                        <span class="badge bg-light text-dark border fw-semibold"><i class="fa-regular fa-clock me-1"></i> <?= htmlspecialchars($med->schedule) ?></span>
                    <?php endif; ?>
                </div>

                <div class="d-flex gap-2 mt-auto">
                    <?php if($med->start_date): ?>
                    <div class="bg-light rounded-3 p-2 px-3 text-center flex-fill">
                        <span class="d-block text-muted small fw-bold">بداية</span>
                        <span class="fw-bold text-dark small"><?= date('d/m', strtotime($med->start_date)) ?></span>
                    </div>
                    <?php endif; ?>
                    <?php if($med->end_date): ?>
                    <div class="bg-light rounded-3 p-2 px-3 text-center flex-fill">
                        <span class="d-block text-muted small fw-bold">نهاية</span>
                        <span class="fw-bold <?= $expired ? 'text-danger' : 'text-dark' ?> small"><?= date('d/m', strtotime($med->end_date)) ?></span>
                    </div>
                    <?php endif; ?>
                    <?php if($daysLeft !== null && !$expired): ?>
                    <div class="bg-warning-subtle rounded-3 p-2 px-3 text-center flex-fill">
                        <span class="d-block text-muted small fw-bold">متبقي</span>
                        <span class="fw-bold text-warning small"><?= $daysLeft ?> يوم</span>
                    </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
        <?php endforeach; ?>
        </div>
    </div>
    <?php endif; ?>
</div>
