title = "Haryt goşmak"
url = "/add-product/:productId?"
layout = "default"
is_hidden = 0
robot_index = "index"
robot_follow = "follow"

[offerform]
productId = "{{ :productId }}"
==
{% set categories = offerform.categories %}
{% set subcategories = offerform.subcategories %}
{% set states = offerform.states %}
{% set cities = offerform.cities %}
{% set productIdOption = offerform.productIdOption %}
{% set productForEditing = offerform.productForEditing %}



<!-- Breadcumb Area -->
<div class="breadcumb_area">
    <div class="container h-100">
        <div class="row h-100 align-items-center">
            <div class="col-12">
                {% if productForEditing %}
                    <h5>{{ 'productAdd.HarytUytgetmek'|_ }}</h5>
                {% else %}
                    <h5>{{ 'productAdd.HarytGosmak'|_ }}</h5>
                {% endif %}
                <ol class="breadcrumb">
                    <li class="breadcrumb-item"><a href="{{ 'index'|page }}">{{ 'breadcrumbEsasySahypa'|_ }}</a></li>
                    {% if productForEditing %}
                        <li class="breadcrumb-item active">{{ 'productAdd.HarytUytgetmek'|_ }}</li>
                    {% else %}
                        <li class="breadcrumb-item active">{{ 'productAdd.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">
                    {% if productForEditing %}
                        {{ 'productAdd.HarytUytgetmek'|_ }}
                    {% else %}
                        {{ 'productAdd.HarytGosmak'|_ }}
                    {% endif %}
                    </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">{{ 'productAdd.Kategoriya'|_ }}</label>
                                    <select class="custom-select d-block w-100 form-control" id="category" data-belongsto="product" name="category_id" onChange="getCategories(this.value);" required>
                                        <option value="" disabled>{{ 'productAdd.KategoriyaSayla'|_ }}</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">{{ 'productAdd.IkinjiKategoriya'|_ }}</label>
                                    <select class="custom-select d-block w-100 form-control" id="subcategoryAdd" name="subcategory_id" disabled>
                                        <option value=null>{{ 'productAdd.Subkategoriya'|_ }}</option>
                                        
                                    </select>
                                </div>
                                <div class="col-md-6 mb-3">
                                    <label for="city">{{ 'productAdd.Welayat'|_ }}</label>
                                    <select class="custom-select d-block w-100 form-control" id="city" name="state_id">
                                        <option value=null disabled>{{ 'productAdd.WelayatSayla'|_ }}</option>
                                        {% for state in states %}
                                            <option value="{{ state.id }}">{{ state.name }}</option>
                                        {% endfor %}
                                    </select>
                                </div>
                                <div class="col-md-6 mb-3">
                                    <label for="subCityAdd">{{ 'productAdd.Saher'|_ }}</label>
                                    <select class="custom-select d-block w-100 form-control" id="subCityAdd" name="city_id" disabled>
                                        <option value=null>{{ 'productAdd.SaherSayla'|_ }}</option>
                                    </select>
                                </div>
                                
                                <div class="col-md-6 mb-3">
                                    <label for="name">{{ 'productAdd.HarydynAdy'|_ }}</label>
                                    <input type="text" class="form-control" id="name" placeholder="{{ 'productAdd.HarydynAdy'|_ }}" name="name" required>
                                </div>
                                <div class="col-md-6 mb-3">
                                    <label for="price">{{ 'productAdd.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="shortDescription">{{ 'productAdd.GysgaBeyany'|_ }}</label>
                                    <textarea class="form-control" id="shortDescription" cols="30" name="short_description" rows="10" placeholder="{{ 'productAdd.GysgaBeyany'|_ }}" style="height: 160px;" required></textarea>
                                </div>
                                <div class="col-md-12 mb-3">
                                    <label for="description">{{ 'productAdd.GinsileyinBeyany'|_ }}</label>
                                    <textarea class="form-control" id="description" cols="30" rows="20" name="description" style="height: 160px;" placeholder="{{ 'productAdd.GinsileyinBeyany'|_ }}" required></textarea>
                                </div>
                                <div class="col-md-6 mb-3">
                                    <label for="phone">{{ 'productAdd.TelefonBelgisi2'|_ }}</label>
                                    <input type="tel" class="form-control" id="phone" name="phone" value="+993" required>
                                </div>
                                <div class="col-md-6 mb-3">
                                    <label for="stock">{{ 'productAdd.SkladdaBarmy'|_ }} ?</label>
                                    <select class="custom-select d-block w-100 form-control" id="stock" name="stock">
                                        <option value="1">{{ 'productAdd.ElimizdeBar'|_ }}</option>
                                        <option value="0">{{ 'productAdd.ZakazaGelyar'|_ }}</option>
                                    </select>
                                </div>
                                <div class="col-md-6 mb-3">
                                    <label for="new_img">{{ 'productAdd.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="new_file">{{ 'productAdd.FaylGosmak'|_ }}</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">{{ 'productAdd.HarytGosmak'|_ }}</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="categoryEdit">{{ 'productAdd.Kategoriya'|_ }}</label>
                                        <select class="custom-select d-block w-100 form-control" id="categoryEdit" data-belongsto="product" name="category_id" onChange="getCategoriesForEdit(this.value);" required>
                                             <option value="">{{ 'productAdd.KategoriyaSayla'|_ }}</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="subcategoryEdit">{{ 'productAdd.IkinjiKategoriya'|_ }}</label>
                                        
                                        <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 %}
                                                        {% set secondLevelCategory = prodCat.parent %}
                                                        <option value="{{ secondLevelCategory.id }}" {{ productForEditing.categories.contains(secondLevelCategory.id) ? 'selected' : '' }} data-file="{{ secondLevelCategory.is_file_category }}">{{ secondLevelCategory.name }}</option>
                                                        {% for thirdLevel in secondLevelCategory.subs %}
                                                            <option value="{{ thirdLevel.id }}" {{ productForEditing.categories.contains(thirdLevel.id) ? 'selected' : '' }} data-file="{{ thirdLevel.is_file_category }}">{{ thirdLevel.name }}</option>
                                                        {% endfor %}
                                                    {% endif %}
                                                {% endif %}
                                            {% endfor %}
                                            
                                            
                                        </select>
                                    </div>
                                    <div class="col-md-6 mb-3">
                                        <label for="cityEdit">{{ 'productAdd.Welayat'|_ }}</label>
                                        <select class="custom-select d-block w-100 form-control" id="cityEdit" name="state_id">
                                            <option value=null>{{ 'productAdd.WelayatSayla'|_ }}</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="subCityEdit">{{ 'productAdd.Saher'|_ }}</label>
                                        <select class="custom-select d-block w-100 form-control" id="subCityEdit" name="city_id">
                                            <option value=null>{{ 'productAdd.SaherSayla'|_ }}</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">{{ 'productAdd.HarydynAdy'|_ }}</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">{{ 'productAdd.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="shortDescription">{{ 'productAdd.GysgaBeyany'|_ }}</label>
                                        <textarea class="form-control" id="shortDescription" cols="30" name="short_description" rows="10" placeholder="{{ 'productAdd.GysgaBeyany'|_ }}" style="height: 160px;" required>{{ productForEditing.short_description }}</textarea>
                                    </div>
                                    <div class="col-md-12 mb-3">
                                        <label for="description">{{ 'productAdd.GinsileyinBeyany'|_ }}</label>
                                        <textarea class="form-control" id="description" cols="30" rows="20" name="description" style="height: 160px;" placeholder="{{ 'productAdd.GinsileyinBeyany'|_ }}" required>{{ productForEditing.description|striptags|raw  }}</textarea>
                                    </div>
                                    
                                    <div class="col-md-6 mb-3">
                                        <label for="phone">{{ 'productAdd.TelefonBelgisi'|_ }}</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="stock">{{ 'productAdd.SkladdaBarmy'|_ }} ?</label>
                                        <select class="custom-select d-block w-100 form-control" id="stock" name="stock">
                                            <option value="1" {{ productForEditing.stock == 1 ? 'selected': '' }}>{{ 'productAdd.ElimizdeBar'|_ }}</option>
                                            <option value="0" {{ productForEditing.stock == 0 ? 'selected': '' }}>{{ 'productAdd.ZakazaGelyar'|_ }}</option>
                                        </select>
                                    </div>
                                    <div class="col-md-6 mb-3">
                                        <label for="new_img">{{ 'productAdd.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="new_file">{{ 'productAdd.FaylGosmak'|_ }}</label>
                                        <input type="file" class="form-control" id="new_file" name="new_file" multiple>
                                    </div>
                                    
                                    <div class="col-12">
                                         <!-- Shortcodes Title -->
                                        <div class="shortcodes_title mb-30 mt-5">
                                            <h5>{{ 'productAdd.OnkiSuratlar'|_ }}</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>
                                    
                                    
                                    
                                </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">{{ 'productAdd.Uytgetmek'|_ }}</button>
                            </form>
                        </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
</div>

{% put scripts %}

<script>
function getCategories(catId) {
    $.request('onGetCategorySubs', {
    data: {'mainCat': catId},
    success: function(data) {
             if(data.length > 0){
                 $('#subcategoryAdd').prop("disabled", false);
                 $('#subcategoryAdd').html(`<option value=null>Subkategoriýa saýla</option>`);
                 for (var sub of data) {
                     $('#subcategoryAdd').append(`<option value="`+sub.id+`">`+sub.name+`</option>`);
                 }
             }else{
                 $('#subcategoryAdd').prop("disabled", true);
             }
        }
    });   
}
        
function getCategoriesForEdit(catId) {
    console.log(catId);
    $.request('onGetCategorySubs', {
    data: {'mainCat': catId},
    success: function(data) {
             if(data.length > 0){
                 $('#subcategoryEdit').prop("disabled", false);
                 $('#subcategoryEdit').html(`<option value=null>Subkategoriýa saýla</option>`);
                 for (var sub of data) {
                     $('#subcategoryEdit').append(`<option value="`+sub.id+`">`+sub.name+`</option>`);
                 }
             }else{
                 $('#subcategoryEdit').prop("disabled", true);
             }  
        }
    });   
}
</script>


<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");
      }
    });
    
    $("#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-file="'+data[i]['is_file_category']+'">'+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 %}