<?php $isEdit = $action === 'edit'; ?>

<h2><?= $isEdit ? 'Редактирование товара' : 'Добавление товара' ?></h2>

<a href="/admin/products" class="btn btn-primary" style="margin-bottom: 20px;">
    <i class="fas fa-arrow-left"></i> Назад к списку
</a>

<div class="form-container">
    <form action="/admin/products/<?= $isEdit ? 'edit/' . $product['product_id'] : 'add' ?>" method="POST">
        <div class="form-group">
            <label>Название товара *</label>
            <input type="text" name="name" class="form-control" 
                   value="<?= htmlspecialchars($product['name'] ?? '') ?>" required>
        </div>

        <div class="form-group">
            <label>Категория *</label>
            <select name="category_id" class="form-control" required>
                <option value="">Выберите категорию</option>
                <?php foreach ($categories as $cat): ?>
                <option value="<?= $cat['category_id'] ?>" 
                        <?= ($product['category_id'] ?? 0) == $cat['category_id'] ? 'selected' : '' ?>>
                    <?= htmlspecialchars($cat['name']) ?>
                </option>
                <?php endforeach; ?>
            </select>
        </div>

        <div class="form-group">
            <label>Описание</label>
            <textarea name="description" class="form-control" rows="4"><?= htmlspecialchars($product['description'] ?? '') ?></textarea>
        </div>

        <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 15px;">
            <div class="form-group">
                <label>Цена *</label>
                <input type="number" name="price" class="form-control" step="0.01"
                       value="<?= $product['price'] ?? '' ?>" required>
            </div>

            <div class="form-group">
                <label>Старая цена (для скидки)</label>
                <input type="number" name="old_price" class="form-control" step="0.01"
                       value="<?= $product['old_price'] ?? '' ?>">
            </div>
        </div>

        <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 15px;">
            <div class="form-group">
                <label>Артикул (SKU)</label>
                <input type="text" name="sku" class="form-control"
                       value="<?= htmlspecialchars($product['sku'] ?? '') ?>"
                       placeholder="Автоматически, если пусто">
            </div>

            <div class="form-group">
                <label>Количество на складе *</label>
                <input type="number" name="stock_quantity" class="form-control"
                       value="<?= $product['stock_quantity'] ?? 0 ?>" required>
            </div>
        </div>

        <div class="form-group">
            <label>URL изображения</label>
            <input type="text" name="image_url" class="form-control"
                   value="<?= htmlspecialchars($product['image_url'] ?? '') ?>"
                   placeholder="img/название.jpg">
        </div>

        <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 15px;">
            <div class="form-group">
                <label>Цвет</label>
                <input type="text" name="color" class="form-control"
                       value="<?= htmlspecialchars($product['color'] ?? '') ?>">
            </div>

            <div class="form-group">
                <label>Материал</label>
                <input type="text" name="material" class="form-control"
                       value="<?= htmlspecialchars($product['material'] ?? '') ?>">
            </div>
        </div>

        <div class="form-group">
            <label>
                <input type="checkbox" name="is_available" value="1" 
                       <?= ($product['is_available'] ?? true) ? 'checked' : '' ?>>
                Товар доступен для покупки
            </label>
        </div>

        <div class="form-group">
            <label>
                <input type="checkbox" name="is_featured" value="1" 
                       <?= ($product['is_featured'] ?? false) ? 'checked' : '' ?>>
                Рекомендуемый товар
            </label>
        </div>

        <button type="submit" class="btn btn-success">
            <i class="fas fa-save"></i> <?= $isEdit ? 'Сохранить изменения' : 'Добавить товар' ?>
        </button>
    </form>
</div>

