<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>التقرير اليومي — <?= e($date) ?><?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>

<!-- Date Filter -->
<div class="card" style="margin-bottom:20px;padding:15px;">
    <form method="GET" action="/payments/daily-report" style="display:flex;gap:10px;align-items:end;">
        <div class="form-group">
            <label class="form-label" style="font-size:12px;">التاريخ</label>
            <input type="date" name="date" value="<?= e($date) ?>" class="form-input">
        </div>
        <div class="form-group">
            <label class="form-label" style="font-size:12px;">الفرع</label>
            <select name="branch_id" class="form-select">
                <option value="">كل الفروع</option>
                <?php foreach ($branches as $b): ?>
                    <option value="<?= (int) $b['id'] ?>" <?= $branchId == $b['id'] ? 'selected' : '' ?>><?= e($b['name_ar']) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <button type="submit" class="btn btn-primary">عرض التقرير</button>
    </form>
</div>

<!-- Summary Cards -->
<div style="display:grid;grid-template-columns:1fr 1fr 1fr;gap:15px;margin-bottom:20px;">
    <div class="card" style="padding:20px;text-align:center;background:#F0FDF4;border:1px solid #BBF7D0;">
        <div style="font-size:32px;font-weight:700;color:#059669;"><?= money($report['grand_total']['total'] ?? '0') ?></div>
        <div style="color:#6B7280;font-size:14px;">إجمالي التحصيل</div>
        <div style="color:#9CA3AF;font-size:12px;margin-top:5px;"><?= (int) ($report['grand_total']['count'] ?? 0) ?> دفعة</div>
    </div>
    <div class="card" style="padding:20px;">
        <h4 style="color:#0D7377;margin:0 0 10px;font-size:14px;">حسب النوع</h4>
        <?php foreach ($report['by_type'] as $t): ?>
            <div style="display:flex;justify-content:space-between;padding:4px 0;font-size:13px;">
                <span><?= e(\App\Modules\Payments\Services\PaymentService::getPaymentTypeLabel($t['payment_type'])) ?></span>
                <strong><?= money($t['total']) ?> <small style="color:#9CA3AF;">(<?= (int) $t['count'] ?>)</small></strong>
            </div>
        <?php endforeach; ?>
    </div>
    <div class="card" style="padding:20px;">
        <h4 style="color:#0D7377;margin:0 0 10px;font-size:14px;">حسب طريقة الدفع</h4>
        <?php foreach ($report['by_method'] as $m): ?>
            <div style="display:flex;justify-content:space-between;padding:4px 0;font-size:13px;">
                <span><?= e(\App\Modules\Payments\Models\Payment::getPaymentMethodLabel($m['payment_method'])) ?></span>
                <strong><?= money($m['total']) ?> <small style="color:#9CA3AF;">(<?= (int) $m['count'] ?>)</small></strong>
            </div>
        <?php endforeach; ?>
    </div>
</div>

<!-- Individual Payments Table -->
<div class="card">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;display:flex;justify-content:space-between;align-items:center;">
        <h3 style="margin:0;color:#0D7377;">تفاصيل الدفعات</h3>
        <button onclick="window.print()" class="btn btn-sm btn-outline">🖨️ طباعة</button>
    </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 $grandTotal = '0.00'; ?>
            <?php foreach ($payments as $i => $p): ?>
                <?php $grandTotal = bcadd($grandTotal, $p['amount'], 2); ?>
                <tr>
                    <td><?= $i + 1 ?></td>
                    <td style="direction:ltr;text-align:right;font-weight:600;"><?= e($p['receipt_number'] ?? '—') ?></td>
                    <td><a href="/members/<?= (int) $p['member_id'] ?>" style="color:#0D7377;"><?= e($p['member_name']) ?></a></td>
                    <td style="font-size:13px;"><?= e(\App\Modules\Payments\Services\PaymentService::getPaymentTypeLabel($p['payment_type'])) ?></td>
                    <td style="font-size:13px;"><?= e(\App\Modules\Payments\Models\Payment::getPaymentMethodLabel($p['payment_method'])) ?></td>
                    <td style="font-weight:700;direction:ltr;text-align:right;"><?= money($p['amount']) ?></td>
                </tr>
            <?php endforeach; ?>
            <?php if (empty($payments)): ?>
                <tr><td colspan="6" style="text-align:center;padding:40px;color:#6B7280;">لا توجد دفعات في هذا التاريخ</td></tr>
            <?php else: ?>
                <tr style="background:#F0FDF4;font-weight:700;">
                    <td colspan="5" style="padding:12px 15px;">الإجمالي</td>
                    <td style="padding:12px 15px;direction:ltr;text-align:right;font-size:18px;color:#059669;"><?= money($grandTotal) ?></td>
                </tr>
            <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<?php $__template->endSection(); ?>