// source --> /wp-content/themes/blankslate/js/calc.js 
$('document').ready(function () {

    $('body').on('click', '#add_btn', add_line);
    $('body').on('click', '.remove_btn', remove_line);
    $('body').on('change', 'select', calculate);
    $('body').on('keyup', 'input', calculate);
    
    calculate();
})

function calculate(e) {
    var weight_options = JSON.parse("[[75,41,0.469],[90,41,0.63],[110,41,0.93],[125,41,1.22],[140,41,1.53],[160,41,1.98],[180,41,2.47],[200,41,3.03],[225,41,3.84],[250,41,4.81],[280,41,5.96],[315,41,7.49],[355,41,9.53],[400,41,12.1],[450,41,15.2],[500,41,19],[560,41,23.6],[630,41,29.9],[710,41,38.1],[800,41,48.3],[900,41,60.9],[1000,41,75.4],[1200,41,108],[1400,41,148],[1600,41,193],[63,33,0.392],[75,33,0.543],[90,33,0.782],[110,33,1.16],[125,33,1.5],[140,33,1.87],[160,33,2.41],[180,33,3.05],[200,33,3.82],[225,33,4.76],[250,33,5.9],[280,33,7.38],[315,33,9.35],[355,33,11.8],[400,33,15.1],[450,33,19],[500,33,23.4],[560,33,29.4],[630,33,37.1],[710,33,47.3],[800,33,59.9],[900,33,75.9],[1000,33,93.5],[1200,33,134],[1400,33,183],[1600,33,239],[50,26,0.308],[63,26,0.488],[75,26,0.668],[90,26,0.969],[110,26,1.42],[125,26,1.83],[140,26,2.31],[160,26,3.03],[180,26,3.78],[200,26,4.68],[225,26,5.88],[250,26,7.29],[280,26,9.09],[315,26,11.6],[355,26,14.6],[400,26,18.6],[450,26,23.5],[500,26,29],[560,26,36.3],[630,26,46],[710,26,58.5],[800,26,74.1],[900,26,93.8],[1000,26,116],[1200,26,167],[1400,26,227],[1600,26,296],[40,21,0.244],[50,21,0.369],[63,21,0.573],[75,21,0.821],[90,21,1.18],[110,21,1.77],[125,21,2.26],[140,21,2.83],[160,21,3.71],[180,21,4.66],[200,21,5.77],[225,21,7.29],[250,21,8.92],[280,21,11.3],[315,21,14.2],[355,21,18],[400,21,22.9],[450,21,29],[500,21,35.8],[560,21,44.8],[630,21,56.5],[710,21,72.1],[800,21,91.4],[900,21,116],[1000,21,143],[1200,21,206],[1400,21,280],[40,17.6,0.281],[50,17.6,0.436],[63,17.6,0.682],[75,17.6,0.97],[90,17.6,1.4],[110,17.6,2.07],[125,17.6,2.66],[140,17.6,3.35],[160,17.6,4.35],[180,17.6,5.47],[200,17.6,6.78],[225,17.6,8.55],[250,17.6,10.6],[280,17.6,13.2],[315,17.6,16.7],[355,17.6,21.2],[400,17.6,26.9],[450,17.6,34],[500,17.6,42],[560,17.6,52.6],[630,17.6,66.6],[710,17.6,84.7],[800,17.6,108],[900,17.6,136],[1000,17.6,168],[1200,17.6,242],[32,17,0.193],[40,17,0.292],[50,17,0.449],[63,17,0.715],[75,17,1.01],[90,17,1.45],[110,17,2.16],[125,17,2.75],[140,17,3.46],[160,17,4.51],[180,17,5.71],[200,17,7.04],[225,17,8.94],[250,17,11],[280,17,13.8],[315,17,17.4],[355,17,22.2],[400,17,28],[450,17,35.5],[500,17,43.9],[560,17,55],[630,17,69.6],[710,17,88.4],[800,17,112],[900,17,142],[1000,17,175],[1200,17,252],[25,13.6,0.148],[32,13.6,0.229],[40,13.6,0.353],[50,13.6,0.545],[63,13.6,0.869],[75,13.6,1.23],[90,13.6,1.76],[110,13.6,2.61],[125,13.6,3.37],[140,13.6,4.22],[160,13.6,5.5],[180,13.6,6.98],[200,13.6,8.56],[225,13.6,10.9],[250,13.6,13.4],[280,13.6,16.8],[315,13.6,21.3],[355,13.6,27],[400,13.6,34.2],[450,13.6,43.3],[500,13.6,53.5],[560,13.6,67.1],[630,13.6,84.8],[710,13.6,108],[800,13.6,137],[900,13.6,173],[1000,13.6,214],[20,11,0.116],[25,11,0.169],[32,11,0.277],[40,11,0.427],[50,11,0.663],[63,11,1.05],[75,11,1.46],[90,11,2.12],[110,11,3.14],[125,11,4.08],[140,11,5.08],[160,11,6.67],[180,11,8.43],[200,11,10.4],[225,11,13.2],[250,11,16.2],[280,11,20.3],[315,11,25.7],[355,11,32.6],[400,11,41.4],[450,11,52.4],[500,11,64.7],[560,11,81],[630,11,103],[710,11,131],[16,9,0.09],[20,9,0.132],[25,9,0.198],[32,9,0.325],[40,9,0.507],[50,9,0.786],[63,9,1.25],[75,9,1.76],[90,9,2.54],[110,9,3.78],[125,9,4.87],[140,9,6.12],[160,9,7.97],[180,9,10.1],[200,9,12.5],[225,9,15.8],[250,9,19.4],[280,9,24.4],[315,9,30.8],[355,9,39.2],[400,9,49.7],[450,9,62.9],[500,9,77.5],[560,9,97.3],[630,9,123],[16,7.4,0.102],[20,7.4,0.162],[25,7.4,0.24],[32,7.4,0.385],[40,7.4,0.6],[50,7.4,0.935],[63,7.4,1.47],[75,7.4,2.09],[90,7.4,3],[110,7.4,4.49],[125,7.4,5.78],[140,7.4,7.27],[160,7.4,9.46],[180,7.4,12],[200,7.4,14.8],[225,7.4,18.7],[250,7.4,23.1],[280,7.4,28.9],[315,7.4,36.6],[355,7.4,46.4],[400,7.4,59],[450,7.4,74.6],[500,7.4,92.1],[10,6,0.051],[12,6,0.064],[16,6,0.115],[20,6,0.18],[25,6,0.277],[32,6,0.453],[40,6,0.701],[50,6,1.47],[63,6,1.73],[75,6,2.45],[90,6,3.52],[110,6,5.25],[125,6,6.77],[140,6,8.49],[160,6,11.1],[180,6,14],[200,6,17.3],[225,6,21.9],[250,6,27],[280,6,33.9],[315,6,42.8],[355,6,54.4],[400,6,69]]");
    var length_options = JSON.parse("[[20,20000],[25,20000],[32,20000],[40,20000],[50,20000],[63,17328],[75,9000],[90,7776],[110,5280],[125,3876],[140,3240],[160,2436],[180,1932],[200,1512],[225,1188],[250,960],[280,756],[315,624],[355,468],[400,360],[450,276],[500,240],[560,168],[630,144],[710,96],[800,72],[900,72],[1000,48],[1200,36],[1400,12],[1600,12]]");
    var totat = {weight: 0, price: 0, cars: 0};
	
    $('#calc_options_table tr:not(:first-child)').each(function (index) {
        var price = 280; 
        //var row_data = {diam: 0, sdr: 0, count: 0, weight: 0, price: 0, cars: 0};
		var row_data = {'diameters[]': 0, 'sdrs[]': 0, 'meters[]': 0, weight: 0, price: 0, cars: 0};
        $(this).find('select').each(function () {		
            row_data[$(this).attr('name')] = parseFloat($(this).val());			
        });
        $(this).find('input').each(function () {
            row_data[$(this).attr('name')] = parseInt($(this).val());
			
        });

        $.each(weight_options, function (index, data) {
            //if (data[0] == row_data['diam'] && data[1] == row_data['sdr']) {
			if (data[0] == row_data['diameters[]'] && data[1] == row_data['sdrs[]']) {
                row_data['weight'] = data[2] * row_data['meters[]'];
            }

        });

        if (row_data['weight']) {
            row_data['price'] = price * row_data['weight'];
        }

        row_data['per_meter'] = 0;
        if (row_data['diameters[]'] < 20) {
            return 7776;
        } else {
            $.each(length_options, function (opt_index, opt_data) {
                if (opt_data[0] === row_data['diameters[]']) {
                    row_data['per_meter'] = opt_data[1];
                }
            });
        }

        if (row_data['per_meter']) {
            row_data['cars'] = row_data['meters[]'] / row_data['per_meter'];
        }

        if (row_data['weight'] == 0 || isNaN(row_data['weight']) ||
                row_data['price'] == 0 || isNaN(row_data['price']) ||
                row_data['cars'] == 0 || isNaN(row_data['cars'])) {

            //row_data = {diam: 0, sdr: 0, count: 0, weight: 0, price: 0, cars: 0};
			var row_data = {'diameters[]': 0, 'sdrs[]': 0, 'meters[]': 0, weight: 0, price: 0, cars: 0};
        }



        $('#calc_result_table tr:not(:first-child)').eq(index).find('td').each(function (td_index) {
            if (td_index == 0) {
                $(this).text(row_data['weight'].toFixed(2));
                totat['weight'] += row_data['weight'];
            }
            if (td_index == 1) {
                $(this).text(row_data['price'].toFixed(2));
                totat['price'] += row_data['price'];
            }
            if (td_index == 2) {
                $(this).text(Math.ceil(row_data['cars']));
                totat['cars'] += row_data['cars'];
            }
        })

    });
    $('#calc_result_table tr:last-child td').each(function (total_td_index) {
        if (total_td_index == 0) {
            $(this).text(totat['weight'].toFixed(2));
        }
        if (total_td_index == 1) {
            $(this).text(totat['price'].toFixed(2));
        }
        if (total_td_index == 2) {
            $(this).text(Math.ceil(totat['cars']));
        }
    });
	
let $blocks = $('#calc_options_table'),
$sections = $blocks.find('tr');	


for(let i = 1; i <= $sections.length; i++) {
	
	
        	
	
	var diam = $(`tr[data-line="${i}"] select[name="diameters[]"]`).val();
	var sdr = $(`tr[data-line="${i}"] select[name="sdrs[]"]`).val();
	var metr = $(`tr[data-line="${i}"] input[name="meters[]"]`).val();
	var rez_in = $(`input[name="calc-r${i}"]`);
	var tot_v = `${i}. ` + diam + ' SDR: ' + sdr + ' Метров: ' + metr;
	rez_in.val(tot_v);
	
var values = "";
$(".add-control-input").each(function(i) {
    values += (i > 0 ? "\n" : "") + this.value;
});
$(".total-rez").val(values);
	
	
	

    
	
	
}	

	
	
	
	
	
}

