<div class="page-header">
 <ul class="breadcrumb">
  <li><a href="<%= dashboard_path %>">Home</a></li>
  <li>Daily Sale Report</li>
 </ul>
</div>

<div class="container">
    <%= render :partial=>'shift_sale_report_filter',
    		   :locals=>{ 	:period_type => true, :shift_name => false, :report_path =>reports_saleitem_index_path} %>
	<hr />
</div>

<div class="container">
	<div class="row">
		<div class="col-md-12 text-right">
			<a href="javascript:export_to('<%=reports_saleitem_index_path%>.xls')" class = "btn btn-default">Export to Excel</a>
		</div>	  			
	</div>
</div>

<div class="container margin-top-20">
	<div class="card row">
	  <div class="table-responsive">
	 <table class="table table-striped">
	  <thead>
		<tr>
  			<th>Date</th>		  	
			<th id="date"></th>
		</tr>
		<tr>
			<th>Menu Category</th>
			<th>Code</th>
			<th>Product</th>
			<th>Total Item</th>
			<th>Unit Price</th>
     	  	<th>Revenue</th>
		</tr>
	  </thead>
	  <tbody>

	  </tbody>
	</table>
		</div>
	</div>
</div>

<script type="text/javascript">
$(function(){

	var cate = [];
	var y;
	var count = 0;
	var sub_total = 0;
	var sub_total_arr = [];

	<% @sale_data.each do |result| %>

		count = count + 1;
		if(count == 1)
			$('#date').append('<%= result.date_name rescue '-'%>');

		y = $.inArray(<%= result.menu_category_id %>, cate);
		if(y == -1){
			//add sub total row
			sub_total_arr.push(sub_total);
			var total_row = '<tr><td colspan="4"></td>'+
								'<td > Sub Total</td> ' +
								'<td><span class="underline">'+ sub_total +'</span></td>'+
							'</tr>';


			if(count != 1){
				$('.table').append(total_row);
				sub_total = 0;
			}

			cate.push(<%= result.menu_category_id %>);
			var th = '<tr><td colspan="6"><%= result.menu_category_name rescue '-'%></td></tr>';
			var tr = '<tr>'+
					     '<td></td>'+
					     '<td><%= result.code  rescue '-'%></td>' +
					     '<td><%= result.product_name  rescue '-'%></td>' +
					     '<td><%= result.total_item.to_i  rescue '-'%></td>' +
					     '<td><%= number_with_precision(result.unit_price, :precision => 0)   rescue '-'%></td>'+
					     '<td><%= number_with_precision(result.grand_total, :precision => 0)  rescue '-'%>'+
					     '</td>'+
					'</tr>';


			$('.table').append(th);
			$('.table').append(tr);

			sub_total = parseInt(sub_total) +  parseInt(<%= result.grand_total  rescue '-'%>);
		}
		else{
			var tr = '<tr>'+
					      '<td></td>'+
					      '<td><%= result.code  rescue '-'%></td>' +
					      '<td><%= result.product_name  rescue '-'%></td>' +
					      '<td><%= result.total_item.to_i  rescue '-'%></td>' +
					      '<td><%= number_with_precision(result.unit_price, :precision => 0)  rescue '-'%></td>'+
					      '<td><%= number_with_precision(result.grand_total, :precision => 0)  rescue '-'%></td></tr>';
			$('.table').append(tr);

			sub_total = parseInt(sub_total) +  parseInt(<%= result.grand_total  rescue '-'%>);
		}

	<% end %>

		last_line_subtotal(sub_total);
		sub_total_arr.push(parseInt(sub_total));
		grand_total(sub_total_arr);
})

function last_line_subtotal(sub_total){
	
	var total_row = '<tr><td colspan="4"></td>'+
						'<td > Sub Total</td> ' +
						'<td><span class="underline">'+ sub_total +'</span></td>'+
					'</tr>';

	$('.table').append(total_row);
}

function grand_total(sub_total_arr){

	var total = 0;
	for(var i=0; i< sub_total_arr.length; i++){
		//total_1 = (total_1) + (sub_total_arr[i]);
		total = parseInt(total) + parseInt(sub_total_arr[i]);
	}
	var row = '<tr><td colspan="4"></td>'+
					'<td > Grand Total</td> ' +
					'<td><span class="double_underline">'+ total +'</span></td>'+
				'</tr>';
	$('.table').append(row);
}


</script>