<div class="page-header">
    <div>
        <a href="<?= APP_URL ?>/stock-takings" class="btn btn-ghost btn-sm mb-4 no-print" 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 Stock Taking
        </a>
        <h1>Stock Taking Report</h1>
        <div class="page-subtitle">Reference: <?= e($stockTaking['reference']) ?></div>
    </div>
    <div class="flex gap-2 no-print">
        <?php if ($stockTaking['stock_updated'] == 0): ?>
        <form action="<?= APP_URL ?>/stock-takings/update-stock" method="POST" onsubmit="return confirm('Update stock levels based on this stock taking? This will adjust your inventory based on the counted quantities.')">
            <?= Security::csrfField() ?>
            <input type="hidden" name="id" value="<?= $stockTaking['id'] ?>">
            <button type="submit" class="btn btn-success">
                <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>
                Update Stock
            </button>
        </form>
        <?php endif; ?>
        <button class="btn btn-outline" onclick="window.print()">
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" style="width:16px;">
                <polyline points="6 9 6 2 18 2 18 9"></polyline>
                <path d="M6 18H4a2 2 0 0 1-2-2v-5a2 2 0 0 1 2-2h16a2 2 0 0 1 2 2v5a2 2 0 0 1-2 2h-2"></path>
                <rect x="6" y="14" width="12" height="8"></rect>
            </svg>
            Print
        </button>
        
        <form action="<?= APP_URL ?>/stock-takings/delete" method="POST" onsubmit="return confirm('<?= $stockTaking['stock_updated'] ? 'WARNING: Stock has already been updated for this session. Deleting it will remove the adjustment record but will NOT revert the stock changes. Continue?' : 'Are you sure you want to delete this stock taking session?' ?>')">
            <?= Security::csrfField() ?>
            <input type="hidden" name="id" value="<?= $stockTaking['id'] ?>">
            <button type="submit" class="btn btn-outline btn-danger">
                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" style="width:16px;">
                    <polyline points="3 6 5 6 21 6"></polyline>
                    <path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"></path>
                </svg>
                Delete
            </button>
        </form>
    </div>
</div>

<!-- Summary Cards -->
<div class="grid-4 gap-4 mb-6 summary-cards">
    <div class="card">
        <div class="card-body p-4">
            <div class="text-sm text-muted">Total Items</div>
            <div class="text-2xl font-bold"><?= number_format(array_sum(array_column($summary, 'item_count'))) ?></div>
        </div>
    </div>
    <div class="card">
        <div class="card-body p-4">
            <div class="text-sm text-muted">Shortages</div>
            <div class="text-2xl font-bold text-red-600"><?= $summary['shortage']['item_count'] ?></div>
            <div class="text-sm text-muted"><?= money($summary['shortage']['total_amount']) ?></div>
        </div>
    </div>
    <div class="card">
        <div class="card-body p-4">
            <div class="text-sm text-muted">Overstock</div>
            <div class="text-2xl font-bold text-orange-600"><?= $summary['over']['item_count'] ?></div>
            <div class="text-sm text-muted"><?= money($summary['over']['total_amount']) ?></div>
        </div>
    </div>
    <div class="card">
        <div class="card-body p-4">
            <div class="text-sm text-muted">Matched</div>
            <div class="text-2xl font-bold text-green-600"><?= $summary['equal']['item_count'] ?></div>
        </div>
    </div>
</div>

<!-- Stock Taking Details -->
<div class="card mb-6">
    <div class="card-header">
        <h3 class="card-title">Session Details</h3>
    </div>
    <div class="card-body">
        <div class="grid-4 gap-4">
            <div>
                <div class="text-sm text-muted">Reference</div>
                <div class="font-semibold"><?= e($stockTaking['reference']) ?></div>
            </div>
            <div>
                <div class="text-sm text-muted">Location</div>
                <div class="font-semibold"><?= e($stockTaking['location_name']) ?></div>
            </div>
            <div>
                <div class="text-sm text-muted">Mode</div>
                <div class="font-semibold"><?= ucfirst($stockTaking['mode']) ?></div>
            </div>
            <div>
                <div class="text-sm text-muted">Date</div>
                <div class="font-semibold"><?= date('M j, Y g:i A', strtotime($stockTaking['created_at'])) ?></div>
            </div>
            <div>
                <div class="text-sm text-muted">Created By</div>
                <div class="font-semibold"><?= e($stockTaking['user_name']) ?></div>
            </div>
            <div>
                <div class="text-sm text-muted">Status</div>
                <div class="badge badge-success">Completed</div>
            </div>
            <div>
                <div class="text-sm text-muted">Stock Updated</div>
                <?php if ($stockTaking['stock_updated'] == 1): ?>
                    <div class="badge badge-success">Yes</div>
                <?php else: ?>
                    <div class="badge badge-warning">No</div>
                <?php endif; ?>
            </div>
            <?php if ($stockTaking['notes']): ?>
            <div class="col-span-2">
                <div class="text-sm text-muted">Notes</div>
                <div class="font-semibold"><?= e($stockTaking['notes']) ?></div>
            </div>
            <?php endif; ?>
        </div>
    </div>
</div>

