{% extends 'nav.html.twig' %}
{% block title %}Магазин{% endblock %}
{% block content %}
<aside id="filterProducts" class="bd-aside sticky-xl-top text-muted align-self-start mb-3 mb-xl-5 px-2">
<form style="padding-left: 30px; margin-bottom: 30px !important; width: 300px" id="formFilter">
<h5 style="padding-left: 10px" class="pt-4 pb-3 mb-4 border-bottom">Фильтры</h5>
<nav style="width: 300px" class="small">
<h5 class="type-filter">Тип изделия</h5>
{% for groupProducts in groupsProducts %}
<label class="filter-label cursor-pointer">
{{ groupProducts.title }}
<input class="checkboxFilter cursor-pointer" onchange="filter()" id="{{ groupProducts.name }}" name="{{ groupProducts.name }}" type="checkbox">
</label><br>
{% endfor %}
<br>
<h5 class="type-filter">Материал</h5>
{% for materialProducts in materialsProducts %}
<label class="filter-label cursor-pointer">
{{ materialProducts.title }}
<input class="checkboxFilter cursor-pointer" onchange="filter()" id="{{ materialProducts.name }}" name="{{ materialProducts.name }}" type="checkbox">
</label><br>
{% endfor %}
<div style="padding-top: 30px; padding-left: 30px">
<label for="priceRange" class="form-label">Цена: до </label>
<span id="priceValue">{{ maxPrice }}</span><span>₽</span><br>
<span style="margin-right: 10px">0₽</span><input onchange="filter()" oninput="updatePriceValue(this.value)" name = "price" type="range" min="0" max="{{ maxPrice }}" class="form-range" id="priceRange" style="cursor: pointer"><span style="margin-left: 10px">{{ maxPrice }}₽</span>
</div>
</nav>
<h5 style="padding-left: 10px; margin-top: 530px;" class="pt-4 pb-3 mb-4 border-bottom">Сортировка</h5>
<select id = "selectSort" onchange="filter()" style="background: none; border: none; cursor: pointer; width: 260px" class="form-select" aria-label="Default select example">
<option class="cursor-pointer" value="0" selected>По умолчанию</option>
<option class="cursor-pointer" value="1">Убыванию цены</option>
<option class="cursor-pointer" value="2">Возрастанию цены</option>
<option class="cursor-pointer" value="3">Количеству просмотров</option>
<option class="cursor-pointer" value="4">Количеству продаж</option>
</select>
</form>
</aside>
<div class="card-table">
{% for product in products %}
<div class="card" style="width: 18rem;">
<a onclick="openProductCard('{{ product.id }}');" style="cursor: pointer">
{% if product.images is not null and product.images|length > 0 %}
<img class="card-img-top" height="350" src="../assets/images/{{ product.images[0].path }}">
{% endif %}
</a>
<div class="card-body">
<a onclick="openProductCard('{{ product.id }}');" style="cursor: pointer">
<h6 class="card-title">{{ product.title }}</h6>
<p style="height: 70px; font-size: 12px" class="card-text">
{% if product.description | length > 30 %}
{{ product.description | slice (0, 30) ~ '...' }}
{% else %}
{{ product.description }}
{% endif %}
</p>
</a>
<button type="button" class="btn btn-primary buttonsCard" onclick="addProductToCart('grid', {{ product.id }})">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-cart" viewBox="0 0 16 16">
<path d="M0 1.5A.5.5 0 0 1 .5 1H2a.5.5 0 0 1 .485.379L2.89 3H14.5a.5.5 0 0 1 .491.592l-1.5 8A.5.5 0 0 1 13 12H4a.5.5 0 0 1-.491-.408L2.01 3.607 1.61 2H.5a.5.5 0 0 1-.5-.5zM3.102 4l1.313 7h8.17l1.313-7H3.102zM5 12a2 2 0 1 0 0 4 2 2 0 0 0 0-4zm7 0a2 2 0 1 0 0 4 2 2 0 0 0 0-4zm-7 1a1 1 0 1 1 0 2 1 1 0 0 1 0-2zm7 0a1 1 0 1 1 0 2 1 1 0 0 1 0-2z"></path>
</svg>
</button>
<button type="button" class="btn btn-primary buttonsCard">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-heart" viewBox="0 0 16 13">
<path d="m8 2.748-.717-.737C5.6.281 2.514.878 1.4 3.053c-.523 1.023-.641 2.5.314 4.385.92 1.815 2.834 3.989 6.286 6.357 3.452-2.368 5.365-4.542 6.286-6.357.955-1.886.838-3.362.314-4.385C13.486.878 10.4.28 8.717 2.01L8 2.748zM8 15C-7.333 4.868 3.279-3.04 7.824 1.143c.06.055.119.112.176.171a3.12 3.12 0 0 1 .176-.17C12.72-3.042 23.333 4.867 8 15z"></path>
</svg>
</button>
<span style="right: 20px; position: absolute; font-size: 20px">{{ product.price }}₽</span>
<br><span class="master-name">{{ product.seller.lastname }} {{ product.seller.name }}</span>
<div style="text-align: right">
<small>{{ product.views }}<svg xmlns="http://www.w3.org/2000/svg" width="16" height="18" fill="currentColor" class="bi bi-eye" viewBox="0 0 16 10">
<path d="M16 8s-3-5.5-8-5.5S0 8 0 8s3 5.5 8 5.5S16 8 16 8zM1.173 8a13.133 13.133 0 0 1 1.66-2.043C4.12 4.668 5.88 3.5 8 3.5c2.12 0 3.879 1.168 5.168 2.457A13.133 13.133 0 0 1 14.828 8c-.058.087-.122.183-.195.288-.335.48-.83 1.12-1.465 1.755C11.879 11.332 10.119 12.5 8 12.5c-2.12 0-3.879-1.168-5.168-2.457A13.134 13.134 0 0 1 1.172 8z"></path>
<path d="M8 5.5a2.5 2.5 0 1 0 0 5 2.5 2.5 0 0 0 0-5zM4.5 8a3.5 3.5 0 1 1 7 0 3.5 3.5 0 0 1-7 0z"></path>
</svg>
</small>
<small>{{ product.bought }}<svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" fill="currentColor" class="bi bi-bag" viewBox="0 0 16 15">
<path d="M8 1a2.5 2.5 0 0 1 2.5 2.5V4h-5v-.5A2.5 2.5 0 0 1 8 1zm3.5 3v-.5a3.5 3.5 0 1 0-7 0V4H1v10a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V4h-3.5zM2 5h12v9a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V5z"/>
</svg>
</small>
</div>
</div>
</div>
{% endfor %}
</div>
<!-- Modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target=".bd-example-modal-lg">Large modal</button>
<div class="modal fade bd-example-modal-lg" id="productCardModal" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg" style="max-width: 1000px">
<div class="modal-content" id="productCardModalContainer">
...
</div>
</div>
</div>
{% endblock %}
{% block pagescripts %}
<script>
var descriptionProductCardVisible = false;
document.addEventListener('DOMContentLoaded', function () {
var urlParams = new URLSearchParams(window.location.search);
var productIdParam = urlParams.get('product_id');
var priceRange = document.getElementById('priceRange');
var priceValue = document.getElementById('priceValue');
var priceParam = urlParams.get('price');
priceRange.value = priceParam ? priceParam : priceRange.max;
priceValue.textContent = priceRange.value
if (productIdParam) {
openProductCard(productIdParam);
}
});
function filter() {
event.preventDefault();
let spinner = document.getElementById('loadingSpinner');
let form = document.getElementById('formFilter');
let inputs = form.getElementsByTagName('input');
let sort = form.getElementsByTagName('select');
let formSearch = document.getElementById('searchFilter');
let search = formSearch.querySelector('input[name="search"]').value;
sort = sort[0].value;
let price = inputs['price'].value;
let numberOfInputs = inputs.length;
let url = "?";
if (search !== "")
url = url + "search=" + search + "&";
url = url + "price=" + price + "&";
for (let i = 0; i < numberOfInputs; i++) {
if (!inputs[i].checked) continue;
url = url + inputs[i].name + "=" + inputs[i].checked + "&";
}
url = url + "sort=" + sort;
window.history.pushState('page2', 'Title', url);
spinner.style.display = 'block';
sendGetRequest(url)
.then(function (response) {
document.body.innerHTML = response;
for (let i = 0; i < numberOfInputs; i++) {
if (inputs[i].checked) {
document.getElementById(inputs[i].id).checked = true;
}
}
document.getElementById('priceRange').value = price;
document.getElementById('selectSort').value = sort;
document.getElementById('searchBar').value = search;
document.getElementById('priceValue').textContent = price;
let clearSearchIcon = document.getElementById('clearSearch');
clearSearchIcon.style.visibility = document.getElementById('searchBar').value ? 'inherit' : 'hidden';
spinner.style.display = 'none';
})
.catch(function () {
spinner.style.display = 'none';
})
}
/**
* Функция для отображения текущего значения слайдера фильтра цены во время его изменения
*
* @param value
*/
function updatePriceValue(value) {
document.getElementById('priceValue').textContent = value;
}
/**
* Функция, которая меняет стиль отображения крестика в поле поиска при вводе текста
*/
function showCross() {
let clearSearchIcon = document.getElementById('clearSearch');
clearSearchIcon.style.visibility = document.getElementById('searchBar').value ? 'inherit' : 'hidden';
}
/**
* Функция, которая очищает поле поиска по нажатию на крестик и сбрасывает имеющийся поиск товаров
*/
function clearSearch() {
document.getElementById('searchBar').value = '';
filter();
}
/**
* Отображает модальное окно карты товара и отслеживает его состояние
*
* @param productId
*/
function openProductCard(productId) {
let productModal = document.getElementById('productCardModalContainer');
let responseUrl = '/get-product/' + productId;
let url = '?product_id=' + productId;
let originalUrl = window.location.href;
$('#productCardModal').on('hidden.bs.modal', function () {
window.history.replaceState('page2', 'Title', originalUrl);
});
descriptionProductCardVisible = false;
window.history.pushState('page2', 'Title', url);
sendGetRequest(responseUrl)
.then(function (response) {
productModal.innerHTML = response;
$('#productCardModal').modal('show');
document.getElementById('addProductToCartBtn').disabled = false
})
}
/**
* Отвечает за логику работы кнопки "Посмотреть полностью" в модальном окне карты товара
*/
function toggleProductCardDescription() {
let container = document.getElementById('descriptionProductCard');
descriptionProductCardVisible = !descriptionProductCardVisible;
container.classList.toggle('collapsed');
container.classList.toggle('expanded')
}
/**
* Отправляет запрос на добавление товара в корзину и
* выводит сообщение, если он успешно добавлен.
*
* @param from
* @param product
*/
function addProductToCart(from, product) {
let Data;
if (from === 'Form'){
Data = new FormData(document.getElementById('modalProductCardForm'));
} else {
Data = new FormData();
Data.append('product', product);
Data.append('count', '1');
Data.append('sizeOption', 'anyone');
}
let url = 'addProductToCart';
sendPostRequest(url, Data)
.then(function (response) {
let responseData = JSON.parse(response);
if (responseData.guest) {
addToLocalCart(responseData.item);
showSuccessAlert('Товар успешно добавлен в корзину');
} else {
showSuccessAlert('Товар успешно добавлен в корзину');
}
})
}
/**
* Добавляет товар в localStorage
*
* @param cartItem
*/
function addToLocalCart(cartItem) {
let found = false;
let cart = JSON.parse(localStorage.getItem('cart')) || [];
cart.forEach(function(item) {
if (item['product_id'] === cartItem['product_id'] && item['size'] === cartItem['size']) {
item['count'] += cartItem['count'];
found = true
}
})
if (!found) cart.push(cartItem);
localStorage.setItem('cart', JSON.stringify(cart));
}
</script>
{% endblock %}