<?php
use App\Modules\Rentals\Models\RentalEntity;
use App\Modules\Rentals\Models\RentalContract;
$__template->layout('Layout.main');
?>
<?php $__template->section('title'); ?><?= e($entity->name_ar) ?><?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<?php if (can('rental.manage_entity')): ?>
<a href="/rentals/entities/<?= (int) $entity->id ?>/edit" class="btn btn-outline"><i data-lucide="edit" 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="arrow-right" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> العودة للقائمة</a>
<?php $__template->endSection(); ?>

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

<?php
$entStatus = $entity->status ?? 'pending';
$entType   = $entity->entity_type ?? '';
$typeLabel = RentalEntity::getEntityTypes()[$entType] ?? $entType;
?>

<!-- Header Card -->
<div class="card" style="margin-bottom:20px;padding:20px;">
    <div style="display:flex;align-items:center;justify-content:space-between;flex-wrap:wrap;gap:10px;">
        <div>
            <h2 style="margin:0 0 8px;color:#1A1A2E;"><?= e($entity->name_ar) ?></h2>
            <?php if (!empty($entity->name_en)): ?>
            <div style="font-size:14px;color:#6B7280;margin-bottom:8px;"><?= e($entity->name_en) ?></div>
            <?php endif; ?>
        </div>
        <div style="display:flex;gap:8px;align-items:center;">
            <span class="badge" style="background:#0D737715;color:#0D7377;font-size:12px;padding:4px 12px;border-radius:10px;font-weight:600;">
                <?= e($typeLabel) ?>
            </span>
            <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>
        </div>
    </div>
</div>

<!-- Info Grid -->
<div style="display:grid;grid-template-columns:1fr 1fr;gap:20px;margin-bottom:20px;">
    <div class="card" style="padding:20px;">
        <h4 style="color:#0D7377;margin:0 0 15px;">بيانات الاتصال</h4>
        <table style="width:100%;font-size:14px;">
            <tr><td style="padding:8px 0;color:#6B7280;width:40%;">جهة الاتصال</td><td style="padding:8px 0;font-weight:600;"><?= e($entity->contact_person ?? '—') ?></td></tr>
            <tr><td style="padding:8px 0;color:#6B7280;">الهاتف</td><td style="padding:8px 0;direction:ltr;text-align:right;"><?= e($entity->contact_phone ?? '—') ?></td></tr>
            <tr><td style="padding:8px 0;color:#6B7280;">البريد الإلكتروني</td><td style="padding:8px 0;direction:ltr;text-align:right;"><?= e($entity->contact_email ?? '—') ?></td></tr>
            <tr><td style="padding:8px 0;color:#6B7280;">العنوان</td><td style="padding:8px 0;"><?= e($entity->address ?? '—') ?></td></tr>
        </table>
    </div>
    <div class="card" style="padding:20px;">
        <h4 style="color:#0D7377;margin:0 0 15px;">بيانات إدارية</h4>
        <table style="width:100%;font-size:14px;">
            <tr><td style="padding:8px 0;color:#6B7280;width:40%;">الرقم الضريبي</td><td style="padding:8px 0;direction:ltr;text-align:right;"><?= e($entity->tax_number ?? '—') ?></td></tr>
            <tr><td style="padding:8px 0;color:#6B7280;">الخطاب الرسمي</td><td style="padding:8px 0;"><?= $entity->official_letter_received ? '<span style="color:#059669;font-weight:600;">نعم</span>' : '<span style="color:#DC2626;">لا</span>' ?></td></tr>
            <tr><td style="padding:8px 0;color:#6B7280;">اعتماد مجلس الإدارة</td><td style="padding:8px 0;"><?= $entity->board_approved ? '<span style="color:#059669;font-weight:600;">نعم</span>' : '<span style="color:#DC2626;">لا</span>' ?></td></tr>
            <tr><td style="padding:8px 0;color:#6B7280;">تاريخ الاعتماد</td><td style="padding:8px 0;"><?= e($entity->approval_date ?? '—') ?></td></tr>
        </table>
        <?php if (!empty($entity->notes)): ?>
        <div style="margin-top:15px;padding-top:12px;border-top:1px solid #E5E7EB;">
            <h4 style="color:#6B7280;font-size:13px;margin:0 0 6px;">ملاحظات</h4>
            <p style="font-size:14px;margin:0;color:#374151;"><?= nl2br(e($entity->notes)) ?></p>
        </div>
        <?php endif; ?>
    </div>
</div>

<!-- Contracts Section -->
<div class="card" style="padding:0;">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;display:flex;justify-content:space-between;align-items:center;">
        <h3 style="margin:0;color:#1A1A2E;"><i data-lucide="file-text" style="width:18px;height:18px;vertical-align:middle;margin-left:6px;"></i> العقود</h3>
        <a href="/rentals/contracts/create" class="btn btn-sm btn-primary"><i data-lucide="plus" style="width:14px;height:14px;vertical-align:middle;margin-left:4px;"></i> عقد جديد</a>
    </div>
    <?php if (!empty($contracts)): ?>
    <div style="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 ($contracts as $c):
                    $cStatus = $c['status'] ?? 'draft';
                ?>
                <tr style="border-bottom:1px solid #F3F4F6;">
                    <td style="padding:12px 16px;">
                        <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;direction:ltr;text-align:right;"><?= e($c['start_date'] ?? '') ?></td>
                    <td style="padding:12px 16px;direction:ltr;text-align:right;"><?= e($c['end_date'] ?? '') ?></td>
                    <td style="padding:12px 16px;font-weight:600;"><?= money((float) ($c['total_amount'] ?? 0)) ?></td>
                    <td style="padding:12px 16px;text-align:center;">
                        <span class="badge" style="background:<?= RentalContract::getStatusColor($cStatus) ?>15;color:<?= RentalContract::getStatusColor($cStatus) ?>;font-size:12px;padding:4px 12px;border-radius:10px;font-weight:600;">
                            <?= e(RentalContract::getStatusLabel($cStatus)) ?>
                        </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 else: ?>
    <div style="padding:40px 20px;text-align:center;">
        <i data-lucide="file-x" style="width:40px;height:40px;color:#D1D5DB;"></i>
        <p style="color:#9CA3AF;font-size:14px;margin:10px 0 0;">لا توجد عقود</p>
    </div>
    <?php endif; ?>
</div>

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