<?php
/**
 * @var object $product
 * @var array $categories
 * @var array $suppliers
 */
?>
<div class="page-title mb-4">
    <h5><i class="fas fa-edit me-2 text-gold"></i>Edit Product</h5>
</div>

<div class="glass-card p-0">
    <div class="card-header">
        <div class="card-header-left">
            <i class="fas fa-box card-icon"></i>
            <h6 class="card-title">Product Information</h6>
        </div>
        <div>
            <span class="text-muted small"><span class="text-danger">*</span> Required fields</span>
        </div>
    </div>
    <div class="card-body">
        <form method="POST" action="<?= url('inventory/update/' . $product->id) ?>">
            <?= csrf_field() ?>

            <div class="row g-3">
                <div class="col-md-6">
                    <div class="mb-3">
                        <label class="form-label">Product Name <span class="text-danger">*</span></label>
                        <input type="text" name="name" class="form-control bg-transparent <?= errorClass('name') ?>"
                               value="<?= e(old('name', $product->name)) ?>" required maxlength="255">
                        <?= errorMsg('name') ?>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="mb-3">
                        <label class="form-label">SKU</label>
                        <input type="text" name="sku" class="form-control bg-transparent <?= errorClass('sku') ?>"
                               value="<?= e(old('sku', $product->sku)) ?>" maxlength="100">
                        <?= errorMsg('sku') ?>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="mb-3">
                        <label class="form-label">Barcode</label>
                        <input type="text" name="barcode" class="form-control bg-transparent <?= errorClass('barcode') ?>"
                               value="<?= e(old('barcode', $product->barcode)) ?>" maxlength="100">
                        <?= errorMsg('barcode') ?>
                    </div>
                </div>
            </div>

            <div class="row g-3">
                <div class="col-md-4">
                    <div class="mb-3">
                        <label class="form-label">Category</label>
                        <select name="category_id" class="form-select bg-transparent <?= errorClass('category_id') ?>" required>
                            <?php foreach ($categories as $cat): ?>
                                <option value="<?= $cat->id ?>" <?= (old('category_id', $product->category_id) == $cat->id) ? 'selected' : '' ?>>
                                    <?= e($cat->name) ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                        <?= errorMsg('category_id') ?>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="mb-3">
                        <label class="form-label">Supplier</label>
                        <select name="supplier_id" class="form-select bg-transparent <?= errorClass('supplier_id') ?>">
                            <option value="">Select Supplier</option>
                            <?php foreach ($suppliers as $sup): ?>
                                <option value="<?= $sup->id ?>" <?= (old('supplier_id', $product->supplier_id) == $sup->id) ? 'selected' : '' ?>>
                                    <?= e($sup->name) ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                        <?= errorMsg('supplier_id') ?>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="mb-3">
                        <label class="form-label">Unit Type</label>
                        <select name="unit_type" class="form-select bg-transparent <?= errorClass('unit_type') ?>">
                            <option value="">Select Unit</option>
                            <?php
                            $unitTypes = ['piece', 'bottle', 'tube', 'jar', 'box', 'pack', 'litre', 'ml', 'gram', 'kg', 'set', 'pair', 'roll', 'other'];
                            $selectedUnit = old('unit_type', $product->unit_type ?? '');
                            foreach ($unitTypes as $ut):
                            ?>
                                <option value="<?= $ut ?>" <?= $selectedUnit === $ut ? 'selected' : '' ?>>
                                    <?= ucfirst($ut) ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                        <?= errorMsg('unit_type') ?>
                    </div>
                </div>
            </div>

            <div class="row g-3">
                <div class="col-md-12">
                    <div class="mb-3">
                        <label class="form-label">Description</label>
                        <textarea name="description" class="form-control bg-transparent <?= errorClass('description') ?>"
                                  rows="3" maxlength="2000"><?= e(old('description', $product->description ?? '')) ?></textarea>
                        <?= errorMsg('description') ?>
                    </div>
                </div>
            </div>

            <div class="row g-3">
                <div class="col-md-3">
                    <div class="mb-3">
                        <label class="form-label">Unit Price <span class="text-danger">*</span></label>
                        <input type="number" step="0.01" min="0" name="unit_price"
                               class="form-control bg-transparent <?= errorClass('unit_price') ?>"
                               value="<?= e(old('unit_price', $product->unit_price ?? 0)) ?>">
                        <?= errorMsg('unit_price') ?>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="mb-3">
                        <label class="form-label">Cost Price</label>
                        <input type="number" step="0.01" min="0" name="cost_price"
                               class="form-control bg-transparent <?= errorClass('cost_price') ?>"
                               value="<?= e(old('cost_price', $product->cost_price ?? 0)) ?>">
                        <?= errorMsg('cost_price') ?>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="mb-3">
                        <label class="form-label">Tax (%)</label>
                        <input type="number" step="0.01" min="0" max="100" name="tax_percent"
                               class="form-control bg-transparent <?= errorClass('tax_percent') ?>"
                               value="<?= e(old('tax_percent', $product->tax_percent ?? 0)) ?>">
                        <?= errorMsg('tax_percent') ?>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="mb-3">
                        <label class="form-label">Reorder Level</label>
                        <input type="number" min="0" name="reorder_level"
                               class="form-control bg-transparent <?= errorClass('reorder_level') ?>"
                               value="<?= e(old('reorder_level', $product->reorder_level ?? 0)) ?>">
                        <?= errorMsg('reorder_level') ?>
                    </div>
                </div>
            </div>

            <div class="row g-3">
                <div class="col-md-4">
                    <div class="mb-3">
                        <label class="form-label">Current Stock</label>
                        <input type="number" min="0" name="current_stock"
                               class="form-control bg-transparent <?= errorClass('current_stock') ?>"
                               value="<?= e(old('current_stock', $product->current_stock ?? 0)) ?>">
                        <?= errorMsg('current_stock') ?>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="mb-3">
                        <label class="form-label">Min Stock</label>
                        <input type="number" min="0" name="min_stock"
                               class="form-control bg-transparent <?= errorClass('min_stock') ?>"
                               value="<?= e(old('min_stock', $product->min_stock ?? 0)) ?>">
                        <?= errorMsg('min_stock') ?>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="mb-3">
                        <label class="form-label">Max Stock</label>
                        <input type="number" min="0" name="max_stock"
                               class="form-control bg-transparent <?= errorClass('max_stock') ?>"
                               value="<?= e(old('max_stock', $product->max_stock ?? 0)) ?>">
                        <?= errorMsg('max_stock') ?>
                    </div>
                </div>
            </div>

            <div class="row g-3 mb-3">
                <div class="col-md-6">
                    <div class="form-check">
                        <input type="checkbox" name="track_inventory" class="form-check-input" value="1"
                               id="track_inventory" <?= (old('track_inventory', $product->track_inventory ?? 1) == 1) ? 'checked' : '' ?>>
                        <label class="form-check-label" for="track_inventory">Track Inventory</label>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="form-check">
                        <input type="checkbox" name="is_active" class="form-check-input" value="1"
                               id="is_active" <?= (old('is_active', $product->is_active ?? 1) == 1) ? 'checked' : '' ?>>
                        <label class="form-check-label" for="is_active">Active</label>
                    </div>
                </div>
            </div>

            <div class="mt-4 d-flex gap-2">
                <button type="submit" class="btn btn-gold">
                    <i class="fas fa-save me-1"></i>Update Product
                </button>
                <a href="<?= url('inventory') ?>" class="btn btn-outline-secondary">
                    <i class="fas fa-times me-1"></i>Cancel
                </a>
            </div>
        </form>
    </div>
</div>
