jQuery(function($){
let offset=0;
let searchTerm='';
function loadResults(initial=false){
$.ajax({
url: znd_ajax.ajax_url,
type: 'POST',
data: {
action: 'znd_search_products',
search_term: searchTerm,
offset: offset
},
success: function(response){
if(!response.success){
$('#znd-search-results').html('<p>Ничего не найдено</p>');
$('#znd-load-more').hide();
return;
}
if(response.data.initial){
let nightPriceCol=response.data.show_night_price
? '<th class="desktop-only">Цена ночью</th>'
: '';
const tableHTML=`
<table class="znd-doctor-services-table">
<thead>
<tr>
<th class="desktop-only">Код по приказу</th>
<th>Наименование</th>
<th class="desktop-only">Цена</th>
${nightPriceCol}
<th></th>
</tr>
</thead>
<tbody>${response.data.html}</tbody>
</table>
`;
$('#znd-search-results').html(tableHTML);
}else{
$('#znd-search-results tbody').append(response.data.html);
}
offset +=12;
if(response.data.has_more){
$('#znd-load-more').show();
}else{
$('#znd-load-more').hide();
}}
});
}
$('#znd-search-form').on('submit', function(e){
e.preventDefault();
offset=0;
searchTerm=$(this).find('input[name="search_term"]').val().trim();
loadResults(true);
});
$('#znd-load-more').on('click', function(){
loadResults();
});
});