<?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/revenue-analysis" 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="branch_id" class="form-select">
                <option value="">جميع الفروع</option>
                <?php foreach ($branches as $b): ?>
                <option value="<?= (int) $b['id'] ?>" <?= $filters['branch_id'] == $b['id'] ? 'selected' : '' ?>><?= e($b['name_ar']) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div>
            <button type="submit" class="btn btn-primary">عرض</button>
        </div>
    </form>
</div>

<!-- Grand Totals -->
<div style="display:grid;grid-template-columns:repeat(3,1fr);gap:15px;margin-bottom:25px;">
    <div class="card" style="padding:20px;text-align:center;">
        <div style="font-size:28px;font-weight:700;color:#059669;"><?= money($grand_total) ?></div>
        <div style="color:#6B7280;margin-top:5px;">إجمالي الإيرادات</div>
    </div>
    <div class="card" style="padding:20px;text-align:center;">
        <div style="font-size:28px;font-weight:700;color:#0D7377;"><?= number_format($grand_count) ?></div>
        <div style="color:#6B7280;margin-top:5px;">عدد المعاملات</div>
    </div>
    <div class="card" style="padding:20px;text-align:center;">
        <div style="font-size:28px;font-weight:700;color:#DC2626;"><?= money($voided_total) ?> <span style="font-size:14px;font-weight:400;">(<?= $voided_count ?>)</span></div>
        <div style="color:#6B7280;margin-top:5px;">المدفوعات الملغاة</div>
    </div>
</div>

<div style="display:grid;grid-template-columns:1fr 1fr;gap:15px;margin-bottom:25px;">
    <!-- Revenue by Type -->
    <div class="card" style="padding:20px;">
        <h3 style="margin:0 0 15px;">الإيرادات حسب النوع</h3>
        <?php if (!empty($by_type)): ?>
        <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 = bccomp($grand_total, '0', 2) > 0 ? bcdiv(bcmul((string) $t['total'], '100', 2), $grand_total, 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:6px;">
                                <div style="flex:1;background:#E5E7EB;border-radius:4px;height:8px;">
                                    <div style="width:<?= min(100, (float)$pct) ?>%;background:#059669;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>
        <?php else: ?>
        <p style="color:#9CA3AF;">لا توجد بيانات</p>
        <?php endif; ?>
    </div>

    <!-- Revenue by Method -->
    <div class="card" style="padding:20px;">
        <h3 style="margin:0 0 15px;">الإيرادات حسب طريقة الدفع</h3>
        <?php if (!empty($by_method)): ?>
        <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_method as $m):
                        $pct = bccomp($grand_total, '0', 2) > 0 ? bcdiv(bcmul((string) $m['total'], '100', 2), $grand_total, 1) : '0.0';
                    ?>
                    <tr>
                        <td><?= e($method_labels[$m['payment_method']] ?? $m['payment_method']) ?></td>
                        <td><?= (int) $m['cnt'] ?></td>
                        <td style="font-weight:600;direction:ltr;text-align:left;"><?= money($m['total']) ?></td>
                        <td>
                            <div style="display:flex;align-items:center;gap:6px;">
                                <div style="flex:1;background:#E5E7EB;border-radius:4px;height:8px;">
                                    <div style="width:<?= min(100, (float)$pct) ?>%;background:#7C3AED;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>
        <?php else: ?>
        <p style="color:#9CA3AF;">لا توجد بيانات</p>
        <?php endif; ?>
    </div>
</div>

<!-- Revenue by Branch -->
<?php if (!empty($by_branch)): ?>
<div class="card" style="padding:20px;margin-bottom:25px;">
    <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_branch as $b):
                    $pct = bccomp($grand_total, '0', 2) > 0 ? bcdiv(bcmul((string) $b['total'], '100', 2), $grand_total, 1) : '0.0';
                ?>
                <tr>
                    <td>
                        <?php if ($b['branch_id']): ?>
                            <a href="?<?= http_build_query(array_merge($filters, ['branch_id' => $b['branch_id']])) ?>"><?= e($b['branch_name']) ?></a>
                        <?php else: ?>
                            <span style="color:#6B7280;"><?= e($b['branch_name']) ?></span>
                        <?php endif; ?>
                    </td>
                    <td><?= (int) $b['cnt'] ?></td>
                    <td style="font-weight:600;direction:ltr;text-align:left;"><?= money($b['total']) ?></td>
                    <td>
                        <div style="display:flex;align-items:center;gap:6px;">
                            <div style="flex:1;background:#E5E7EB;border-radius:4px;height:8px;">
                                <div style="width:<?= min(100, (float)$pct) ?>%;background:#0284C7;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; ?>

<!-- Monthly Breakdown -->
<?php if (!empty($monthly)): ?>
<div class="card" style="padding:20px;margin-bottom:25px;">
    <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
                $maxMonthly = '0.00';
                foreach ($monthly as $m) {
                    if (bccomp((string) $m['total'], $maxMonthly, 2) > 0) $maxMonthly = (string) $m['total'];
                }
                foreach ($monthly as $m):
                    $barPct = bccomp($maxMonthly, '0', 2) > 0 ? bcdiv(bcmul((string) $m['total'], '100', 2), $maxMonthly, 1) : '0.0';
                ?>
                <tr>
                    <td style="font-family:monospace;"><?= e($m['month']) ?></td>
                    <td><?= (int) $m['cnt'] ?></td>
                    <td style="font-weight:600;direction:ltr;text-align:left;"><?= money($m['total']) ?></td>
                    <td style="width:40%;">
                        <div style="background:#E5E7EB;border-radius:4px;height:20px;">
                            <div style="width:<?= min(100, (float)$barPct) ?>%;background:#0D7377;border-radius:4px;height:20px;display:flex;align-items:center;padding:0 8px;">
                                <?php if ((float)$barPct > 15): ?>
                                <span style="font-size:11px;color:#fff;"><?= money($m['total']) ?></span>
                                <?php endif; ?>
                            </div>
                        </div>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
<?php endif; ?>

<!-- Daily Breakdown (Current Month) -->
<?php if (!empty($daily)): ?>
<div class="card" style="padding: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
                $maxDaily = '0.00';
                foreach ($daily as $d) {
                    if (bccomp((string) $d['total'], $maxDaily, 2) > 0) $maxDaily = (string) $d['total'];
                }
                foreach ($daily as $d):
                    $barPct = bccomp($maxDaily, '0', 2) > 0 ? bcdiv(bcmul((string) $d['total'], '100', 2), $maxDaily, 1) : '0.0';
                    $dayName = date('D', strtotime($d['day']));
                ?>
                <tr>
                    <td style="font-family:monospace;"><?= e($d['day']) ?> <span style="color:#6B7280;font-size:12px;">(<?= $dayName ?>)</span></td>
                    <td><?= (int) $d['cnt'] ?></td>
                    <td style="font-weight:600;direction:ltr;text-align:left;"><?= money($d['total']) ?></td>
                    <td style="width:40%;">
                        <div style="background:#E5E7EB;border-radius:4px;height:16px;">
                            <div style="width:<?= min(100, (float)$barPct) ?>%;background:#059669;border-radius:4px;height:16px;"></div>
                        </div>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
<?php endif; ?>

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