{% extends "front/_common/_base.html.twig" %}
{% block linkcanonical %}
<link rel="canonical" href="{{ absolute_url(path('network')) }}">
{% endblock %}
{% block cssadicional %}{% endblock %}
{% block classbody %}{% endblock %}
{% block content %}
<section class="red_section">
<div class="repuestos_banner">
<div class="repuestos_container ww_center">
<ul>
<li><a href="{{ path('home') }}">Inicio</a></li>
<li><span>/</span></li>
<li><a href="{{ path('after_sales') }}">Post Venta</a></li>
<li><span>/</span></li>
<li><a class="active">Repuestos</a></li>
</ul>
<h2>{{ sd.title }}</h2>
</div>
</div>
<div class="red_section_container ww_center">
<div class="red_section_left">
<div class="red_section_info">
{% if sd.text1 %}
<h3>{{ sd.text1 }}</h3>
{% endif %}
{% if sd.text2 %}
<h4>{{ sd.text2 }}</h4>
{% endif %}
{% if sd.text3 %}
<p>{{ sd.text3 }}</p>
{% endif %}
</div>
<div class="red_section_items">
{% for item in categories %}
<div class="red_section_datos">
<span class="red_section_ico">
<img src="{{ asset(item.icon|image_path) }}" width="" alt="{{ item.title }}">
</span>
<div class="red_section_text">
<h2>{{ item.title }}</h2>
{% if item.description %}
<p>{{ item.description }}</p>
{% endif %}
</div>
</div>
{% endfor %}
</div>
<div class="red_section_select">
<div class="red_section_select_val">
<span class="icon-arrow_drop_down"></span>
<select name="" id="department">
<option value="all" selected>Todos los departamentos</option>
{% for item in departments %}
<option value="{{ item.slug }}">{{ item.title }}</option>
{% endfor %}
</select>
</div>
<div class="red_section_select_val">
<span class="icon-arrow_drop_down"></span>
<select name="" id="city" disabled>
<option value="all" selected>Todas las provincias</option>
</select>
</div>
</div>
<div class="red_titlte_mapa">
<h2></h2>
</div>
<div class="red_section_mapa_responsive"></div>
<div class="red_section_box" id="establishments">
{% include 'front/network/establishment.html.twig' with {'establishments': establishments} %}
</div>
<div class="red_info_resultado" id="red_info_resultado" style="display: none;">
<p>{{ sd.noResults }}</p>
</div>
</div>
<div class="red_section_mapa">
{% include 'front/network/map.html.twig' %}
</div>
</div>
</section>
{% endblock %}
{% block jsfinal %}
<script type="text/javascript">
let itemClick = false;
$('#Peru_Regions g').on('click', function () {
itemClick = true;
$('.st0').removeClass('is_active');
$(this).find('.st0').addClass('is_active');
const department = $(this).find('.st0').data('department');
const department_name = $(this).find('.st0').data('name');
if ($('#department option[value="' + department + '"]').length > 0) {
$('#department').val(department).trigger('change');
} else {
$('#department option:selected').prop('selected', false);
updateEstablishmentsByDepartment(department);
}
$('.red_titlte_mapa h2').text(department_name);
})
let div = document.querySelector('.red_section_mapa');
function myFunction(x) {
if (x.matches) {
document.querySelector('.red_section_mapa_responsive').appendChild(div)
} else {
document.querySelector('.red_section_container').appendChild(div)
}
}
var x = window.matchMedia("(max-width: 1023px)")
myFunction(x)
x.addListener(myFunction)
$('#department').on('change', function () {
const department = $('#department').val();
if (department === 'all') {
$('.st0').removeClass('is_active');
updateAllEstablishments();
} else {
updateCities(department);
updateEstablishments(department, null);
}
const department_name = $('#department option:selected').text();
if (!itemClick) {
$('.red_titlte_mapa h2').text(department_name);
itemClick = false;
}
if ($('#p_' + department).length > 0) {
$('.st0').removeClass('is_active');
$('#p_' + department).addClass('is_active');
}
})
$('#city').on('change', function () {
const department = $('#department').val();
let city = $('#city').val();
if (city === 'all') {
city = null;
}
updateEstablishments(department, city);
})
function updateCities(department) {
$.ajax({
url: '{{ path('cities_by_department') }}',
type: 'post',
data: {department: department},
beforeSend: function () {
$('#department').prop('disabled', true);
$('#city').html("").prop('disabled', true);
},
success: function (data) {
$('#city').html(createCitySelect(data));
},
complete: function () {
$('#department').prop('disabled', false);
$('#city').prop('disabled', false);
}
})
}
function updateEstablishments(department, city) {
if (city == null) {
updateEstablishmentsByDepartment(department);
} else {
updateEstablishmentsByCity(department, city);
}
}
const noResultsMessage = '{{ sd.noResults }}';
const noResultMessageContainer = $('#red_info_resultado');
function updateAllEstablishments() {
$.ajax({
url: '{{ path('network_establishments') }}',
type: 'post',
beforeSend: function () {
$('#department').prop('disabled', true);
$('#city').prop('disabled', true);
$('#establishments').html("");
$('.red_info_resultado').hide();
},
success: function (data) {
if (data) {
$('#establishments').html(data);
} else {
$('.red_info_resultado').show();
}
},
complete: function () {
$('#department').prop('disabled', false);
}
})
}
function updateEstablishmentsByDepartment(department) {
$.ajax({
url: '{{ path('establishments_by_department') }}',
type: 'post',
data: {department: department},
beforeSend: function () {
$('#department').prop('disabled', true);
$('#city').html("").prop('disabled', true);
$('#establishments').hide().html("");
$('.red_info_resultado').hide();
},
success: function (data) {
if (data.status) {
$('#establishments').show().html(data.content);
$('#city').prop('disabled', false);
} else {
const message = data.message ? data.message : noResultsMessage;
noResultMessageContainer.text(message);
$('#establishments').hide();
$('.red_info_resultado').show();
}
},
complete: function () {
$('#department').prop('disabled', false);
$('#city').html(createCitySelect([]));
}
})
}
function updateEstablishmentsByCity(department, city) {
$.ajax({
url: '{{ path('establishments_by_city') }}',
type: 'post',
data: {department: department, city: city},
beforeSend: function () {
$('#department').prop('disabled', true);
$('#city').prop('disabled', true);
$('#establishments').html("");
$('.red_info_resultado').hide();
},
success: function (data) {
if (data.status) {
$('#establishments').html(data.content);
} else {
const message = data.message ? data.message : noResultsMessage;
noResultMessageContainer.text(message);
$('.red_info_resultado').show();
}
},
complete: function () {
$('#department').prop('disabled', false);
$('#city').prop('disabled', false);
}
})
}
function createCitySelect(data) {
let options = "<option value=\"all\" selected>Todas las provincias</option>";
$.each(data, function (i, v) {
options += "<option value=\"" + v['slug'] + "\">" + v['title'] + "</option>";
});
return options;
}
</script>
{% endblock %}