{{ header }}
{% if journal3.get('pageTitlePosition') == 'top' %}
  <h1 class="title page-title {{ journal3.get('title_color_scheme') }}"><span class="page-title-text">{{ heading_title }}</span></h1>
{% endif %}
<div class="breadcrumbs {{ journal3.get('breadcrumbs_color_scheme') }}">
  <ul class="breadcrumb">
    {% for breadcrumb in breadcrumbs %}
      <li><a href="{{ breadcrumb.href }}">{{ breadcrumb.text }}</a></li>
    {% endfor %}
  </ul>
</div>
{{ journal3_top }}
<div id="account-order" class="container">
  <div class="row">
    {{ column_left }}
    <div id="content" class="col">
      {{ content_top }}
      <table class="table table-bordered table-hover">
        <thead>
        <tr>
          <td class="text-left" colspan="2">{{ text_order_detail }}</td>
        </tr>
        </thead>
        <tbody>
        <tr>
          <td class="text-left" style="width: 50%;">{% if invoice_no %} <b>{{ text_invoice_no }}</b> {{ invoice_no }}<br />
            {% endif %} <b>{{ text_order_id }}</b> #{{ order_id }}<br />
            <b>{{ text_date_added }}</b> {{ date_added }}</td>
          <td class="text-left" style="width: 50%;">{% if payment_method %} <b>{{ text_payment_method }}</b> {{ payment_method }}<br />
            {% endif %}
            {% if shipping_method %} <b>{{ text_shipping_method }}</b> {{ shipping_method }} {% endif %}</td>
        </tr>
        </tbody>
      </table>
      <table class="table table-bordered table-hover">
        <thead>
        <tr>
          <td class="text-left" style="width: 50%; vertical-align: top;">{{ text_payment_address }}</td>
          {% if shipping_address %}
            <td class="text-left" style="width: 50%; vertical-align: top;">{{ text_shipping_address }}</td>
          {% endif %} </tr>
        </thead>
        <tbody>
        <tr>
          <td class="text-left">{{ payment_address }}</td>
          {% if shipping_address %}
            <td class="text-left">{{ shipping_address }}</td>
          {% endif %} </tr>
        </tbody>
      </table>
      <div class="table-responsive">
        <table class="table table-bordered table-hover table-order">
          <thead>
            <tr>
              <th>{{ column_product }}</th>
              <th class="text-end">{{ column_quantity }}</th>
              <th class="text-end">{{ column_price }}</th>
              <th class="text-end">{{ column_total }}</th>
              <th class="text-end">{{ column_action }}</th>
            </tr>
          </thead>
          <tbody>
            {% set product_row = 0 %}
            {% for product in products %}
              <tr id="product-row-{{ product_row }}">
                <td><a href="{{ product.view }}">{{ product.name }}</a>
                  <div id="error-{{ product_row }}-product" class="invalid-feedback mt-0"></div>
                  <ul class="list-unstyled mb-0">
                    <li>
                      <small> - {{ text_model }}: {{ product.model }}</small>
                    </li>
                    {% for option in product.option %}
                      <li>
                        <small> - {{ option.name }}: {{ option.value }}</small>
                        <div id="error-{{ product_row }}-option-{{ option.product_option_id }}" class="invalid-feedback mt-0"></div>
                      </li>
                    {% endfor %}
                    {% if product.subscription %}
                      <li>
                        <small> - {{ text_subscription }}: {% if product.subscription %}<a href="{{ product.subscription }}" target="_blank">{{ product.subscription_plan }}</a>{% else %}{{ product.subscription_plan }}{% endif %}</small>
                        <div id="error-{{ product_row }}-subscription" class="invalid-feedback mt-0"></div>
                      </li>
                    {% endif %}
                    {% if product.reward %}
                      <li>
                        <small> - {{ text_points }}: {{ product.reward }}</small>
                      </li>
                    {% endif %}
                  </ul>
                  <form id="form-product-{{ product_row }}">
                    <input type="hidden" name="product_id" value="{{ product.product_id }}"/> <input type="hidden" name="quantity" value="{{ product.quantity }}"/>
                    {% for option in product.option %}
                      {% if option.type == 'select' or option.type == 'radio' %}
                        <input type="hidden" name="option[{{ option.product_option_id }}]" value="{{ option.product_option_value_id }}"/>
                      {% endif %}
                      {% if option.type == 'checkbox' %}
                        <input type="hidden" name="option[{{ option.product_option_id }}][]" value="{{ option.product_option_value_id }}"/>
                      {% endif %}
                      {% if option.type == 'text' or option.type == 'textarea' or option.type == 'file' or option.type == 'date' or option.type == 'datetime' or option.type == 'time' %}
                        <input type="hidden" name="option[{{ option.product_option_id }}]" value="{{ option.value }}"/>
                      {% endif %}
                    {% endfor %}
                    <input type="hidden" name="subscription_plan_id" value="{{ product.subscription_plan_id }}"/>
                  </form>
                </td>
                <td class="text-end">{{ product.quantity }}</td>
                <td class="text-end">{{ product.price }}</td>
                <td class="text-end">{{ product.total }}</td>
                <td class="text-end text-nowrap" style="width: 1px;">
                  <div class="d-grid" style="display: flex; gap: 12px;">
                    <button type="submit" form="form-product-{{ product_row }}" class="btn btn-primary"><i class="fa fa-shopping-cart"></i></button>
                    <a href="{{ product.return }}" class="btn btn-danger"><i class="fa fa-reply"></i></a>
                  </div>
                </td>
              </tr>
              {% set product_row = product_row + 1 %}
            {% endfor %}
            {% for total in totals %}
              <tr>
                <td colspan="2"></td>
                <td class="text-end"><strong>{{ total.title }}</strong></td>
                <td class="text-end">{{ total.text }}</td>
                <td></td>
              </tr>
            {% endfor %}
          </tbody>
        </table>
      </div>
      {% if comment %}
        <div class="form-control mb-3" style="min-height: 64px;">
          <strong class="lead">{{ text_comment }}</strong>
          <br/>
          {{ comment }}
        </div>
      {% endif %}
      <h2>{{ text_history }}</h2>
      <div id="history">{{ history }}</div>
      <div class="buttons clearfix">
        <div class="pull-right"><a href="{{ continue }}" class="btn btn-primary">{{ button_continue }}</a></div>
      </div>
      {{ content_bottom }}
    </div>
    {{ column_right }}
  </div>
</div>
<script type="text/javascript"><!--
var product_row = 0;

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

    var element = this;

    if (e.originalEvent !== undefined && e.originalEvent.submitter !== undefined) {
        var button = e.originalEvent.submitter;
    } else {
        var button = '';
    }

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

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

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

                product_row = $(element).attr('id').substr(13);

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

                    delete json['error']['warning'];
                }

                for (key in json['error']) {
                    $('#error-' + product_row + '-' + 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>');

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

$('#history').on('click', '.pagination a', function(e) {
    e.preventDefault();

    $('#history').load(this.href);
});
//--></script>
{{ footer }}
