<?php
/**
 * @var object $product
 * @var array $stockMovements
 * @var array $batches
 * @var array $purchases
 */
?>
<div class="page-title mb-4">
    <h5><i class="fas fa-box-open me-2 text-gold"></i><?= e($product->name) ?></h5>
</div>

<div class="row g-4">
    <div class="col-lg-8">
        <div class="glass-card p-0">
            <div class="card-header">
                <div class="card-header-left">
                    <i class="fas fa-info-circle card-icon"></i>
                    <h6 class="card-title">Product Details</h6>
                </div>
                <div>
                    <?php if (can('inventory_edit')): ?>
                        <a href="<?= url('inventory/edit/' . $product->id) ?>" class="btn btn-sm btn-outline-gold">
                            <i class="fas fa-edit me-1"></i>Edit
                        </a>
                    <?php endif; ?>
                </div>
            </div>
            <div class="card-body">
                <div class="row g-3">
                    <div class="col-md-6">
                        <table class="table table-sm table-borderless mb-0">
                            <tr>
                                <td class="text-muted" width="140">Name:</td>
                                <td><strong><?= e($product->name) ?></strong></td>
                            </tr>
                            <tr>
                                <td class="text-muted">SKU:</td>
                                <td><code><?= e($product->sku) ?></code></td>
                            </tr>
                            <tr>
                                <td class="text-muted">Barcode:</td>
                                <td><?= e($product->barcode ?? '-') ?></td>
                            </tr>
                            <tr>
                                <td class="text-muted">Category:</td>
                                <td><?= e($product->category_name ?? '-') ?></td>
                            </tr>
                            <tr>
                                <td class="text-muted">Unit Type:</td>
                                <td><?= e(ucfirst($product->unit_type ?? 'Piece')) ?></td>
                            </tr>
                            <tr>
                                <td class="text-muted">Track Inventory:</td>
                                <td>
                                    <span class="badge bg-<?= $product->track_inventory ? 'success' : 'secondary' ?>">
                                        <?= $product->track_inventory ? 'Yes' : 'No' ?>
                                    </span>
                                </td>
                            </tr>
                        </table>
                    </div>
                    <div class="col-md-6">
                        <table class="table table-sm table-borderless mb-0">
                            <tr>
                                <td class="text-muted" width="140">Unit Price:</td>
                                <td><strong class="text-gold"><?= currency($product->unit_price ?? 0) ?></strong></td>
                            </tr>
                            <tr>
                                <td class="text-muted">Cost Price:</td>
                                <td><?= currency($product->cost_price ?? 0) ?></td>
                            </tr>
                            <tr>
                                <td class="text-muted">Tax:</td>
                                <td><?= formatNumber($product->tax_percent ?? 0) ?>%</td>
                            </tr>
                            <tr>
                                <td class="text-muted">Current Stock:</td>
                                <td>
                                    <?php
                                    $stock = (int)$product->current_stock;
                                    $reorder = (int)$product->reorder_level;
                                    if ($stock <= 0):
                                    ?>
                                        <span class="badge bg-danger"><?= $stock ?></span>
                                    <?php elseif ($stock <= $reorder): ?>
                                        <span class="badge bg-warning text-dark"><?= $stock ?></span>
                                    <?php else: ?>
                                        <span class="badge bg-success"><?= $stock ?></span>
                                    <?php endif; ?>
                                </td>
                            </tr>
                            <tr>
                                <td class="text-muted">Reorder Level:</td>
                                <td><?= $reorder ?></td>
                            </tr>
                            <tr>
                                <td class="text-muted">Status:</td>
                                <td>
                                    <span class="badge bg-<?= $product->is_active ? 'success' : 'secondary' ?>">
                                        <?= $product->is_active ? 'Active' : 'Inactive' ?>
                                    </span>
                                </td>
                            </tr>
                        </table>
                    </div>
                </div>

                <?php if (!empty($product->description)): ?>
                    <div class="mt-3">
                        <label class="text-muted small">Description:</label>
                        <p class="mb-0"><?= nl2br(e($product->description)) ?></p>
                    </div>
                <?php endif; ?>
            </div>
        </div>

        <div class="glass-card p-0 mt-4">
            <div class="card-header">
                <div class="card-header-left">
                    <i class="fas fa-history card-icon"></i>
                    <h6 class="card-title">Stock Movement History</h6>
                </div>
            </div>
            <div class="card-body p-0">
                <div class="table-responsive">
                    <table class="table table-dark table-hover mb-0">
                        <thead>
                            <tr>
                                <th>Date</th>
                                <th>Type</th>
                                <th class="text-end">Quantity</th>
                                <th>Reference</th>
                                <th>Notes</th>
                                <th>By</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if (empty($stockMovements)): ?>
                                <tr>
                                    <td colspan="6" class="text-center text-muted py-3">No stock movements recorded</td>
                                </tr>
                            <?php else: ?>
                                <?php foreach ($stockMovements as $movement): ?>
                                    <tr>
                                        <td><?= formatDateTime($movement->created_at) ?></td>
                                        <td>
                                            <span class="badge bg-<?= $movement->quantity > 0 ? 'success' : 'danger' ?>">
                                                <?= ucwords(str_replace('_', ' ', e($movement->movement_type))) ?>
                                            </span>
                                        </td>
                                        <td class="text-end <?= $movement->quantity > 0 ? 'text-success' : 'text-danger' ?>">
                                            <?= $movement->quantity > 0 ? '+' : '' ?><?= $movement->quantity ?>
                                        </td>
                                        <td><small><?= e($movement->reference_type ?? '-') ?> #<?= e($movement->reference_id ?? '-') ?></small></td>
                                        <td><small><?= e(truncate($movement->notes ?? '', 50)) ?></small></td>
                                        <td><small><?= e($movement->user_first_name ?? '') ?> <?= e($movement->user_last_name ?? '') ?></small></td>
                                    </tr>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>

    <div class="col-lg-4">
        <div class="glass-card p-0">
            <div class="card-header">
                <div class="card-header-left">
                    <i class="fas fa-warehouse card-icon"></i>
                    <h6 class="card-title">Supplier Info</h6>
                </div>
            </div>
            <div class="card-body">
                <?php if (!empty($product->supplier_name)): ?>
                    <table class="table table-sm table-borderless mb-0">
                        <tr>
                            <td class="text-muted">Supplier:</td>
                            <td><strong><?= e($product->supplier_name) ?></strong></td>
                        </tr>
                        <?php if (!empty($product->supplier_contact)): ?>
                            <tr>
                                <td class="text-muted">Contact:</td>
                                <td><?= e($product->supplier_contact) ?></td>
                            </tr>
                        <?php endif; ?>
                        <?php if (!empty($product->supplier_email)): ?>
                            <tr>
                                <td class="text-muted">Email:</td>
                                <td><?= e($product->supplier_email) ?></td>
                            </tr>
                        <?php endif; ?>
                        <?php if (!empty($product->supplier_phone)): ?>
                            <tr>
                                <td class="text-muted">Phone:</td>
                                <td><?= e($product->supplier_phone) ?></td>
                            </tr>
                        <?php endif; ?>
                    </table>
                <?php else: ?>
                    <p class="text-muted mb-0">No supplier assigned</p>
                <?php endif; ?>
            </div>
        </div>

        <div class="glass-card p-0 mt-4">
            <div class="card-header">
                <div class="card-header-left">
                    <i class="fas fa-layer-group card-icon"></i>
                    <h6 class="card-title">Batches</h6>
                </div>
                <div class="card-header-right">
                    <?php if (\core\Auth::hasPermission('inventory_edit')): ?>
                        <button type="button" class="btn btn-sm btn-gold" data-bs-toggle="modal" data-bs-target="#addBatchModal">
                            <i class="fas fa-plus me-1"></i>Add Batch
                        </button>
                    <?php endif; ?>
                </div>
            </div>
            <div class="card-body p-0">
                <div class="table-responsive">
                    <table class="table table-dark table-hover mb-0">
                        <thead>
                            <tr>
                                <th>Batch</th>
                                <th>Qty</th>
                                <th>Expiry</th>
                                <th class="text-center">Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if (empty($batches)): ?>
                                <tr>
                                    <td colspan="4" class="text-center text-muted py-3">No batches</td>
                                </tr>
                            <?php else: ?>
                                <?php foreach ($batches as $batch): ?>
                                    <tr>
                                        <td><small><?= e($batch->batch_number ?? '-') ?></small></td>
                                        <td><?= (int)$batch->quantity ?></td>
                                        <td>
                                            <?php if (!empty($batch->expiry_date)): ?>
                                                <span class="badge bg-<?= strtotime($batch->expiry_date) < time() ? 'danger' : (strtotime($batch->expiry_date) < strtotime('+30 days') ? 'warning' : 'success') ?>">
                                                    <?= formatDate($batch->expiry_date) ?>
                                                </span>
                                            <?php else: ?>
                                                -
                                            <?php endif; ?>
                                        </td>
                                        <td class="text-center">
                                            <?php if ((int)$batch->quantity > 0 && strtotime($batch->expiry_date) < time() && \core\Auth::hasPermission('inventory_edit')): ?>
                                                <form method="POST" action="<?= url('inventory/batches/dispose/' . $batch->id) ?>" class="d-inline" onsubmit="return confirm('Dispose <?= (int)$batch->quantity ?> units of batch <?= e($batch->batch_number) ?>?')">
                                                    <input type="hidden" name="_csrf_token" value="<?= csrf_token() ?>">
                                                    <button type="submit" class="btn btn-sm btn-outline-danger" title="Dispose Expired">
                                                        <i class="fas fa-trash-alt"></i>
                                                    </button>
                                                </form>
                                            <?php else: ?>
                                                <span class="text-muted">-</span>
                                            <?php endif; ?>
                                        </td>
                                    </tr>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        <!-- Add Batch Modal -->
        <?php if (\core\Auth::hasPermission('inventory_edit')): ?>
        <div class="modal fade" id="addBatchModal" tabindex="-1" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content bg-dark border-gold">
                    <form method="POST" action="<?= url('inventory/batches/store') ?>">
                        <input type="hidden" name="_csrf_token" value="<?= csrf_token() ?>">
                        <input type="hidden" name="product_id" value="<?= $product->id ?>">
                        <div class="modal-header border-gold">
                            <h6 class="modal-title text-gold">Add Batch</h6>
                            <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
                        </div>
                        <div class="modal-body">
                            <div class="mb-3">
                                <label class="form-label">Batch Number <span class="text-danger">*</span></label>
                                <input type="text" name="batch_number" class="form-control" required maxlength="50">
                            </div>
                            <div class="mb-3">
                                <label class="form-label">Expiry Date <span class="text-danger">*</span></label>
                                <input type="date" name="expiry_date" class="form-control" required>
                            </div>
                            <div class="mb-3">
                                <label class="form-label">Quantity <span class="text-danger">*</span></label>
                                <input type="number" name="quantity" class="form-control" required min="1">
                            </div>
                            <div class="mb-3">
                                <label class="form-label">Cost Price</label>
                                <input type="number" name="cost_price" class="form-control" min="0" step="0.01" value="0">
                            </div>
                        </div>
                        <div class="modal-footer border-gold">
                            <button type="button" class="btn btn-sm btn-outline-secondary" data-bs-dismiss="modal">Cancel</button>
                            <button type="submit" class="btn btn-sm btn-gold">Save Batch</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
        <?php endif; ?>

        <div class="glass-card p-0 mt-4">
            <div class="card-header">
                <div class="card-header-left">
                    <i class="fas fa-shopping-cart card-icon"></i>
                    <h6 class="card-title">Purchase History</h6>
                </div>
            </div>
            <div class="card-body p-0">
                <div class="table-responsive">
                    <table class="table table-dark table-hover mb-0">
                        <thead>
                            <tr>
                                <th>PO #</th>
                                <th>Date</th>
                                <th>Qty</th>
                                <th>Status</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if (empty($purchases)): ?>
                                <tr>
                                    <td colspan="4" class="text-center text-muted py-3">No purchases</td>
                                </tr>
                            <?php else: ?>
                                <?php foreach ($purchases as $purchase): ?>
                                    <tr>
                                        <td><a href="<?= url('purchases/show/' . $purchase->purchase_order_id) ?>" class="text-gold"><?= e($purchase->order_number ?? '-') ?></a></td>
                                        <td><small><?= formatDate($purchase->po_date) ?></small></td>
                                        <td><?= (int)$purchase->quantity ?></td>
                                        <td>
                                            <span class="badge bg-<?= statusBadge($purchase->status ?? 'draft') ?>">
                                                <?= e(ucfirst($purchase->status ?? 'draft')) ?>
                                            </span>
                                        </td>
                                    </tr>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
