{% set products = __SELF__.products %}
{% set categories = __SELF__.categories %}
{% set cities = __SELF__.cities %}
{% set type = __SELF__.type %}
{% set city = __SELF__.city %}
{% set category = __SELF__.category %}
{% set subCategory = __SELF__.subCategory %}



{% for product in products %}
<!-- Quick View Modal Area -->
<div class="modal fade" id="quickview{{product.id}}" tabindex="-1" role="dialog" aria-labelledby="quickview" aria-hidden="true">
    <div class="modal-dialog modal-lg modal-dialog-centered" role="document">
        <div class="modal-content">
            <button type="button" class="close btn" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">&times;</span>
            </button>
            <div class="modal-body">
                <div class="quickview_body">
                    <div class="container">
                        <div class="row">
                            <div class="col-12 col-lg-5">
                                <div class="quickview_pro_img">
                                    <img class="first_img" src="{{ product.images[0].thumb(500,625,{'mode':'crop'}) }}" alt="Gurluşyk, Gurluşyk platform, bezeg gurluşyk, gipsler merkezi, {{ product.name }}">
                                    {% if product.images_count > 1 %}
                                        <img class="hover_img" src="{{ product.images[1].thumb(500,625,{'mode':'crop'}) }}" alt="Gurluşyk, Gurluşyk platform, bezeg gurluşyk, gipsler merkezi, {{ product.name }}">
                                    {% endif %}
                                </div>
                            </div>
                            <div class="col-12 col-lg-7">
                                <div class="quickview_pro_des">
                                    <h4 class="title">{{ product.name }}</h4>
                                    <div class="top_seller_product_rating mb-15">
                                        <i class="fa fa-star" aria-hidden="true"></i>
                                        <i class="fa fa-star" aria-hidden="true"></i>
                                        <i class="fa fa-star" aria-hidden="true"></i>
                                        <i class="fa fa-star" aria-hidden="true"></i>
                                        <i class="fa fa-star" aria-hidden="true"></i>
                                    </div>
                                    <h5 class="price">
                                        {% if product.vendor.web2 > 0 %}
                                            <del style="color: rgb(253, 0, 24);">{{ product.price }} TMT</del>
                                                {{ product.getDiscountedPrice(product.vendor.web2) }} TMT
                                            {% else %}
                                                {{ product.price }} TMT
                                        {% endif %}
                                    </h5> <span class="badge ml-2 {{ product.stock == 0 ? 'badge-secondary' : 'badge-success' }}">{{ product.stock == 0 ? 'productAdd.ZakazaGelyar'|_ : 'productAdd.ElimizdeBar'|_ }}</span>
                                    <p>{{ html_limit(product.description, 100) }}</p>
                                    <a href="{{ 'product'|page({id: product.id}) }}">{{ 'modal.GinisleyinMaglumat'|_ }}</a>
                                </div>
                                <!-- Add to Cart Form -->
                                <form class="cart" method="post">
                                    <div class="quantity">
                                        <input type="number" class="qty-text form-control" id="quantity{{ product.id }}" step="1" min="1" value="1">
                                    </div>
                                    {% if user %}
                                     <div class="product_add_to_cart addToCard" data-id="{{ product.id }}" data-price="{{ product.price }}" data-image="{{ product.images[0].path }}" data-name="{{ product.name }}"  data-vendor="{{ product.vendor.id }}">  
                                            <a href="#" class="btn btn-primary mt-1 mt-md-0 ml-1 ml-md-3">{{ 'product.SebedeGos'|_ }}</a>
                                     </div>
                                    {% else %}
                                        <div class="product_add_to_cart" data-id="{{ product.id }}" data-price="{{ product.price }}" data-image="{{ product.images[0].path }}" data-name="{{ product.name }}"  data-vendor="{{ product.vendor.id }}">  
                                                <a href="{{ 'login'|page }}" class="btn btn-primary mt-1 mt-md-0 ml-1 ml-md-3">{{ 'product.SebedeGos'|_ }}</a>
                                         </div>
                                    {% endif %}
                                    <!-- Wishlist -->
                                    <div class="modal_pro_wishlist">
                                        {% if user %}
                                            <a 
                                                data-request="onCreateFav" 
                                                data-request-flash
                                                data-request-validate
                                                data-request-data="product_id: {{ product.id }}" 
                                                href="#"><i class="icofont-heart"></i></a>
                                        {% else %}
                                            <a href="{{ 'login'|page }}"><i class="icofont-heart"></i></a>
                                        {% endif %}
                                    </div>

                                </form>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<!-- Quick View Modal Area -->
{% endfor %}




