<?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/commissions/report" style="display:flex;gap:10px;align-items:end;flex-wrap:wrap;">
        <div style="min-width:160px;">
            <label class="form-label" style="font-size:12px;">الشهر</label>
            <input type="month" name="month" class="form-input" value="<?= e($month ?? date('Y-m')) ?>">
        </div>
        <div style="min-width:200px;">
            <label class="form-label" style="font-size:12px;">المندوب</label>
            <select name="representative_id" class="form-select">
                <option value="">جميع المندوبين</option>
                <?php foreach ($reps as $rep): ?>
                <option value="<?= (int) $rep['id'] ?>" <?= ($representative_id ?? '') == $rep['id'] ? 'selected' : '' ?>><?= e($rep['name_ar']) ?></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>
    </form>
</div>

<!-- Summary Table -->
<?php if (!empty($summary)): ?>
<div class="card" style="margin-bottom:20px;">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;display:flex;align-items:center;gap:8px;">
        <i data-lucide="bar-chart-3" 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>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($summary as $row): ?>
                <tr>
                    <td style="font-weight:600;"><?= e($row['rep_name']) ?></td>
                    <td><?= (int) ($row['sale_count'] ?? 0) ?></td>
                    <td style="direction:ltr;text-align:left;"><?= money($row['total_sales'] ?? 0) ?></td>
                    <td style="direction:ltr;text-align:left;font-weight:600;color:#059669;"><?= money($row['total_commission'] ?? 0) ?></td>
                    <td style="direction:ltr;text-align:left;"><?= money($row['target_amount'] ?? 0) ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
<?php endif; ?>

<!-- Detail Table -->
<?php if (!empty($commissions)): ?>
<div class="card">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;display:flex;align-items:center;gap:8px;">
        <i data-lucide="list" style="width:18px;height:18px;color:#D97706;"></i>
        <h3 style="margin:0;color:#D97706;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>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($commissions as $comm): ?>
                <tr>
                    <td><?= e($comm['rep_name'] ?? '—') ?></td>
                    <td><code style="font-size:12px;background:#F3F4F6;padding:2px 8px;border-radius:4px;"><?= e($comm['invoice_number'] ?? '—') ?></code></td>
                    <td><?= e($comm['sale_date'] ?? '—') ?></td>
                    <td style="direction:ltr;text-align:left;"><?= money($comm['sale_amount'] ?? 0) ?></td>
                    <td><?= e($comm['commission_rate'] ?? '—') ?>%</td>
                    <td style="direction:ltr;text-align:left;font-weight:600;color:#059669;"><?= money($comm['commission_amount'] ?? 0) ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
<?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(); ?>
