<?php
require_once '../includes/auth.php';
require_once __DIR__ . '/stock_helpers.php';
require_once '../includes/header.php';
require_once '../includes/sidebar.php';

if (!function_exists('e')) {
    function e($value) { return htmlspecialchars((string)$value, ENT_QUOTES, 'UTF-8'); }
}

$q = trim((string)($_GET['q'] ?? ''));
$source = trim((string)($_GET['source'] ?? ''));
$actionType = trim((string)($_GET['action_type'] ?? ''));
$productId = (int)($_GET['product_id'] ?? 0);
$rows = nay_stock_fetch_movements($conn, [
    'q' => $q,
    'source' => $source,
    'action_type' => $actionType,
    'product_id' => $productId,
    'limit' => 200,
]);
$reasonLabels = nay_stock_reason_labels();
?>
<style>
.movements-page{padding:24px;direction:rtl}.movements-wrap{max-width:1450px;margin:0 auto}.topbar{display:flex;justify-content:space-between;align-items:center;gap:16px;flex-wrap:wrap;margin-bottom:22px}.topbar h1{margin:0 0 8px;font-size:30px;color:#17324d}.topbar p{margin:0;color:#71839a}.actions{display:flex;gap:10px;flex-wrap:wrap}.btn{display:inline-flex;align-items:center;justify-content:center;padding:12px 18px;border-radius:14px;text-decoration:none;font-weight:800}.btn.light{background:#fff;color:#24415c;border:1px solid #d9e6f2}.card{background:#fff;border:1px solid #e6eef5;border-radius:24px;box-shadow:0 12px 35px rgba(16,24,40,.05);overflow:hidden}.card-body{padding:22px}.filters{display:flex;gap:10px;flex-wrap:wrap;margin-bottom:16px}.filters input,.filters select{min-width:200px;padding:11px 13px;border:1px solid #d5e2ec;border-radius:12px}.filters button{border:none;background:linear-gradient(135deg,#2f80ed,#56ccf2);color:#fff;padding:11px 15px;border-radius:12px;font-weight:800;cursor:pointer}.table{width:100%;border-collapse:collapse}.table th,.table td{padding:12px;border-bottom:1px solid #edf2f7;text-align:right;vertical-align:top}.table th{background:#f8fbff;color:#23465f}.muted{color:#71839a;font-size:12px}.empty{padding:28px;text-align:center;color:#71839a}
</style>
<div class="movements-page"><div class="movements-wrap">
    <div class="topbar">
        <div><h1>سجل حركة المخزون</h1><p>كل التعديلات اليدوية وتغييرات الطلبات والتصفير والتوريد مسجلة هنا بعد تطبيق الـ SQL الجديد.</p></div>
        <div class="actions"><a class="btn light" href="stock_manage.php">إدارة المخزون</a><a class="btn light" href="stock_monitor.php">مراقبة المخزون</a></div>
    </div>
    <div class="card"><div class="card-body">
        <form method="get" class="filters">
            <input type="text" name="q" value="<?= e($q) ?>" placeholder="ابحث باسم المنتج أو الفاريانت أو الملاحظة">
            <select name="source">
                <option value="">كل المصادر</option>
                <?php foreach (['admin_manual'=>'تعديل يدوي','admin_bulk'=>'تعديل جماعي','admin_bulk_form'=>'حفظ شاشة المخزون','admin_quick'=>'اختصار سريع','admin_order'=>'من الطلبات','store_order'=>'من المتجر'] as $key => $label): ?>
                    <option value="<?= e($key) ?>" <?= $source === $key ? 'selected' : '' ?>><?= e($label) ?></option>
                <?php endforeach; ?>
            </select>
            <select name="action_type">
                <option value="">كل الحركات</option>
                <?php foreach (['set'=>'تعيين','add'=>'إضافة','deduct'=>'خصم','restore'=>'إرجاع','zero'=>'تصفير'] as $key => $label): ?>
                    <option value="<?= e($key) ?>" <?= $actionType === $key ? 'selected' : '' ?>><?= e($label) ?></option>
                <?php endforeach; ?>
            </select>
            <?php if ($productId > 0): ?><input type="hidden" name="product_id" value="<?= (int)$productId ?>"><?php endif; ?>
            <button type="submit">بحث</button>
            <a class="btn light" href="stock_movements.php<?= $productId > 0 ? '?product_id=' . (int)$productId : '' ?>">إعادة ضبط</a>
        </form>
        <?php if ($rows): ?>
            <table class="table">
                <thead><tr><th>الوقت</th><th>العنصر</th><th>الحركة</th><th>قبل / تغير / بعد</th><th>المرجع</th><th>المصدر / المنفذ</th><th>الملاحظة</th></tr></thead>
                <tbody>
                <?php foreach ($rows as $row): ?>
                    <tr>
                        <td><?= e($row['created_at'] ?? '') ?></td>
                        <td>
                            <strong><?= e(!empty($row['variant_name']) ? (($row['product_name'] ?? '—') . ' - ' . $row['variant_name']) : ($row['product_name'] ?? '—')) ?></strong>
                            <div class="muted"><?= e($row['target_type'] ?? '—') ?></div>
                        </td>
                        <td>
                            <div><?= e($reasonLabels[$row['reason_code'] ?? ''] ?? ($row['action_type'] ?? '—')) ?></div>
                            <div class="muted">Action: <?= e($row['action_type'] ?? '—') ?></div>
                        </td>
                        <td>
                            <div>قبل: <strong><?= number_format((int)($row['qty_before'] ?? 0)) ?></strong></div>
                            <div>تغير: <strong><?= number_format((int)($row['qty_change'] ?? 0)) ?></strong></div>
                            <div>بعد: <strong><?= number_format((int)($row['qty_after'] ?? 0)) ?></strong></div>
                        </td>
                        <td>
                            <div><?= e($row['reference_type'] ?? '—') ?></div>
                            <div class="muted"><?= !empty($row['reference_id']) ? ('#' . (int)$row['reference_id']) : '—' ?></div>
                        </td>
                        <td>
                            <div><?= e($row['source'] ?? '—') ?></div>
                            <div class="muted"><?= e($row['admin_name'] ?? 'النظام') ?></div>
                        </td>
                        <td><?= e($row['notes'] ?? '—') ?></td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        <?php else: ?>
            <div class="empty">ماكو حركات مطابقة أو جدول سجل الحركة بعده ما منضاف.</div>
        <?php endif; ?>
    </div></div>
</div></div>
<?php require_once '../includes/footer.php'; ?>
