<% admin_breadcrumb(link_to plural_resource_name(Spree::LegacyUser), spree.admin_users_path) %>
<% admin_breadcrumb(link_to @user.email, edit_admin_user_url(@user)) %>
<% admin_breadcrumb(t('spree.admin.user.order_history')) %>

<%= render 'spree/admin/users/sidebar' %>
<%= render 'spree/admin/users/tabs', current: :orders %>
<%= render partial: 'spree/admin/users/user_page_actions' %>

<fieldset data-hook="admin_user_order_history" class="no-border-bottom">
  <legend><%= t("spree.admin.user.order_history") %></legend>

  <%= paginate @orders, theme: "solidus_admin" %>

  <% if @orders.any? %>
    <%# TODO add search interface %>
    <table class="index" id="listing_orders" data-hook>
      <colgroup>
         <col style="width: 25%;">
         <col style="width: 25%;">
         <col style="width: 25%;">
         <col style="width: 25%;">
      </colgroup>
      <thead>
        <tr data-hook="admin_orders_index_headers">
          <th><%= sort_link @search, :completed_at,   Spree::Order.human_attribute_name(:completed_at), {title: 'orders_completed_at_title'} %></th>
          <th><%= sort_link @search, :number,         Spree::Order.human_attribute_name(:number), {title: 'orders_number_title'} %></th>
          <th><%= sort_link @search, :state %></th>
          <th><%= sort_link @search, :payment_state %></th>
          <% if Spree::Order.checkout_step_names.include?(:delivery) %>
            <th><%= sort_link @search, :shipment_state %></th>
          <% end %>
          <th><%= sort_link @search, :total,          Spree::Order.human_attribute_name(:total), {title: 'orders_total_title'} %></th>
        </tr>
      </thead>
      <tbody>
      <% @orders.each do |order| %>
        <tr data-hook="admin_orders_index_rows">
          <td class="order-completed-at"><%= l(order.completed_at.to_date) if order.completed_at %></td>
          <td class="order-number">
            <% if can?(:admin, order) && can?(:edit, order) %>
              <%= link_to order.number, edit_admin_order_path(order) %>
            <% else %>
              <%= order.number %>
            <% end %>
          </td>
          <td>
            <span class="pill pill-<%= order.state %>">
              <%= t(order.state, scope: 'spree.order_state') %>
            </span>
          </td>
          <td>
            <% if order.payment_state %>
              <span class="pill pill-<%= order.payment_state %>">
                <%= t(order.payment_state, scope: 'spree.payment_states') %>
              </span>
            <% end %>
          </td>
          <% if Spree::Order.checkout_step_names.include?(:delivery) %>
            <td>
              <% if order.shipment_state %>
                <span class="pill pill-<%= order.shipment_state %>">
                  <%= t(order.shipment_state, scope: 'spree.shipment_states') %>
                </span>
              <% end %>
            </td>
          <% end %>
          <td class="order-total"><%= order.display_total.to_html %></td>
        </tr>
      <% end %>
      </tbody>
    </table>
  <% else %>
    <div class="no-objects-found">
      <% if can? :manage, Spree::Order %>
        <%= render 'spree/admin/shared/no_objects_found',
                     resource: Spree::Order,
                     new_resource_url: spree.new_admin_order_path(user_id: @user.id) %>
      <% end %>
    </div>
  <% end %>

  <%= paginate @orders, theme: "solidus_admin" %>
</fieldset>
