<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>وحدات المنشأة: <?= e($facility->name_ar) ?><?php $__template->endSection(); ?>
<?php $__template->section('page_actions'); ?>
<div style="display:flex;gap:8px;">
    <?php if (can('sa.facility.manage')): ?>
    <a href="/sa/facilities/<?= (int) $facility->id ?>/units/create" class="btn btn-primary"><i data-lucide="plus" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> إضافة وحدة</a>
    <?php endif; ?>
    <a href="/sa/facilities/<?= (int) $facility->id ?>" class="btn btn-outline">رجوع للمنشأة</a>
</div>
<?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>

<div class="card" style="padding:10px 15px;margin-bottom:15px;background:#F9FAFB;">
    <span style="font-size:13px;color:#6B7280;">المنشأة:</span>
    <a href="/sa/facilities/<?= (int) $facility->id ?>" style="color:#0D7377;font-weight:600;margin-right:5px;"><?= e($facility->name_ar) ?></a>
    <span style="font-size:12px;color:#9CA3AF;">(<?= e($facility->code) ?>)</span>
</div>

<?php if (!empty($units)): ?>
<div class="card">
    <div class="table-responsive">
        <table class="data-table">
            <thead>
                <tr>
                    <th>الترتيب</th>
                    <th>الكود</th>
                    <th>الاسم</th>
                    <th>النوع</th>
                    <th>نمط الحجز</th>
                    <th>السعة القصوى</th>
                    <th>الحالة</th>
                    <th>إجراءات</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($units as $unit): ?>
                <?php $u = is_object($unit) ? $unit : (object) $unit; ?>
                <tr>
                    <td style="text-align:center;color:#6B7280;"><?= (int) $u->sort_order ?></td>
                    <td style="font-family:monospace;font-size:13px;"><?= e($u->code) ?></td>
                    <td>
                        <span style="font-weight:600;"><?= e($u->name_ar) ?></span>
                        <?php if (!empty($u->name_en)): ?>
                        <br><small style="color:#9CA3AF;" dir="ltr"><?= e($u->name_en) ?></small>
                        <?php endif; ?>
                    </td>
                    <td><?= e($unitTypeOptions[$u->unit_type] ?? $u->unit_type) ?></td>
                    <td>
                        <?php
                        $modeColor = $u->booking_mode === 'exclusive' ? '#7C3AED' : '#0284C7';
                        ?>
                        <span style="color:<?= $modeColor ?>;font-weight:600;"><?= e($bookingModes[$u->booking_mode] ?? $u->booking_mode) ?></span>
                    </td>
                    <td style="text-align:center;font-weight:600;"><?= (int) $u->max_capacity ?></td>
                    <td>
                        <?php if ($u->is_active): ?>
                        <span style="color:#059669;font-weight:600;">مفعّلة</span>
                        <?php else: ?>
                        <span style="color:#DC2626;font-weight:600;">معطّلة</span>
                        <?php endif; ?>
                    </td>
                    <td>
                        <?php if (can('sa.facility.manage')): ?>
                        <a href="/sa/facilities/<?= (int) $facility->id ?>/units/<?= (int) $u->id ?>/edit" class="btn btn-sm btn-outline">تعديل</a>
                        <?php endif; ?>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
<?php else: ?>
<div class="card" style="padding:60px 20px;text-align:center;">
    <h3 style="color:#6B7280;margin:0 0 8px;">لا توجد وحدات</h3>
    <p style="color:#9CA3AF;font-size:14px;margin:0 0 20px;">لم يتم إضافة وحدات لهذه المنشأة بعد.</p>
    <?php if (can('sa.facility.manage')): ?>
    <a href="/sa/facilities/<?= (int) $facility->id ?>/units/create" class="btn btn-primary"><i data-lucide="plus" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> إضافة وحدة</a>
    <?php endif; ?>
</div>
<?php endif; ?>

<?php $__template->endSection(); ?>
