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

<?php $__template->section('page_actions'); ?>
<a href="/academy-contracts" class="btn btn-outline"><i data-lucide="file-text" style="width:15px;height:15px;vertical-align:middle;margin-left:4px;"></i> العقود</a>
<button type="button" class="btn btn-primary" onclick="document.getElementById('generateModal').style.display='flex'">
    <i data-lucide="play" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> إنشاء تسويات شهرية
</button>
<?php $__template->endSection(); ?>

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

<!-- Filters -->
<div class="card" style="margin-bottom:20px;padding:15px;">
    <form method="GET" action="/academy-settlements" 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:140px;">
            <label class="form-label" style="font-size:12px;">الشهر</label>
            <input type="month" name="period_month" value="<?= e($filters['period_month'] ?? '') ?>" class="form-input" style="direction:ltr;text-align:left;">
        </div>
        <div style="min-width:140px;">
            <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:120px;">
            <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:140px;">
            <label class="form-label" style="font-size:12px;">الاتجاه</label>
            <select name="direction" class="form-input">
                <option value="">الكل</option>
                <?php foreach ($directions as $key => $label): ?>
                <option value="<?= e($key) ?>" <?= ($filters['direction'] ?? '') === $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-settlements" class="btn btn-sm btn-outline" style="color:#6B7280;">مسح</a>
    </form>
</div>

<!-- Settlements Table -->
<?php if (!empty($settlements)): ?>
<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>
                    <th style="padding:12px 15px;text-align:center;font-weight:600;color:#374151;">إجراءات</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($settlements as $s):
                    $statusColor = AcademySettlement::getStatusColor($s['status'] ?? '');
                    $statusLabel = AcademySettlement::getStatusLabel($s['status'] ?? '');
                    $dirColor    = AcademySettlement::getDirectionColor($s['direction'] ?? '');
                    $dirLabel    = AcademySettlement::getDirectionLabel($s['direction'] ?? '');
                ?>
                <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($s['settlement_number'] ?? '') ?></code>
                    </td>
                    <td style="padding:12px 15px;font-weight:500;"><?= e($s['academy_name'] ?? '') ?></td>
                    <td style="padding:12px 15px;font-size:13px;"><?= e($s['period_month'] ?? '') ?></td>
                    <td style="padding:12px 15px;direction:ltr;text-align:right;"><?= number_format((float) ($s['total_revenue'] ?? 0), 2) ?> ج.م</td>
                    <td style="padding:12px 15px;direction:ltr;text-align:right;font-weight:600;"><?= number_format((float) ($s['net_amount'] ?? 0), 2) ?> ج.م</td>
                    <td style="padding:12px 15px;text-align:center;">
                        <span style="font-size:11px;font-weight:600;color:<?= $dirColor ?>;"><?= e($dirLabel) ?></span>
                    </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;">
                        <a href="/academy-settlements/<?= (int) $s['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>
</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="calculator" 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']) || !empty($filters['period_month'])): ?>
            لا توجد نتائج مطابقة لبحثك.
        <?php else: ?>
            لم يتم إنشاء أي تسويات بعد. قم بإنشاء تسويات شهرية للعقود النشطة.
        <?php endif; ?>
    </p>
</div>
<?php endif; ?>

<!-- Generate Modal -->
<div id="generateModal" 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:450px;margin:20px;">
        <div style="padding:20px;border-bottom:1px solid #E5E7EB;">
            <h3 style="margin:0;color:#0D7377;font-size:16px;"><i data-lucide="calculator" style="width:18px;height:18px;vertical-align:middle;margin-left:4px;"></i> إنشاء تسويات شهرية</h3>
        </div>
        <form method="POST" action="/academy-settlements/generate">
            <?= 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>
                    <input type="month" name="period_month" class="form-input" required style="direction:ltr;text-align:left;" value="<?= date('Y-m') ?>">
                </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('generateModal').style.display='none'">إلغاء</button>
                <button type="submit" class="btn btn-primary">إنشاء التسويات</button>
            </div>
        </form>
    </div>
</div>

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