<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?><?= e($institution['name_ar']) ?><?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<?php if (can('sa.institution.manage')): ?>
<a href="/sa/institutions/<?= (int)$institution['id'] ?>/edit" class="btn btn-outline"><i data-lucide="edit" style="width:14px;height:14px;vertical-align:middle;margin-left:4px;"></i> تعديل</a>
<form method="POST" action="/sa/institutions/<?= (int)$institution['id'] ?>/archive" style="display:inline;">
    <?= csrf_field() ?>
    <button type="submit" class="btn btn-outline" style="color:#DC2626;" onclick="return confirm('هل تريد أرشفة هذه المؤسسة؟')"><i data-lucide="archive" style="width:14px;height:14px;vertical-align:middle;margin-left:4px;"></i> أرشفة</button>
</form>
<?php endif; ?>
<a href="/sa/institutions" class="btn btn-outline"><i data-lucide="arrow-right" style="width:14px;height:14px;vertical-align:middle;margin-left:4px;"></i> العودة</a>
<?php $__template->endSection(); ?>

<?php $__template->section('content'); ?>

<?php $typeMap = ['school'=>'مدرسة','university'=>'جامعة','company'=>'شركة','club'=>'نادي','government'=>'حكومي','other'=>'أخرى']; ?>

<div style="display:grid;grid-template-columns:repeat(auto-fit,minmax(150px,1fr));gap:10px;margin-bottom:15px;">
    <div class="card" style="padding:15px;text-align:center;">
        <div style="font-size:22px;font-weight:700;"><?= (int)($stats['total_bookings'] ?? 0) ?></div>
        <div style="font-size:12px;color:#6B7280;">إجمالي الحجوزات</div>
    </div>
    <div class="card" style="padding:15px;text-align:center;">
        <div style="font-size:22px;font-weight:700;color:#059669;"><?= (int)($stats['completed'] ?? 0) ?></div>
        <div style="font-size:12px;color:#6B7280;">مكتملة</div>
    </div>
    <div class="card" style="padding:15px;text-align:center;">
        <div style="font-size:22px;font-weight:700;color:#2563EB;"><?= (int)($stats['confirmed'] ?? 0) ?></div>
        <div style="font-size:12px;color:#6B7280;">مؤكدة</div>
    </div>
    <div class="card" style="padding:15px;text-align:center;">
        <div style="font-size:22px;font-weight:700;color:#D97706;"><?= money((float)($stats['total_revenue'] ?? 0)) ?></div>
        <div style="font-size:12px;color:#6B7280;">إجمالي الإيرادات</div>
    </div>
</div>

<div class="card" style="padding:20px;margin-bottom:15px;">
    <h3 style="margin-bottom:15px;border-bottom:1px solid #E5E7EB;padding-bottom:10px;">بيانات المؤسسة</h3>
    <div style="display:grid;grid-template-columns:1fr 1fr;gap:12px;font-size:14px;">
        <div><strong>الكود:</strong> <?= e($institution['code']) ?></div>
        <div><strong>النوع:</strong> <?= e($typeMap[$institution['institution_type']] ?? '') ?></div>
        <div><strong>المسؤول:</strong> <?= e($institution['contact_person'] ?? '—') ?></div>
        <div><strong>الهاتف:</strong> <?= e($institution['phone'] ?? '—') ?></div>
        <div><strong>البريد:</strong> <?= e($institution['email'] ?? '—') ?></div>
        <div><strong>بداية العقد:</strong> <?= e($institution['contract_start'] ?? '—') ?></div>
        <div><strong>نهاية العقد:</strong> <?= e($institution['contract_end'] ?? '—') ?></div>
        <div><strong>سعر الساعة:</strong> <?= $institution['hourly_rate'] ? money((float)$institution['hourly_rate']) : '—' ?></div>
        <div><strong>نسبة الخصم:</strong> <?= e($institution['discount_percent'] ?? 0) ?>%</div>
        <div><strong>الحد الأقصى:</strong> <?= e($institution['max_participants'] ?? '—') ?></div>
    </div>
    <?php if (!empty($institution['address'])): ?>
    <div style="margin-top:10px;"><strong>العنوان:</strong> <?= e($institution['address']) ?></div>
    <?php endif; ?>
    <?php if (!empty($institution['notes'])): ?>
    <div style="margin-top:10px;"><strong>ملاحظات:</strong> <?= e($institution['notes']) ?></div>
    <?php endif; ?>
</div>

<div class="card">
    <h3 style="padding:15px 20px;border-bottom:1px solid #E5E7EB;margin:0;">آخر الحجوزات</h3>
    <div class="table-responsive">
        <table class="data-table">
            <thead>
                <tr>
                    <th>رقم الحجز</th>
                    <th>التاريخ</th>
                    <th>المرفق</th>
                    <th>الوقت</th>
                    <th>المبلغ</th>
                    <th>الحالة</th>
                </tr>
            </thead>
            <tbody>
                <?php if (!empty($bookings)): ?>
                <?php foreach ($bookings as $b): ?>
                <tr>
                    <td style="font-family:monospace;font-size:12px;"><?= e($b['booking_number']) ?></td>
                    <td><?= e($b['booking_date']) ?></td>
                    <td><?= e($b['unit_name'] ?? '') ?></td>
                    <td><?= e($b['start_time'] ?? '') ?> - <?= e($b['end_time'] ?? '') ?></td>
                    <td><?= money((float)($b['total_amount'] ?? 0)) ?></td>
                    <td>
                        <?php
                        $bStatus = match($b['status'] ?? '') {
                            'confirmed' => '<span style="background:#EFF6FF;color:#2563EB;padding:2px 8px;border-radius:8px;font-size:11px;">مؤكد</span>',
                            'completed' => '<span style="background:#ECFDF5;color:#059669;padding:2px 8px;border-radius:8px;font-size:11px;">مكتمل</span>',
                            'cancelled' => '<span style="background:#FEF2F2;color:#DC2626;padding:2px 8px;border-radius:8px;font-size:11px;">ملغي</span>',
                            default => '<span style="background:#F3F4F6;color:#6B7280;padding:2px 8px;border-radius:8px;font-size:11px;">' . e($b['status'] ?? '') . '</span>',
                        };
                        echo $bStatus;
                        ?>
                    </td>
                </tr>
                <?php endforeach; ?>
                <?php else: ?>
                <tr><td colspan="6" style="text-align:center;padding:30px;color:#6B7280;">لا توجد حجوزات</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<script>document.addEventListener('DOMContentLoaded', function() { if (typeof lucide !== 'undefined') lucide.createIcons(); });</script>
<?php $__template->endSection(); ?>
