<?php $__template->layout('Layout.main'); ?>

<?php $__template->section('title'); ?>الخزينة والمدفوعات<?php $__template->endSection(); ?>

<?php $__template->section('content'); ?>
<div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:20px;">
    <h2 style="margin:0;">الخزينة والمدفوعات</h2>
    <a href="/accounting" class="btn btn-outline"><i data-lucide="arrow-right" style="width:16px;height:16px;"></i> العودة للمحاسبة</a>
</div>

<!-- Filters -->
<div class="card" style="padding:20px;margin-bottom:20px;">
    <form method="GET" action="/accounting/reports/treasury" style="display:flex;flex-wrap:wrap;gap:12px;align-items:flex-end;">
        <div style="flex:1;min-width:140px;">
            <label class="form-label">من تاريخ</label>
            <input type="date" name="date_from" class="form-input" value="<?= e($filters['date_from']) ?>">
        </div>
        <div style="flex:1;min-width:140px;">
            <label class="form-label">إلى تاريخ</label>
            <input type="date" name="date_to" class="form-input" value="<?= e($filters['date_to']) ?>">
        </div>
        <div style="flex:1;min-width:160px;">
            <label class="form-label">نوع الدفعة</label>
            <select name="payment_type" class="form-select">
                <option value="">الكل</option>
                <?php
                $typeOptions = [
                    'form_fee' => 'رسوم استمارة', 'membership_fee' => 'قيمة العضوية',
                    'addition_fee' => 'رسوم إضافة', 'annual_subscription' => 'اشتراك سنوي',
                    'development_fee' => 'رسوم تنمية', 'down_payment' => 'مقدم تقسيط',
                    'installment' => 'قسط شهري', 'fine' => 'غرامة',
                    'separation_fee' => 'رسوم فصل', 'divorce_fee' => 'رسوم طلاق',
                    'death_fee' => 'رسوم وفاة', 'waiver_fee' => 'رسوم تنازل',
                    'carnet_replacement' => 'بدل فاقد كارنيه', 'seasonal_fee' => 'عضوية موسمية',
                    'sports_conversion' => 'تحويل رياضي', 'inventory_sale' => 'مبيعات مخزون',
                    'activity_subscription' => 'اشتراك نشاط', 'other' => 'أخرى',
                ];
                foreach ($typeOptions as $val => $label):
                ?>
                <option value="<?= $val ?>" <?= $filters['payment_type'] === $val ? 'selected' : '' ?>><?= $label ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div style="flex:1;min-width:120px;">
            <label class="form-label">طريقة الدفع</label>
            <select name="payment_method" class="form-select">
                <option value="">الكل</option>
                <option value="cash" <?= $filters['payment_method'] === 'cash' ? 'selected' : '' ?>>نقدي</option>
                <option value="check" <?= $filters['payment_method'] === 'check' ? 'selected' : '' ?>>شيك</option>
                <option value="visa" <?= $filters['payment_method'] === 'visa' ? 'selected' : '' ?>>فيزا</option>
                <option value="bank_transfer" <?= $filters['payment_method'] === 'bank_transfer' ? 'selected' : '' ?>>تحويل بنكي</option>
            </select>
        </div>
        <div style="flex:1;min-width:120px;">
            <label class="form-label">الحالة</label>
            <select name="voided" class="form-select">
                <option value="0" <?= $filters['voided'] === '0' ? 'selected' : '' ?>>فعالة فقط</option>
                <option value="1" <?= $filters['voided'] === '1' ? 'selected' : '' ?>>ملغاة فقط</option>
                <option value="all" <?= $filters['voided'] === 'all' ? 'selected' : '' ?>>الكل</option>
            </select>
        </div>
        <div style="flex:2;min-width:180px;">
            <label class="form-label">بحث</label>
            <input type="text" name="search" class="form-input" placeholder="اسم العضو / رقم الإيصال / ملاحظات..." value="<?= e($filters['search']) ?>">
        </div>
        <div>
            <button type="submit" class="btn btn-primary">بحث</button>
        </div>
    </form>
</div>

<!-- Summary Cards -->
<div style="display:grid;grid-template-columns:repeat(auto-fit,minmax(200px,1fr));gap:15px;margin-bottom:20px;">
    <div class="card" style="padding:20px;text-align:center;">
        <div style="font-size:24px;font-weight:700;color:#059669;"><?= money($total_amount) ?></div>
        <div style="color:#6B7280;margin-top:5px;">إجمالي المبلغ</div>
    </div>
    <div class="card" style="padding:20px;text-align:center;">
        <div style="font-size:24px;font-weight:700;color:#0D7377;"><?= number_format($total_count) ?></div>
        <div style="color:#6B7280;margin-top:5px;">عدد المدفوعات</div>
    </div>
    <?php foreach ($by_method as $m): ?>
    <div class="card" style="padding:20px;text-align:center;">
        <div style="font-size:20px;font-weight:700;color:#7C3AED;"><?= money($m['total']) ?></div>
        <div style="color:#6B7280;margin-top:5px;"><?= e($method_labels[$m['payment_method']] ?? $m['payment_method']) ?> (<?= (int) $m['cnt'] ?>)</div>
    </div>
    <?php endforeach; ?>
