<?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'); ?>

<!-- Filter -->
<div class="card" style="margin-bottom:20px;padding:15px;">
    <form method="GET" action="/inventory/reports/audit-variance" style="display:flex;gap:10px;align-items:end;">
        <div style="min-width:280px;">
            <label class="form-label" style="font-size:12px;">اختر الجرد</label>
            <select name="audit_id" class="form-select">
                <option value="">-- اختر جرد --</option>
                <?php foreach ($audits as $audit): ?>
                <option value="<?= (int) $audit['id'] ?>" <?= $auditId == $audit['id'] ? 'selected' : '' ?>>
                    <?= e($audit['audit_number']) ?> — <?= e($audit['warehouse_name']) ?> (<?= e($audit['audit_date']) ?>)
                </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>

<?php if ($auditId && !empty($variances)): ?>

<?php
    $totalPositive = 0;
    $totalNegative = 0;
    $totalCost = 0;
    foreach ($variances as $v) {
        $var = (float) ($v['variance'] ?? 0);
        if ($var > 0) $totalPositive += $var;
        else $totalNegative += abs($var);
        $totalCost += (float) ($v['variance_cost'] ?? 0);
    }
?>

<!-- Variance Summary -->
<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:#ECFDF5;border:1px solid #BBF7D0;">
        <div style="font-size:28px;font-weight:700;color:#059669;">+<?= number_format($totalPositive) ?></div>
        <div style="color:#6B7280;font-size:14px;">فائض</div>
    </div>
    <div class="card" style="padding:20px;text-align:center;background:#FEF2F2;border:1px solid #FECACA;">
        <div style="font-size:28px;font-weight:700;color:#DC2626;">-<?= number_format($totalNegative) ?></div>
        <div style="color:#6B7280;font-size:14px;">عجز</div>
    </div>
    <div class="card" style="padding:20px;text-align:center;background:#EFF6FF;border:1px solid #BFDBFE;">
        <div style="font-size:28px;font-weight:700;color:#2563EB;"><?= money(abs($totalCost)) ?></div>
        <div style="color:#6B7280;font-size:14px;">تكلفة الفروقات</div>
    </div>
</div>

<!-- Variances Table -->
<div class="card">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;display:flex;align-items:center;gap:8px;">
        <i data-lucide="clipboard-list" style="width:20px;height:20px;color:#1e3a5f;"></i>
        <h3 style="margin:0;color:#1e3a5f;">تفاصيل فروقات الجرد</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 ($variances as $v): ?>
                <?php
                    $variance = (float) ($v['variance'] ?? 0);
                    $varColor = $variance > 0 ? '#059669' : ($variance < 0 ? '#DC2626' : '#6B7280');
                    $varBg    = $variance > 0 ? '#ECFDF5' : ($variance < 0 ? '#FEF2F2' : 'transparent');
                    $varSign  = $variance > 0 ? '+' : '';
                ?>
                <tr>
                    <td style="font-weight:600;"><?= e($v['item_name']) ?></td>
                    <td style="direction:ltr;text-align:right;font-family:monospace;font-size:13px;"><?= e($v['sku'] ?? '—') ?></td>
                    <td style="font-weight:600;"><?= number_format((float) ($v['system_quantity'] ?? 0)) ?></td>
                    <td style="font-weight:600;"><?= number_format((float) ($v['physical_quantity'] ?? 0)) ?></td>
                    <td>
                        <span style="display:inline-block;padding:3px 12px;border-radius:10px;font-size:13px;font-weight:700;background:<?= $varBg ?>;color:<?= $varColor ?>;">
                            <?= $varSign ?><?= number_format($variance) ?>
                        </span>
                    </td>
                    <td style="font-weight:700;color:<?= $varColor ?>;direction:ltr;text-align:right;"><?= money(abs((float) ($v['variance_cost'] ?? 0))) ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>

<?php elseif ($auditId && empty($variances)): ?>
<div class="card" style="padding:60px 20px;text-align:center;">
    <div style="margin-bottom:15px;">
        <i data-lucide="check-circle" style="width:48px;height:48px;color:#059669;"></i>
    </div>
    <h3 style="color:#059669;margin:0 0 8px;">لا توجد فروقات</h3>
    <p style="color:#9CA3AF;font-size:14px;margin:0;">جميع الأصناف متطابقة بين النظام والجرد الفعلي</p>
</div>

<?php else: ?>
<div class="card" style="padding:60px 20px;text-align:center;">
    <div style="margin-bottom:15px;">
        <i data-lucide="clipboard-list" 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(); ?>
