<% 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.items_purchased')) %>

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

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

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

  <% if @orders.any? %>
    <%# TODO add search interface %>
    <table class="stock-contents index" id="listing_items" data-hook="stock-contents">
      <colgroup>
        <col style="width: 10%;">
        <col style="width: 10%;">
        <col style="width: 30%;">
        <col style="width: 8%;">
        <col style="width: 8%;">
        <col style="width: 10%;">
        <col style="width: 14%;">
      </colgroup>

      <thead>
        <th><%= sort_link @search, :completed_at, Spree::Order.human_attribute_name(:completed_at), {title: 'orders_completed_at_title'} %></th>
        <th colspan=2><%= Spree::LineItem.human_attribute_name(:description) %></th>
        <th><%= Spree::LineItem.human_attribute_name(:price) %></th>
        <th><%= Spree::LineItem.human_attribute_name(:quantity) %></th>
        <th><%= Spree::Order.human_attribute_name(:total) %></th>
        <th><%= sort_link @search, :state, Spree::Order.human_attribute_name(:state), {title: 'orders_state_title'} %></th>
        <th><%= sort_link @search, :number, t('spree.admin.user.order_num'), {title: 'orders_number_title'} %></th>
      </thead>

      <tbody>
        <% @orders.each do |order| %>
          <% order.line_items.each do |item| %>
            <tr class="stock-item" data-item-quantity="<%= item.quantity %>">
              <td class="order-completed-at"><%= l(order.completed_at.to_date) if order.completed_at %></td>
              <td class="item-image">
                <%= render 'spree/admin/shared/image',
                  image: (item.variant.gallery.images.first || item.variant.product.gallery.images.first),
                  size: :mini %>
              </td>
              <td class="item-name">
                <%= item.product.name %><br><%= "(" + variant_options(item.variant) + ")" unless item.variant.option_values.empty? %>
                <% if item.variant.sku.present? %>
                  <strong><%= t('spree.sku') %>:</strong> <%= item.variant.sku %>
                <% end %>
              </td>
              <td class="item-price"><%= item.single_money.to_html %></td>
              <td class="item-quantity"><%= item.quantity %></td>
              <td class="item-total"><%= item.money.to_html %></td>
              <td class="order-state">
                <span class="pill pill-<%= order.state %>">
                  <%= t(order.state, scope: 'spree.order_state') %>
                </span>
              </td>
              <td class="order-number">
                <% if can?(:admin, order) && can?(:edit, order) %>
                  <%= link_to order.number, edit_admin_order_url(order) %>
                <% else %>
                  <%= order.number %>
                <% end %>
              </td>
            </tr>
          <% end %>
        <% 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>
