<?php
/**
 * @var array $products
 * @var int $days
 * @var bool $isExpiring
 */
?>
<div class="page-title mb-4">
    <h5><i class="fas fa-<?= !empty($isExpiring) ? 'clock' : 'exclamation-triangle' ?> me-2 text-gold"></i>
        <?= !empty($isExpiring) ? 'Expiring Products' : 'Low Stock Products' ?></h5>
</div>

<div class="glass-card p-0">
    <div class="card-header">
        <div class="card-header-left">
            <i class="fas fa-<?= !empty($isExpiring) ? 'clock' : 'boxes' ?> card-icon"></i>
            <h6 class="card-title">
                <?= !empty($isExpiring) ? 'Products Expiring Within ' . e($days ?? 30) . ' Days' : 'Products Below Reorder Level' ?>
            </h6>
        </div>
        <div class="card-header-right">
            <div class="btn-group btn-group-sm">
                <a href="<?= url('inventory/low-stock') ?>" class="btn btn-sm <?= empty($isExpiring) ? 'btn-gold' : 'btn-outline-gold' ?>">
                    <i class="fas fa-exclamation-triangle me-1"></i>Low Stock
                </a>
                <a href="<?= url('inventory/expiring') ?>" class="btn btn-sm <?= !empty($isExpiring) ? 'btn-gold' : 'btn-outline-gold' ?>">
                    <i class="fas fa-clock me-1"></i>Expiring
                </a>
            </div>
        </div>
    </div>
    <div class="card-body p-0">
        <div class="table-responsive">
            <table class="table table-dark table-hover mb-0">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>Product</th>
                        <th>SKU</th>
                        <?php if (!empty($isExpiring)): ?>
                            <th>Batch #</th>
                            <th>Expiry Date</th>
                            <th class="text-center">Batch Qty</th>
                        <?php else: ?>
                            <th>Category</th>
                            <th>Supplier</th>
                            <th class="text-center">Current Stock</th>
                            <th class="text-center">Reorder Level</th>
                            <th>Status</th>
                        <?php endif; ?>
                        <th class="text-end">Price</th>
                        <th class="text-center">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($products)): ?>
                        <tr>
                            <td colspan="<?= !empty($isExpiring) ? 7 : 8 ?>" class="text-center text-muted py-4">
                                <i class="fas fa-check-circle fa-2x mb-2 text-success"></i>
                                <p class="mb-0">
                                    <?= !empty($isExpiring) ? 'No products expiring soon' : 'All products are well stocked' ?>
                                </p>
                            </td>
                        </tr>
                    <?php else: ?>
                        <?php $i = 1; ?>
                        <?php foreach ($products as $product): ?>
                            <tr>
                                <td><?= $i++ ?></td>
                                <td>
                                    <a href="<?= url('inventory/show/' . $product->id) ?>" class="text-gold text-decoration-none">
                                        <strong><?= e($product->product_name ?? $product->name) ?></strong>
                                    </a>
                                </td>
                                <td><code><?= e($product->sku) ?></code></td>
                                <?php if (!empty($isExpiring)): ?>
                                    <td><small><?= e($product->batch_number ?? '-') ?></small></td>
                                    <td>
                                        <span class="badge bg-<?= strtotime($product->expiry_date) < time() ? 'danger' : 'warning' ?>">
                                            <?= formatDate($product->expiry_date) ?>
                                        </span>
                                    </td>
                                    <td class="text-center"><?= (int)$product->quantity ?></td>
                                <?php else: ?>
                                    <td><?= e($product->category_name ?? '-') ?></td>
                                    <td><?= e($product->supplier_name ?? '-') ?></td>
                                    <td class="text-center">
                                        <span class="badge bg-danger"><?= (int)$product->current_stock ?></span>
                                    </td>
                                    <td class="text-center"><?= (int)$product->reorder_level ?></td>
                                    <td>
                                        <span class="badge bg-<?= $product->is_active ? 'success' : 'secondary' ?>">
                                            <?= $product->is_active ? 'Active' : 'Inactive' ?>
                                        </span>
                                    </td>
                                <?php endif; ?>
                                <td class="text-end"><?= currency($product->unit_price ?? 0) ?></td>
                                <td class="text-center">
                                    <?php if (!empty($isExpiring) && (int)$product->quantity > 0 && \core\Auth::hasPermission('inventory_edit')): ?>
                                        <form method="POST" action="<?= url('inventory/batches/dispose/' . $product->id) ?>" class="d-inline" onsubmit="return confirm('Dispose <?= (int)$product->quantity ?> units?')">
                                            <input type="hidden" name="_csrf_token" value="<?= csrf_token() ?>">
                                            <button type="submit" class="btn btn-sm btn-outline-danger" title="Dispose">
                                                <i class="fas fa-trash-alt"></i>
                                            </button>
                                        </form>
                                    <?php else: ?>
                                        <a href="<?= url('inventory/edit/' . $product->id) ?>" class="btn btn-sm btn-outline-gold" title="Edit">
                                            <i class="fas fa-edit"></i>
                                        </a>
                                    <?php endif; ?>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
    <div class="card-footer">
        <a href="<?= url('inventory') ?>" class="btn btn-sm btn-outline-secondary">
            <i class="fas fa-arrow-left me-1"></i>Back to Products
        </a>
    </div>
</div>
