<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>تقرير المبيعات اليومي — <?= e($date ?? '') ?><?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/daily" style="display:flex;gap:10px;flex-wrap:wrap;align-items:end;">
        <div style="min-width:160px;">
            <label class="form-label" style="font-size:12px;">التاريخ</label>
            <input type="date" name="date" value="<?= e($date ?? '') ?>" 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>

<!-- Payment Method Breakdown -->
<?php if (!empty($byMethod)): ?>
<div class="card" style="margin-bottom:20px;padding:20px;">
    <h4 style="color:#0D7377;margin:0 0 12px;font-size:14px;display:flex;align-items:center;gap:6px;">
        <i data-lucide="credit-card" style="width:18px;height:18px;"></i> حسب طريقة الدفع
    </h4>
    <?php
    $methodLabels = [
        'cash'        => 'نقدي',
        'card'        => 'بطاقة',
        'bank'        => 'تحويل بنكي',
        'wallet'      => 'محفظة',
        'installment' => 'تقسيط',
    ];
    ?>
    <?php foreach ($byMethod as $m): ?>
    <div style="display:flex;justify-content:space-between;padding:6px 0;font-size:13px;border-bottom:1px solid #F3F4F6;">
        <span><?= e($methodLabels[$m['payment_method'] ?? ''] ?? ($m['payment_method'] ?? '—')) ?></span>
        <strong><?= money($m['total'] ?? 0) ?> <small style="color:#9CA3AF;">(<?= (int) ($m['cnt'] ?? 0) ?>)</small></strong>
    </div>
    <?php endforeach; ?>
</div>
<?php endif; ?>

<!-- Sales List -->
<?php if (!empty($sales)): ?>
<div class="card">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;display:flex;align-items:center;gap:8px;">
        <i data-lucide="receipt" 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>
                    <th>الضريبة</th>
                    <th>الصافي</th>
                    <th>طريقة الدفع</th>
                    <th>الوقت</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($sales as $i => $sale): ?>
                <tr>
                    <td><?= $i + 1 ?></td>
                    <td style="font-family:monospace;font-weight:600;"><?= e($sale['invoice_number'] ?? '—') ?></td>
                    <td><?= e($sale['customer_name'] ?? 'عميل نقدي') ?></td>
                    <td style="font-weight:600;direction:ltr;text-align:right;"><?= money($sale['subtotal'] ?? 0) ?></td>
                    <td style="color:#D97706;direction:ltr;text-align:right;"><?= money($sale['discount'] ?? 0) ?></td>
                    <td style="color:#2563EB;direction:ltr;text-align:right;"><?= money($sale['tax'] ?? 0) ?></td>
                    <td style="font-weight:700;color:#059669;direction:ltr;text-align:right;"><?= money($sale['grand_total'] ?? 0) ?></td>
                    <td style="font-size:13px;"><?= e($methodLabels[$sale['payment_method'] ?? ''] ?? ($sale['payment_method'] ?? '—')) ?></td>
                    <td style="font-size:13px;color:#6B7280;white-space:nowrap;"><?= e($sale['created_at'] ?? '—') ?></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="shopping-cart" 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(); ?>