<!-- Breadcumb Area -->
<div class="breadcumb_area">
    <div class="container h-100">
        <div class="row h-100 align-items-center">
            <div class="col-12">
                <h5>{{ 'breadcrumbHarytlar'|_ }}</h5>
                <ol class="breadcrumb">
                    <li class="breadcrumb-item"><a href="{{ 'index'|page }}">{{ 'breadcrumbEsasySahypa'|_ }}</a></li>
                    <li class="breadcrumb-item active">VIP harytlar</li>
                </ol>
            </div>
        </div>
    </div>
</div>
<!-- Breadcumb Area -->


<section class="shop_grid_area section_padding_100">
    <div class="container">
    
    {% if products.count() == 0 %}
        <div class="container mb-5">
            <div class="row d-block justify-content-center align-items-center">
                <div class="col-6 d-block mx-auto">
                   <div class="text-center">
                      <img src="{{ 'assets/img/core-img/no-products.png'|theme }}" alt="Gurluşyk, Gurluşyk platform, bezeg gurluşyk, gipsler merkezi">
                  </div>
                </div>
                <div class="col-6 d-block mx-auto">
                   <div class="text-center">
                      <p>{{ 'product.SuwagtlykcaHarytYok'|_ }}</p>
                  </div>
                </div>
              </div>
        </div>
    {% else %}
        <div class="row">
            <div class="col-12 col-md-4 col-lg-3">
                <div class="shop_sidebar_area">
                    <form action="{{ 'vip-products'|page({type: type}) }}" id="filter_form" method="GET">
                    
                     <div class="widget catagory mb-30">
                        <h6 class="widget-title">{{ 'filter.Kategoriyalar'|_ }}</h6>
                        <div class="widget-desc">
                            <select name="category" class="form-control" id="category" onChange="getCategories(this.value);">
                            <option value="">{{ 'filter.KategoriyaSayla'|_ }}</option>
                                    {% for mainCat in categories %}
                                        <option value="{{ mainCat.slug }}" {{ mainCat.id == category.id ? 'selected' : ''  }}>{{ mainCat.name }} </option>
                                    {% endfor %}
                            </select>
                        </div>
                    </div>
                    
                        
                        <div class="widget catagory mb-30">
                            <h6 class="widget-title">{{ 'productAdd.IkinjiKategoriya'|_ }}</h6>
                            <div class="widget-desc">
                                <select name="subcategory" id="subcategory" class="form-control">
                                    <option value="">{{ 'productAdd.Subkategoriya'|_ }}</option>
                                    {% for sub in category.subs %}
                                        <option value="{{ sub.slug }}" {{ subCategory.id == sub.id ? 'selected' : ''  }}>{{ sub.name }} </option>
                                        {% for subSub in sub.subs %}
                                            <option value="{{ subSub.slug }}" {{ subCategory.id == subSub.id ? 'selected' : ''  }}>{{ subSub.name }} </option>
                                        {% endfor %}
                                    {% endfor %}
                                </select>
                            </div>
                        </div>
                        

                         <div class="widget brands mb-30">
                            <h6 class="widget-title">{{ 'filter.Saherler'|_ }}</h6>
                            <div class="widget-desc">
                            <select name="city" class="form-control">
                                <option value="">{{ 'filter.SaherSayla'|_ }}</option>
                                {% for city in cities %}
                                    <option value="{{ city.id }}" {% if input('city') == city.id %} selected {% endif %}>{{ city.name }} </option>
                                {% endfor %}
                            </select>
                            </div>
                        </div>

                        <!-- Single Widget -->
                        <div class="widget price mb-30">
                            <h6 class="widget-title">{{ 'filter.Baha'|_ }}</h6>
                            <div class="row">
                                <div class="col-6">
                                    <input class="form-control" type="number" name="min_price"
                                           placeholder="0" value="{{input('min_price') ? input('min_price') : null}}">
                                </div>
                                <div class="col-6">
                                    <input class="form-control" type="number" name="max_price"
                                           placeholder="0" value="{{input('max_price') ? input('max_price') : null}}">
                                </div>
                            </div>
                        </div>
                       <button type="submit" id="filterButton" class="btn btn-primary" style="width: 100%"> {{ 'filter.Filterlemek'|_ }} </button>
                 

                </div>
            </div>

            <div class="col-12 col-md-8 col-lg-9">
                <!-- Shop Top Sidebar -->
                <div class="shop_top_sidebar_area d-flex flex-wrap align-items-center justify-content-between">
                    <div class="row" style="width: 100%">
                        <div class="col-8">
                        </div>
                        <div class="col-4">
                            <select class="form-control" onchange="sortHandle()" id="sort_input">
                                <option value="">{{ 'sortSaylanmadyk'|_ }}</option>
                                <option value="price">{{ 'sortArzandanGymmada'|_ }}</option>
                                <option value="-price">{{ 'sortGymmatdanArzana'|_ }}</option>
                                <option value="created_at">{{ 'sortTazedenKona'|_ }}</option>
                                <option value="-created_at">{{ 'sortKonedenTaza'|_ }}</option>
                            </select>
                        </div>
                    </div>
                    
                    </form>


                </div>

                <div class="shop_grid_product_area">
                    <div class="row justify-content-start">
                        {% for product in products %}
                        <!-- Single Product -->
                        <div class="col-12 col-sm-6 col-md-4 col-lg-4">
                            <div class="single-product-area mb-30">
                            <a href="{{ 'product'|page({id: product.id}) }}">
                                <div class="product_image">
                                    <!-- Product Image -->
                                    <img class="normal_img" src="{{ product.images[0].thumb(680,700,{'mode':'crop'}) }}" alt="Gurluşyk, Gurluşyk platform, bezeg gurluşyk, gipsler merkezi, {{ product.name }}">
                                    {% if product_images_count > 1 %}
                                        <img class="hover_img" src="{{ product.images[1].thumb(680,700,{'mode':'crop'}) }}" alt="Gurluşyk, Gurluşyk platform, bezeg gurluşyk, gipsler merkezi, {{ product.name }}">
                                    {% endif %}
                                     {% if product.type_title %}
                                    <div class="product_badge">
                                        <span>{{product.type_title}}</span>
                                    </div>
                                    {% endif %}
                                    <div class="product_wishlist">
                                        {% if user %}
                                            <a 
                                                data-request="onCreateFav" 
                                                data-request-flash
                                                data-request-validate
                                                data-request-data="product_id: {{ product.id }}" 
                                                href="#"><i class="icofont-heart"></i></a>
                                        {% else %}
                                            <a href="{{ 'login'|page }}"><i class="icofont-heart"></i></a>
                                        {% endif %}
                                    </div>
                                </div>
                            </a>

                                <!-- Product Description -->
                                <div class="product_description">
                                    <!-- Add to cart -->
                                    {% if user %}
                                        <div class="product_add_to_cart addToCard" data-id="{{ product.id }}" data-price="{{ product.price }}" data-image="{{ product.images[0].path }}" data-name="{{ product.name }}"  data-vendor="{{ product.vendor.id }}">
                                            <input type="hidden" min="1" id="quantity{{ product.id }}" value="1">
                                            <a href="#"><i class="icofont-shopping-cart"></i> {{ 'product.SebedeGos'|_ }}</a>
                                        </div>
                                    {% else %}
                                        <div class="product_add_to_cart">
                                            <a href="{{ 'login'|page }}"><i class="icofont-shopping-cart"></i> {{ 'product.SebedeGos'|_ }}</a>
                                        </div>
                                    {% endif %}
                                    <!-- Quick View -->
                                    <div class="product_quick_view">
                                        <a href="#" data-toggle="modal" data-target="#quickview{{product.id}}"><i class="icofont-eye-alt"></i> {{ 'parallaxDolyMaglumat'|_ }}</a>
                                    </div>
                                     <p class="brand_name">{{product.vendor.shop_title}}</p>
                                    <a href="{{ 'product'|page({id: product.id}) }}">{{ product.name }}</a>
                                    <h6 class="product-price">
                                        {% if product.vendor.web2 > 0 %}
                                            <del style="color: rgb(253, 0, 24);">{{ product.price }} TMT</del>
                                                {{ product.getDiscountedPrice(product.vendor.web2) }} TMT
                                            {% else %}
                                                {{ product.price }} TMT
                                        {% endif %}
                                    </h6>
                                    <span class="badge mt-2 {{ product.stock == 0 ? 'badge-secondary' : 'badge-success' }}">{{ product.stock == 0 ? 'productAdd.ZakazaGelyar'|_ : 'productAdd.ElimizdeBar'|_ }}</span>
                                </div>
                            </div>
                        </div>
                        {% endfor %}

                    </div>
                </div>
                {% if products.hasPages %}
                    <div class="shop_pagination_area mt-30">
                        <nav aria-label="Page navigation">
                            <ul class="pagination pagination-sm justify-content-center">
                                {% if products.currentPage > 1 %}
                                    <li class="page-item"><a class="page-link" href="{{ products.previousPageUrl }}{{params}}">{{ 'paginationOnki'|_ }}</a></li>
                                {% endif %}
                    
                                {% for page in range(1, products.lastPage) %}
                                    {% if page == products.currentPage %}
                                        <li class="page-item active"><a class="page-link" href="{{ products.url(page) }}{{params}}">{{ page }}</a></li>
                                    {% elseif page > products.currentPage - 3 and page < products.currentPage + 3 %}
                                        <li class="page-item"><a class="page-link" href="{{ products.url(page) }}{{params}}">{{ page }}</a></li>
                                    {% elseif page == products.currentPage + 3 or page == products.currentPage - 3 %}
                                        <li>...</li>
                                    {% endif %}
                                {% endfor %}
                    
                                {% if products.lastPage > products.currentPage %}
                                    <li class="page-item"><a class="page-link" href="{{ products.nextPageUrl }}{{params}}">{{ 'paginationIndiki'|_ }}</a></li>
                                {% endif %}
                            </ul>
                        </nav>
                    </div>
                    {% endif %}


            </div>
        </div>
    {% endif %}
    </div>
