<div class="page-header">
    <div>
        <a href="<?= APP_URL ?>/inventory" class="btn btn-ghost btn-sm mb-4" style="padding-left:0;">
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><line x1="19" y1="12" x2="5" y2="12"></line><polyline points="12 19 5 12 12 5"></polyline></svg>
            Back to Inventory
        </a>
        <h1>Stock Reconciliation</h1>
        <div class="page-subtitle">Compare stock table vs stock movements</div>
    </div>
</div>

<?php if (empty($discrepancies)): ?>
<div class="card">
    <div class="card-body text-center py-8">
        <div style="color: #155724; background: #d4edda; padding: 15px; border-radius: 5px; display: inline-block;">
            <strong>✓ No discrepancies found</strong>
            <br>Stock table matches stock_movements for all products.
        </div>
    </div>
</div>
<?php else: ?>
<div class="card mb-6">
    <div class="card-body" style="background: #fff3cd; color: #856404; padding: 15px; border-radius: 5px;">
        <strong>Found <?= count($discrepancies) ?> products with discrepancies</strong>
        <p class="text-sm mt-2 mb-0">The stock table doesn't match the calculated balance from stock movements. This can happen when stock is updated manually without recording movements.</p>
    </div>
</div>

<div class="card">
    <div class="card-header">
        <h3 class="card-title">Discrepancies</h3>
    </div>
    <div class="table-wrapper">
        <table class="data-table">
            <thead>
                <tr>
                    <th>Product</th>
                    <th>SKU</th>
                    <th>Stock Table</th>
                    <th>Movement Balance</th>
                    <th>Difference</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($discrepancies as $d): ?>
                    <?php $diff = $d['stock_qty'] - $d['movement_qty']; ?>
                    <?php $rowStyle = $diff > 0 ? 'background: #f8d7da;' : 'background: #d1ecf1;'; ?>
                <tr style="<?= $rowStyle ?>">
                    <td>
                        <strong><?= e($d['name']) ?></strong>
                    </td>
                    <td>
                        <?= e($d['sku'] ?: '-') ?>
                    </td>
                    <td>
                        <span class="font-semibold"><?= number_format($d['stock_qty'], 2) ?></span>
                    </td>
                    <td>
                        <span class="font-semibold"><?= number_format($d['movement_qty'], 2) ?></span>
                    </td>
                    <td>
                        <span class="font-semibold <?= $diff > 0 ? 'text-red-600' : 'text-blue-600' ?>">
                            <?= $diff > 0 ? '+' : '' ?><?= number_format($diff, 2) ?>
                        </span>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>

<div class="card mt-6">
    <div class="card-body">
        <h3 class="font-semibold mb-4">Fix Discrepancies</h3>
        <p class="text-sm text-muted mb-4">
            <strong>Recommended:</strong> Update the stock table to match the movement history. Stock movements are the source of truth for inventory tracking.
        </p>
        <form action="<?= APP_URL ?>/inventory/reconcile-fix" method="POST">
            <?= Security::csrfField() ?>
            <button type="submit" class="btn btn-success" onclick="return confirm('Update stock table to match movement history? This will correct the stock quantities for all products shown above.')">
                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" style="width:16px;">
                    <path d="M11 4H4a2 2 0 0 1-2h3a2 2 0 0 1-2z"></path>
                    <path d="M17 5v-2h3a2 2 0 0 1-2z"></path>
                    <polyline points="20 12 20 22 4 22 4 12"></polyline>
                </svg>
                Fix All Discrepancies
            </button>
        </form>
    </div>
</div>
<?php endif; ?>
