DmitrMakeev commited on
Commit
66896aa
·
verified ·
1 Parent(s): 7792ca7

Update nutri_call.html

Browse files
Files changed (1) hide show
  1. nutri_call.html +69 -37
nutri_call.html CHANGED
@@ -331,49 +331,55 @@
331
 
332
  <script>
333
  document.getElementById('calculate-btn').addEventListener('click', function() {
334
- // 1. Собираем данные (как РАБОТАЛО РАНЬШЕ)
 
 
 
 
 
 
335
  const requestData = {
336
  fertilizerConstants: {
337
  "Кальциевая селитра": {
338
- "N (NO3-)": parseFloat(document.getElementById('fert_ca_no3').value) / 100,
339
- "Ca": parseFloat(document.getElementById('fert_ca_ca').value) / 100
340
  },
341
  "Калий азотнокислый": {
342
- "N (NO3-)": parseFloat(document.getElementById('fert_kno3_no3').value) / 100,
343
- "K": parseFloat(document.getElementById('fert_kno3_k').value) / 100
344
  },
345
  "Аммоний азотнокислый": {
346
- "N (NO3-)": parseFloat(document.getElementById('fert_nh4no3_no3').value) / 100,
347
- "N (NH4+)": parseFloat(document.getElementById('fert_nh4no3_nh4').value) / 100
348
  },
349
  "Сульфат магния": {
350
- "Mg": parseFloat(document.getElementById('fert_mgso4_mg').value) / 100,
351
- "S": parseFloat(document.getElementById('fert_mgso4_s').value) / 100
352
  },
353
  "Монофосфат калия": {
354
- "P": parseFloat(document.getElementById('fert_kh2po4_p').value) / 100,
355
- "K": parseFloat(document.getElementById('fert_kh2po4_k').value) / 100
356
  },
357
  "Калий сернокислый": {
358
- "K": parseFloat(document.getElementById('fert_k2so4_k').value) / 100,
359
- "S": parseFloat(document.getElementById('fert_k2so4_s').value) / 100
360
  }
361
  },
362
  profileSettings: {
363
- 'P': parseFloat(document.getElementById('profile_p').value),
364
- 'K': parseFloat(document.getElementById('profile_k').value),
365
- 'Mg': parseFloat(document.getElementById('profile_mg').value),
366
- 'Ca': parseFloat(document.getElementById('profile_ca').value),
367
- 'S': parseFloat(document.getElementById('profile_s').value),
368
- 'N (NO3-)': parseFloat(document.getElementById('profile_no3').value),
369
- 'N (NH4+)': parseFloat(document.getElementById('profile_nh4').value),
370
- 'liters': parseInt(document.getElementById('liters-input').value) || 1
371
  }
372
  };
373
 
374
- console.log("Отправляемые данные:", requestData);
375
 
376
- // 2. Отправка (старый рабочий вариант)
377
  fetch('/calculation', {
378
  method: 'POST',
379
  headers: {
@@ -381,11 +387,23 @@ document.getElementById('calculate-btn').addEventListener('click', function() {
381
  },
382
  body: JSON.stringify(requestData)
383
  })
384
- .then(response => response.json())
 
 
 
 
 
 
 
385
  .then(data => {
386
- console.log("ОТВЕТ СЕРВЕРА:", data);
387
 
388
- // 3. Заполнение результатов (как было)
 
 
 
 
 
389
  const fertilizerMap = {
390
  "Сульфат магния": "magnesium_sulfate",
391
  "Кальциевая селитра": "calcium_nitrate",
@@ -394,21 +412,35 @@ document.getElementById('calculate-btn').addEventListener('click', function() {
394
  "Калий сернокислый": "potassium_sulfate",
395
  "Калий азотнокислый": "potassium_nitrate"
396
  };
397
-
398
- // Заполняем граммовки
399
- data.fertilizers.forEach(fert => {
400
- const fieldId = fertilizerMap[fert.name];
401
- if (fieldId) {
402
- document.getElementById(fieldId).value = fert.grams.toFixed(3);
403
- }
404
  });
405
-
 
 
 
 
 
 
 
 
 
 
 
406
  // Выводим EC
407
- document.getElementById('ec-value').textContent = data.ec.toFixed(2);
 
 
 
 
 
408
  })
409
  .catch(error => {
410
- console.error("ОШИБКА:", error);
411
- alert("Ошибка расчета: " + error.message);
412
  });
413
  });
414
  </script>
 
331
 
332
  <script>
333
  document.getElementById('calculate-btn').addEventListener('click', function() {
334
+ // 1. Собираем данные с защитой от null/undefined
335
+ const getValue = (id) => {
336
+ const val = parseFloat(document.getElementById(id)?.value);
337
+ return isNaN(val) ? 0 : val;
338
+ };
339
+
340
+ // 2. Формируем запрос
341
  const requestData = {
342
  fertilizerConstants: {
343
  "Кальциевая селитра": {
344
+ "N (NO3-)": getValue('fert_ca_no3') / 100,
345
+ "Ca": getValue('fert_ca_ca') / 100
346
  },
347
  "Калий азотнокислый": {
348
+ "N (NO3-)": getValue('fert_kno3_no3') / 100,
349
+ "K": getValue('fert_kno3_k') / 100
350
  },
351
  "Аммоний азотнокислый": {
352
+ "N (NO3-)": getValue('fert_nh4no3_no3') / 100,
353
+ "N (NH4+)": getValue('fert_nh4no3_nh4') / 100
354
  },
355
  "Сульфат магния": {
356
+ "Mg": getValue('fert_mgso4_mg') / 100,
357
+ "S": getValue('fert_mgso4_s') / 100
358
  },
359
  "Монофосфат калия": {
360
+ "P": getValue('fert_kh2po4_p') / 100,
361
+ "K": getValue('fert_kh2po4_k') / 100
362
  },
363
  "Калий сернокислый": {
364
+ "K": getValue('fert_k2so4_k') / 100,
365
+ "S": getValue('fert_k2so4_s') / 100
366
  }
367
  },
368
  profileSettings: {
369
+ 'P': getValue('profile_p'),
370
+ 'K': getValue('profile_k'),
371
+ 'Mg': getValue('profile_mg'),
372
+ 'Ca': getValue('profile_ca'),
373
+ 'S': getValue('profile_s'),
374
+ 'N (NO3-)': getValue('profile_no3'),
375
+ 'N (NH4+)': getValue('profile_nh4'),
376
+ 'liters': parseInt(document.getElementById('liters-input')?.value) || 1
377
  }
378
  };
379
 
380
+ console.log("Отправляемые данные:", JSON.stringify(requestData, null, 2));
381
 
382
+ // 3. Отправка на сервер
383
  fetch('/calculation', {
384
  method: 'POST',
385
  headers: {
 
387
  },
388
  body: JSON.stringify(requestData)
389
  })
390
+ .then(response => {
391
+ if (!response.ok) {
392
+ return response.json().then(err => {
393
+ throw new Error(err.message || `HTTP error! status: ${response.status}`);
394
+ });
395
+ }
396
+ return response.json();
397
+ })
398
  .then(data => {
399
+ console.log("Ответ сервера:", data);
400
 
401
+ // 4. Обработка ответа
402
+ if (data.error) {
403
+ throw new Error(data.error);
404
+ }
405
+
406
+ // Заполнение результатов
407
  const fertilizerMap = {
408
  "Сульфат магния": "magnesium_sulfate",
409
  "Кальциевая селитра": "calcium_nitrate",
 
412
  "Калий сернокислый": "potassium_sulfate",
413
  "Калий азотнокислый": "potassium_nitrate"
414
  };
415
+
416
+ // Сначала обнуляем все поля
417
+ Object.values(fertilizerMap).forEach(id => {
418
+ const field = document.getElementById(id);
419
+ if (field) field.value = "0";
 
 
420
  });
421
+
422
+ // Заполняем данные из ответа
423
+ if (data.fertilizers && Array.isArray(data.fertilizers)) {
424
+ data.fertilizers.forEach(fert => {
425
+ const fieldId = fertilizerMap[fert.name];
426
+ if (fieldId) {
427
+ const field = document.getElementById(fieldId);
428
+ if (field) field.value = fert.grams?.toFixed(3) || "0";
429
+ }
430
+ });
431
+ }
432
+
433
  // Выводим EC
434
+ if (data.ec !== undefined) {
435
+ const ecField = document.getElementById('ec-value');
436
+ if (ecField) ecField.textContent = data.ec.toFixed(2);
437
+ }
438
+
439
+ alert("Расчет успешно завершен!");
440
  })
441
  .catch(error => {
442
+ console.error("Ошибка:", error);
443
+ alert(`Ошибка расчета: ${error.message}`);
444
  });
445
  });
446
  </script>