<?php
use App\Modules\AcademyContracts\Models\AcademyContract;
use App\Modules\AcademyContracts\Models\AcademySettlement;
$__template->layout('Layout.main');
?>
<?php $__template->section('title'); ?>عقد <?= e($contract->contract_number) ?><?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<?php if (in_array($contract->status, ['draft', 'pending_approval'])): ?>
<a href="/academy-contracts/<?= (int) $contract->id ?>/edit" class="btn btn-outline"><i data-lucide="edit-3" style="width:15px;height:15px;vertical-align:middle;margin-left:4px;"></i> تعديل</a>
<?php endif; ?>
<a href="/academy-contracts" class="btn btn-outline"><i data-lucide="arrow-right" style="width:15px;height:15px;vertical-align:middle;margin-left:4px;"></i> العودة للقائمة</a>
<?php $__template->endSection(); ?>

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

<?php
$statusColor = AcademyContract::getStatusColor($contract->status ?? '');
$statusLabel = AcademyContract::getStatusLabel($contract->status ?? '');
$typeLabel   = AcademyContract::getContractTypeLabel($contract->contract_type ?? '');
?>

<!-- Header Card -->
<div class="card" style="margin-bottom:20px;overflow:hidden;">
    <div style="padding:25px;display:flex;align-items:start;gap:20px;">
        <div style="width:72px;height:72px;border-radius:16px;background:linear-gradient(135deg, #0D737715, #0D737730);display:flex;align-items:center;justify-content:center;flex-shrink:0;">
            <i data-lucide="file-signature" style="width:36px;height:36px;color:#0D7377;"></i>
        </div>
        <div style="flex:1;">
            <div style="display:flex;align-items:center;gap:12px;margin-bottom:8px;flex-wrap:wrap;">
                <h2 style="margin:0;font-size:22px;font-weight:700;color:#1A1A2E;"><?= e($contract->contract_number) ?></h2>
                <span style="display:inline-block;padding:4px 12px;border-radius:12px;font-size:12px;font-weight:600;background:<?= $statusColor ?>15;color:<?= $statusColor ?>;">
                    <?= e($statusLabel) ?>
                </span>
            </div>
            <div style="display:flex;gap:15px;flex-wrap:wrap;font-size:13px;color:#6B7280;margin-top:8px;">
                <?php if ($academy): ?>
                <span style="display:inline-flex;align-items:center;gap:4px;">
                    <i data-lucide="graduation-cap" style="width:14px;height:14px;"></i>
                    <?= e($academy->name_ar) ?>
                </span>
                <?php endif; ?>
                <span style="display:inline-flex;align-items:center;gap:4px;">
                    <i data-lucide="briefcase" style="width:14px;height:14px;"></i>
                    <?= e($typeLabel) ?>
                </span>
                <span style="display:inline-flex;align-items:center;gap:4px;">
                    <i data-lucide="calendar" style="width:14px;height:14px;"></i>
                    <?= e($contract->start_date) ?> - <?= e($contract->end_date) ?>
                </span>
            </div>
        </div>
        <div style="flex-shrink:0;display:flex;gap:8px;">
            <?php if ($contract->status === 'draft' || $contract->status === 'pending_approval'): ?>
            <form method="POST" action="/academy-contracts/<?= (int) $contract->id ?>/activate" style="display:inline;">
                <?= csrf_field() ?>
                <button type="submit" class="btn btn-primary" onclick="return confirm('هل أنت متأكد من تفعيل هذا العقد؟')">
                    <i data-lucide="check-circle" style="width:15px;height:15px;vertical-align:middle;margin-left:4px;"></i> تفعيل
                </button>
            </form>
            <?php endif; ?>
            <?php if ($contract->status === 'active'): ?>
            <button type="button" class="btn btn-outline" style="color:#DC2626;border-color:#DC2626;" onclick="document.getElementById('terminateModal').style.display='flex'">
                <i data-lucide="x-circle" style="width:15px;height:15px;vertical-align:middle;margin-left:4px;"></i> إنهاء العقد
            </button>
            <?php endif; ?>
        </div>
    </div>
</div>

