<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>تقرير المبيعات الشهري<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<button onclick="window.print()" class="btn btn-outline"><i data-lucide="printer" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> طباعة</button>
<?php $__template->endSection(); ?>

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

<!-- Filters -->
<div class="card" style="margin-bottom:20px;padding:15px;">
    <form method="GET" action="/sales/reports/monthly" style="display:flex;gap:10px;flex-wrap:wrap;align-items:end;">
        <div style="min-width:200px;">
            <label class="form-label" style="font-size:12px;">الشهر</label>
            <input type="month" name="month" value="<?= e($month ?? '') ?>" class="form-input">
        </div>
        <div style="min-width:160px;">
            <label class="form-label" style="font-size:12px;">المخزن</label>
            <select name="warehouse_id" class="form-select">
                <option value="">كل المخازن</option>
                <?php foreach ($warehouses as $wh): ?>
                <option value="<?= (int) $wh['id'] ?>" <?= $warehouseId == $wh['id'] ? 'selected' : '' ?>><?= e($wh['name_ar']) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <button type="submit" class="btn btn-primary"><i data-lucide="search" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> عرض التقرير</button>
    </form>
</div>

<!-- Summary Cards -->
<div style="display:grid;grid-template-columns:repeat(5,1fr);gap:15px;margin-bottom:20px;">
    <div class="card" style="padding:18px;text-align:center;background:#F0FDF4;border:1px solid #BBF7D0;">
        <div style="font-size:26px;font-weight:700;color:#059669;"><?= (int) ($summary['sale_count'] ?? 0) ?></div>
        <div style="color:#6B7280;font-size:13px;">عدد المبيعات</div>
    </div>
    <div class="card" style="padding:18px;text-align:center;background:#ECFDF5;border:1px solid #A7F3D0;">
        <div style="font-size:26px;font-weight:700;color:#059669;"><?= money($summary['total_revenue'] ?? 0) ?></div>
        <div style="color:#6B7280;font-size:13px;">الإيرادات</div>
    </div>
    <div class="card" style="padding:18px;text-align:center;background:#FFFBEB;border:1px solid #FDE68A;">
        <div style="font-size:26px;font-weight:700;color:#D97706;"><?= money($summary['total_discounts'] ?? 0) ?></div>
        <div style="color:#6B7280;font-size:13px;">الخصومات</div>
    </div>
    <div class="card" style="padding:18px;text-align:center;background:#EFF6FF;border:1px solid #BFDBFE;">
        <div style="font-size:26px;font-weight:700;color:#2563EB;"><?= money($summary['total_tax'] ?? 0) ?></div>
        <div style="color:#6B7280;font-size:13px;">الضريبة</div>
    </div>
    <div class="card" style="padding:18px;text-align:center;background:#FEF2F2;border:1px solid #FECACA;">
        <div style="font-size:26px;font-weight:700;color:#DC2626;"><?= money($summary['total_refunds'] ?? 0) ?></div>
        <div style="color:#6B7280;font-size:13px;">المرتجعات</div>
    </div>
</div>

<!-- Daily Breakdown Table -->
<?php if (!empty($dailyBreakdown)): ?>
<div class="card">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;display:flex;align-items:center;gap:8px;">
        <i data-lucide="calendar-days" style="width:20px;height:20px;color:#0D7377;"></i>
        <h3 style="margin:0;color:#0D7377;">التفصيل اليومي</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
                    $grandTotal = '0.00';
                    $grandRefunded = '0.00';
                    $grandNet = '0.00';
                    $grandCount = 0;
                ?>
                <?php foreach ($dailyBreakdown as $day): ?>
                <?php
                    $dayTotal    = (string) ($day['total'] ?? 0);
                    $dayRefunded = (string) ($day['refunded'] ?? 0);
                    $dayNet      = bcsub($dayTotal, $dayRefunded, 2);
                    $grandTotal    = bcadd($grandTotal, $dayTotal, 2);
                    $grandRefunded = bcadd($grandRefunded, $dayRefunded, 2);
                    $grandNet      = bcadd($grandNet, $dayNet, 2);
                    $grandCount   += (int) ($day['cnt'] ?? 0);
                ?>
                <tr>
                    <td style="font-weight:600;white-space:nowrap;"><?= e($day['sale_date'] ?? '—') ?></td>
                    <td style="font-weight:600;"><?= (int) ($day['cnt'] ?? 0) ?></td>
                    <td style="font-weight:700;color:#059669;direction:ltr;text-align:right;"><?= money($dayTotal) ?></td>
                    <td style="color:#DC2626;direction:ltr;text-align:right;"><?= money($dayRefunded) ?></td>
                    <td style="font-weight:700;color:#0D7377;direction:ltr;text-align:right;"><?= money($dayNet) ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
            <tfoot>
                <tr style="background:#F0FDF4;font-weight:700;">
                    <td style="padding:12px 15px;">الإجمالي</td>
                    <td style="padding:12px 15px;"><?= $grandCount ?></td>
                    <td style="padding:12px 15px;direction:ltr;text-align:right;color:#059669;"><?= money($grandTotal) ?></td>
                    <td style="padding:12px 15px;direction:ltr;text-align:right;color:#DC2626;"><?= money($grandRefunded) ?></td>
                    <td style="padding:12px 15px;direction:ltr;text-align:right;font-size:16px;color:#0D7377;"><?= money($grandNet) ?></td>
                </tr>
            </tfoot>
        </table>
    </div>
</div>
<?php else: ?>
<div class="card" style="padding:60px 20px;text-align:center;">
    <div style="margin-bottom:15px;">
        <i data-lucide="calendar" 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(); ?>
