<% admin_layout "full-width" %>

<% admin_breadcrumb(plural_resource_name(Spree::Order)) %>

<% content_for :page_actions do %>
  <li>
    <%= link_to t('spree.new_order'), new_admin_order_url, id: 'admin_new_order', class: 'btn btn-primary' %>
  </li>
<% end if can? :create, Spree::Order %>

<% content_for :table_filter_title do %>
  <%= t('spree.filter') %>
<% end %>

<% content_for :table_filter do %>
  <div data-hook="admin_orders_index_search">
    <%= search_form_for [:admin, @search] do |f| %>
      <div class="row">
        <% Spree::Backend::Config.search_fields[controller_path].each do |field_config| %>
          <% if !field_config.has_key?(:if) || field_config[:if].call %>
            <%= render field_config.except(:if).deep_merge(locals: { f: f }) %>
          <% end %>
        <% end %>
      </div>

      <div class="clearfix"></div>

      <div class="actions filter-actions">
        <div data-hook="admin_orders_index_search_buttons">
          <%= button_tag t('spree.filter_results'), class: 'btn btn-primary' %>
        </div>
      </div>
    <% end %>
  </div>
<% end %>

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

<% if @orders.any? %>
  <table class="index" id="listing_orders" data-hook>
    <colgroup>
       <col style="width: 13%;">
       <col style="width: 10%;">
       <col style="width: 10%;">
       <col style="width: 12%;">
       <% if Spree::Order.checkout_step_names.include?(:delivery) %>
         <col style="width: 12%;">
       <% end %>
       <col style="width: 25%;">
       <col style="width: 10%;">
       <col style="width: 8%;">
    </colgroup>
    <thead>
      <tr data-hook="admin_orders_index_headers">
        <% if @show_only_completed %>
          <th><%= sort_link @search, :completed_at %></th>
        <% else %>
          <th><%= sort_link @search, :created_at %></th>
        <% end %>
        <th><%= sort_link @search, :number %></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, :email %></th>
        <th class="align-right"><%= sort_link @search, :total %></th>
        <th data-hook="admin_orders_index_header_actions" class="actions"></th>
      </tr>
    </thead>
    <tbody>
    <% @orders.each do |order| %>
      <tr data-hook="admin_orders_index_rows">
        <td><%= l (@show_only_completed ? order.completed_at : order.created_at).to_date %></td>
        <td><%= link_to order.number, edit_admin_order_path(order) %></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>
          <% if order.user %>
            <%= link_to order.email, edit_admin_user_path(order.user) %>
          <% else %>
            <%= order.email %>
          <% end %>
          <%= link_to_with_icon('email', t('spree.actions.send_email'), "mailto:#{order.email}", no_text: true) %>
        </td>
        <td class="align-right"><%= order.display_total.to_html %></td>
        <td class='actions align-center' data-hook="admin_orders_index_row_actions">
          <%= link_to_edit_url edit_admin_order_path(order), title: "admin_edit_#{dom_id(order)}", no_text: true %>
        </td>
      </tr>
    <% end %>
    </tbody>
  </table>
<% else %>
  <div class="no-objects-found">
    <%= render 'spree/admin/shared/no_objects_found',
               resource: Spree::Order,
               new_resource_url: spree.new_admin_order_path %>
  </div>
<% end %>

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