0, 'name' => '', 'slug' => '', 'description' => '', 'logo' => '', 'featured' => 0, 'status' => 1]; if ($id) { $stmt = $db->prepare("SELECT * FROM brands WHERE id = ?"); $stmt->execute([$id]); $brand = $stmt->fetch(PDO::FETCH_ASSOC); if (!$brand) { admin_set_flash('error', 'Not found'); header('Location: ' . ADMIN_PATH . 'pages/brands.php'); exit; } $page_title = 'Edit Brand'; } if ($_SERVER['REQUEST_METHOD'] == 'POST' && verify_csrf($_POST[CSRF_TOKEN_NAME] ?? '')) { $name = trim($_POST['name']); $slug = !empty($_POST['slug']) ? create_slug($_POST['slug']) : create_slug($name); $description = trim($_POST['description'] ?? ''); $featured = isset($_POST['featured']) ? 1 : 0; $status = intval($_POST['status']); $logo = $brand['logo'] ?? ''; if (isset($_FILES['logo']) && $_FILES['logo']['error'] == 0) { $new_logo = upload_image($_FILES['logo'], 'brands', $name); if ($new_logo) $logo = $new_logo; } try { if ($id) { $stmt = $db->prepare("UPDATE brands SET name=?, slug=?, description=?, logo=?, featured=?, status=? WHERE id=?"); $stmt->execute([$name, $slug, $description, $logo, $featured, $status, $id]); admin_set_flash('success', 'Brand updated'); } else { $stmt = $db->prepare("INSERT INTO brands (name, slug, description, logo, featured, status) VALUES (?, ?, ?, ?, ?, ?)"); $stmt->execute([$name, $slug, $description, $logo, $featured, $status]); admin_set_flash('success', 'Brand added'); } header('Location: ' . ADMIN_PATH . 'pages/brands.php'); exit; } catch (Exception $e) { $error = 'Failed: ' . $e->getMessage(); } } ?>