<?php
use App\Modules\AcademyContracts\Models\AcademySettlement;
$__template->layout('Layout.main');
?>
<?php $__template->section('title'); ?>تسوية <?= e($settlement->settlement_number) ?><?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<?php if ($settlement->status === 'pending_approval'): ?>
<form method="POST" action="/academy-settlements/<?= (int) $settlement->id ?>/approve" 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; ?>
<a href="/academy-settlements" 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 = AcademySettlement::getStatusColor($settlement->status ?? '');
$statusLabel = AcademySettlement::getStatusLabel($settlement->status ?? '');
$dirColor    = AcademySettlement::getDirectionColor($settlement->direction ?? '');
$dirLabel    = AcademySettlement::getDirectionLabel($settlement->direction ?? '');
?>

<!-- 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, <?= $dirColor ?>15, <?= $dirColor ?>30);display:flex;align-items:center;justify-content:center;flex-shrink:0;">
            <i data-lucide="calculator" style="width:36px;height:36px;color:<?= $dirColor ?>;"></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($settlement->settlement_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="calendar" style="width:14px;height:14px;"></i>
                    <?= e($settlement->period_month) ?>
                </span>
                <?php if ($contract): ?>
                <a href="/academy-contracts/<?= (int) $contract['id'] ?>" style="display:inline-flex;align-items:center;gap:4px;color:#0D7377;text-decoration:none;">
                    <i data-lucide="file-text" style="width:14px;height:14px;"></i>
                    <?= e($contract['contract_number'] ?? '') ?>
                </a>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>

<!-- Direction Banner -->
<div class="card" style="margin-bottom:20px;background:<?= $dirColor ?>08;border:1px solid <?= $dirColor ?>30;">
    <div style="padding:20px;text-align:center;">
        <div style="font-size:14px;color:#6B7280;margin-bottom:8px;">اتجاه التسوية</div>
        <div style="font-size:20px;font-weight:700;color:<?= $dirColor ?>;"><?= e($dirLabel) ?></div>
        <div style="font-size:28px;font-weight:800;color:<?= $dirColor ?>;margin-top:8px;direction:ltr;">
            <?= number_format((float) $settlement->net_amount, 2) ?> ج.م
        </div>
    </div>
</div>

<!-- Financial Breakdown -->
<div style="display:grid;grid-template-columns:1fr 1fr;gap:20px;margin-bottom:20px;">
    <!-- Revenue Summary -->
    <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="trending-up" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;color:#059669;"></i> ملخص الإيرادات</h3>
        </div>
        <div style="padding:20px;">
            <table style="width:100%;font-size:13px;">
                <tr style="border-bottom:1px solid #F3F4F6;">
                    <td style="padding:10px 0;color:#6B7280;">إجمالي الإيرادات</td>
                    <td style="padding:10px 0;font-weight:700;text-align:left;direction:ltr;font-size:15px;color:#1A1A2E;"><?= number_format((float) $settlement->total_revenue, 2) ?> ج.م</td>
                </tr>
                <tr style="border-bottom:1px solid #F3F4F6;">
                    <td style="padding:10px 0;color:#6B7280;">الحد الأدنى المضمون</td>
                    <td style="padding:10px 0;font-weight:600;text-align:left;direction:ltr;"><?= number_format((float) $settlement->minimum_guarantee, 2) ?> ج.م</td>
                </tr>
                <?php if ((float) $settlement->revenue_surplus > 0): ?>
                <tr style="border-bottom:1px solid #F3F4F6;">
                    <td style="padding:10px 0;color:#059669;">فائض الإيرادات</td>
                    <td style="padding:10px 0;font-weight:600;text-align:left;direction:ltr;color:#059669;">+<?= number_format((float) $settlement->revenue_surplus, 2) ?> ج.م</td>
                </tr>
                <?php endif; ?>
                <?php if ((float) $settlement->revenue_shortfall > 0): ?>
                <tr style="border-bottom:1px solid #F3F4F6;">
                    <td style="padding:10px 0;color:#DC2626;">عجز الإيرادات</td>
                    <td style="padding:10px 0;font-weight:600;text-align:left;direction:ltr;color:#DC2626;">-<?= number_format((float) $settlement->revenue_shortfall, 2) ?> ج.م</td>
                </tr>
                <?php endif; ?>
            </table>
        </div>
    </div>

    <!-- Settlement Calculation -->
    <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="receipt" 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:10px 0;color:#6B7280;">عمولة النادي</td>
                    <td style="padding:10px 0;font-weight:600;text-align:left;direction:ltr;"><?= number_format((float) $settlement->club_commission, 2) ?> ج.م</td>
                </tr>
                <tr style="border-bottom:1px solid #F3F4F6;">
                    <td style="padding:10px 0;color:#6B7280;">حصة الأكاديمية</td>
                    <td style="padding:10px 0;font-weight:600;text-align:left;direction:ltr;"><?= number_format((float) $settlement->academy_share, 2) ?> ج.م</td>
                </tr>
                <?php if ((float) $settlement->penalty_amount > 0): ?>
                <tr style="border-bottom:1px solid #F3F4F6;">
                    <td style="padding:10px 0;color:#DC2626;">غرامة</td>
                    <td style="padding:10px 0;font-weight:600;text-align:left;direction:ltr;color:#DC2626;"><?= number_format((float) $settlement->penalty_amount, 2) ?> ج.م</td>
                </tr>
                <?php endif; ?>
                <tr style="background:#F9FAFB;">
                    <td style="padding:12px 0;font-weight:700;color:#1A1A2E;">صافي المبلغ</td>
                    <td style="padding:12px 0;font-weight:800;text-align:left;direction:ltr;font-size:16px;color:<?= $dirColor ?>;"><?= number_format((float) $settlement->net_amount, 2) ?> ج.م</td>
                </tr>
            </table>
        </div>
    </div>
</div>

<!-- Approval Info -->
<?php if ($settlement->approved_by): ?>
<div class="card" style="margin-bottom:20px;">
    <div style="padding:15px 20px;display:flex;align-items:center;gap:8px;">
        <i data-lucide="shield-check" style="width:16px;height:16px;color:#059669;"></i>
        <span style="font-size:13px;color:#6B7280;">تم الاعتماد بواسطة الموظف #<?= (int) $settlement->approved_by ?> في <?= e($settlement->approved_at ?? '') ?></span>
    </div>
</div>
<?php endif; ?>

<!-- Notes -->
<?php if (!empty($settlement->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($settlement->notes) ?>
    </div>
</div>
<?php endif; ?>

<!-- Dispute Info -->
<?php if (!empty($settlement->dispute_reason)): ?>
<div class="card" style="margin-bottom:20px;border:1px solid #FCA5A5;">
    <div style="padding:15px 20px;border-bottom:1px solid #FCA5A5;background:#FEF2F2;">
        <h3 style="margin:0;font-size:14px;color:#991B1B;"><i data-lucide="alert-triangle" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> سبب النزاع</h3>
    </div>
    <div style="padding:20px;font-size:14px;color:#991B1B;line-height:1.7;">
        <?= e($settlement->dispute_reason) ?>
    </div>
</div>
<?php endif; ?>

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