<%= render :partial => "submenu" %>

<% comma_columns = %w(unit_price price sum average subtotal total payment_total deliv_fee charge) %>

<div id="main"><!-- メイン -->

<h2><%= @title %></h2>

<% form_tag :action => 'index', :page => params[:page] do %>
  <% if session[:admin_user].master_shop? %>
    <table class="data2" cellspacing="1">
  <tr>
   <th style="width:10%">販売元</th>
   <td style="width:90%">
     <%= retailers = Retailer.find(:all, :order => :id).map {|r| [r.name, r.id]}
	 select 'search', 'retailer_id', retailers, {:selected => @selected_retailer, :include_blank=>false} %>
   </td>
  </tr>
    </table>
  <% end %>
 <% if @sale_start_enabled %>
  <table class="data2" cellspacing="1">
   <tr>
    <th>販売開始期間</th>
    <td>
     <%= date_select 'search', 'sale_start_from', :use_month_numbers => true, :include_blank => true %>
      ～<%= date_select 'search', 'sale_start_to', :use_month_numbers => true, :include_blank => true %>
    </td>
   </tr>
  </table>
 <% end %>
 <table class="data2" cellspacing="1" cellpadding="0">
  <tr>
   <th style="width:10%;"></th>
   <th style="width:90%;">受注日</th>
  </tr>
  <tr>
   <th style="width:10%;">月度集計</th>
   <td style="width:90%;">
    <%= date_select 'search', 'month', :use_month_numbers => true, :discard_day => true %>&nbsp;
    <%= submit_tag '月度で集計する', :name=>'search[by_month]', :class => "btn_s" %>
   </td>
  </tr>
  <tr>
   <th style="width:10%">期間集計</th>
   <td style="width:90%">
    <%= date_select 'search', 'date_from', :use_month_numbers => true %>
      ～<%= date_select 'search', 'date_to', :use_month_numbers => true %>&nbsp;
      <%= submit_tag '期間で集計する', :name=>'search[by_date]', :class => "btn_s" %>
   </td>
  </tr>
 </table>
<% end %>

<% if @records %>
  <div class="t_center">
   <% if @links %>
     <p><%= @title %> (
      <% @links and @links.each_slice(2) do | label, type | %>
       <%= link_to_unless(params[:type]==type, label, params.merge(:type=>type)) %>
      <% end %>
      )
     </p>
     <br />
   <% end %>
   <% form_tag :action => 'csv', :page => params[:page] do %>
     <div class="btn_box space20_bottom">
       <%= hidden_field_tag 'type', params[:type] %>
       <% %w(month date_from date_to sale_start_from sale_start_to).each do | name | %>
         <%- (1..3).each do | i | -%>
           <%= hidden_field 'search', "#{name}(#{i}i)" %>
         <%- end -%>
       <% end %>
       <% %w(by_month by_date type).each do | name | %>
         <%= hidden_field('search', name) if @search.send(name) %>
       <% end %>
       <%= submit_tag '検索結果を CSV ダウンロード', :class=>"btn"  %>
       <%= image_tag("btn_side.gif", :width => "6", :height => "34", :class => "btn_side", :alt => "") %>
     </div>
   <% end %>
   <p><%= image_tag url_for(:action=>:graph), :alt=>'' %></p>
  </div>
  <br />
  <table class="data" cellspacing="1">
    <tr>
      <% @labels.each do | label | %>
        <th class="result_title"><%= label and label.sub("\n", '<br />') %></th>
      <% end %>
    </tr>
    <% @records.each do |record| %>
      <tr>
        <% @list_view.each do | column | %>
          <td>
            <%- value = record[column] || 0 -%>
            <%= comma_columns.include?(column) ? number_with_delimiter(value) : value %>
          </td>
        <% end %>
      </tr>
    <% end %>
    <% if @total %>
      <tr>
        <% @list_view.each do | column | %>
          <td>
            <%- value = @total[column] || 0 -%>
            <%= comma_columns.include?(column) ? number_with_delimiter(value) : value %>
          </td>
        <% end %>
      </tr>
    <% end %>
    <tr>
      <% @labels.each do | label | %>
        <th class="result_title"><%= label and label.sub("\n", '<br />') %></th>
      <% end %>
    </tr>
  </table>
<% end %>

</div><!-- /メイン -->
