{% extends 'base.html.twig' %}
{% block title %}Infoauto - Precios{% endblock %}
{% block body %}
<h1>Infoauto - Precios</h1>
<div class="row mt-3 border border-1 container-options">
<div class="col-12 col-lg-2 border-end container-brands">
<h4 class="text-center">Marca</h4>
<div class="list-group">
{% for key, item in brands %}
<button type="button" class="list-group-item list-group-item-action border-0 btn-brand" data-key="{{ key }}" data-id="{{ item.brandId }}" data-name="{{ item.brandName }}">
{{ item.brandName }}
</button>
{% endfor %}
</div>
</div>
<div class="col-12 col-lg-2 border-end container-models">
<h4 class="text-center">Modelo</h4>
<div class="list-group"></div>
</div>
<div class="col-12 col-lg-2 border-end container-years">
<h4 class="text-center">Fabricación</h4>
<div class="list-group"></div>
</div>
<div class="col-12 col-lg-4 border-end container-versions">
<h4 class="text-center">Versión</h4>
<div class="list-group"></div>
</div>
<div class="col-12 col-lg-2 container-prices">
<h4 class="text-center">Precio</h4>
<div class="list-group"></div>
</div>
</div>
<style>
.container-options {
height: calc(100vh - 230px);
}
.container-brands, .container-models, .container-years, .container-versions, .container-prices {
overflow-y: auto;
height: 100%;
}
</style>
{% endblock %}
{% block javascripts %}
{{ parent() }}
<script src="{{ asset('js/jquery.tmpl.min.js') }}"></script>
<script type="text/javascript">
var brands = {{ brands|json_encode|raw }};
var brandSelected = 0;
var modelSelected = 0;
var yearSelected = 0;
var versionSelected = 0;
console.log(brands);
$('.btn-brand').click(function() {
$('.container-brands .list-group button').removeClass('active');
$(this).addClass('active');
$('.container-models .list-group button').remove();
brands[$(this).data('key')].brandGroups.sort((a, b) => a.name.localeCompare(b.name));
brandSelected = $(this).data('id');
$.each(brands[$(this).data('key')].brandGroups, function(index, item) {
$('.container-models .list-group').append( $('#button-model-template').tmpl({ type: 'model', key: index, id: item.id, name: item.name, list_price: item.list_price, groups: JSON.stringify(item) }) );
});
});
$(document).off('click', '.btn-model').on('click', '.btn-model', function() {
$('.container-models .list-group button').removeClass('active');
$(this).addClass('active');
$('.container-years .list-group button').remove();
$('.container-versions .list-group button').remove();
$('.container-prices .list-group button').remove();
modelSelected = $(this).data('id');
if ($(this).data('list-price')) {
$('.container-years .list-group').append($('#button-year-template').tmpl({type: 'year', year: '0km'}));
}
let groups = JSON.parse( $(this).attr('data-groups') );
for (var i = groups.prices_to; i >= groups.prices_from; i--) {
$('.container-years .list-group').append( $('#button-year-template').tmpl({ type: 'year', year: i }) );
}
});
$(document).off('click', '.btn-year').on('click', '.btn-year', function() {
$('.container-years .list-group button').removeClass('active');
$(this).addClass('active');
$('.container-versions .list-group button').remove();
$('.container-prices .list-group button').remove();
yearSelected = $(this).data('year');
$('.spinner-' + yearSelected + ' div').removeClass('spinner-hide');
$.get('{{ path('infoauto_get_versions') }}', {brandId:brandSelected, modelId:modelSelected, modelYear:yearSelected }, function(data) {
$.each(data, function(index, item) {
$('.container-versions .list-group').append( $('#button-version-template').tmpl({ type: 'version', name: item.name, price: item.price, codia: item.codia }) );
});
$('.spinner-' + yearSelected + ' > div').addClass('spinner-hide');
});
});
$(document).off('click', '.btn-version').on('click', '.btn-version', function() {
$('.container-versions .list-group button').removeClass('active');
$(this).addClass('active');
$('.container-prices .list-group button').remove();
$('.container-prices .list-group').append( $('#button-price-template').tmpl({ type: 'price', price: $(this).data('price'), codia: $(this).data('codia') }) );
});
function formatNumber(num) {
return new Intl.NumberFormat('es-ES', { minimumFractionDigits: 0 }).format(num);
}
</script>
<script id="button-model-template" type="text/x-jquery-tmpl">
<button type="button" class="list-group-item list-group-item-action border-0 btn-${type}" data-key="${key}" data-id="${id}" data-name="${name}" data-list-price="${list_price}" data-groups='${groups}'>
${name}
</button>
</script>
<script id="button-year-template" type="text/x-jquery-tmpl">
<button type="button" class="list-group-item list-group-item-action border-0 btn-${type} spinner-${year}" data-year="${year}">
${year}
<div class="spinner-border text-white float-end spinner-hide" role="status">
<span class="visually-hidden"></span>
</div>
</button>
</script>
<script id="button-version-template" type="text/x-jquery-tmpl">
<button type="button" class="list-group-item list-group-item-action border-0 btn-${type}" data-price="${price}" data-codia="${codia}">
${name}
</button>
</script>
<script id="button-price-template" type="text/x-jquery-tmpl">
<button type="button" class="list-group-item list-group-item-action border-0 btn-${type}" data-price="${price}" data-codia="${codia}">
$ ${price}.000
</button>
</script>
{% endblock %}