{{ header }}
<div id="product-info" class="container">
  <ul class="breadcrumb">
    {% for breadcrumb in breadcrumbs %}
      <li class="breadcrumb-item"><a href="{{ breadcrumb.href }}">{{ breadcrumb.text }}</a></li>
    {% endfor %}
  </ul>
  <div class="row">{{ column_left }}
    <div id="content" class="col">
      {{ content_top }}

      <div class="row mb-3">

        {% if thumb or images %}
          <div class="col-sm-5">
            <div class="image magnific-popup">

              {% if thumb %}
                <a class="product-main-image" href="{{ popup }}" title="{{ heading_title }}"><img src="{{ thumb }}" title="{{ heading_title }}" alt="{{ heading_title }}" class="img-fluid mb-3"/></a>
              {% endif %}

              {% if images %}
                <div>
                  {% for image in images %}
                    <a href="{{ image.popup }}" title="{{ heading_title }}"><img src="{{ image.thumb }}" title="{{ heading_title }}" alt="{{ heading_title }}" class="img-thumbnail"/></a>&nbsp;
                  {% endfor %}
                </div>
              {% endif %}

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

        <div class="col-sm-7 product-description">
          <h1>{{ heading_title }}</h1>
          {% if review_status %}
            <div class="rating pb-0">
              <p>{% for i in 1..5 %}
                  {% if rating < i %}
                    <span class="fa-stack"><i class="fa-regular fa-star fa-stack-1x"></i></span>
                  {% else %}
                    <span class="fa-stack"><i class="fa-solid fa-star fa-stack-1x"></i><i class="fa-regular fa-star fa-stack-1x"></i></span>
                  {% endif %}
                {% endfor %}
                <a href="" onclick="$('a[href=\'#tab-review\']').tab('show'); return false;">{{ text_reviews }}</a> / <a href="" onclick="$('a[href=\'#tab-review\']').tab('show'); return false;">{{ text_write }}</a></p>
            </div>
          {% endif %}
          {% if price %}
            <ul class="list-unstyled price-wrap">
              <li class="price-info d-flex align-items-center">
                {% if not special %}
                  <h2><span class="price-new">{{ price }}</span></h2>
                {% else %}
                  <h2><span class="price-new">{{ special }}</span></h2>
                  <span class="price-old ms-2">{{ price }}</span>
                {% endif %}
              </li>

              {% if tax %}
                <li>{{ text_tax }} {{ tax }}</li>
              {% endif %}

              {% if points %}
                <li>{{ text_points }} {{ points }}</li>
              {% endif %}

              {% if discounts %}
                <li>
                  <hr>
                </li>
                {% for discount in discounts %}
                  <li>{{ discount.quantity }}{{ text_discount }}{{ discount.price }}</li>
                {% endfor %}
              {% endif %}
            </ul>
          {% endif %}

          <ul class="list-unstyled model-wrap">

            {% if manufacturer %}
              <li><span class="text-title">{{ text_manufacturer }}</span> <a href="{{ manufacturers }}">{{ manufacturer }}</a></li>
            {% endif %}

            <li><span class="text-title">{{ text_model }}</span> {{ model }}</li>

            {% if reward %}
              <li><span class="text-title">{{ text_reward }}</span> {{ reward }}</li>
            {% endif %}

            <li><span class="text-title">{{ text_stock }}</span> {{ stock }}</li>
          </ul>
          <div id="product">
            <form id="form-product">
              {% if options %}
              <h3>{{ text_option }}</h3>
              <div>
                {% for option in options %}

                  {% if option.type == 'select' %}
                    <div class="mb-3{% if option.required %} required{% endif %}">
                      <label for="input-option-{{ option.product_option_id }}" class="form-label">{{ option.name }}</label>
                      <select name="option[{{ option.product_option_id }}]" id="input-option-{{ option.product_option_id }}" class="form-select">
                        <option value="">{{ text_select }}</option>
                        {% for option_value in option.product_option_value %}
                          <option value="{{ option_value.product_option_value_id }}">{{ option_value.name }}
                            {% if option_value.price %}
                              ({{ option_value.price_prefix }}{{ option_value.price }})
                            {% endif %}</option>
                        {% endfor %}
                      </select>
                      <div id="error-option-{{ option.product_option_id }}" class="invalid-feedback"></div>
                    </div>
                  {% endif %}

                  {% if option.type == 'radio' %}
                    <div class="mb-3{% if option.required %} required{% endif %}">
                      <label class="form-label">{{ option.name }}</label>
                      <div id="input-option-{{ option.product_option_id }}">
                        {% for option_value in option.product_option_value %}
                          <div class="form-check">
                            <input type="radio" name="option[{{ option.product_option_id }}]" value="{{ option_value.product_option_value_id }}" id="input-option-value-{{ option_value.product_option_value_id }}" class="form-check-input"/>
                            <label for="input-option-value-{{ option_value.product_option_value_id }}" class="form-check-label">{% if option_value.image %}<img src="{{ option_value.image }}" alt="{{ option_value.name }} {% if option_value.price %}{{ option_value.price_prefix }} {{ option_value.price }}{% endif %}" class="img-thumbnail"/>{% endif %}
                              {{ option_value.name }}
                              {% if option_value.price %}
                                ({{ option_value.price_prefix }}{{ option_value.price }})
                              {% endif %}
                            </label>
                          </div>
                        {% endfor %}
                      </div>
                      <div id="error-option-{{ option.product_option_id }}" class="invalid-feedback"></div>
                    </div>
                  {% endif %}

                  {% if option.type == 'checkbox' %}
                    <div class="mb-3{% if option.required %} required{% endif %}">
                      <label class="form-label">{{ option.name }}</label>
                      <div id="input-option-{{ option.product_option_id }}">
                        {% for option_value in option.product_option_value %}
                          <div class="form-check">
                            <input type="checkbox" name="option[{{ option.product_option_id }}][]" value="{{ option_value.product_option_value_id }}" id="input-option-value-{{ option_value.product_option_value_id }}" class="form-check-input"/>
                            <label for="input-option-value-{{ option_value.product_option_value_id }}" class="form-check-label">
                              {% if option_value.image %}
                                <img src="{{ option_value.image }}" alt="{{ option_value.name }} {% if option_value.price %}{{ option_value.price_prefix }} {{ option_value.price }}{% endif %}" class="img-thumbnail"/>{% endif %}
                              {{ option_value.name }}
                              {% if option_value.price %}
                                ({{ option_value.price_prefix }}{{ option_value.price }})
                              {% endif %}</label>
                          </div>
                        {% endfor %}
                      </div>
                      <div id="error-option-{{ option.product_option_id }}" class="invalid-feedback"></div>
                    </div>
                  {% endif %}

                  {% if option.type == 'text' %}
                    <div class="mb-3{% if option.required %} required{% endif %}">
                      <label for="input-option-{{ option.product_option_id }}" class="form-label">{{ option.name }}</label>
                      <input type="text" name="option[{{ option.product_option_id }}]" value="{{ option.value }}" placeholder="{{ option.name }}" id="input-option-{{ option.product_option_id }}" class="form-control"/>
                      <div id="error-option-{{ option.product_option_id }}" class="invalid-feedback"></div>
                    </div>
                  {% endif %}

                  {% if option.type == 'textarea' %}
                    <div class="mb-3{% if option.required %} required{% endif %}">
                      <label for="input-option-{{ option.product_option_id }}" class="form-label">{{ option.name }}</label>
                      <textarea name="option[{{ option.product_option_id }}]" rows="5" placeholder="{{ option.name }}" id="input-option-{{ option.product_option_id }}" class="form-control">{{ option.value }}</textarea>
                      <div id="error-option-{{ option.product_option_id }}" class="invalid-feedback"></div>
                    </div>
                  {% endif %}

                  {% if option.type == 'file' %}
                    <div class="mb-3{% if option.required %} required{% endif %}">
                      <label for="button-upload-{{ option.product_option_id }}" class="form-label">{{ option.name }}</label>
                      <div>
                        <button type="button" id="button-upload-{{ option.product_option_id }}" data-oc-toggle="upload" data-oc-url="{{ upload }}" data-oc-target="#input-option-{{ option.product_option_id }}" data-oc-size-max="{{ config_file_max_size }}" data-oc-size-error="{{ error_upload_size }}" class="btn btn-light btn-block"><i class="fa-solid fa-upload"></i> {{ button_upload }}</button>
                        <input type="hidden" name="option[{{ option.product_option_id }}]" value="" id="input-option-{{ option.product_option_id }}"/>
                      </div>
                      <div id="error-option-{{ option.product_option_id }}" class="invalid-feedback"></div>
                    </div>
                  {% endif %}

                  {% if option.type == 'date' %}
                    <div class="mb-3{% if option.required %} required{% endif %}">
                      <label for="input-option-{{ option.product_option_id }}" class="form-label">{{ option.name }}</label>
                      <div class="input-group">
                        <input type="text" name="option[{{ option.product_option_id }}]" value="{{ option.value }}" id="input-option-{{ option.product_option_id }}" class="form-control date"/>
                        <div class="input-group-text"><i class="fa-regular fa-calendar"></i></div>
                      </div>
                      <div id="error-option-{{ option.product_option_id }}" class="invalid-feedback"></div>
                    </div>
                  {% endif %}

                  {% if option.type == 'datetime' %}
                    <div class="mb-3{% if option.required %} required{% endif %}">
                      <label for="input-option-{{ option.product_option_id }}" class="form-label">{{ option.name }}</label>
                      <div class="input-group">
                        <input type="text" name="option[{{ option.product_option_id }}]" value="{{ option.value }}" id="input-option-{{ option.product_option_id }}" class="form-control datetime"/>
                        <div class="input-group-text"><i class="fa-regular fa-calendar"></i></div>
                      </div>
                      <div id="error-option-{{ option.product_option_id }}" class="invalid-feedback"></div>
                    </div>
                  {% endif %}

                  {% if option.type == 'time' %}
                    <div class="mb-3{% if option.required %} required{% endif %}">
                      <label for="input-option-{{ option.product_option_id }}" class="form-label">{{ option.name }}</label>
                      <div class="input-group">
                        <input type="text" name="option[{{ option.product_option_id }}]" value="{{ option.value }}" id="input-option-{{ option.product_option_id }}" class="form-control time"/>
                        <div class="input-group-text"><i class="fa-regular fa-calendar"></i></div>
                      </div>
                      <div id="error-option-{{ option.product_option_id }}" class="invalid-feedback"></div>
                    </div>
                  {% endif %}

                {% endfor %}
                {% endif %}

                {% if subscription_plans %}
                  <hr/>
                  <h3>{{ text_subscription }}</h3>
                  <div class="mb-3 required">

                    <select name="subscription_plan_id" id="input-subscription" class="form-select">
                      <option value="">{{ text_select }}</option>
                      {% for subscription_plan in subscription_plans %}
                        <option value="{{ subscription_plan.subscription_plan_id }}">{{ subscription_plan.name }}</option>
                      {% endfor %}
                    </select>

                    {% for subscription_plan in subscription_plans %}
                      <div id="subscription-description-{{ subscription_plan.subscription_plan_id }}" class="form-text subscription d-none">{{ subscription_plan.description }}</div>
                    {% endfor %}
                    <div id="error-subscription" class="invalid-feedback"></div>

                  </div>
                {% endif %}

                <div class="mb-3 d-flex align-items-center">
                  <div class="quantity-wrap me-3">
                    <div class="input-group input-group-lg">
                      <button type="button" class="btn btn-light" id="button-minus"><i class="fa-solid fa-minus"></i></button>
                      <input type="text" name="quantity" minimum="{{ minimum }}" value="{{ minimum }}" size="2" id="input-quantity" class="form-control px-0"/>
                      <button type="button" class="btn btn-light" id="button-plus"><i class="fa-solid fa-plus"></i></button>
                    </div>
                  </div>
                  <input type="hidden" name="product_id" value="{{ product_id }}" id="input-product-id"/>
                  <div id="error-quantity" class="form-text"></div>
                  <br/>
                  <button type="submit" id="button-cart" class="btn btn-primary btn-lg btn-block me-2"><i class="fa-solid fa-cart-shopping me-2"></i> {{ button_cart }}</button>

                  <div class="btn-group-lg">
                    <button type="button" formaction="{{ add_to_wishlist }}" data-bs-toggle="tooltip" class="btn btn-default button-wishlist" title="{{ button_wishlist }}"><i class="fa-solid fa-heart"></i></button>
                    <button type="button" formaction="{{ add_to_compare }}" data-bs-toggle="tooltip" class="btn btn-default button-compare" title="{{ button_compare }}"><i class="fa-solid fa-arrow-right-arrow-left"></i></button>
                  </div>
                </div>

                {% if minimum > 1 %}
                  <div class="alert alert-info"><i class="fa-solid fa-circle-info"></i> {{ text_minimum }}</div>
                {% endif %}
              </div>
            </form>
          </div>
        </div>

        <div class="col-12 mt-5">
          <ul class="nav nav-tabs">
            <li class="nav-item"><a href="#tab-description" data-bs-toggle="tab" class="nav-link active">{{ tab_description }}</a></li>

            {% if attribute_groups %}
              <li class="nav-item"><a href="#tab-specification" data-bs-toggle="tab" class="nav-link">{{ tab_attribute }}</a></li>
            {% endif %}

            {% if review_status %}
              <li class="nav-item"><a href="#tab-review" data-bs-toggle="tab" class="nav-link">{{ tab_review }}</a></li>
            {% endif %}

          </ul>
          <div class="tab-content">

            <div id="tab-description" class="tab-pane fade show active mb-4">{{ description }}</div>

            {% if attribute_groups %}
              <div id="tab-specification" class="tab-pane fade">
                <div class="table-responsive">
                  <table class="table table-bordered">
                    {% for attribute_group in attribute_groups %}
                      <thead>
                        <tr>
                          <td colspan="2"><strong>{{ attribute_group.name }}</strong></td>
                        </tr>
                      </thead>
                      <tbody>
                        {% for attribute in attribute_group.attribute %}
                          <tr>
                            <td>{{ attribute.name }}</td>
                            <td>{{ attribute.text }}</td>
                          </tr>
                        {% endfor %}
                      </tbody>
                    {% endfor %}
                  </table>
                </div>
              </div>
            {% endif %}

            {% if review_status %}
              <div id="tab-review" class="tab-pane fade mb-4">{{ review }}</div>
            {% endif %}

          </div>
        </div>
      </div>

      {% if products %}
        <h3>{{ text_related }}</h3>
        <div class="row row-cols-1 row-cols-sm-2 row-cols-md-3 row-cols-xl-4">
          {% for product in products %}
            <div class="col mb-3">{{ product }}</div>
          {% endfor %}
        </div>
      {% endif %}

      {% if tags %}
        <p>{{ text_tags }}
          {% for i in 0..(tags|length - 1) %}
            <a href="{{ tags[i].href }}">{{ tags[i].tag }}</a>{% if not loop.end %},{% endif %}
          {% endfor %}
        </p>
      {% endif %}

      {{ content_bottom }}</div>
    {{ column_right }}</div>
