<?php
use App\Modules\ActivitySubscriptions\Models\ActivitySubscription;
$__template->layout('Layout.main');
?>
<?php $__template->section('title'); ?>اشتراكات الأنشطة<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/activity-subscriptions/enroll" class="btn btn-primary" style="gap:6px;">
    <i data-lucide="user-plus" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> تسجيل لاعب جديد
</a>
<form method="POST" action="/activity-subscriptions/generate" style="display:inline-flex;gap:6px;align-items:center;">
    <?= csrf_field() ?>
    <input type="month" name="month" value="<?= e(date('Y-m')) ?>" class="form-input" style="width:160px;font-size:13px;">
    <button type="submit" class="btn btn-outline" onclick="return confirm('هل تريد توليد الاشتراكات لهذا الشهر؟')">
        <i data-lucide="refresh-cw" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> توليد
    </button>
</form>
<a href="/activity-subscriptions/pricing" class="btn btn-outline"><i data-lucide="settings" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> التسعير</a>
<?php $__template->endSection(); ?>

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

<!-- Workflow Guide -->
<div class="card" style="margin-bottom:20px;padding:16px 20px;border-right:4px solid #1e3a5f;background:#F0FDFA;">
    <div style="display:flex;align-items:start;gap:12px;">
        <i data-lucide="info" style="width:20px;height:20px;color:#1e3a5f;flex-shrink:0;margin-top:2px;"></i>
        <div style="font-size:13px;color:#374151;line-height:1.8;">
            <strong style="color:#1e3a5f;">كيف تعمل الاشتراكات:</strong>
            اللاعب يختار الرياضة والمستوى ونوع الخدمة (فردي، مجموعة صغيرة، مجموعة، فريق) - والنظام يقوم بتعيينه تلقائيا في المجموعة المناسبة.
            الاشتراك الشهري يتولد تلقائيا بناء على التسجيل الفعال. استخدم زر "تسجيل لاعب جديد" لبدء العملية.
            <br>
            <span style="color:#6B7280;">
                <i data-lucide="arrow-left" style="width:12px;height:12px;vertical-align:middle;"></i>
                تسجيل لاعب
                <i data-lucide="arrow-left" style="width:12px;height:12px;vertical-align:middle;"></i>
                اختيار رياضة ومستوى
                <i data-lucide="arrow-left" style="width:12px;height:12px;vertical-align:middle;"></i>
                تحديد نوع الخدمة
                <i data-lucide="arrow-left" style="width:12px;height:12px;vertical-align:middle;"></i>
                التعيين التلقائي في مجموعة
                <i data-lucide="arrow-left" style="width:12px;height:12px;vertical-align:middle;"></i>
                توليد اشتراك شهري
            </span>
        </div>
    </div>
</div>

<?php
$currentStatus = $filters['status'] ?? '';
$allStatuses = ActivitySubscription::getStatuses();
?>

<!-- Status Filter Tabs -->
<div class="card" style="margin-bottom:20px;padding:0;">
    <div style="display:flex;align-items:center;gap:0;overflow-x:auto;border-bottom:2px solid #E5E7EB;">
        <a href="/activity-subscriptions?<?= http_build_query(array_merge($filters, ['status' => '', 'page' => 1])) ?>"
           style="padding:12px 20px;font-size:14px;font-weight:600;text-decoration:none;border-bottom:3px solid <?= $currentStatus === '' ? '#1e3a5f' : 'transparent' ?>;color:<?= $currentStatus === '' ? '#1e3a5f' : '#6B7280' ?>;white-space:nowrap;">
            الكل
        </a>
        <?php foreach ($allStatuses as $sKey => $sLabel): ?>
        <a href="/activity-subscriptions?<?= http_build_query(array_merge($filters, ['status' => $sKey, 'page' => 1])) ?>"
           style="padding:12px 20px;font-size:14px;font-weight:600;text-decoration:none;border-bottom:3px solid <?= $currentStatus === $sKey ? '#1e3a5f' : 'transparent' ?>;color:<?= $currentStatus === $sKey ? '#1e3a5f' : '#6B7280' ?>;white-space:nowrap;">
            <?= e($sLabel) ?>
        </a>
        <?php endforeach; ?>
    </div>
