<?php
use App\Modules\Rentals\Models\RentalEntity;
$__template->layout('Layout.main');
?>
<?php $__template->section('title'); ?>الجهات المستأجرة<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<?php if (can('rental.manage_entity')): ?>
<a href="/rentals/entities/create" class="btn btn-primary"><i data-lucide="plus" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> جهة جديدة</a>
<?php endif; ?>
<?php $__template->endSection(); ?>

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

<?php
$currentStatus = $filters['status'] ?? '';
$allStatuses = RentalEntity::getStatuses();
?>

<!-- Status Filter Tabs -->
<div class="card" style="margin-bottom:20px;padding:0;">
    <div style="display:flex;align-items:center;gap:0;overflow-x:auto;border-bottom:2px solid #E5E7EB;">
        <a href="/rentals/entities?<?= http_build_query(array_merge($filters, ['status' => '', 'page' => 1])) ?>"
           style="padding:12px 20px;font-size:14px;font-weight:600;text-decoration:none;border-bottom:3px solid <?= $currentStatus === '' ? '#0D7377' : 'transparent' ?>;color:<?= $currentStatus === '' ? '#0D7377' : '#6B7280' ?>;white-space:nowrap;">
            الكل
        </a>
        <?php foreach ($allStatuses as $sKey => $sLabel): ?>
        <a href="/rentals/entities?<?= http_build_query(array_merge($filters, ['status' => $sKey, 'page' => 1])) ?>"
           style="padding:12px 20px;font-size:14px;font-weight:600;text-decoration:none;border-bottom:3px solid <?= $currentStatus === $sKey ? '#0D7377' : 'transparent' ?>;color:<?= $currentStatus === $sKey ? '#0D7377' : '#6B7280' ?>;white-space:nowrap;">
            <?= e($sLabel) ?>
        </a>
        <?php endforeach; ?>
    </div>
</div>

<!-- Search & Filters -->
<div class="card" style="margin-bottom:20px;padding:15px;">
    <form method="GET" action="/rentals/entities" style="display:flex;flex-wrap:wrap;gap:10px;align-items:end;">
        <div style="flex:1;min-width:200px;">
            <label class="form-label" style="font-size:12px;">بحث</label>
            <input type="text" name="q" value="<?= e($filters['q'] ?? '') ?>" placeholder="اسم الجهة، جهة الاتصال، الهاتف..." class="form-input">
        </div>
        <div style="min-width:160px;">
            <label class="form-label" style="font-size:12px;">نوع الجهة</label>
            <select name="entity_type" class="form-input">
                <option value="">الكل</option>
                <?php foreach ($entityTypes as $tKey => $tLabel): ?>
                <option value="<?= e($tKey) ?>" <?= ($filters['entity_type'] ?? '') === $tKey ? 'selected' : '' ?>><?= e($tLabel) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <?php if ($currentStatus !== ''): ?>
        <input type="hidden" name="status" value="<?= e($currentStatus) ?>">
        <?php endif; ?>
        <button type="submit" class="btn btn-outline"><i data-lucide="search" style="width:16px;height:16px;vertical-align:middle;"></i> بحث</button>
        <a href="/rentals/entities" class="btn btn-sm btn-outline" style="color:#6B7280;">مسح</a>
    </form>
</div>

<!-- Entities Table -->
<?php if (!empty($entities)): ?>
<div class="card" style="padding:0;overflow-x:auto;">
    <table style="width:100%;border-collapse:collapse;font-size:14px;">
        <thead>
            <tr style="background:#F9FAFB;border-bottom:2px solid #E5E7EB;">
                <th style="padding:12px 16px;text-align:right;font-weight:600;color:#374151;">الاسم</th>
                <th style="padding:12px 16px;text-align:right;font-weight:600;color:#374151;">نوع الجهة</th>
                <th style="padding:12px 16px;text-align:right;font-weight:600;color:#374151;">جهة الاتصال</th>
                <th style="padding:12px 16px;text-align:right;font-weight:600;color:#374151;">الهاتف</th>
                <th style="padding:12px 16px;text-align:center;font-weight:600;color:#374151;">الحالة</th>
                <th style="padding:12px 16px;text-align:center;font-weight:600;color:#374151;">إجراءات</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($entities as $ent):
                $entStatus = $ent['status'] ?? 'pending';
                $entType   = $ent['entity_type'] ?? '';
                $typeLabel = $entityTypes[$entType] ?? $entType;
            ?>
            <tr style="border-bottom:1px solid #F3F4F6;">
                <td style="padding:12px 16px;font-weight:600;">
                    <a href="/rentals/entities/<?= (int) $ent['id'] ?>" style="color:#0D7377;text-decoration:none;"><?= e($ent['name_ar'] ?? '') ?></a>
                </td>
                <td style="padding:12px 16px;"><?= e($typeLabel) ?></td>
                <td style="padding:12px 16px;"><?= e($ent['contact_person'] ?? '—') ?></td>
                <td style="padding:12px 16px;direction:ltr;text-align:right;"><?= e($ent['contact_phone'] ?? '—') ?></td>
                <td style="padding:12px 16px;text-align:center;">
                    <span class="badge" style="background:<?= RentalEntity::getStatusColor($entStatus) ?>15;color:<?= RentalEntity::getStatusColor($entStatus) ?>;font-size:12px;padding:4px 12px;border-radius:10px;font-weight:600;">
                        <?= e(RentalEntity::getStatusLabel($entStatus)) ?>
                    </span>
                </td>
                <td style="padding:12px 16px;text-align:center;">
                    <a href="/rentals/entities/<?= (int) $ent['id'] ?>" class="btn btn-sm btn-outline" style="font-size:12px;padding:4px 10px;">
                        <i data-lucide="eye" style="width:13px;height:13px;vertical-align:middle;"></i> عرض
                    </a>
                </td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
</div>

<?php if (isset($pagination) && ($pagination['last_page'] ?? 1) > 1): ?>
<div style="padding:15px;">
    <?php $__template->include('Shared.Components.pagination', ['pagination' => $pagination]); ?>
</div>
<?php endif; ?>

<?php else: ?>
<div class="card" style="padding:60px 20px;text-align:center;">
    <div style="margin-bottom:15px;">
        <i data-lucide="building-2" style="width:48px;height:48px;color:#D1D5DB;"></i>
    </div>
    <h3 style="color:#6B7280;margin:0 0 8px;">لا توجد جهات مستأجرة</h3>
    <p style="color:#9CA3AF;font-size:14px;margin:0 0 20px;">
        <?php if (!empty($filters['q']) || !empty($filters['entity_type']) || !empty($filters['status'])): ?>
            لا توجد نتائج مطابقة لبحثك. جرب تغيير معايير البحث.
        <?php else: ?>
            ابدأ بإضافة جهة مستأجرة جديدة.
        <?php endif; ?>
    </p>
    <a href="/rentals/entities/create" class="btn btn-primary"><i data-lucide="plus" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> جهة جديدة</a>
</div>
<?php endif; ?>

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