{#**
 * Copyright since 2007 PrestaShop SA and Contributors
 * PrestaShop is an International Registered Trademark & Property of PrestaShop SA
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.md.
 * It is also available through the world-wide-web at this URL:
 * https://opensource.org/licenses/OSL-3.0
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@prestashop.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
 * versions in the future. If you wish to customize PrestaShop for your
 * needs please refer to https://devdocs.prestashop.com/ for more information.
 *
 * @author    PrestaShop SA and Contributors <contact@prestashop.com>
 * @copyright Since 2007 PrestaShop SA and Contributors
 * @license   https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
 *#}

<table class="table mb-3{% if orderForViewing.documents.documents is empty %} table-empty{% endif %}">
  <thead>
  <tr>
    <th>
      {{ 'Date'|trans({}, 'Admin.Global') }}
    </th>
    <th>
      {{ 'Document'|trans({}, 'Admin.Orderscustomers.Feature') }}
    </th>
    <th>
      {{ 'Number'|trans({}, 'Admin.Orderscustomers.Feature') }}
    </th>
    <th>
      {{ 'Amount'|trans({}, 'Admin.Global') }}
    </th>
    <th class="text-right d-print-none">
      {{ 'Actions'|trans({}, 'Admin.Global') }}
    </th>
  </tr>
  </thead>
  <tbody>
  {% if  orderForViewing.documents.documents is not empty %}
    {% for document in orderForViewing.documents.documents %}
      <tr>
        <td class="documents-table-column-date">
          {{ document.createdAt|date_format_lite }}
        </td>
        <td class="documents-table-column-type">
          {% if document.type == 'invoice' %}
            {{ 'Invoice'|trans({}, 'Admin.Global') }}
          {% elseif document.type == 'credit_slip' %}
            {{ 'Credit slip'|trans({}, 'Admin.Orderscustomers.Feature') }}
          {% else %}
            {{ 'Delivery slip'|trans({}, 'Admin.Orderscustomers.Feature') }}
          {% endif %}
        </td>
        <td class="documents-table-column-download-link">
          {% if document.type == 'invoice' %}
            <a target="_blank" rel="noopener noreferrer nofollow"
               href="{{ getAdminLink('AdminPdf', true, {'submitAction': 'generateInvoicePDF', 'id_order_invoice': document.id}) }}"
            >
              {{ document.referenceNumber }}
            </a>
          {% elseif document.type == 'delivery_slip' %}
            <a target="_blank" rel="noopener noreferrer nofollow"
               href="{{ getAdminLink('AdminPdf', true, {'submitAction': 'generateDeliverySlipPDF', 'id_order_invoice': document.id}) }}"
            >
              {{ document.referenceNumber }}
            </a>
          {% elseif document.type == 'credit_slip' %}
            <a href="{{ getAdminLink('AdminPdf', true, {'submitAction': 'generateOrderSlipPDF', 'id_order_slip': document.id}) }}">
              {{ document.referenceNumber }}
            </a>
          {% endif %}
        </td>
        <td class="documents-table-column-amount">
          {% if document.amount %}
            <p class="mb-0">
              {{ document.amount }}

              {% if document.amountMismatch %}
                ({{ document.amountMismatch }})
              {% endif %}
            </p>
          {% else %}
            --
          {% endif %}
        </td>
        <td class="text-right documents-table-column-actions">
          {% if document.type == 'invoice' %}
            {% if (document.isAddPaymentAllowed and document.amount) %}
              <button href="#" class="js-enter-payment-btn btn btn-primary btn-sm" data-payment-amount="{{ document.numericalAmount }}">
                {{ 'Enter payment'|trans({}, 'Admin.Orderscustomers.Feature') }}
              </button>
            {% endif %}
            <button href="#" class="js-open-invoice-note-btn btn btn-primary btn-sm">
              {% if document.note is empty %}
                {{ 'Add note'|trans({}, 'Admin.Orderscustomers.Feature') }}
              {% else %}
                {{ 'Edit note'|trans({}, 'Admin.Orderscustomers.Feature') }}
              {% endif %}
            </button>
          {% endif %}
        </td>
      </tr>
      {% if document.type == 'invoice' %}
        <tr class="d-none">
          <td colspan="5">
            <form action="{{ path('admin_orders_update_invoice_note', {
              'orderId': orderForViewing.id,
              'orderInvoiceId': document.id
            }) }}" method="post">
              <div class="input-group">
                <input type="text" class="form-control" name="invoice_note" value="{{ document.note }}">
                <button class="btn btn-secondary ml-2 btn-sm js-cancel-invoice-note-btn" type="button">
                  {{ 'Cancel'|trans({}, 'Admin.Actions') }}
                </button>
                <button class="btn btn-primary ml-2 btn-sm" type="submit">
                  {{ 'Save'|trans({}, 'Admin.Actions') }}
                </button>
              </div>
            </form>
          </td>
        </tr>
      {% endif %}
    {% endfor %}
  {% else %}
    <tr>
      <td colspan="5" class="text-center">
        {{ 'There is no available document'|trans({}, 'Admin.Orderscustomers.Notification') }}
      </td>
    </tr>
  {% endif %}
  </tbody>
</table>

{% if  orderForViewing.documents.documents is empty and orderForViewing.invoiceManagementIsEnabled %}
  <form action="{{ path('admin_orders_generate_invoice', {'orderId': orderForViewing.id}) }}" method="POST">
    <button class="btn btn-primary">
      <i class="material-icons">autorenew</i>
      {{ 'Generate invoice'|trans({}, 'Admin.Orderscustomers.Feature') }}
    </button>
  </form>
{% endif %}
