<div class="page-header">
  <ol class="breadcrumb">
    <li class="breadcrumb-item"><a href="<%= dashboard_path %>"><%= t("views.right_panel.button.home") %></a></li>
    <li class="breadcrumb-item active"><%= t("views.right_panel.detail.product_sale_report") %></li>
    <span class="float-right">
      <%= link_to 'Back', dashboard_path %>
    </span>
  </ol>
</div>
<div class="row">
	<div class="col-md-12">
		<div class="text-right">
			<a href="javascript:export_to('<%=reports_product_sale_index_path%>.xls')" class = "btn btn-info wave-effects "><%= t("views.btn.exp_to_excel") %></a>
		</div>	  			

		<div class="margin-top-20">
			<div class="card">
				<div class="p-l-20 p-t-20 p-b-20">
					<div class="col-md-6">
						<label class="font-16" for="order_by">Order by Qty</label>
						<select name="order_by" id="order_by" class="form-control" style="width:10%">
							<option value="asc">ASC</option>
							<option value="desc">DESC</option>
						</select>
					</div>
				</div>

				<div class="table-responsive">	
					<% if @print_settings.precision.to_i > 0
						precision = @print_settings.precision
					else
						precision = 0
					end
					#check delimiter
					if @print_settings.delimiter
						delimiter = ","
				    else
						delimiter = ""
				    end %>				
					<table class="table table-striped" id="items_table" border="0">
						<thead>
							<tr>
								<th>&nbsp;</th>
								<th><%= t("views.right_panel.header.menu_category") %></th>
								<th><%= t("views.right_panel.detail.code") %></th>
								<th><%= t("views.right_panel.detail.product") %></th>
								<th><%= t("views.right_panel.detail.total") %> <%= t("views.right_panel.detail.item") %></th>
								<!-- <th><%= t("views.right_panel.detail.unit_price") %></th> -->
								<th><%= t("views.right_panel.detail.total") %></th>
							</tr>	
						</thead>
						<tbody id="tbd_data">
							<% unless @sale_data.blank? %>
								<% acc_arr = Array.new %>
								<% cate_arr = Array.new %>
								<% grand_total = 0 %>
								<% total_qty = 0 %>
								<% total_item = {} %>
								<% total_data = {} %>
								<% @sale_data.each do |sale| 
									if !total_item.has_key?(sale.item_code)
										total_item[sale.item_code] = sale.total_item
										total_data[sale.item_code] = sale.grand_total
									else
										if sale.status_type == "void"
											total_item[sale.item_code] += sale.total_item
										end
										if sale.status_type == "void" || sale.status_type == "Discount" || sale.status_type == "foc" 
											total_data[sale.item_code] += sale.grand_total
										end 
									end 
								end %>

								<% @sale_data.each do |sale| %>
									<% grand_total += sale.grand_total %>
									<% if sale.status_type != "Discount" && sale.status_type != "foc"
										total_qty += sale.total_item 
									end %>
									<% if sale.status_type == "foc" && sale.price > 0
										total_qty += sale.total_item
									end %>
									
									<% if sale.status_type != "Discount" && sale.price.to_f >= 0 %>
										<tr>
											<td>&nbsp;</td>
											<% if !cate_arr.include?(sale.menu_category_id) %>	
											<td><%= sale.menu_category_name %></td>
												<% cate_arr.push(sale.menu_category_id) %>
											<% else %>
												<% if sale.total_item > 0 %>
													<% cate_arr = Array.new %>
												<% end %>
											<td>&nbsp;</td>
											<% end %>
											<td><%= sale.item_code rescue '-' %></td>
											<td><%= sale.product_name rescue '-' %></td>
											<td><%= total_item[sale.item_code] rescue '-' %></td>
											<!-- <td><%= number_with_precision(sale.unit_price ,  precision:precision.to_i,delimiter:delimiter) rescue '-'%></td> -->
											<td><%= number_with_precision(total_data[sale.item_code] ,  precision:precision.to_i,delimiter:delimiter) rescue '-' %></td>
										</tr>
										<!-- sub total -->
									<!-- end sub total -->
									<% end %>
								<% end %>
							<tr>
								<td colspan="3"></td>
								<td><strong>Total</strong></td>
								<td><strong><%= total_qty %></strong></td>
								<td><strong><%= number_with_precision(grand_total ,  precision:precision.to_i,delimiter:delimiter) rescue '-' %></strong></td>
							</tr>
							
						<% end %>
						</tbody>
					</table>
				</div>
			</div>
		</div>
	</div>
</div>

<script type="text/javascript">
	$(function(){
		$('#order_by').val('<%= @order_by %>');
	});
	
	$('#order_by').on('change', function(){
		var order_by = $("#order_by").val();
		window.location.href = "?order_by=" + order_by;
	});
</script>