{#
 # For the full copyright and license information, please view the
 # docs/licenses/LICENSE.txt file that was distributed with this source code.
 #}

<table id="documents-grid-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="{{ path('admin_orders_generate_invoice_pdf', {orderId: orderForViewing.id}) }}"
            >
              {{ document.referenceNumber }}
            </a>
          {% elseif document.type == 'delivery_slip' %}
            <a target="_blank" rel="noopener noreferrer nofollow"
               href="{{ path('admin_orders_generate_delivery_slip_pdf', {orderId: orderForViewing.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 %}
            {% if document.note is empty %}
              <button href="#" class="js-open-invoice-note-btn btn btn-primary btn-sm">
                {{ 'Add note'|trans({}, 'Admin.Orderscustomers.Feature') }}
              </button>
            {% else %}
              <button href="#" class="js-open-invoice-note-btn btn btn-primary btn-sm btn-edit">
                {{ 'Edit note'|trans({}, 'Admin.Orderscustomers.Feature') }}
              </button>
            {% endif %}
          {% 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 invoice-note" 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 js-save-invoice-note-btn" type="submit">
                  {{ 'Save'|trans({}, 'Admin.Actions') }}
                </button>
              </div>
            </form>
          </td>
        </tr>
      {% endif %}
    {% endfor %}
  {% else %}
    <tr>
      <td colspan="5" class="text-center alert-available">
        {{ '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 %}