</div>
<script type="text/javascript"><!--
$('#input-subscription').on('change', function(e) {
    var element = this;

    $('.subscription').addClass('d-none');

    $('#subscription-description-' + $(element).val()).removeClass('d-none');
});

$('#form-product').on('submit', function(e) {
    e.preventDefault();

    $.ajax({
        url: 'index.php?route=checkout/cart.add&language={{ language }}',
        type: 'post',
        data: $('#form-product').serialize(),
        dataType: 'json',
        contentType: 'application/x-www-form-urlencoded',
        cache: false,
        processData: false,
        beforeSend: function() {
            $('#button-cart').button('loading');
        },
        complete: function() {
            $('#button-cart').button('reset');
        },
        success: function(json) {
            console.log(json);

            $('#form-product').find('.is-invalid').removeClass('is-invalid');
            $('#form-product').find('.invalid-feedback').removeClass('d-block');

            if (json['error']) {
                for (key in json['error']) {
                    $('#input-' + key.replaceAll('_', '-')).addClass('is-invalid').find('.form-control, .form-select, .form-check-input, .form-check-label').addClass('is-invalid');
                    $('#error-' + key.replaceAll('_', '-')).html(json['error'][key]).addClass('d-block');
                }
            }

            if (json['success']) {
                $('#alert').prepend('<div class="alert alert-success alert-dismissible"><i class="fa-solid fa-circle-check"></i> ' + json['success'] + ' <button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>');

                $('#header-cart').load('index.php?route=common/cart.info');
            }
        },
        error: function(xhr, ajaxOptions, thrownError) {
            console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
    });
});

// quantity-wrap
$('.quantity-wrap .btn').on('click', function() {
    var element = this;
    var minimum = $(this).siblings('input').attr('minimum');

    var value = parseInt($(element).parent().find('input').val());

    if ($(element).attr('id') == 'button-minus' && value > minimum) {
        value--;
    } else if ($(element).attr('id') == 'button-plus') {
        value++;
    }

    $(element).parent().find('input').val(value);
});

$('.button-wishlist, .button-compare').click(function() {
  const element = this;
    $.ajax({
        url: $(element).attr('formaction'),
        type: 'post',
        dataType: 'json',
        data: {product_id: $('input[name=\'product_id\']').val()},
        beforeSend: function() {
            $(element).button('loading');
        },
        complete: function() {
            $(element).button('reset');
        },
        success: function(json) {
            $('#alert').prepend('<div class="alert alert-success alert-dismissible"><i class="fa-solid fa-circle-check"></i> ' + json['success'] + ' <button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>');
        },
        error: function(xhr, ajaxOptions, thrownError) {
            console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
    });
});

$(document).ready(function() {
    $('.magnific-popup').magnificPopup({
        type: 'image',
        delegate: 'a',
        gallery: {
            enabled: true
        }
    });
});
//--></script>
{{ footer }}
