<div class='toolbar order-links'>
  <%= button_link_to t(:new_order), new_admin_order_url, :icon => 'add', :id => 'admin_new_order' %>
</div>

<%= render :partial => 'spree/admin/shared/order_sub_menu' %>

<h1><%= t(:listing_orders) %></h1>

<table class="index" id="listing_orders" data-hook>
  <thead>
    <tr data-hook="admin_orders_index_headers">
      <% if @show_only_completed %>
        <th><%= sort_link @search, :completed_at, t('activerecord.attributes.order.completed_at') %></th>
      <% else %>
        <th><%= sort_link @search, :created_at,   t(:order_date) %></th>
      <% end %>
      <th><%= sort_link @search, :number,         t(:order_number) %></th>
      <th><%= sort_link @search, :state,          t(:status) %></th>
      <th><%= sort_link @search, :payment_state,  t(:payment_state) %></th>
      <th><%= sort_link @search, :shipment_state, t(:shipment_state) %></th>
      <th><%= sort_link @search, :email,          t(:customer) %></th>
      <th><%= sort_link @search, :total,          t(:total) %></th>
      <th data-hook="admin_orders_index_header_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, admin_order_path(order) %></td>
      <td><%= t("order_state.#{order.state.downcase}") %></td>
      <td><%= link_to t("payment_states.#{order.payment_state}"), admin_order_payments_path(order) if order.payment_state %></td>
      <td><%= link_to t("shipment_states.#{order.shipment_state}"), admin_order_shipments_path(order) if order.shipment_state %></td>
      <td><%= order.email %></td>
      <td><%= number_to_currency order.total %></td>
      <td class='actions' data-hook="admin_orders_index_row_actions">
        <%= link_to_edit_url edit_admin_order_path(order), :title => "admin_edit_#{dom_id(order)}" %>
      </td>
    </tr>
  <% end %>
  </tbody>
</table>
<%= paginate @orders %>


<% content_for :sidebar do %>
  <div class="box" data-hook="admin_orders_index_search">
    <h3><%= t(:search) %></h3>
    <%= form_for [:admin, @search] do |f| %>
      <%= label_tag nil, t(:date_range) %><br />
      <div class="date-range-filter">
        <div class="left sub-field">
          <%= f.text_field :created_at_greater_than, :class => 'datepicker' %><br />
          <%= label_tag nil, t(:start), :class => 'sub' %>
        </div>
        <div class="right sub-field">
          <%= f.text_field :created_at_less_than, :class => 'datepicker' %><br />
          <%= label_tag nil, t(:stop), :class => 'sub' %>
        </div>
      </div>

      <p><%= label_tag nil, t(:status) %><br />
      <%= f.select :state_equals, Spree::Order.state_machines[:state].states.collect {|s| [t("order_state.#{s.name}"), s.value]}, {:include_blank => true} %></p>

      <p>
        <%= label_tag nil, t(:order_number) %><br />
        <%= f.text_field :number_like, :size => 25 %>
      </p>
      <p>
        <%= label_tag nil, t(:email) %><br />
        <%= f.text_field :email_like, :size => 25 %>
      </p>
      <p>
        <%= label_tag nil, t(:first_name_begins_with) %><br />
        <%= f.text_field :bill_address_firstname_starts_with, :size => 25 %>
      </p>
      <p>
        <%= label_tag nil, t(:last_name_begins_with) %><br />
        <%= f.text_field :bill_address_lastname_starts_with, :size => 25 %>
      </p>
      <p>
        <%= f.check_box :completed_at_is_not_null, {:checked => @show_only_completed}, '1', '' %>
        <%= label_tag nil, t(:show_only_complete_orders) %>
      </p>
      <div data-hook="admin_orders_index_search_buttons">
        <p><%= button t(:search) %></p>
      </div>
    <% end %>
  </div>
<% end %>
