fetchAll("SELECT category_id, name FROM categories WHERE is_active = 1 ORDER BY name ASC"); $productTypesList = db()->fetchAll("SELECT type_id, name FROM product_types WHERE is_active = 1 ORDER BY sort_order ASC, name ASC"); $productId = $_GET['id'] ?? ''; $product = null; $isEdit = false; $errors = []; if ($productId) { $product = db()->fetch("SELECT * FROM products WHERE product_id = :id", ['id' => $productId]); if ($product) { $isEdit = true; $pageTitle = 'Edit Product'; $product['images'] = json_decode($product['images'] ?? '[]', true); $product['tags'] = json_decode($product['tags'] ?? '[]', true); } } if (!$product) { $product = [ 'product_id' => '', 'name' => '', 'description' => '', 'price' => '', 'sale_price' => '', 'cost_price' => '', 'sku' => '', 'barcode' => '', 'category' => '', 'tags' => [], 'images' => [], 'stock' => 100, 'low_stock_threshold' => 10, 'weight' => '', 'is_active' => 1, 'is_featured' => 0 ]; $pageTitle = 'Add Product'; } // Handle form submission if ($_SERVER['REQUEST_METHOD'] === 'POST') { $name = trim($_POST['name'] ?? ''); $description = trim($_POST['description'] ?? ''); $price = floatval($_POST['price'] ?? 0); $salePrice = !empty($_POST['sale_price']) ? floatval($_POST['sale_price']) : null; $costPrice = !empty($_POST['cost_price']) ? floatval($_POST['cost_price']) : null; $sku = trim($_POST['sku'] ?? ''); $barcode = trim($_POST['barcode'] ?? ''); $category = trim($_POST['category'] ?? ''); $stock = intval($_POST['stock'] ?? 0); $lowStockThreshold = intval($_POST['low_stock_threshold'] ?? 10); $weight = !empty($_POST['weight']) ? floatval($_POST['weight']) : null; $isActive = isset($_POST['is_active']) ? 1 : 0; $isFeatured = isset($_POST['is_featured']) ? 1 : 0; $imageUrls = array_filter(array_map('trim', explode("\n", $_POST['image_urls'] ?? ''))); // Validate if (empty($name)) $errors['name'] = 'Product name is required'; if ($price <= 0) $errors['price'] = 'Price must be greater than 0'; if (empty($errors)) { $data = [ 'name' => $name, 'description' => $description, 'price' => $price, 'sale_price' => $salePrice, 'cost_price' => $costPrice, 'sku' => $sku ?: null, 'barcode' => $barcode ?: null, 'category' => $category ?: null, 'product_type_id' => trim($_POST['product_type_id'] ?? '') ?: null, 'images' => json_encode($imageUrls), 'stock' => $stock, 'low_stock_threshold' => $lowStockThreshold, 'weight' => $weight, 'is_active' => $isActive, 'is_featured' => $isFeatured ]; if ($isEdit) { db()->update('products', $data, 'product_id = :id', ['id' => $productId]); setFlash('success', 'Product updated successfully'); } else { $data['product_id'] = generateId('prod_'); db()->insert('products', $data); setFlash('success', 'Product created successfully'); } header('Location: /admin/products.php'); exit; } // Keep form values on error $product = array_merge($product, $_POST); $product['images'] = $imageUrls; } // Get categories for dropdown $categories = db()->fetchAll( "SELECT DISTINCT category FROM products WHERE category IS NOT NULL AND category != '' ORDER BY category" ); ?>