<div class="d-flex justify-content-between align-items-center mb-4">
    <h3 class="mb-0"><?= e($facility['name_en']) ?> <small class="text-muted">(<?= e($facility['facility_code']) ?>)</small></h3>
    <div class="d-flex gap-2">
        <?php if (auth_can('facilities.manage')): ?>
        <a href="/facilities/<?= $facility['id'] ?>/edit" class="btn btn-warning"><i class="fas fa-edit me-1"></i><?= lang('common.edit') ?></a>
        <a href="/facilities/<?= $facility['id'] ?>/maintenance" class="btn btn-secondary"><i class="fas fa-tools me-1"></i><?= lang('facilities.maintenance') ?></a>
        <?php endif; ?>
        <?php if ($facility['is_bookable'] && auth_can('bookings.view')): ?>
        <a href="/bookings/calendar/<?= $facility['id'] ?>" class="btn btn-primary"><i class="fas fa-calendar me-1"></i><?= lang('facilities.booking_calendar') ?></a>
        <?php endif; ?>
    </div>
</div>

<div class="row g-3">
    <div class="col-md-8">
        <div class="card mb-3">
            <div class="card-header"><h6 class="mb-0"><?= lang('common.details') ?></h6></div>
            <div class="card-body">
                <div class="row g-2">
                    <div class="col-md-6"><strong><?= lang('facilities.type') ?>:</strong> <?= e($facility['type_name_en'] ?? '-') ?></div>
                    <div class="col-md-6"><strong><?= lang('common.status') ?>:</strong> <?= status_badge($facility['status']) ?></div>
                    <div class="col-md-6"><strong><?= lang('facilities.name') ?> (AR):</strong> <?= e($facility['name_ar']) ?></div>
                    <div class="col-md-6"><strong><?= lang('facilities.location') ?>:</strong> <?= e($facility['location'] ?? '-') ?></div>
                    <div class="col-md-4"><strong><?= lang('facilities.capacity') ?>:</strong> <?= $facility['capacity'] ? number_format($facility['capacity']) : '-' ?></div>
                    <div class="col-md-4"><strong><?= lang('facilities.area_sqm') ?>:</strong> <?= $facility['area_sqm'] ? number_format($facility['area_sqm'], 2) . ' m²' : '-' ?></div>
                    <div class="col-md-4"><strong><?= lang('facilities.indoor') ?>:</strong> <?= $facility['is_indoor'] ? '✓ Yes' : '✗ No' ?></div>
                    <div class="col-md-4"><strong><?= lang('facilities.bookable') ?>:</strong> <?= $facility['is_bookable'] ? '✓ Yes' : '✗ No' ?></div>
                    <div class="col-md-4"><strong><?= lang('facilities.fee_members') ?>:</strong> <?= format_money($facility['booking_fee_members']) ?>/hr</div>
                    <div class="col-md-4"><strong><?= lang('facilities.operating_start') ?>:</strong> <?= e($facility['operating_hours_start'] ?? '-') ?> - <?= e($facility['operating_hours_end'] ?? '-') ?></div>
                </div>
            </div>
        </div>

        <?php if (!empty($amenities)): ?>
        <div class="card mb-3">
            <div class="card-header"><h6 class="mb-0"><?= lang('facilities.amenities') ?></h6></div>
            <div class="card-body">
                <div class="d-flex flex-wrap gap-2">
                    <?php foreach ($amenities as $a): ?>
                    <span class="badge <?= $a['is_available'] ? 'bg-success' : 'bg-secondary' ?>">
                        <?= e($a['amenity_name_en']) ?>
                    </span>
                    <?php endforeach; ?>
                </div>
            </div>
        </div>
        <?php endif; ?>

        <div class="card">
            <div class="card-header"><h6 class="mb-0"><?= lang('facilities.upcoming_bookings') ?></h6></div>
            <div class="card-body p-0">
                <?php if (empty($recentBookings)): ?>
                <div class="text-center text-muted py-4"><?= lang('common.no_data') ?></div>
                <?php else: ?>
                <div class="table-responsive">
                    <table class="table table-sm table-hover mb-0">
                        <thead><tr><th>Date</th><th>Time</th><th>Member</th><th>Status</th></tr></thead>
                        <tbody>
                            <?php foreach ($recentBookings as $b): ?>
                            <tr>
                                <td><?= e($b['booking_date']) ?></td>
                                <td><?= e($b['start_time']) ?> - <?= e($b['end_time']) ?></td>
                                <td><?= e($b['member_name'] ?? '-') ?> <small class="text-muted"><?= e($b['membership_number'] ?? '') ?></small></td>
                                <td><?= status_badge($b['status']) ?></td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
                <?php endif; ?>
            </div>
        </div>
    </div>

    <div class="col-md-4">
        <?php if (!empty($timeSlots)): ?>
        <div class="card mb-3">
            <div class="card-header"><h6 class="mb-0"><?= lang('facilities.time_slots') ?></h6></div>
            <div class="card-body p-0">
                <?php
                $dayNames = ['','Mon','Tue','Wed','Thu','Fri','Sat','Sun'];
                ?>
                <table class="table table-sm mb-0">
                    <thead><tr><th>Day</th><th>Time</th><th>Type</th></tr></thead>
                    <tbody>
                        <?php foreach ($timeSlots as $ts): ?>
                        <tr>
                            <td><?= $dayNames[$ts['day_of_week']] ?? $ts['day_of_week'] ?></td>
                            <td><?= e($ts['start_time']) ?>-<?= e($ts['end_time']) ?></td>
                            <td><span class="badge bg-secondary"><?= e($ts['slot_type']) ?></span></td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
        <?php endif; ?>

        <?php if (!empty($maintenanceRecords)): ?>
        <div class="card">
            <div class="card-header"><h6 class="mb-0"><?= lang('facilities.recent_maintenance') ?></h6></div>
            <div class="card-body p-0">
                <div class="list-group list-group-flush">
                    <?php foreach ($maintenanceRecords as $mr): ?>
                    <div class="list-group-item">
                        <div class="d-flex justify-content-between">
                            <strong><?= e($mr['title']) ?></strong>
                            <?= status_badge($mr['status']) ?>
                        </div>
                        <small class="text-muted"><?= e($mr['start_date']) ?> • <?= e(ucfirst($mr['maintenance_type'])) ?></small>
                    </div>
                    <?php endforeach; ?>
                </div>
            </div>
        </div>
        <?php endif; ?>
    </div>
</div>