<!-- Financial Details -->
<div style="display:grid;grid-template-columns:1fr 1fr;gap:20px;margin-bottom:20px;">
    <div class="card">
        <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;">
            <h3 style="margin:0;font-size:14px;color:#374151;"><i data-lucide="banknote" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;color:#D97706;"></i> الشروط المالية</h3>
        </div>
        <div style="padding:20px;">
            <table style="width:100%;font-size:13px;">
                <tr style="border-bottom:1px solid #F3F4F6;">
                    <td style="padding:8px 0;color:#6B7280;">الحد الأدنى المضمون</td>
                    <td style="padding:8px 0;font-weight:600;text-align:left;direction:ltr;"><?= number_format((float) $contract->minimum_revenue_guarantee, 2) ?> ج.م</td>
                </tr>
                <tr style="border-bottom:1px solid #F3F4F6;">
                    <td style="padding:8px 0;color:#6B7280;">نسبة عمولة النادي</td>
                    <td style="padding:8px 0;font-weight:600;text-align:left;direction:ltr;"><?= number_format((float) $contract->club_commission_pct, 2) ?>%</td>
                </tr>
                <tr style="border-bottom:1px solid #F3F4F6;">
                    <td style="padding:8px 0;color:#6B7280;">نسبة حصة الأكاديمية</td>
                    <td style="padding:8px 0;font-weight:600;text-align:left;direction:ltr;"><?= number_format((float) $contract->academy_share_pct, 2) ?>%</td>
                </tr>
                <tr style="border-bottom:1px solid #F3F4F6;">
                    <td style="padding:8px 0;color:#6B7280;">الإيجار الشهري</td>
                    <td style="padding:8px 0;font-weight:600;text-align:left;direction:ltr;"><?= number_format((float) $contract->fixed_monthly_rent, 2) ?> ج.م</td>
                </tr>
                <tr style="border-bottom:1px solid #F3F4F6;">
                    <td style="padding:8px 0;color:#6B7280;">مبلغ التأمين</td>
                    <td style="padding:8px 0;font-weight:600;text-align:left;direction:ltr;"><?= number_format((float) $contract->deposit_amount, 2) ?> ج.م</td>
                </tr>
                <tr>
                    <td style="padding:8px 0;color:#6B7280;">حالة التأمين</td>
                    <td style="padding:8px 0;font-weight:600;"><?= e($contract->deposit_status ?? '-') ?></td>
                </tr>
            </table>
        </div>
    </div>

    <div class="card">
        <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;">
            <h3 style="margin:0;font-size:14px;color:#374151;"><i data-lucide="settings" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;color:#7C3AED;"></i> إعدادات التسوية</h3>
        </div>
        <div style="padding:20px;">
            <table style="width:100%;font-size:13px;">
                <tr style="border-bottom:1px solid #F3F4F6;">
                    <td style="padding:8px 0;color:#6B7280;">يوم التسوية</td>
                    <td style="padding:8px 0;font-weight:600;"><?= (int) $contract->settlement_day ?> من كل شهر</td>
                </tr>
                <tr style="border-bottom:1px solid #F3F4F6;">
                    <td style="padding:8px 0;color:#6B7280;">فترة السماح</td>
                    <td style="padding:8px 0;font-weight:600;"><?= (int) $contract->grace_period_days ?> يوم</td>
                </tr>
                <tr style="border-bottom:1px solid #F3F4F6;">
                    <td style="padding:8px 0;color:#6B7280;">نسبة الغرامة</td>
                    <td style="padding:8px 0;font-weight:600;text-align:left;direction:ltr;"><?= number_format((float) $contract->penalty_rate_pct, 2) ?>%</td>
                </tr>
                <tr style="border-bottom:1px solid #F3F4F6;">
                    <td style="padding:8px 0;color:#6B7280;">تجديد تلقائي</td>
                    <td style="padding:8px 0;font-weight:600;"><?= (int) $contract->auto_renew ? 'نعم' : 'لا' ?></td>
                </tr>
                <tr>
                    <td style="padding:8px 0;color:#6B7280;">مهلة التجديد</td>
                    <td style="padding:8px 0;font-weight:600;"><?= (int) $contract->renewal_notice_days ?> يوم</td>
                </tr>
            </table>
        </div>
    </div>
</div>

<!-- Notes -->
<?php if (!empty($contract->notes)): ?>
<div class="card" style="margin-bottom:20px;">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;">
        <h3 style="margin:0;font-size:14px;color:#374151;"><i data-lucide="message-square" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;color:#6B7280;"></i> ملاحظات</h3>
    </div>
    <div style="padding:20px;font-size:14px;color:#4B5563;line-height:1.7;">
        <?= e($contract->notes) ?>
    </div>
</div>
<?php endif; ?>