<!-- Shortages Table -->
<?php if ($summary['shortage']['item_count'] > 0): ?>
<div class="card mb-6">
    <div class="card-header">
        <h3 class="card-title">Shortages</h3>
        <div class="card-actions">
            <span class="badge badge-danger"><?= $summary['shortage']['item_count'] ?> items</span>
            <span class="text-sm font-semibold text-red-600"><?= money($summary['shortage']['total_amount']) ?></span>
        </div>
    </div>
    <div class="table-wrapper">
        <table class="data-table">
            <thead>
                <tr>
                    <th>Product</th>
                    <th>Category</th>
                    <th>Current Stock</th>
                    <th>Counted Qty</th>
                    <th>Difference</th>
                    <th>Amount</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($items as $item): ?>
                    <?php if ($item['status'] === 'shortage'): ?>
                <tr>
                    <td>
                        <strong><?= e($item['product_name']) ?></strong>
                        <div class="text-sm text-muted"><?= e($item['sku'] ?: '-') ?></div>
                    </td>
                    <td>
                        <span class="text-sm"><?= e($item['category_name'] ?: '-') ?></span>
                    </td>
                    <td>
                        <span class="text-sm"><?= number_format($item['current_stock'], 2) ?></span>
                    </td>
                    <td>
                        <span class="text-sm"><?= number_format($item['counted_qty'], 2) ?></span>
                    </td>
                    <td>
                        <span class="text-sm font-semibold text-red-600"><?= number_format($item['difference'], 2) ?></span>
                    </td>
                    <td>
                        <span class="text-sm font-semibold text-red-600"><?= money($item['total_amount']) ?></span>
                    </td>
                </tr>
                    <?php endif; ?>
                <?php endforeach; ?>
            </tbody>
            <tfoot>
                <tr class="bg-gray-50">
                    <td colspan="5" class="text-right font-semibold">Total Shortage Amount:</td>
                    <td class="font-semibold text-red-600"><?= money($summary['shortage']['total_amount']) ?></td>
                </tr>
            </tfoot>
        </table>
    </div>
</div>
<?php endif; ?>

<!-- Overstock Table -->
<?php if ($summary['over']['item_count'] > 0): ?>
<div class="card mb-6">
    <div class="card-header">
        <h3 class="card-title">Overstock</h3>
        <div class="card-actions">
            <span class="badge badge-warning"><?= $summary['over']['item_count'] ?> items</span>
            <span class="text-sm font-semibold text-orange-600"><?= money($summary['over']['total_amount']) ?></span>
        </div>
    </div>
    <div class="table-wrapper">
        <table class="data-table">
            <thead>
                <tr>
                    <th>Product</th>
                    <th>Category</th>
                    <th>Current Stock</th>
                    <th>Counted Qty</th>
                    <th>Difference</th>
                    <th>Amount</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($items as $item): ?>
                    <?php if ($item['status'] === 'over'): ?>
                <tr>
                    <td>
                        <strong><?= e($item['product_name']) ?></strong>
                        <div class="text-sm text-muted"><?= e($item['sku'] ?: '-') ?></div>
                    </td>
                    <td>
                        <span class="text-sm"><?= e($item['category_name'] ?: '-') ?></span>
                    </td>
                    <td>
                        <span class="text-sm"><?= number_format($item['current_stock'], 2) ?></span>
                    </td>
                    <td>
                        <span class="text-sm"><?= number_format($item['counted_qty'], 2) ?></span>
                    </td>
                    <td>
                        <span class="text-sm font-semibold text-orange-600">+<?= number_format($item['difference'], 2) ?></span>
                    </td>
                    <td>
                        <span class="text-sm font-semibold text-orange-600"><?= money($item['total_amount']) ?></span>
                    </td>
                </tr>
                    <?php endif; ?>
                <?php endforeach; ?>
            </tbody>
            <tfoot>
                <tr class="bg-gray-50">
                    <td colspan="5" class="text-right font-semibold">Total Overstock Amount:</td>
                    <td class="font-semibold text-orange-600"><?= money($summary['over']['total_amount']) ?></td>
                </tr>
            </tfoot>
        </table>
    </div>
</div>
<?php endif; ?>

<!-- Matched Items Table -->
<?php if ($summary['equal']['item_count'] > 0): ?>
<div class="card mb-6">
    <div class="card-header">
        <h3 class="card-title">Matched Items</h3>
        <div class="card-actions">
            <span class="badge badge-success"><?= $summary['equal']['item_count'] ?> items</span>
        </div>
    </div>
    <div class="table-wrapper">
        <table class="data-table">
            <thead>
                <tr>
                    <th>Product</th>
                    <th>Category</th>
                    <th>Stock</th>
                    <th>Status</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($items as $item): ?>
                    <?php if ($item['status'] === 'equal'): ?>
                <tr>
                    <td>
                        <strong><?= e($item['product_name']) ?></strong>
                        <div class="text-sm text-muted"><?= e($item['sku'] ?: '-') ?></div>
                    </td>
                    <td>
                        <span class="text-sm"><?= e($item['category_name'] ?: '-') ?></span>
                    </td>
                    <td>
                        <span class="text-sm"><?= number_format($item['current_stock'], 2) ?></span>
                    </td>
                    <td>
                        <span class="badge badge-success">Matched</span>
                    </td>
                </tr>
                    <?php endif; ?>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
<?php endif; ?>

<?php if ($summary['shortage']['item_count'] === 0 && $summary['over']['item_count'] === 0 && $summary['equal']['item_count'] === 0): ?>
<div class="card">
    <div class="card-body text-center py-8">
        <div class="text-muted">No items were counted in this stock taking session.</div>
    </div>
</div>
<?php endif; ?>