</div>

<!-- Collection Summary -->
<?php if (!empty($collectionSummary) && (int) ($collectionSummary['total'] ?? 0) > 0):
    $cs = $collectionSummary;
    $csTotal = (int) $cs['total'];
    $csPaid = (int) $cs['paid'];
    $csRate = $csTotal > 0 ? round(($csPaid / $csTotal) * 100, 1) : 0;
    $csRateColor = $csRate >= 80 ? '#059669' : ($csRate >= 50 ? '#D97706' : '#DC2626');
?>
<div style="display:grid;grid-template-columns:repeat(5, 1fr);gap:12px;margin-bottom:20px;">
    <div class="card" style="padding:15px;text-align:center;border-right:3px solid #1e3a5f;">
        <div style="font-size:20px;font-weight:700;color:#1e3a5f;"><?= number_format((float) $cs['total_expected'], 0) ?></div>
        <div style="font-size:11px;color:#6B7280;">ج.م المتوقع (<?= e($summaryMonth) ?>)</div>
    </div>
    <div class="card" style="padding:15px;text-align:center;border-right:3px solid #059669;">
        <div style="font-size:20px;font-weight:700;color:#059669;"><?= number_format((float) $cs['total_collected'], 0) ?></div>
        <div style="font-size:11px;color:#6B7280;">ج.م محصّل</div>
    </div>
    <div class="card" style="padding:15px;text-align:center;border-right:3px solid #DC2626;">
        <div style="font-size:20px;font-weight:700;color:#DC2626;"><?= number_format((float) $cs['total_outstanding'], 0) ?></div>
        <div style="font-size:11px;color:#6B7280;">ج.م مستحق</div>
    </div>
    <div class="card" style="padding:15px;text-align:center;border-right:3px solid <?= $csRateColor ?>;">
        <div style="font-size:20px;font-weight:700;color:<?= $csRateColor ?>;"><?= $csRate ?>%</div>
        <div style="font-size:11px;color:#6B7280;">نسبة التحصيل</div>
    </div>
    <div class="card" style="padding:15px;text-align:center;border-right:3px solid #6B7280;">
        <div style="font-size:20px;font-weight:700;color:#374151;"><?= $csTotal ?></div>
        <div style="font-size:11px;color:#6B7280;">
            <?= $csPaid ?> مسدد &middot; <?= (int) $cs['pending'] ?> معلق &middot; <?= (int) $cs['overdue'] ?> متأخر
        </div>
    </div>
</div>
<?php endif; ?>

<!-- Search & Filters -->
<div class="card" style="margin-bottom:20px;padding:15px;">
    <form method="GET" action="/activity-subscriptions" style="display:flex;flex-wrap:wrap;gap:10px;align-items:end;">
        <div style="flex:1;min-width:200px;">
            <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:160px;">
            <label class="form-label" style="font-size:12px;">الشهر</label>
            <input type="month" name="subscription_month" value="<?= e($filters['subscription_month'] ?? '') ?>" class="form-input">
        </div>
        <div style="min-width:160px;">
            <label class="form-label" style="font-size:12px;">النشاط</label>
            <select name="discipline_id" class="form-input">
                <option value="">الكل</option>
                <?php foreach ($disciplines as $d): ?>
                <option value="<?= (int) $d['id'] ?>" <?= ($filters['discipline_id'] ?? '') == $d['id'] ? 'selected' : '' ?>><?= e($d['name_ar']) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <?php if ($currentStatus !== ''): ?>
        <input type="hidden" name="status" value="<?= e($currentStatus) ?>">
        <?php endif; ?>
        <button type="submit" class="btn btn-outline"><i data-lucide="search" style="width:16px;height:16px;vertical-align:middle;"></i> بحث</button>
        <a href="/activity-subscriptions" class="btn btn-sm btn-outline" style="color:#6B7280;">مسح</a>
    </form>
</div>

