{% set categories = __SELF__.categories %}
{% set subcategories = __SELF__.subcategories %}
{% set states = __SELF__.states %}
{% set cities = __SELF__.cities %}
{% set productIdOption = __SELF__.productIdOption %}
{% set productForEditing = __SELF__.productForEditing %}



<!-- Breadcumb Area -->
<div class="breadcumb_area">
    <div class="container h-100">
        <div class="row h-100 align-items-center">
            <div class="col-12">
                <h5>{{ 'product.HarytGosmak'|_ }}</h5>
                <ol class="breadcrumb">
                    <li class="breadcrumb-item"><a href="{{ 'index'|page }}">Esasy sahypa</a></li>
                    <li class="breadcrumb-item"><a href="{{ 'my-offers'|page }}">{{ 'breadcrumbYza'|_ }}</a></li>
                    {% if productForEditing %}
                        <li class="breadcrumb-item active">{{ 'product.HarytUytgetmek'|_ }}</li>
                    {% else %}
                        <li class="breadcrumb-item active">{{ 'product.HarytGosmak'|_ }}</li>
                    {% endif %}
                </ol>
            </div>
        </div>
    </div>
</div>
<!-- Breadcumb Area -->

<!-- addProducts Area -->
<div class="bigshop_reg_log_area section_padding_100_50" id="mainDiv">
    <div class="container">
        <div class="row">
            <div class="col-12 col-md-12">
                <div class="login_form mb-50">
                    <h5 class="mb-3">Haryt maglumatlary</h5>

                    {% if not productForEditing %}
                        <div class="shortcodes_content">
                            <form id="offerForm" action="#"
                            data-request="onSave"
                            data-request-flash
                            data-request-validate
                            data-request-files
                            data-request-success="resetForm()"
                            data-request-error="resetForm()"
                            enctype="multipart/form-data">
                            
                            <div class="row">
                                <div class="col-md-6 mb-3">
                                    <label for="category">Kategoriýa goshmak</label>
                                    <select class="custom-select d-block w-100 form-control" id="category" data-belongsto="product" name="category_id" required>
                                        <option value="" disabled>Kategoriýa saýla</option>
                                        {% for category in categories %}
                                            <option value="{{ category.id }}" data-file="{{ category.is_file_category }}">{{ category.name }}</option>
                                        {% endfor %}
                                    </select>
                                </div>
                                <div class="col-md-6 mb-3">
                                    <label for="subcategory">Ikinji kategoriýa</label>
                                    <select class="custom-select d-block w-100 form-control" id="subcategoryAdd" name="subcategory_id" disabled>
                                        <option value=null>Subkategoriýa saýla</option>

                                    </select>
                                </div>
                                <div class="col-md-6 mb-3">
                                    <label for="city">Welaýat</label>
                                    <select class="custom-select d-block w-100 form-control" id="city" name="state_id">
                                        <option value=null disabled>Welaýat saýla</option>
                                        {% for state in states %}
                                            <option value="{{ state.id }}">{{ state.name }}</option>
                                        {% endfor %}
                                    </select>
                                </div>
                                <div class="col-md-6 mb-3">
                                    <label for="subCity">Şäher</label>
                                    <select class="custom-select d-block w-100 form-control" id="subCityAdd" name="city_id" disabled>
                                        <option value=null>Şäher saýla</option>
                                    </select>
                                </div>
                                
                                <div class="col-md-6 mb-3">
                                    <label for="name">Harydyň ady</label>
                                    <input type="text" class="form-control" id="name" placeholder="Harydyň ady" name="name" required>
                                </div>
                                <div class="col-md-6 mb-3">
                                    <label for="price">Bahasy</label>
                                    <input type="number" class="form-control" id="price" name="price" min="0" required>
                                </div>
                                <div class="col-md-12 mb-3">
                                    <label for="order-notes">Gysga beýany</label>
                                    <textarea class="form-control" id="shortDescription" cols="30" name="short_description" rows="10" placeholder="Gysga beýany" style="height: 160px;" required></textarea>
                                </div>
                                <div class="col-md-12 mb-3">
                                    <label for="order-notes">Giňişleýin beýany</label>
                                    <textarea class="form-control" id="description" cols="30" rows="20" name="description" style="height: 160px;" placeholder="Giňişleýin beýany" required></textarea>
                                </div>
                                <div class="col-md-6 mb-3">
                                    <label for="phone">Telefon belgisi</label>
                                    <input type="tel" class="form-control" id="phone" name="phone" value="+993" required>
                                </div>
                                <div class="col-md-6 mb-3">
                                    <label for="images">Suratlar</label>
                                    <input type="file" class="form-control" id="new_img" name="new_img[]" multiple required>
                                    <input type="hidden" value="0" name="is_file" id="isFile">
                                </div>
                                <div class="col-md-6 mb-3 d-none" id="newFileDiv">
                                    <label for="files">Faýl goşmak</label>
                                    <input type="file" class="form-control" id="new_file" name="new_file" multiple>
                                </div>
                            </div>
                            <input type="hidden" name="resetForm" id="resetForm" value="0"/>
                            <button type="submit" class="btn btn-primary btn-sm mt-3">Haryt goşmak</button>
                        </form>
                        </div>
                    {% endif %}

                    {% if productForEditing %}
                        <div class="shortcodes_content">
                            <form id="offerUpdateForm" action="#"
                                data-request="onUpdate"
                                data-request-flash
                                data-request-validate
                                data-request-files
                                enctype="multipart/form-data">
                                
                                <div class="row">
                                    <div class="col-md-6 mb-3">
                                        <label for="category">Kategoriýa</label>
                                        {{ productForEditing.category }}
                                        <select class="custom-select d-block w-100 form-control" id="categoryEdit" data-belongsto="product" name="category_id" required>
                                            <option value="">Kategoriýa saýla</option>
                                            {% for category in categories %}
                                                {% for prodCat in productForEditing.categories %}
                                                    {% if prodCat.primary_key == 0 %}
                                                        <option value="{{ category.id }}" {{ category.id == prodCat.id ? 'selected' : '' }} data-file="{{ category.is_file_category }}">{{ category.name }}</option>
                                                    {% elseif prodCat.parent.parent %}
                                                        <option value="{{ category.id }}" {{ category.id == prodCat.parent.parent.id ? 'selected' : '' }} data-file="{{ category.is_file_category }}">{{ category.name }}</option>
                                                    {% else %}
                                                        <option value="{{ category.id }}" {{ category.id == prodCat.parent.id ? 'selected' : '' }} data-file="{{ category.is_file_category }}">{{ category.name }}</option>
                                                    {% endif %}
                                                {% endfor %}
                                            {% endfor %}
                                        </select>
                                    </div>
                                    <div class="col-md-6 mb-3">
                                        <label for="subcategory">Ikinji kategoriýa</label>
                                        <!-- {{ productForEditing.categories }} -->
                                        <select class="custom-select d-block w-100 form-control" id="subcategoryEdit" name="subcategory_id" required>
                                            {% for prodCat in productForEditing.categories %}
                                                {% if prodCat.primary_key == 0 %}
                                                    <option value=null disabled>Birinji dereje Sub-kategoriýa saýla</option>
                                                {% else %}
                                                    {% if prodCat.subs.count > 0 %}
                                                    <option value="{{ prodCat.id }}" {{ productForEditing.categories.contains(prodCat.id) ? 'selected' : '' }} data-file="{{ prodCat.is_file_category }}">{{ prodCat.name }}</option>
                                                        {% for sub in prodCat.subs %}
                                                            <option value="{{ sub.id }}" {{ productForEditing.categories.contains(sub.id) ? 'selected' : '' }} data-file="{{ sub.is_file_category }}">{{ sub.name }}</option>
                                                        {% endfor %}
                                                    {% else %}
                                                        <option></option>
                                                    {% endif %}
                                                {% endif %}                                   
                                            {% endfor %}
                                        </select>
                                    </div>
                                    <div class="col-md-6 mb-3">
                                        <label for="city">Welaýat</label>
                                        <select class="custom-select d-block w-100 form-control" id="cityEdit" name="state_id">
                                            <option value=null>Welaýat saýla</option>
                                            {% for state in states %}
                                                <option value="{{ state.id }}" {{ productForEditing.place_id == state.id ? 'selected' : '' }}>{{ state.name }}</option>
                                            {% endfor %}
                                        </select>
                                    </div>
                                    <div class="col-md-6 mb-3">
                                        <label for="subCity">Şäher</label>
                                        <select class="custom-select d-block w-100 form-control" id="subCityEdit" name="city_id">
                                            <option value=null>Şäher saýla</option>
                                            {% for city in cities %}
                                                <option value="{{ city.id }}" {{ productForEditing.place_id == city.id ? 'selected' : '' }}>{{ city.name }}</option>
                                            {% endfor %}
                                        </select>
                                    </div>
                                    
                                    <div class="col-md-6 mb-3">
                                        <label for="name">Harydyň ady</label>
                                        <input type="text" class="form-control" id="name" placeholder="Harydyň ady" name="name" value="{{ productForEditing.name }}" required>
                                    </div>
                                    <div class="col-md-6 mb-3">
                                        <label for="price">Bahasy</label>
                                        <input type="number" class="form-control" id="price" name="price" min="0" value="{{ productForEditing.price }}" required>
                                    </div>
                                    <div class="col-md-12 mb-3">
                                        <label for="order-notes">Gysga beýany</label>
                                        <textarea class="form-control" id="shortDescription" cols="30" name="short_description" rows="10" placeholder="Gysga beýany" required>{{ productForEditing.short_description }}</textarea>
                                    </div>
                                    <div class="col-md-12 mb-3">
                                        <label for="order-notes">Giňişleýin beýany</label>
                                        <textarea class="form-control" id="description" cols="30" rows="20" name="description" placeholder="Giňişleýin beýany" required>{{ productForEditing.description|striptags|raw  }}</textarea>
                                    </div>
                                    <div class="col-md-6 mb-3">
                                        <label for="phone">Telefon belgisi</label>
                                        <input type="tel" class="form-control" id="phone" name="phone" value="{{ productForEditing.phone ? productForEditing.phone : '+993' }}" required>
                                    </div>
                                    <div class="col-md-6 mb-3">
                                        <label for="images">Suratlar</label>
                                        <input type="file" class="form-control" id="new_img" name="new_img[]" multiple>
                                        <input type="hidden" value="0" name="is_file" id="isFile">
                                    </div>
                                    <div class="col-md-6 mb-3 d-none" id="newFileDiv">
                                        <label for="files">Faýl goşmak</label>
                                        <input type="file" class="form-control" id="new_file" name="new_file" multiple>
                                    </div>
                                </div>
                                <input type="hidden" name="resetForm" id="resetForm" value="0"/>
                                <input type="hidden" name="product_id" value="{{ productForEditing.id }}"/>
                                <button type="submit" class="btn btn-primary btn-sm mt-3">Üýtgetmek</button>
                            </form>

                            <!-- Shortcodes Title -->
                            <div class="shortcodes_title mb-30 mt-5">
                                <h5>Öňki suratlar</h5>
                            </div>
                
                            <!-- Gallery Area -->
                           
                            {% for key, image in productForEditing.images %}
                                <div class="col-9 col-sm-12 col-md-6 col-lg-4" id="image-{{ key }}">
                                    <div class="single-product-area mb-30">
                                        <div class="product_image">
                                            <!-- Product Image -->
                                            <img class="normal_img" src="{{ image.path }}" alt="Gurluşyk, Gurluşyk platform, bezeg gurluşyk, gipsler merkezi">
                                    
                                        </div>
                                        <div class="product_description">
                                            <div class="product_add_to_cart">
                                                <a href="#"
                                                     data-request="onImageDelete"
                                                     data-request-data="product_id: {{ productForEditing.id }}, product_image_id: {{ image.id }}"
                                                     data-request-flash
                                                style="width:100%; background-color:red;"><i class="icofont-trash"></i> Pozmak</a>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            {% endfor %}
                                  

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

