<?php
use App\Modules\AcademyContracts\Models\AcademyContract;
$__template->layout('Layout.main');
?>
<?php $__template->section('title'); ?>عقود الأكاديميات<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/academy-settlements" class="btn btn-outline"><i data-lucide="calculator" style="width:15px;height:15px;vertical-align:middle;margin-left:4px;"></i> التسويات</a>
<?php if (can('academy_contract.manage')): ?>
<a href="/academy-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; ?>
<?php $__template->endSection(); ?>

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

<!-- Filters -->
<div class="card" style="margin-bottom:20px;padding:15px;">
    <form method="GET" action="/academy-contracts" style="display:flex;gap:10px;align-items:end;flex-wrap:wrap;">
        <div style="flex:1;min-width:180px;">
            <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:150px;">
            <label class="form-label" style="font-size:12px;">الحالة</label>
            <select name="status" class="form-input">
                <option value="">الكل</option>
                <?php foreach ($statuses as $key => $label): ?>
                <option value="<?= e($key) ?>" <?= ($filters['status'] ?? '') === $key ? 'selected' : '' ?>><?= e($label) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div style="min-width:150px;">
            <label class="form-label" style="font-size:12px;">الأكاديمية</label>
            <select name="academy_id" class="form-input">
                <option value="">الكل</option>
                <?php foreach ($academies as $acad): ?>
                <option value="<?= (int) $acad['id'] ?>" <?= ($filters['academy_id'] ?? '') == $acad['id'] ? 'selected' : '' ?>><?= e($acad['name_ar']) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div style="min-width:150px;">
            <label class="form-label" style="font-size:12px;">نوع العقد</label>
            <select name="contract_type" class="form-input">
                <option value="">الكل</option>
                <?php foreach ($contractTypes as $key => $label): ?>
                <option value="<?= e($key) ?>" <?= ($filters['contract_type'] ?? '') === $key ? 'selected' : '' ?>><?= e($label) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <button type="submit" class="btn btn-outline"><i data-lucide="search" style="width:16px;height:16px;vertical-align:middle;"></i> بحث</button>
        <a href="/academy-contracts" class="btn btn-sm btn-outline" style="color:#6B7280;">مسح</a>
    </form>
</div>

<!-- Contracts Table -->
<?php if (!empty($contracts)): ?>
<div class="card" style="overflow:hidden;">
    <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 15px;text-align:right;font-weight:600;color:#374151;">رقم العقد</th>
                    <th style="padding:12px 15px;text-align:right;font-weight:600;color:#374151;">الأكاديمية</th>
                    <th style="padding:12px 15px;text-align:right;font-weight:600;color:#374151;">نوع العقد</th>
                    <th style="padding:12px 15px;text-align:right;font-weight:600;color:#374151;">الفترة</th>
                    <th style="padding:12px 15px;text-align:right;font-weight:600;color:#374151;">الحد الأدنى</th>
                    <th style="padding:12px 15px;text-align:center;font-weight:600;color:#374151;">الحالة</th>
                    <th style="padding:12px 15px;text-align:center;font-weight:600;color:#374151;">إجراءات</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($contracts as $c):
                    $statusColor = AcademyContract::getStatusColor($c['status'] ?? '');
                    $statusLabel = AcademyContract::getStatusLabel($c['status'] ?? '');
                    $typeLabel   = AcademyContract::getContractTypeLabel($c['contract_type'] ?? '');
                ?>
                <tr style="border-bottom:1px solid #F3F4F6;">
                    <td style="padding:12px 15px;">
                        <code style="font-size:12px;background:#F3F4F6;padding:2px 8px;border-radius:4px;"><?= e($c['contract_number'] ?? '') ?></code>
                    </td>
                    <td style="padding:12px 15px;font-weight:500;"><?= e($c['academy_name'] ?? '') ?></td>
                    <td style="padding:12px 15px;"><?= e($typeLabel) ?></td>
                    <td style="padding:12px 15px;font-size:12px;color:#6B7280;">
                        <?= e($c['start_date'] ?? '') ?> - <?= e($c['end_date'] ?? '') ?>
                    </td>
                    <td style="padding:12px 15px;direction:ltr;text-align:right;">
                        <?= number_format((float) ($c['minimum_revenue_guarantee'] ?? 0), 2) ?> ج.م
                    </td>
                    <td style="padding:12px 15px;text-align:center;">
                        <span style="display:inline-block;padding:3px 10px;border-radius:10px;font-size:11px;font-weight:600;background:<?= $statusColor ?>15;color:<?= $statusColor ?>;">
                            <?= e($statusLabel) ?>
                        </span>
                    </td>
                    <td style="padding:12px 15px;text-align:center;">
                        <div style="display:flex;gap:6px;justify-content:center;">
                            <a href="/academy-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>
                            <a href="/academy-contracts/<?= (int) $c['id'] ?>/edit" class="btn btn-sm btn-outline" style="font-size:12px;padding:4px 10px;">
                                <i data-lucide="edit-3" style="width:13px;height:13px;vertical-align:middle;"></i> تعديل
                            </a>
                        </div>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</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['academy_id'])): ?>
            لا توجد نتائج مطابقة لبحثك. جرب تغيير معايير البحث.
        <?php else: ?>
            ابدأ بإنشاء عقد جديد لأكاديمية.
        <?php endif; ?>
    </p>
    <a href="/academy-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(); ?>
