<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>تفاصيل الاشتراك<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/sa/subscriptions" class="btn btn-outline"><i data-lucide="arrow-right" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> العودة للقائمة</a>
<?php $__template->endSection(); ?>

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

<?php
$sub = $subscription;
$statusColors = [
    'unpaid' => 'background:#FEF3C7;color:#92400E;',
    'paid' => 'background:#ECFDF5;color:#059669;',
    'partial' => 'background:#DBEAFE;color:#1D4ED8;',
    'exempt' => 'background:#F3E8FF;color:#7C3AED;',
    'overdue' => 'background:#FEE2E2;color:#DC2626;',
];
$statusStyle = $statusColors[$sub['payment_status'] ?? ''] ?? 'background:#F3F4F6;color:#374151;';
?>

<div class="card" style="margin-bottom:20px;padding:20px;">
    <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:20px;">
        <h3 style="margin:0;font-size:18px;">اشتراك: <?= e($sub['subscription_number'] ?? '') ?></h3>
        <span style="<?= $statusStyle ?>padding:5px 14px;border-radius:10px;font-size:13px;font-weight:600;">
            <?= e($statusOptions[$sub['payment_status'] ?? ''] ?? '') ?>
        </span>
    </div>

    <div style="display:grid;grid-template-columns:repeat(auto-fit, minmax(200px, 1fr));gap:15px;">
        <div>
            <div style="font-size:12px;color:#6B7280;">اللاعب</div>
            <div style="font-weight:600;"><?= e($sub['player_name'] ?? '—') ?></div>
            <?php if (!empty($sub['player_code'])): ?>
            <div style="font-size:11px;color:#9CA3AF;"><?= e($sub['player_code']) ?></div>
            <?php endif; ?>
        </div>
        <div>
            <div style="font-size:12px;color:#6B7280;">المجموعة</div>
            <div style="font-weight:600;"><?= e($sub['group_name'] ?? '—') ?></div>
            <?php if (!empty($sub['group_code'])): ?>
            <div style="font-size:11px;color:#9CA3AF;"><?= e($sub['group_code']) ?></div>
            <?php endif; ?>
        </div>
        <div>
            <div style="font-size:12px;color:#6B7280;">الفترة</div>
            <div style="font-weight:600;"><?= e($sub['period_start'] ?? '') ?> - <?= e($sub['period_end'] ?? '') ?></div>
        </div>
        <div>
            <div style="font-size:12px;color:#6B7280;">المبلغ الأصلي</div>
            <div style="font-weight:600;"><?= money((float) ($sub['amount'] ?? 0)) ?></div>
        </div>
        <div>
            <div style="font-size:12px;color:#6B7280;">الخصم</div>
            <div style="font-weight:600;"><?= money((float) ($sub['discount_amount'] ?? 0)) ?></div>
        </div>
        <div>
            <div style="font-size:12px;color:#6B7280;">المبلغ النهائي</div>
            <div style="font-weight:600;font-size:16px;color:#059669;"><?= money((float) ($sub['final_amount'] ?? 0)) ?></div>
        </div>
        <?php if (!empty($sub['paid_at'])): ?>
        <div>
            <div style="font-size:12px;color:#6B7280;">تاريخ الدفع</div>
            <div style="font-weight:600;"><?= e($sub['paid_at']) ?></div>
        </div>
        <div>
            <div style="font-size:12px;color:#6B7280;">المبلغ المدفوع</div>
            <div style="font-weight:600;"><?= money((float) ($sub['paid_amount'] ?? 0)) ?></div>
        </div>
        <?php endif; ?>
        <?php if (!empty($sub['exemption_reason'])): ?>
        <div>
            <div style="font-size:12px;color:#6B7280;">سبب الإعفاء</div>
            <div style="font-weight:600;"><?= e($sub['exemption_reason']) ?></div>
        </div>
        <?php endif; ?>
    </div>
</div>

<!-- Actions -->
<?php if (in_array($sub['payment_status'] ?? '', ['unpaid', 'overdue', 'partial'], true)): ?>
<div style="display:flex;gap:15px;flex-wrap:wrap;">
    <!-- Pay Button -->
    <div class="card" style="flex:1;min-width:280px;padding:20px;">
        <h4 style="margin:0 0 15px;font-size:15px;">تسجيل الدفع</h4>
        <form method="POST" action="/sa/subscriptions/<?= (int) $sub['id'] ?>/pay">
            <?= csrf_field() ?>
            <p style="color:#6B7280;font-size:13px;margin-bottom:15px;">
                سيتم تسجيل دفع المبلغ الكامل: <strong><?= money((float) ($sub['final_amount'] ?? 0)) ?></strong>
            </p>
            <button type="submit" class="btn btn-primary" onclick="return confirm('تأكيد تسجيل الدفع؟');">
                <i data-lucide="check-circle" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> تأكيد الدفع
            </button>
        </form>
    </div>

    <!-- Exempt Button -->
    <div class="card" style="flex:1;min-width:280px;padding:20px;">
        <h4 style="margin:0 0 15px;font-size:15px;">إعفاء من الدفع</h4>
        <form method="POST" action="/sa/subscriptions/<?= (int) $sub['id'] ?>/exempt">
            <?= csrf_field() ?>
            <div style="margin-bottom:12px;">
                <label class="form-label" style="font-size:12px;">سبب الإعفاء</label>
                <input type="text" name="exemption_reason" class="form-input" required placeholder="اذكر سبب الإعفاء...">
            </div>
            <button type="submit" class="btn btn-outline" onclick="return confirm('تأكيد الإعفاء؟');">
                <i data-lucide="shield" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> إعفاء
            </button>
        </form>
    </div>
</div>
<?php endif; ?>

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