</section>


{% put scripts %}
<script>
    function getCategories(catId) {
        if(catId == ''){
            return;
        }
        $.request('onGetCategorySubs', {
        data: {'mainCat': catId},
        success: function(data) {
                 if(data.length > 0){
                     $('#subcategory').prop("disabled", false);
                     $('#subcategory').html(`<option value="">Subkategoriýa saýla</option>`);
                     for (var sub of data) {
                         $('#subcategory').append(`<option value="`+sub.slug+`">`+sub.name+`</option>`);
                     }
                 }else{
                     $('#subcategory').prop("disabled", true);
                 }
                
            }
        });   
    }
    
    function sortHandle() {
        event.preventDefault();
        let sort = document.getElementById("sort_input");
        let value = sort.value;
        if($('#filterButton').prop("disabled")){
            toastrCustomError("Kategoriýa saýlanmadyk");
        }else{
            submitForm(value);
        }
    }

    function submitForm(value) {
        let form = document.getElementById("filter_form");
        let sort = document.createElement("input");
        sort.name = "sort";
        sort.type = "hidden";
        sort.value = value;
        form.appendChild(sort);
        form.submit()
    }


$(document).ready(function() {
    var category = $('#category').val();
    var subcategory = $('#subcategory').val();
    if(category == '' && subcategory == ''){
        $('#filterButton').attr('disabled','disabled');
    }
    
    
    $('#category').on('change', function(e){
        category=e.target.value;
        var subcategory = $('#subcategory').val();
        if(category == '' && subcategory == ''){
            $('#filterButton').attr('disabled','disabled');
        }else{
            $('#filterButton').attr('disabled',false);
        }
    });
    
    $('#subcategory').on('change', function(e){
        subcategory=e.target.value;
        var category = $('#category').val();
        if(category == '' && subcategory == ''){
            $('#filterButton').attr('disabled','disabled');
        }else{
            $('#filterButton').attr('disabled',false);
        }
    });
    
});    
    

</script>
{% endput %}