<?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/by-item" style="display:flex;gap:10px;flex-wrap:wrap;align-items:end;">
        <div style="min-width:150px;">
            <label class="form-label" style="font-size:12px;">من تاريخ</label>
            <input type="date" name="date_from" value="<?= e($dateFrom ?? '') ?>" class="form-input">
        </div>
        <div style="min-width:150px;">
            <label class="form-label" style="font-size:12px;">إلى تاريخ</label>
            <input type="date" name="date_to" value="<?= e($dateTo ?? '') ?>" class="form-input">
        </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>
        <a href="/sales/reports/by-item" class="btn btn-outline" style="color:#6B7280;">مسح</a>
    </form>
</div>

<?php if (!empty($rows)): ?>

<?php
    $totalQty     = 0;
    $totalRevenue = '0.00';
    $totalCost    = '0.00';
    $totalProfit  = '0.00';
    foreach ($rows as $r) {
        $totalQty     += (float) ($r['total_qty'] ?? 0);
        $totalRevenue  = bcadd($totalRevenue, (string) ($r['total_revenue'] ?? 0), 2);
        $totalCost     = bcadd($totalCost, (string) ($r['total_cost'] ?? 0), 2);
        $totalProfit   = bcadd($totalProfit, (string) ($r['profit'] ?? 0), 2);
    }
?>

<!-- Summary Cards -->
<div style="display:grid;grid-template-columns:repeat(4,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;"><?= number_format($totalQty) ?></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($totalRevenue) ?></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($totalCost) ?></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($totalProfit) ?></div>
        <div style="color:#6B7280;font-size:13px;">الربح</div>
    </div>
</div>

<!-- Items Table -->
<div class="card">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;display:flex;align-items:center;gap:8px;">
        <i data-lucide="package" 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>SKU</th>
                    <th>الكمية المباعة</th>
                    <th>الإيرادات</th>
                    <th>التكلفة</th>
                    <th>الربح</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($rows as $row): ?>
                <?php
                    $profit = (float) ($row['profit'] ?? 0);
                    $profitColor = $profit >= 0 ? '#059669' : '#DC2626';
                ?>
                <tr>
                    <td style="font-weight:600;"><?= e($row['item_name_ar'] ?? '') ?></td>
                    <td style="direction:ltr;text-align:right;font-family:monospace;font-size:13px;"><?= e($row['sku'] ?? '—') ?></td>
                    <td style="font-weight:700;"><?= number_format((float) ($row['total_qty'] ?? 0)) ?></td>
                    <td style="font-weight:700;color:#059669;direction:ltr;text-align:right;"><?= money($row['total_revenue'] ?? 0) ?></td>
                    <td style="color:#DC2626;direction:ltr;text-align:right;"><?= money($row['total_cost'] ?? 0) ?></td>
                    <td style="font-weight:700;color:<?= $profitColor ?>;direction:ltr;text-align:right;"><?= money($row['profit'] ?? 0) ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
            <tfoot>
                <tr style="background:#F0FDF4;font-weight:700;">
                    <td colspan="2" style="padding:12px 15px;">الإجمالي</td>
                    <td style="padding:12px 15px;"><?= number_format($totalQty) ?></td>
                    <td style="padding:12px 15px;direction:ltr;text-align:right;color:#059669;"><?= money($totalRevenue) ?></td>
                    <td style="padding:12px 15px;direction:ltr;text-align:right;color:#DC2626;"><?= money($totalCost) ?></td>
                    <td style="padding:12px 15px;direction:ltr;text-align:right;font-size:16px;color:<?= (float) $totalProfit >= 0 ? '#059669' : '#DC2626' ?>;"><?= money($totalProfit) ?></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="package-search" 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(); ?>
