<?php $__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="arrow-right" style="width:15px;height:15px;vertical-align:middle;margin-left:4px;"></i> التسويات</a>
<?php $__template->endSection(); ?>

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

<!-- Filters -->
<div class="card" style="margin-bottom:20px;">
    <div style="padding:15px 20px;">
        <form method="GET" style="display:flex;align-items:end;gap:15px;flex-wrap:wrap;">
            <div class="form-group" style="margin:0;min-width:180px;">
                <label class="form-label" style="font-size:12px;">الأكاديمية</label>
                <select name="academy_id" class="form-select">
                    <option value="">— كل الأكاديميات —</option>
                    <?php foreach ($academies as $acad): ?>
                    <option value="<?= (int) $acad['id'] ?>" <?= $filterAcademy == $acad['id'] ? 'selected' : '' ?>><?= e($acad['name_ar']) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="form-group" style="margin:0;min-width:150px;">
                <label class="form-label" style="font-size:12px;">من شهر</label>
                <input type="month" name="from_month" value="<?= e($filterFrom) ?>" class="form-input" style="direction:ltr;text-align:left;">
            </div>
            <div class="form-group" style="margin:0;min-width:150px;">
                <label class="form-label" style="font-size:12px;">إلى شهر</label>
                <input type="month" name="to_month" value="<?= e($filterTo) ?>" class="form-input" style="direction:ltr;text-align:left;">
            </div>
            <button type="submit" class="btn btn-primary" style="padding:9px 20px;"><i data-lucide="filter" style="width:14px;height:14px;vertical-align:middle;margin-left:4px;"></i> عرض</button>
        </form>
    </div>
</div>

<?php if (empty($reportData)): ?>
<div class="card" style="padding:40px;text-align:center;color:#9CA3AF;">
    <i data-lucide="file-x" style="width:40px;height:40px;margin-bottom:10px;opacity:0.5;"></i>
    <p>لا توجد عقود نشطة بحد أدنى مضمون.</p>
</div>
<?php else: ?>

<!-- Summary Cards -->
<?php
$grandRevenue = array_sum(array_column($reportData, 'total_revenue'));
$grandGuarantee = array_sum(array_column($reportData, 'total_guarantee'));
$grandDeficit = array_sum(array_column($reportData, 'total_deficit'));
$grandSurplus = array_sum(array_column($reportData, 'total_surplus'));
?>
<div style="display:grid;grid-template-columns:repeat(4,1fr);gap:15px;margin-bottom:20px;">
    <div class="card" style="padding:16px;text-align:center;">
        <div style="font-size:12px;color:#6B7280;">إجمالي الإيرادات</div>
        <div style="font-size:20px;font-weight:800;color:#1A1A2E;margin-top:4px;"><?= money($grandRevenue) ?></div>
    </div>
    <div class="card" style="padding:16px;text-align:center;">
        <div style="font-size:12px;color:#6B7280;">إجمالي الضمان المطلوب</div>
        <div style="font-size:20px;font-weight:800;color:#7C3AED;margin-top:4px;"><?= money($grandGuarantee) ?></div>
    </div>
    <div class="card" style="padding:16px;text-align:center;">
        <div style="font-size:12px;color:#6B7280;">إجمالي الفائض</div>
        <div style="font-size:20px;font-weight:800;color:#059669;margin-top:4px;"><?= money($grandSurplus) ?></div>
    </div>
    <div class="card" style="padding:16px;text-align:center;">
        <div style="font-size:12px;color:#6B7280;">إجمالي العجز</div>
        <div style="font-size:20px;font-weight:800;color:#DC2626;margin-top:4px;"><?= money($grandDeficit) ?></div>
    </div>
</div>

<!-- Report Table -->
<?php foreach ($reportData as $row): ?>
<div class="card" style="margin-bottom:16px;">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;display:flex;justify-content:space-between;align-items:center;">
        <div>
            <strong style="font-size:15px;color:#1A1A2E;"><?= e($row['contract']['academy_name']) ?></strong>
            <span style="margin-right:10px;font-size:12px;color:#6B7280;">عقد <?= e($row['contract']['contract_number'] ?? '') ?></span>
        </div>
        <div style="display:flex;gap:12px;">
            <span style="font-size:12px;color:#6B7280;">الضمان الشهري: <strong style="color:#7C3AED;"><?= money((float) $row['contract']['minimum_revenue_guarantee']) ?></strong></span>
            <?php if ($row['net_position'] >= 0): ?>
            <span style="padding:4px 10px;border-radius:20px;font-size:11px;font-weight:700;background:#ECFDF5;color:#059669;">فائض <?= money($row['total_surplus']) ?></span>
            <?php else: ?>
            <span style="padding:4px 10px;border-radius:20px;font-size:11px;font-weight:700;background:#FEF2F2;color:#DC2626;">عجز <?= money($row['total_deficit']) ?></span>
            <?php endif; ?>
        </div>
    </div>
    <?php if (!empty($row['settlements'])): ?>
    <div style="padding:0;">
        <table style="width:100%;border-collapse:collapse;font-size:13px;">
            <thead>
                <tr style="background:#F9FAFB;">
                    <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: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>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($row['settlements'] as $s): ?>
                <tr style="border-top:1px solid #F3F4F6;">
                    <td style="padding:10px 15px;direction:ltr;text-align:right;"><?= e($s['period_month']) ?></td>
                    <td style="padding:10px 15px;"><?= money((float) $s['total_revenue']) ?></td>
                    <td style="padding:10px 15px;"><?= money((float) $s['minimum_guarantee']) ?></td>
                    <td style="padding:10px 15px;color:#059669;font-weight:600;"><?= (float) $s['revenue_surplus'] > 0 ? money((float) $s['revenue_surplus']) : '—' ?></td>
                    <td style="padding:10px 15px;color:#DC2626;font-weight:600;"><?= (float) $s['revenue_shortfall'] > 0 ? money((float) $s['revenue_shortfall']) : '—' ?></td>
                    <td style="padding:10px 15px;">
                        <?php
                        $statusColors = ['pending_approval' => '#F59E0B', 'approved' => '#059669', 'paid' => '#2563EB', 'draft' => '#6B7280', 'disputed' => '#DC2626'];
                        $statusLabels = ['pending_approval' => 'بانتظار الاعتماد', 'approved' => 'معتمدة', 'paid' => 'مدفوعة', 'draft' => 'مسودة', 'disputed' => 'متنازع عليها'];
                        $sc = $statusColors[$s['status']] ?? '#6B7280';
                        $sl = $statusLabels[$s['status']] ?? $s['status'];
                        ?>
                        <span style="padding:3px 8px;border-radius:4px;font-size:11px;font-weight:600;background:<?= $sc ?>15;color:<?= $sc ?>;"><?= e($sl) ?></span>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php else: ?>
    <div style="padding:20px;text-align:center;color:#9CA3AF;font-size:13px;">لا توجد تسويات في هذه الفترة</div>
    <?php endif; ?>
</div>
<?php endforeach; ?>

<?php endif; ?>

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