<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>تقرير إحالات الأعضاء<?php $__template->endSection(); ?>

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

<!-- Filters -->
<div class="card" style="margin-bottom:20px;padding:15px;">
    <form method="GET" action="/sales/referrals" style="display:flex;gap:10px;align-items:end;flex-wrap:wrap;">
        <div style="min-width:200px;">
            <label class="form-label" style="font-size:12px;">المندوب</label>
            <select name="rep_id" class="form-select">
                <option value="">جميع المندوبين</option>
                <?php foreach ($reps as $rep): ?>
                <option value="<?= (int) $rep['id'] ?>" <?= ($filters['rep_id'] ?? '') == $rep['id'] ? 'selected' : '' ?>><?= e($rep['name_ar']) ?> (<?= e($rep['code']) ?>)</option>
                <?php endforeach; ?>
            </select>
        </div>
        <div style="min-width:140px;">
            <label class="form-label" style="font-size:12px;">من تاريخ</label>
            <input type="date" name="date_from" class="form-input" value="<?= e($filters['date_from'] ?? '') ?>">
        </div>
        <div style="min-width:140px;">
            <label class="form-label" style="font-size:12px;">إلى تاريخ</label>
            <input type="date" name="date_to" class="form-input" value="<?= e($filters['date_to'] ?? '') ?>">
        </div>
        <div style="min-width:140px;">
            <label class="form-label" style="font-size:12px;">الحالة</label>
            <select name="status" class="form-select">
                <option value="">الكل</option>
                <option value="pending" <?= ($filters['status'] ?? '') === 'pending' ? 'selected' : '' ?>>معلقة</option>
                <option value="approved" <?= ($filters['status'] ?? '') === 'approved' ? 'selected' : '' ?>>معتمدة</option>
                <option value="paid" <?= ($filters['status'] ?? '') === 'paid' ? 'selected' : '' ?>>مدفوعة</option>
            </select>
        </div>
        <button type="submit" class="btn btn-outline"><i data-lucide="search" style="width:16px;height:16px;vertical-align:middle;"></i> عرض</button>
    </form>
</div>

<!-- Summary Cards -->
<div style="display:grid;grid-template-columns:1fr 1fr;gap:15px;margin-bottom:20px;">
    <div class="card" style="padding:20px;text-align:center;">
        <div style="font-size:13px;color:#6B7280;margin-bottom:5px;">عدد الإحالات</div>
        <div style="font-size:28px;font-weight:700;color:#0D7377;"><?= (int) ($totals['total_count'] ?? 0) ?></div>
    </div>
    <div class="card" style="padding:20px;text-align:center;">
        <div style="font-size:13px;color:#6B7280;margin-bottom:5px;">إجمالي العمولات</div>
        <div style="font-size:28px;font-weight:700;color:#059669;"><?= money($totals['total_amount'] ?? 0) ?></div>
    </div>
</div>

<!-- Data Table -->
<?php if (!empty($rows)): ?>
<div class="card">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;display:flex;align-items:center;gap:8px;">
        <i data-lucide="users" style="width:18px;height:18px;color:#0D7377;"></i>
        <h3 style="margin:0;color:#0D7377;font-size:15px;">تفاصيل الإحالات</h3>
    </div>
    <div class="table-responsive">
        <table class="data-table">
            <thead>
                <tr>
                    <th>المندوب</th>
                    <th>العضو</th>
                    <th>رقم العضوية</th>
                    <th>قيمة العضوية</th>
                    <th>نسبة العمولة</th>
                    <th>مبلغ العمولة</th>
                    <th>التاريخ</th>
                    <th>الحالة</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($rows as $row): ?>
                <tr>
                    <td style="font-weight:600;"><?= e($row['rep_name'] ?? '—') ?> <small style="color:#6B7280;">(<?= e($row['rep_code'] ?? '') ?>)</small></td>
                    <td><?= e($row['member_name'] ?? '—') ?></td>
                    <td><code style="font-size:12px;background:#F3F4F6;padding:2px 8px;border-radius:4px;"><?= e($row['membership_number'] ?? '—') ?></code></td>
                    <td style="direction:ltr;text-align:left;"><?= money($row['membership_value'] ?? 0) ?></td>
                    <td><?= e($row['commission_rate'] ?? '0') ?>%</td>
                    <td style="direction:ltr;text-align:left;font-weight:600;color:#059669;"><?= money($row['commission_amount'] ?? 0) ?></td>
                    <td><?= e($row['commission_date'] ?? '—') ?></td>
                    <td>
                        <?php
                        $statusColor = match ($row['status'] ?? '') {
                            'paid' => '#059669',
                            'approved' => '#2563EB',
                            'pending' => '#D97706',
                            default => '#6B7280',
                        };
                        $statusLabel = match ($row['status'] ?? '') {
                            'paid' => 'مدفوعة',
                            'approved' => 'معتمدة',
                            'pending' => 'معلقة',
                            default => $row['status'] ?? '—',
                        };
                        ?>
                        <span style="background:<?= $statusColor ?>15;color:<?= $statusColor ?>;padding:3px 10px;border-radius:4px;font-size:12px;font-weight:600;"><?= $statusLabel ?></span>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>

<!-- Pagination -->
<?php if (isset($pagination) && ($pagination['last_page'] ?? 1) > 1): ?>
<div style="margin-top: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="file-search" 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;">لا توجد إحالات أعضاء مسجلة بعد.</p>
</div>
<?php endif; ?>

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