<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(:completed_at, :scope => 'activerecord.attributes.spree/order') %></th>
      <% else %>
        <th><%= sort_link @search, :created_at,   t(:created_at, :scope => 'activerecord.attributes.spree/order') %></th>
      <% end %>
      <th><%= sort_link @search, :number,         t(:number, :scope => 'activerecord.attributes.spree/order') %></th>
      <th><%= sort_link @search, :state,          t(:state, :scope => 'activerecord.attributes.spree/order') %></th>
      <th><%= sort_link @search, :payment_state,  t(:payment_state, :scope => 'activerecord.attributes.spree/order') %></th>
      <th><%= sort_link @search, :shipment_state, t(:shipment_state, :scope => 'activerecord.attributes.spree/order') %></th>
      <th><%= sort_link @search, :email,          t(:email, :scope => 'activerecord.attributes.spree/order') %></th>
      <th><%= sort_link @search, :total,          t(:total, :scope => 'activerecord.attributes.spree/order') %></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>
    <%= search_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_gt, :class => 'datepicker' %><br />
          <%= label_tag nil, t(:start), :class => 'sub' %>
        </div>
        <div class="right sub-field">
          <%= f.text_field :created_at_lt, :class => 'datepicker' %><br />
          <%= label_tag nil, t(:stop), :class => 'sub' %>
        </div>
      </div>

      <p><%= label_tag nil, t(:status) %><br />
      <%= f.select :state_eq, 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_cont, :size => 25 %>
      </p>
      <p>
        <%= label_tag nil, t(:email) %><br />
        <%= f.text_field :email_cont, :size => 25 %>
      </p>
      <p>
        <%= label_tag nil, t(:first_name_start) %><br />
        <%= f.text_field :bill_address_firstname_start, :size => 25 %>
      </p>
      <p>
        <%= label_tag nil, t(:last_name_start) %><br />
        <%= f.text_field :bill_address_lastname_start, :size => 25 %>
      </p>
      <p>
        <%= f.check_box :completed_at_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 %>
