gdias před 4 měsíci
rodič
revize
d8614f871a
1 změnil soubory, kde provedl 108 přidání a 6 odebrání
  1. 108 6
      src/lib/component/Product.svelte

+ 108 - 6
src/lib/component/Product.svelte

@@ -21,6 +21,7 @@
 	let noCategory = false;
 	let isAddingProduct = false;
 	let editingProductId = null;
+	let isEditingProduct = false;
 	let productFormData = {
 		name: '',
 		category: '',
@@ -47,6 +48,7 @@
 		};
 		editingProductId = null;
 		isAddingProduct = false;
+		isEditingProduct = false;
 	}
 
 	function handleProductSubmit(event) {
@@ -142,7 +144,7 @@
 			sendToKitchen: product.sendToKitchen
 		};
 		editingProductId = product.id;
-		isAddingProduct = true;
+		isEditingProduct = true;
 	}
 
 	async function handleUpdateProduct(event) {
@@ -159,7 +161,7 @@
 			update_product_id: editingProductId,
 			product_name: productFormData.name,
 			product_price: Number(productFormData.price),
-			product_is_kitchen: productFormData.sendToKitchen ? 1 : 0,
+			product_is_kitchen: productFormData.sendToKitchen,
 			company_id: company
 		});
 
@@ -174,14 +176,43 @@
 			const response = await fetch('https://dev2.mixtech.dev.br/product/update', requestOptions);
 			const result = await response.json();
 
-			if (result.status === 'ok') {
-				//console.log('Produto atualizado com sucesso!');
-				isAddingProduct = false;
+			if (result.status === 'ok' && productFormData.description != '') {
+				// Atualizar a descrição após o produto
+				const descPayload = {
+					product_id: editingProductId,
+					company_id: company,
+					description_text: productFormData.description
+				};
+
+				try {
+					const descRes = await fetch('https://dev2.mixtech.dev.br/description/update', {
+						method: 'POST',
+						headers: {
+							'Content-Type': 'application/json',
+							Authorization: `Bearer ${token}`
+						},
+						body: JSON.stringify(descPayload),
+						redirect: 'follow'
+					});
+
+					const descResult = await descRes.json();
+
+					if (descResult.status === 'ok') {
+						console.log('Descrição atualizada com sucesso!');
+					} else {
+						console.error('Erro ao atualizar descrição:', descResult.msg);
+					}
+				} catch (error) {
+					console.error(error);
+				}
+
+				isEditingProduct = false;
 				editingProductId = null;
-				// Aqui você pode recarregar a lista de produtos, se quiser
+				fetchAllItems(); // para recarregar lista
 			} else {
 				console.error('Erro ao atualizar produto:', result.msg || result);
 			}
+			isEditingProduct = false;
 		} catch (error) {
 			console.error('Erro na requisição de atualização:', error);
 		}
@@ -508,6 +539,77 @@
 							>
 						</div>
 
+						<div class="flex space-x-2">
+							<button
+								type="button"
+								on:click={resetProductForm}
+								class="rounded-lg bg-gray-700 px-4 py-2 hover:bg-gray-600"
+							>
+								Cancelar
+							</button>
+							<button
+								type="submit"
+								class="flex items-center rounded-lg bg-emerald-600 px-4 py-2 hover:bg-emerald-700"
+							>
+								<img src={save_icon} alt="Salvar" class="mr-2 h-4 w-4" /> Salvar
+							</button>
+						</div>
+					</form>
+				{:else if isEditingProduct}
+					<form on:submit={handleUpdateProduct} class="space-y-4 p-4">
+						<div>
+							<p class="mb-1 block text-sm text-gray-400">Nome do Produto</p>
+							<input
+								bind:value={productFormData.name}
+								class="w-full rounded-md border border-gray-600 bg-gray-700 px-3 py-2 focus:ring-emerald-500"
+								placeholder="Ex: Cerveja..."
+							/>
+						</div>
+						<div>
+							<p class="mb-1 block text-sm text-gray-400">Descrição do Produto</p>
+							<input
+								bind:value={productFormData.description}
+								class="w-full rounded-md border border-gray-600 bg-gray-700 px-3 py-2 focus:ring-emerald-500"
+								placeholder="Descrição"
+							/>
+						</div>
+
+						<div>
+							<p class="mb-1 block text-sm text-gray-400">Categoria</p>
+							<select
+								bind:value={productFormData.category}
+								class="w-full rounded-md border border-gray-600 bg-gray-700 px-3 py-2 focus:ring-emerald-500"
+							>
+								{#each categories as category}
+									<option value={category.name}>{category.name}</option>
+								{/each}
+							</select>
+						</div>
+
+						<div>
+							<p class="mb-1 block text-sm text-gray-400">Preço (R$)</p>
+							<input
+								type="number"
+								min="0"
+								step="0.01"
+								bind:value={productFormData.price}
+								class="w-full rounded-md border border-gray-600 bg-gray-700 px-3 py-2 focus:ring-emerald-500"
+								placeholder="0.00"
+							/>
+						</div>
+
+						<div class="flex items-center">
+							<input
+								id="sendToKitchen"
+								type="checkbox"
+								bind:checked={productFormData.sendToKitchen}
+								class="h-4 w-4 rounded border border-gray-600 bg-gray-700"
+							/>
+							<label for="sendToKitchen" class="ml-2 text-sm text-gray-300"
+								>Enviar para a cozinha</label
+							>
+						</div>
+
 						<div class="flex space-x-2">
 							<button
 								type="button"