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

<?php $__template->section('page_actions'); ?>
<?php if (can('rental.manage_contract')): ?>
<a href="/rentals/contracts/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="/rentals/entities" class="btn btn-outline" style="margin-right:8px;"><i data-lucide="building-2" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> الجهات المستأجرة</a>
<?php $__template->endSection(); ?>

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

<?php
$currentStatus = $filters['status'] ?? '';
$allStatuses = RentalContract::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"
           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?status=<?= e($sKey) ?><?= ($filters['q'] ?? '') !== '' ? '&q=' . urlencode($filters['q']) : '' ?><?= ($filters['entity_id'] ?? '') !== '' ? '&entity_id=' . urlencode($filters['entity_id']) : '' ?><?= ($filters['facility_id'] ?? '') !== '' ? '&facility_id=' . urlencode($filters['facility_id']) : '' ?>"
           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 Bar -->
<div class="card" style="margin-bottom:20px;padding:15px;">
    <form method="GET" action="/rentals" style="display:flex;gap:10px;align-items:end;flex-wrap:wrap;">
        <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:180px;">
            <label class="form-label" style="font-size:12px;">الجهة المستأجرة</label>
            <select name="entity_id" class="form-input">
                <option value="">الكل</option>
                <?php foreach ($entities as $ent): ?>
                <option value="<?= (int) $ent['id'] ?>" <?= ($filters['entity_id'] ?? '') == $ent['id'] ? 'selected' : '' ?>><?= e($ent['name_ar']) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div style="min-width:180px;">
            <label class="form-label" style="font-size:12px;">المرفق</label>
            <select name="facility_id" class="form-input">
                <option value="">الكل</option>
                <?php foreach ($facilities as $fac): ?>
                <option value="<?= (int) $fac['id'] ?>" <?= ($filters['facility_id'] ?? '') == $fac['id'] ? 'selected' : '' ?>><?= e($fac['name_ar']) ?></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" class="btn btn-sm btn-outline" style="color:#6B7280;">مسح</a>
    </form>
</div>

<!-- Contracts Table -->
<?php if (!empty($contracts)): ?>
<div class="card" style="padding:0;overflow-x:auto;">
    <table class="table" style="width:100%;border-collapse:collapse;">
        <thead>
            <tr style="background:#F9FAFB;">
                <th style="padding:12px 16px;font-size:13px;font-weight:600;color:#374151;text-align:right;white-space:nowrap;">رقم العقد</th>
                <th style="padding:12px 16px;font-size:13px;font-weight:600;color:#374151;text-align:right;white-space:nowrap;">الجهة المستأجرة</th>
                <th style="padding:12px 16px;font-size:13px;font-weight:600;color:#374151;text-align:right;white-space:nowrap;">المرفق</th>
                <th style="padding:12px 16px;font-size:13px;font-weight:600;color:#374151;text-align:right;white-space:nowrap;">من</th>
                <th style="padding:12px 16px;font-size:13px;font-weight:600;color:#374151;text-align:right;white-space:nowrap;">إلى</th>
                <th style="padding:12px 16px;font-size:13px;font-weight:600;color:#374151;text-align:right;white-space:nowrap;">الإجمالي</th>
                <th style="padding:12px 16px;font-size:13px;font-weight:600;color:#374151;text-align:center;white-space:nowrap;">التأمين</th>
                <th style="padding:12px 16px;font-size:13px;font-weight:600;color:#374151;text-align:center;white-space:nowrap;">الحالة</th>
                <th style="padding:12px 16px;font-size:13px;font-weight:600;color:#374151;text-align:center;white-space:nowrap;">إجراءات</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($contracts as $c):
                $contractStatus = $c['status'] ?? 'draft';
                $depositStatus  = $c['deposit_status'] ?? 'pending';
                $entityName     = $entityMap[(int) ($c['entity_id'] ?? 0)] ?? '-';
                $facilityName   = $facilityMap[(int) ($c['facility_id'] ?? 0)] ?? '-';
            ?>
            <tr style="border-bottom:1px solid #F3F4F6;">
                <td style="padding:12px 16px;font-size:13px;">
                    <a href="/rentals/contracts/<?= (int) $c['id'] ?>" style="color:#0D7377;font-weight:600;text-decoration:none;">
                        <?= e($c['contract_number'] ?? '') ?>
                    </a>
                </td>
                <td style="padding:12px 16px;font-size:13px;"><?= e($entityName) ?></td>
                <td style="padding:12px 16px;font-size:13px;"><?= e($facilityName) ?></td>
                <td style="padding:12px 16px;font-size:13px;direction:ltr;text-align:right;"><?= e($c['start_date'] ?? '') ?></td>
                <td style="padding:12px 16px;font-size:13px;direction:ltr;text-align:right;"><?= e($c['end_date'] ?? '') ?></td>
                <td style="padding:12px 16px;font-size:13px;font-weight:600;"><?= money((float) ($c['total_amount'] ?? 0)) ?></td>
                <td style="padding:12px 16px;text-align:center;">
                    <span class="badge" style="background:<?= RentalContract::getDepositStatusColor($depositStatus) ?>15;color:<?= RentalContract::getDepositStatusColor($depositStatus) ?>;font-size:11px;padding:3px 10px;border-radius:10px;">
                        <?= e(RentalContract::getDepositStatusLabel($depositStatus)) ?>
                    </span>
                </td>
                <td style="padding:12px 16px;text-align:center;">
                    <span class="badge" style="background:<?= RentalContract::getStatusColor($contractStatus) ?>15;color:<?= RentalContract::getStatusColor($contractStatus) ?>;font-size:11px;padding:3px 10px;border-radius:10px;">
                        <?= e(RentalContract::getStatusLabel($contractStatus)) ?>
                    </span>
                </td>
                <td style="padding:12px 16px;text-align:center;">
                    <a href="/rentals/contracts/<?= (int) $c['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="file-text" 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['status']) || !empty($filters['entity_id']) || !empty($filters['facility_id'])): ?>
            لا توجد نتائج مطابقة لبحثك. جرب تغيير معايير البحث.
        <?php else: ?>
            ابدأ بإنشاء عقد تأجير جديد.
        <?php endif; ?>
    </p>
    <a href="/rentals/contracts/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(); ?>