</div>

<!-- By Type Summary (collapsible) -->
<?php if (!empty($by_type)): ?>
<div class="card" style="padding:20px;margin-bottom:20px;">
    <h3 style="margin:0 0 15px;">ملخص حسب النوع</h3>
    <div class="table-responsive">
        <table class="data-table" style="width:100%;">
            <thead>
                <tr><th>النوع</th><th>العدد</th><th>المبلغ</th><th>النسبة</th></tr>
            </thead>
            <tbody>
                <?php foreach ($by_type as $t):
                    $pct = $total_amount > 0 ? bcdiv(bcmul((string) $t['total'], '100', 2), (string) $total_amount, 1) : '0.0';
                ?>
                <tr>
                    <td><?= e(\App\Modules\Payments\Services\PaymentService::getPaymentTypeLabel($t['payment_type'])) ?></td>
                    <td><?= (int) $t['cnt'] ?></td>
                    <td style="font-weight:600;direction:ltr;text-align:left;"><?= money($t['total']) ?></td>
                    <td>
                        <div style="display:flex;align-items:center;gap:8px;">
                            <div style="flex:1;background:#E5E7EB;border-radius:4px;height:8px;">
                                <div style="width:<?= min(100, (float)$pct) ?>%;background:#0D7377;border-radius:4px;height:8px;"></div>
                            </div>
                            <span style="font-size:12px;color:#6B7280;min-width:40px;text-align:left;"><?= $pct ?>%</span>
                        </div>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
<?php endif; ?>

<!-- Payments Table -->
<div class="card" style="padding:20px;">
    <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:15px;">
        <h3 style="margin:0;">المدفوعات (<?= number_format($total_count) ?>)</h3>
        <span style="color:#6B7280;font-size:13px;">صفحة <?= $page ?> من <?= $total_pages ?></span>
    </div>

    <?php if (!empty($payments)): ?>
    <div class="table-responsive">
        <table class="data-table" style="width:100%;">
            <thead>
                <tr>
                    <th>#</th>
                    <th>الإيصال</th>
                    <th>التاريخ</th>
                    <th>العضو / الاسم</th>
                    <th>رقم الاستمارة</th>
                    <th>النوع</th>
                    <th>الطريقة</th>
                    <th>المبلغ</th>
                    <th>المحصّل</th>
                    <th>الحالة</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($payments as $p): ?>
                <tr style="<?= $p['is_voided'] ? 'opacity:0.5;text-decoration:line-through;' : '' ?>">
                    <td><?= (int) $p['id'] ?></td>
                    <td style="font-family:monospace;font-size:12px;"><?= e($p['receipt_number'] ?? '—') ?></td>
                    <td><?= e($p['payment_date']) ?></td>
                    <td>
                        <?php if ($p['member_id']): ?>
                            <a href="/members/<?= (int) $p['member_id'] ?>"><?= e($p['member_name'] ?? 'عضو #' . $p['member_id']) ?></a>
                        <?php else: ?>
                            <span style="color:#6B7280;">زائر</span>
                        <?php endif; ?>
                    </td>
                    <td style="font-family:monospace;"><?= e($p['form_number'] ?? '—') ?></td>
                    <td><span style="background:#F3F4F6;padding:2px 8px;border-radius:4px;font-size:12px;"><?= e(\App\Modules\Payments\Services\PaymentService::getPaymentTypeLabel($p['payment_type'])) ?></span></td>
                    <td><?= e($method_labels[$p['payment_method']] ?? $p['payment_method']) ?></td>
                    <td style="font-weight:600;direction:ltr;text-align:left;"><?= money($p['amount']) ?></td>
                    <td style="font-size:12px;color:#6B7280;"><?= e($p['received_by_name'] ?? '—') ?></td>
                    <td>
                        <?php if ($p['is_voided']): ?>
                            <span style="background:#FEE2E2;color:#DC2626;padding:2px 8px;border-radius:4px;font-size:12px;">ملغاة</span>
                        <?php else: ?>
                            <span style="background:#D1FAE5;color:#059669;padding:2px 8px;border-radius:4px;font-size:12px;">فعالة</span>
                        <?php endif; ?>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>

    <!-- Pagination -->
    <?php if ($total_pages > 1): ?>
    <div style="display:flex;justify-content:center;gap:5px;margin-top:20px;">
        <?php
        $baseUrl = '/accounting/reports/treasury?' . http_build_query(array_filter($filters));
        for ($i = max(1, $page - 3); $i <= min($total_pages, $page + 3); $i++):
        ?>
        <a href="<?= $baseUrl ?>&page=<?= $i ?>"
           class="btn <?= $i === $page ? 'btn-primary' : 'btn-outline' ?>"
           style="min-width:40px;text-align:center;"><?= $i ?></a>
        <?php endfor; ?>
    </div>
    <?php endif; ?>

    <?php else: ?>
    <p style="color:#9CA3AF;text-align:center;padding:40px 0;">لا توجد مدفوعات تطابق معايير البحث</p>
    <?php endif; ?>
</div>

<script>if(typeof lucide!=='undefined')lucide.createIcons();</script>
<?php $__template->endSection(); ?>
