jQuery(document).ready(function($) {
// Обработка выбора марки в фильтрах
$('#vehicle-make').change(function() {
var make_id = $(this).val();
$('#vehicle-table-container').hide();
if (make_id) {
$.post(vnAjax.ajaxurl, {
action: 'vn_get_terms',
parent_id: make_id,
level: 'make'
}, function(response) {
$('#vehicle-model').html(response.options).prop('disabled', false);
$('#vehicle-year').prop('disabled', true).html('');
$('#vehicle-version').prop('disabled', true).html('');
$('#vehicle-makes-blocks').hide();
$('#vehicle-models-blocks').html(response.blocks).show();
$('#vehicle-years-blocks, #vehicle-versions-blocks, #vehicle-table-container').hide().empty();
}).fail(function(xhr, status, error) {
console.error('Ошибка AJAX (get_terms):', status, error);
});
} else {
$('#vehicle-model').prop('disabled', true).html('');
$('#vehicle-year').prop('disabled', true).html('');
$('#vehicle-version').prop('disabled', true).html('');
$('#vehicle-makes-blocks').show();
$('#vehicle-models-blocks, #vehicle-years-blocks, #vehicle-versions-blocks, #vehicle-table-container').hide().empty();
}
});
// Обработка выбора модели в фильтрах
$('#vehicle-model').change(function() {
var model_id = $(this).val();
$('#vehicle-table-container').hide();
if (model_id) {
$.post(vnAjax.ajaxurl, {
action: 'vn_get_terms',
parent_id: model_id,
level: 'model'
}, function(response) {
$('#vehicle-year').html(response.options).prop('disabled', false);
$('#vehicle-version').prop('disabled', true).html('');
$('#vehicle-models-blocks').hide();
$('#vehicle-years-blocks').html(response.blocks).show();
$('#vehicle-versions-blocks, #vehicle-table-container').hide().empty();
}).fail(function(xhr, status, error) {
console.error('Ошибка AJAX (get_terms):', status, error);
});
} else {
$('#vehicle-year').prop('disabled', true).html('');
$('#vehicle-version').prop('disabled', true).html('');
$('#vehicle-models-blocks').show();
$('#vehicle-years-blocks, #vehicle-versions-blocks, #vehicle-table-container').hide().empty();
}
});
// Обработка выбора года в фильтрах
$('#vehicle-year').change(function() {
var year_id = $(this).val();
$('#vehicle-table-container').hide();
if (year_id) {
$.post(vnAjax.ajaxurl, {
action: 'vn_get_terms',
parent_id: year_id,
level: 'year'
}, function(response) {
$('#vehicle-version').html(response.options).prop('disabled', false);
$('#vehicle-years-blocks').hide();
$('#vehicle-versions-blocks').html(response.blocks).show();
$('#vehicle-table-container').hide().empty();
}).fail(function(xhr, status, error) {
console.error('Ошибка AJAX (get_terms):', status, error);
});
} else {
$('#vehicle-version').prop('disabled', true).html('');
$('#vehicle-years-blocks').show();
$('#vehicle-versions-blocks, #vehicle-table-container').hide().empty();
}
});
// Функция для вызова таблицы
function loadTable(term_id) {
$.post(vnAjax.ajaxurl, {
action: 'vn_get_table',
term_id: term_id
}, function(response) {
$('#vehicle-table-container').html(response.table).show();
$('#vehicle-versions-blocks').hide();
}).fail(function(xhr, status, error) {
console.error('Ошибка AJAX (get_table):', status, error);
});
}
// Обработка выбора версии в фильтрах
$('#vehicle-version').change(function() {
var version_id = $(this).val();
if (version_id) {
loadTable(version_id);
} else {
$('#vehicle-table-container').hide().empty();
$('#vehicle-versions-blocks').show();
}
});
// Навигация по блокам с синхронизацией фильтров
$(document).on('click', '.vehicle-block', function(e) {
e.preventDefault();
var $this = $(this);
var term_id = $this.data('id');
var level = $this.data('level');
if (level === 'make') {
$('#vehicle-make').val(term_id);
$('#vehicle-model').prop('disabled', true).html('');
$('#vehicle-year').prop('disabled', true).html('');
$('#vehicle-version').prop('disabled', true).html('');
$('#vehicle-table-container').hide().empty();
} else if (level === 'model') {
$('#vehicle-model').val(term_id);
$('#vehicle-year').prop('disabled', true).html('');
$('#vehicle-version').prop('disabled', true).html('');
$('#vehicle-table-container').hide().empty();
} else if (level === 'year') {
$('#vehicle-year').val(term_id);
$('#vehicle-version').prop('disabled', true).html('');
$('#vehicle-table-container').hide().empty();
} else if (level === 'final') {
$('#vehicle-version').val(term_id);
loadTable(term_id);
return;
}
$.post(vnAjax.ajaxurl, {
action: 'vn_get_terms',
parent_id: term_id,
level: level
}, function(response) {
if (level === 'make') {
$('#vehicle-model').html(response.options).prop('disabled', false);
$('#vehicle-makes-blocks').hide();
$('#vehicle-models-blocks').html(response.blocks).show();
$('#vehicle-years-blocks, #vehicle-versions-blocks, #vehicle-table-container').hide().empty();
} else if (level === 'model') {
$('#vehicle-year').html(response.options).prop('disabled', false);
$('#vehicle-models-blocks').hide();
$('#vehicle-years-blocks').html(response.blocks).show();
$('#vehicle-versions-blocks, #vehicle-table-container').hide().empty();
} else if (level === 'year') {
$('#vehicle-version').html(response.options).prop('disabled', false);
$('#vehicle-years-blocks').hide();
$('#vehicle-versions-blocks').html(response.blocks).show();
$('#vehicle-table-container').hide().empty();
}
}).fail(function(xhr, status, error) {
console.error('Ошибка AJAX (get_terms):', status, error);
});
});
// Переключение таблиц Stage 1 и Stage 2
$(document).on('click', '.toggle-table-btn', function(e) {
e.preventDefault();
var stage = $(this).data('stage');
$('.toggle-table-btn').removeClass('active');
$(this).addClass('active');
$('.vehicle-table-wrapper').hide();
$('#vehicle-table-' + stage).show();
});
});
BMW – Racehub