<!-- Subscriptions Table -->
<?php if (!empty($subscriptions)): ?>
<div class="card" style="padding:0;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 16px;text-align:right;font-weight:600;color:#374151;">اللاعب</th>
                <th style="padding:12px 16px;text-align:right;font-weight:600;color:#374151;">الشهر</th>
                <th style="padding:12px 16px;text-align:right;font-weight:600;color:#374151;">النشاط</th>
                <th style="padding:12px 16px;text-align:right;font-weight:600;color:#374151;">نوع اللاعب</th>
                <th style="padding:12px 16px;text-align:right;font-weight:600;color:#374151;">السعر الأساسي</th>
                <th style="padding:12px 16px;text-align:right;font-weight:600;color:#374151;">السعر المطبق</th>
                <th style="padding:12px 16px;text-align:right;font-weight:600;color:#374151;">الخصم</th>
                <th style="padding:12px 16px;text-align:right;font-weight:600;color:#374151;">الإجمالي</th>
                <th style="padding:12px 16px;text-align:center;font-weight:600;color:#374151;">الحالة</th>
                <th style="padding:12px 16px;text-align:center;font-weight:600;color:#374151;">إجراءات</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($subscriptions as $s):
                $statusColor = ActivitySubscription::getStatusColor($s['status'] ?? 'pending');
                $statusLabel = ActivitySubscription::getStatusLabel($s['status'] ?? 'pending');
                $canPay = in_array($s['status'] ?? '', ['pending', 'overdue']);
                $canExempt = in_array($s['status'] ?? '', ['pending', 'overdue']);
            ?>
            <tr style="border-bottom:1px solid #F3F4F6;">
                <td style="padding:12px 16px;">
                    <div style="font-weight:600;"><?= e($s['player_name'] ?? '—') ?></div>
                </td>
                <td style="padding:12px 16px;"><?= e($s['subscription_month'] ?? '') ?></td>
                <td style="padding:12px 16px;"><?= e($s['discipline_name'] ?? '—') ?></td>
                <td style="padding:12px 16px;"><?= e($s['player_type'] ?? '') ?></td>
                <td style="padding:12px 16px;"><?= money($s['base_rate'] ?? 0) ?></td>
                <td style="padding:12px 16px;"><?= money($s['applied_rate'] ?? 0) ?></td>
                <td style="padding:12px 16px;">
                    <?php if (((float) ($s['discount'] ?? 0)) > 0): ?>
                    <span style="color:#059669;font-weight:600;"><?= money($s['discount'] ?? 0) ?>-</span>
                    <?php else: ?>
                    <span style="color:#9CA3AF;">—</span>
                    <?php endif; ?>
                </td>
                <td style="padding:12px 16px;font-weight:700;"><?= money($s['total_amount'] ?? 0) ?></td>
                <td style="padding:12px 16px;text-align:center;">
                    <span class="badge" style="background:<?= $statusColor ?>15;color:<?= $statusColor ?>;font-size:12px;padding:4px 12px;border-radius:10px;font-weight:600;">
                        <?= e($statusLabel) ?>
                    </span>
                </td>
                <td style="padding:12px 16px;text-align:center;">
                    <div style="display:flex;gap:4px;justify-content:center;">
                        <a href="/activity-subscriptions/<?= (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>
                        <?php if ($canPay): ?>
                        <form method="POST" action="/activity-subscriptions/<?= (int) $s['id'] ?>/pay" style="display:inline;">
                            <?= csrf_field() ?>
                            <button type="submit" class="btn btn-sm btn-primary" style="font-size:12px;padding:4px 10px;" onclick="return confirm('تأكيد الدفع؟')">
                                <i data-lucide="credit-card" style="width:13px;height:13px;vertical-align:middle;"></i> دفع
                            </button>
                        </form>
                        <?php endif; ?>
                    </div>
                </td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
</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="receipt" 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['subscription_month']) || !empty($filters['discipline_id'])): ?>
            لا توجد نتائج مطابقة لبحثك. جرب تغيير معايير البحث.
        <?php else: ?>
            لم يتم توليد اشتراكات بعد. استخدم زر "توليد" لإنشاء اشتراكات الشهر.
        <?php endif; ?>
    </p>
</div>
<?php endif; ?>

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