function remove_line(e) {
    e.preventDefault();
    var parent = $(this).parents('tr');
    var parent_index = $('#calc_options_table tr:not(:first-child)').index(parent.get(0));
    
    $('#calc_result_table tr:not(:first-child)').eq(parent_index).remove();
    parent.remove();
    
    calculate();
}


function add_line(e) {
    e.preventDefault();

    var last_options_line = $('#calc_options_table tr:last-child');
    var new_options_line = last_options_line.clone();
	var last_options_line_data = $(last_options_line).data('line') + 1;
	new_options_line.attr('data-line',last_options_line_data);
    new_options_line.find('select option.default').each(function () {
        $(this).prop('selected', true);
    });
    new_options_line.find('input').each(function () {
        $(this).val(1);
    });
    new_options_line.find('td:last-child').html($('<a>', {class: 'remove_btn', href: '#'}).append($('<img>', {src: "/wp-content/uploads/2025/03/delite.png"})))
    last_options_line.after(new_options_line);

    var last_result_line = $($('#calc_result_table tr').get($('#calc_result_table tr').length - 3));
    var new_result_line = last_result_line.clone();
    new_result_line.find('td').each(function () {
        $(this).text('0.00');
    });
    new_result_line.find('td:last-child').text('0');

    last_result_line.after(new_result_line);
    
	
var newRowAdd ='<input type="hidden" class="add-control-input" id="calc-row' + last_options_line_data + '" name="calc-r' + last_options_line_data + '">';

$('#rezult').append(newRowAdd);	
	
	
	
	
calculate();
};