в итоге сделал так понимаю что это не лучший вариант, но хоть работает. Буду ещё думать, как оптимизировать весь процесс.
var orderNumber = document.getElementById('id_order').value;
var department = document.getElementById('department').value;
var customer = document.getElementById('customer').value;
var contractNumber = document.getElementById('id_contract').value;
var objectName = document.getElementById('name_object').value;
var orderDate = document.getElementById('date_contract').value;
var note = document.getElementById('note').value;
// Создаем массив с полученными значениями
var valuesArray = [orderNumber, department, customer, contractNumber, objectName, orderDate, note];
// Получаем значения из блоков <td>
var tdElements = document.getElementsByClassName('val');
var tdValuesArray = [];
for (var i = 0; i < tdElements.length; i++) {
var value = tdElements[i].value;
tdValuesArray.push(value);
}
var tdInputs = document.getElementsByClassName('number');
var tdInputArray =[];
for (var i = 0; i < tdInputs.length; i++){
var value = tdInputs[i].value;
tdInputArray.push(value);
}
var printer_line = tdValuesArray.concat(tdInputArray);
var mergedArray =valuesArray.concat(printer_line);
if (isAddFormatClicked == false && isAddRowClicked == false){
spetional();
}
handleAddFormat();
if (isAddRowClicked == true){
const selectElements1 = document.querySelectorAll('.val1');
var selectElementsArray1 = [];
for (var i = 0; i < selectElements1.length; i++) {
var value = selectElements1[i].value;
selectElementsArray1.push(value);
}
var tdInputs1 = document.getElementsByClassName('number1');
var tdInputArray1 =[];
for (var i = 0; i < tdInputs1.length; i++){
var value = tdInputs1[i].value;
tdInputArray1.push(value);
}
var printer_line1 = selectElementsArray1.concat(tdInputArray1);
console.log("Что в printer_line1", printer_line1);
console.log("чё тут",isAddFormatClicked1, selectedFormat1)
main_condition(selectElements1);
setTimeout(()=>{
additional_dispatch1();
console.log("printer_line1 не завимо была ли нажата кнопка", printer_line1)
},1000);
}
async function main_condition(selectElements1) {
console.log("доступ к isAddFormatClicked1, selectedFormat1 в фукции",isAddFormatClicked1, selectedFormat1)
if (isAddFormatClicked1 == true) {
if(selectedFormat1 == null){
console.log("проверка доступен ли массив selectElements1", selectElements1);
const newSelect = document.createElement('select1');
const selectedPrinterId = selectElements[1].value;
console.log("что в newSelect, selectedPrinterId", newSelect, selectedPrinterId);
setTimeout(async () => {
const selectedFormat = await fetchDataAndProcess(newSelect, selectedPrinterId);
console.log("прошёл selectedFormat", selectedFormat);
printer_line1.splice(5, 1);
printer_line1.splice(5, 0, selectedFormat.toString());
additional_dispatch1()
},2000);
}else{
setTimeout(async () => {
console.log("проверяю что selectedFormat1 точно не null", selectedFormat1);
printer_line1.splice(5, 1);
printer_line1.splice(5, 0, selectedFormat1.toString());
additional_dispatch1()
},2000);
}
}
}
async function handleAddFormat(){
console.log("handleAddFormat в нём", isAddFormatClicked, selectedFormat)
if(isAddFormatClicked == true){
printer_line.splice(5, 1);
if(selectedFormat == null){
const newSelect = document.createElement('select');
const selectedPrinterId = selectElements[1].value;
const selectedFormat = await fetchDataAndProcess(newSelect, selectedPrinterId);
printer_line.splice(5, 0, selectedFormat.toString());
main_dispatch();
additional_dispatch();
} else{
console.log("я сюда то зашёл или нет")
printer_line.splice(5, 0, selectedFormat.toString());
main_dispatch();
additional_dispatch();
}
}else{
main_dispatch();
}
}
async function spetional(){
if(isAddFormatClicked == true){
main_dispatch();
printer_line.splice(5, 1);
if(selectedFormat == null){
const newSelect = document.createElement('select');
console.log("доступ к selectElements", selectElements[1].value);
const selectedPrinterId = selectElements[1].value;
const selectedFormat = await fetchDataAndProcess(newSelect, selectedPrinterId);
printer_line.splice(5, 0, selectedFormat.toString());
main_dispatch();
additional_dispatch();
} else{
printer_line.splice(5, 0, selectedFormat.toString());
additional_dispatch();
}
}
}
function main_dispatch(){
console.log("Отправка записи valuesArray + printer_line", mergedArray)
fetch('http://:3000/main_entry', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(mergedArray)
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error));
}
function additional_dispatch(){
console.log("отправка printer_line", printer_line)
fetch('http://:3000/small_main_entry', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(printer_line)
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error));
}
function additional_dispatch1(){
console.log("отправка printer_line1", printer_line1)
setTimeout(()=>{
fetch('http://:3000/small_main_entry', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(printer_line1)
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error));
}, 1000);
}
async function fetchDataAndProcess(newSelect, selectedPrinterId) {
try {
const url = `http://:3000/printer_format?printer_id=${encodeURIComponent(selectedPrinterId)}`;
const keyVal = 'format_id';
const keyText = 'format';
const response = await fetch(url);
const jsonData = await response.json();
const result = jsonData.map(item => item[keyVal]);
const myVariable = result.map(Number);
for (const element of myVariable) {
console.log(typeof element); // number
}
const minValue = Math.min(...myVariable);
return minValue;
} catch (error) {
console.error(error);
}