{% put scripts %}
<script>
    function resetForm() {
        document.getElementById('offerForm').reset(); // Reset the form
    }
</script>
<script>
$(document).ready(function() {
    $('input[name*="phone"]').inputmask("+99399999999");
    $("#category").on("change", function() {
      var selectedValue = $(this).val();
      var isFile = $(this).find(':selected').data('file');
      if (isFile > 0){
        $("#isFile").val(1);
        $("#newFileDiv").removeClass("d-none");
        $("#new_file").attr("required", "true");
      }else{
        $("#isFile").val(0);
        $("#new_file").removeAttr("required");
        $("#newFileDiv").addClass("d-none");
      }
      
      //get subcategories
      var url = '{{ route("category.subs", ":id") }}';
      url = url.replace(':id', selectedValue);
      $.get(url, function (data){
          console.log(data);
            if(data.length > 0)
            {
                $('#subcategoryAdd').attr("disabled", false);
                $('#subcategoryAdd').empty();
                for (let i = 0; i < data.length; i++) {
                    $('#subcategoryAdd').append('<option value ="'+data[i]['id']+'" data-file="'+data[i]['is_file_category']+'">'+data[i]['name']+'</option>');
                    
                    for (let j = 0; j < data[i].subs.length; j++) {
                         $('#subcategoryAdd').append('<option value ="'+data[i].subs[j]['id']+'" data-file="'+data[i].subs[j]['is_file_category']+'">--'+data[i].subs[j]['name']+'</option><hr>');
                    }
                    
                    $('#subcategoryAdd').append('<option value=null disabled><hr></option>');
                    
                }
            }
            else
            {
                $('#subcategoryAdd').empty();
                $('#subcategoryAdd').append('<option value ="">Ikinji kategoriýa ýok</option>');
                $('#subcategoryAdd').attr("disabled", true);
            }
        });
    });

    $("#categoryEdit").on("change", function() {
      var selectedValue = $(this).val();
      var isFile = $(this).find(':selected').data('file');
      if (isFile > 0){
        $("#isFile").val(1);
        $("#newFileDiv").removeClass("d-none");
      }else{
        $("#isFile").val(0);
        $("#new_file").val(null);
        $("#newFileDiv").addClass("d-none");
      }
      
        //get subcategories
        var url = '{{ route("category.subs", ":id") }}';
        url = url.replace(':id', selectedValue);
        $.get(url, function (data){
            if(data.length > 0)
            {
                $('#subcategoryEdit').attr("disabled", false);
                $('#subcategoryEdit').empty();
                for (let i = 0; i < data.length; i++) {
                    $('#subcategoryEdit').append('<option value ="'+data[i]['id']+'" data-file="'+data[i]['is_file_category']+'">'+data[i]['name']+'</option>');
                    
                     for (let j = 0; j < data[i].subs.length; j++) {
                         $('#subcategoryEdit').append('<option value ="'+data[i].subs[j]['id']+'" data-file="'+data[i].subs[j]['is_file_category']+'">--'+data[i].subs[j]['name']+'</option><hr>');
                    }
                    
                    $('#subcategoryEdit').append('<option value=null disabled><hr></option>');
                }
                
        
                
            }
            else
            {
                $('#subcategoryEdit').empty();
                $('#subcategoryEdit').append('<option value ="">Ikinji kategoriýa ýok</option>');
                $('#subcategoryEdit').attr("disabled", true);
            }
        });
      
    });
    
    
     //places
    $("#city").on("change", function() {
      var selectedValue = $(this).val();
      var url = '{{ route("place.subs", ":id") }}';
      url = url.replace(':id', selectedValue);
      $.get(url, function (data){
            if(data.length > 0)
            {
                $('#subCityAdd').attr("disabled", false);
                $('#subCityAdd').empty();
                for (let i = 0; i < data.length; i++) {
                    $('#subCityAdd').append('<option value ="'+data[i]['id']+'">'+data[i]['name']+'</option>');
                }
            }
            else
            {
                $('#subCityAdd').empty();
                $('#subCityAdd').append('<option value ="">Degişli şäher ýok</option>');
                $('#subCityAdd').attr("disabled", true);
            }
        });
    });


    $("#cityEdit").on("change", function() {
        var selectedValue = $(this).val();
        var url = '{{ route("place.subs", ":id") }}';
        url = url.replace(':id', selectedValue);
        $.get(url, function (data){
            if(data.length > 0)
            {
                $('#subCityEdit').attr("disabled", false);
                $('#subCityEdit').empty();
                for (let i = 0; i < data.length; i++) {
                    $('#subCityEdit').append('<option value ="'+data[i]['id']+'" data-file="'+data[i]['is_file_category']+'">'+data[i]['name']+'</option>');
                }
            }
            else
            {
                $('#subCityEdit').empty();
                $('#subCityEdit').append('<option value ="">Degişli şäher ýok</option>');
                $('#subCityEdit').attr("disabled", true);
            }
        });
    });
    
    
    
    
  });
</script>
{% endput %}