<!-- Settlement History -->
<div class="card" style="margin-bottom:20px;">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;display:flex;justify-content:space-between;align-items:center;">
        <h3 style="margin:0;font-size:14px;color:#374151;"><i data-lucide="calculator" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;color:#059669;"></i> سجل التسويات</h3>
        <a href="/academy-settlements?academy_id=<?= (int) $contract->academy_id ?>" class="btn btn-sm btn-outline" style="font-size:12px;">عرض الكل</a>
    </div>
    <?php if (!empty($settlements)): ?>
    <div style="overflow-x:auto;">
        <table style="width:100%;border-collapse:collapse;font-size:13px;">
            <thead>
                <tr style="background:#F9FAFB;border-bottom:1px solid #E5E7EB;">
                    <th style="padding:10px 15px;text-align:right;font-weight:600;color:#6B7280;">الشهر</th>
                    <th style="padding:10px 15px;text-align:right;font-weight:600;color:#6B7280;">الإيرادات</th>
                    <th style="padding:10px 15px;text-align:right;font-weight:600;color:#6B7280;">صافي المبلغ</th>
                    <th style="padding:10px 15px;text-align:center;font-weight:600;color:#6B7280;">الاتجاه</th>
                    <th style="padding:10px 15px;text-align:center;font-weight:600;color:#6B7280;">الحالة</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($settlements as $s):
                    $sStatusColor = AcademySettlement::getStatusColor($s['status'] ?? '');
                    $sStatusLabel = AcademySettlement::getStatusLabel($s['status'] ?? '');
                    $dirColor     = AcademySettlement::getDirectionColor($s['direction'] ?? '');
                    $dirLabel     = AcademySettlement::getDirectionLabel($s['direction'] ?? '');
                ?>
                <tr style="border-bottom:1px solid #F3F4F6;">
                    <td style="padding:10px 15px;">
                        <a href="/academy-settlements/<?= (int) $s['id'] ?>" style="color:#0D7377;text-decoration:none;font-weight:500;"><?= e($s['period_month']) ?></a>
                    </td>
                    <td style="padding:10px 15px;direction:ltr;text-align:right;"><?= number_format((float) ($s['total_revenue'] ?? 0), 2) ?> ج.م</td>
                    <td style="padding:10px 15px;direction:ltr;text-align:right;font-weight:600;"><?= number_format((float) ($s['net_amount'] ?? 0), 2) ?> ج.م</td>
                    <td style="padding:10px 15px;text-align:center;">
                        <span style="font-size:11px;font-weight:600;color:<?= $dirColor ?>;"><?= e($dirLabel) ?></span>
                    </td>
                    <td style="padding:10px 15px;text-align:center;">
                        <span style="display:inline-block;padding:2px 8px;border-radius:8px;font-size:11px;font-weight:600;background:<?= $sStatusColor ?>15;color:<?= $sStatusColor ?>;">
                            <?= e($sStatusLabel) ?>
                        </span>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php else: ?>
    <div style="padding:40px 20px;text-align:center;color:#9CA3AF;font-size:14px;">
        لا توجد تسويات لهذا العقد بعد
    </div>
    <?php endif; ?>
</div>

<!-- Terminate Modal -->
<?php if ($contract->status === 'active'): ?>
<div id="terminateModal" style="display:none;position:fixed;inset:0;z-index:9999;align-items:center;justify-content:center;background:rgba(0,0,0,0.5);">
    <div class="card" style="width:100%;max-width:500px;margin:20px;">
        <div style="padding:20px;border-bottom:1px solid #E5E7EB;">
            <h3 style="margin:0;color:#991B1B;font-size:16px;"><i data-lucide="alert-triangle" style="width:18px;height:18px;vertical-align:middle;margin-left:4px;"></i> إنهاء العقد</h3>
        </div>
        <form method="POST" action="/academy-contracts/<?= (int) $contract->id ?>/terminate">
            <?= csrf_field() ?>
            <div style="padding:20px;">
                <p style="color:#6B7280;font-size:14px;margin:0 0 15px;">هل أنت متأكد من إنهاء هذا العقد؟ هذا الإجراء لا يمكن التراجع عنه.</p>
                <div class="form-group">
                    <label class="form-label">سبب الإنهاء <span style="color:#DC2626;">*</span></label>
                    <textarea name="termination_reason" class="form-input" rows="3" required placeholder="اذكر سبب إنهاء العقد..."></textarea>
                </div>
            </div>
            <div style="padding:15px 20px;border-top:1px solid #E5E7EB;display:flex;gap:10px;justify-content:flex-end;">
                <button type="button" class="btn btn-outline" onclick="document.getElementById('terminateModal').style.display='none'">إلغاء</button>
                <button type="submit" class="btn btn-primary" style="background:#991B1B;border-color:#991B1B;">تأكيد الإنهاء</button>
            </div>
        </form>
    </div>
</div>
<?php endif; ?>

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