<div class="page-header">
    <div>
        <a href="<?= APP_URL ?>/products" 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 Products
        </a>
        <h1>Edit Product: <?= e($product['name']) ?></h1>
    </div>
</div>

<form action="<?= APP_URL ?>/products/<?= $product['id'] ?>/edit" method="POST" enctype="multipart/form-data">
    <?= Security::csrfField() ?>

    <div class="grid-2">
        <div class="card mb-6">
            <div class="card-header"><h3 class="card-title">Basic Details</h3></div>
            
            <div class="form-group">
                <label class="form-label">Product Name <span class="required">*</span></label>
                <input type="text" name="name" class="form-control" value="<?= e($product['name']) ?>" required>
            </div>

            <div class="form-row">
                <div class="form-group">
                    <label class="form-label">SKU <span class="required">*</span></label>
                    <input type="text" name="sku" class="form-control" value="<?= e($product['sku']) ?>" required>
                </div>
                <div class="form-group">
                    <label class="form-label">Barcode</label>
                    <input type="text" name="barcode" class="form-control" value="<?= e($product['barcode']) ?>">
                </div>
            </div>

            <div class="form-row">
                <div class="form-group">
                    <label class="form-label">Category</label>
                    <select name="category_id" class="form-control">
                        <option value="">Select Category...</option>
                        <?php foreach ($categories as $cat): ?>
                            <option value="<?= $cat['id'] ?>" <?= $product['category_id'] == $cat['id'] ? 'selected' : '' ?>><?= e($cat['name']) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="form-group">
                    <label class="form-label">Brand</label>
                    <select name="brand_id" class="form-control">
                        <option value="">Select Brand...</option>
                        <?php foreach ($brands as $brand): ?>
                            <option value="<?= $brand['id'] ?>" <?= $product['brand_id'] == $brand['id'] ? 'selected' : '' ?>><?= e($brand['name']) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
            </div>

            <div class="form-group">
                <label class="form-label">Product Image</label>
                <?php if ($product['image']): ?>
                    <div class="mb-2">
                        <img src="<?= APP_URL ?>/uploads/<?= Auth::tenantId() ?>/<?= e($product['image']) ?>" style="width:100px; height:100px; object-fit:cover; border-radius:8px; border:1px solid var(--border);">
                    </div>
                <?php endif; ?>
                <input type="file" name="image" class="form-control" accept=".jpg,.jpeg,.png,.webp">
                <div class="form-hint">Leave empty to keep current image.</div>
            </div>
            
            <div class="form-group">
                <label class="form-label">Description</label>
                <textarea name="description" class="form-control"><?= e($product['description']) ?></textarea>
            </div>
        </div>

        <div>
            <div class="card mb-6">
                <div class="card-header"><h3 class="card-title">Pricing & Units</h3></div>
                
                <div class="form-row">
                    <div class="form-group">
                        <label class="form-label">Cost Price</label>
                        <div class="input-group">
                            <span class="input-group-text"><?= setting('currency_symbol', '$') ?></span>
                            <input type="number" step="0.01" name="cost_price" class="form-control" value="<?= $product['cost_price'] ?>">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="form-label">Selling Price <span class="required">*</span></label>
                        <div class="input-group">
                            <span class="input-group-text"><?= setting('currency_symbol', '$') ?></span>
                            <input type="number" step="0.01" name="selling_price" class="form-control" value="<?= $product['selling_price'] ?>" required>
                        </div>
                    </div>
                </div>

                <div class="form-row">
                    <div class="form-group">
                        <label class="form-label">Base Unit</label>
                        <input type="text" name="unit" class="form-control" value="<?= e($product['unit']) ?>">
                    </div>
                    <div class="form-group">
                        <label class="form-label">Low Stock Alert</label>
                        <input type="number" name="alert_qty" class="form-control" value="<?= $product['alert_qty'] ?>">
                    </div>
                </div>

                <hr style="border:0; border-top:1px solid var(--border); margin:20px 0;">

                <div class="form-group">
                    <label class="form-label">Wholesale Price</label>
                    <div class="input-group">
                        <span class="input-group-text"><?= setting('currency_symbol', '$') ?></span>
                        <input type="number" step="0.01" name="wholesale_price" class="form-control" value="<?= $product['wholesale_price'] ?>">
                    </div>
                </div>
                
                <div class="form-row">
                    <div class="form-group">
                        <label class="form-label">Wholesale Unit</label>
                        <input type="text" name="wholesale_unit" class="form-control" value="<?= e($product['wholesale_unit']) ?>">
                    </div>
                    <div class="form-group">
                        <label class="form-label">Items per Wholesale Unit</label>
                        <input type="number" name="wholesale_qty" class="form-control" value="<?= $product['wholesale_qty'] ?>">
                    </div>
                </div>
            </div>

            <div class="card mb-6">
                <div class="card-header"><h3 class="card-title">Tracking Features</h3></div>
                
                <?php $planHasBatch = PlanGuard::hasFeature('batch_tracking'); ?>
                <div class="form-group">
                    <label class="form-switch <?= !$planHasBatch ? 'opacity-50' : '' ?>">
                        <input type="checkbox" name="batch_tracking" value="1" class="switch-input" <?= $product['batch_tracking'] ? 'checked' : '' ?> <?= !$planHasBatch ? 'disabled' : '' ?>>
                        <div class="switch-track"></div>
                        <span class="switch-label">Enable Batch & Expiry Tracking</span>
                    </label>
                </div>

                <div class="form-group">
                    <label class="form-switch">
                        <input type="checkbox" name="serial_tracking" value="1" class="switch-input" <?= $product['serial_tracking'] ? 'checked' : '' ?>>
                        <div class="switch-track"></div>
                        <span class="switch-label">Enable Serial Number Tracking</span>
                    </label>
                </div>
            </div>
            
            <div class="flex justify-between">
                <a href="<?= APP_URL ?>/products" class="btn btn-outline">Cancel</a>
                <button type="submit" class="btn btn-primary">Update Product</button>
            </div>
        </div>
    </div>
</form>
