<div class="page-header">
  <ol class="breadcrumb">
    <li class="breadcrumb-item"><a href="<%= root_path %>"><%= t("views.right_panel.button.home") %></a></li>
    <li class="breadcrumb-item active"><%= t("views.right_panel.detail.order_reservation_report") %></li>
    <span class="float-right">
      <%= link_to 'Back', dashboard_path %>
    </span>
  </ol>
</div>
<div class="row">
	<div class="col-md-12">

		<!-- <div class="container"> -->
		<%= render :partial=>'shift_sale_report_filter',
		:locals=>{ 	:period_type => true, :shift_name => true, :providers => true, :payment_types => true, :report_path =>reports_order_reservation_index_path} %>
		<hr />
		<!-- </div> -->

		<!-- <div class="container"> -->
		<!-- <div class="row"> -->
			<div class="text-right">
				<a href="javascript:export_to('<%=reports_order_reservation_index_path%>.xls')" class = "btn btn-info wave-effects"><%= t("views.btn.exp_to_excel") %></a>
			</div>
		<!-- </div> -->
		<!-- </div> -->

		<div class="margin-top-20">
			<div class="card">
				<table class="table table-striped" border="0">
					<thead>
						<tr>
							<th colspan="15"> <%= t("views.right_panel.detail.from_date") %> : <%= @from.strftime("%Y-%b-%d") rescue '-' %>  - <%= t("views.right_panel.detail.to_date") %> : <%= @to.strftime("%Y-%b-%d")  rescue '-'%></th>
						</tr>
						<% if @shift_from %>
						<tr>
							<% if @shift_data.employee %>
							<% cashier_name = !@shift_data.nil? ? @shift_data.employee.name : '-' %>
							<% end %>
							<th colspan="15"><%= t("views.right_panel.detail.shift_name") %> =  <%= @shift_from %> - <%= @shift_to %> ( <%= cashier_name %> )</th>
						</tr>
						<% end %>

						<tr>
							<th><%= t("views.right_panel.detail.receipt_date") %></th>
							<th> <%= t("views.right_panel.detail.receipt_no") %></th>
							<th><%= t :reference_number %></th>
							<th><%= t :customer %></th>
							<!-- <th><%= t("views.right_panel.detail.type") %></th> -->
							<th><%= t("views.right_panel.detail.provider") %></th>
							<th><%= t :payment_method %></th>
							<th><%= t :payment %> <%= t("views.right_panel.detail.status")%></th>
							<th><%= t("views.right_panel.detail.sub_total") %></th>
							<th><%= t("views.right_panel.detail.discount_amount") %></th>
							<th><%= t("views.right_panel.detail.delivery_fee") %></th>
							<th><%= t("views.right_panel.detail.convenience_charge") %></th>
							<!-- <th><%= t("views.right_panel.detail.delivery_tax") %></th>
							<th><%= t("views.right_panel.detail.convenience_tax") %></th>
							<th><%= t("views.right_panel.detail.commercial_tax") %></th> -->
							<th><%= t("views.right_panel.detail.total") %> <%= t("views.right_panel.detail.tax") %></th>
							<th><%= t("views.right_panel.detail.grand_total") %></th>
							<th><%= t("views.right_panel.detail.transaction_fee") %></th>
						</tr>
					</thead>
					<tbody>
					    <%
					    	discount_amount = 0.0
					    	delivery_fee = 0.0
					    	convenience_charge = 0.0
					    	delivery_tax = 0.0
					    	convenience_tax = 0.0
					    	commercial_tax = 0.0

					    	total_discount_amount = 0
					    	total_delivery_fee = 0
					    	total_convenience_charge = 0
					    	total_delivery_tax = 0
					    	total_convenience_tax = 0
					    	total_commercial_tax = 0
					    	total_tax = 0.0
					    	total_amount = 0.0
					    	grand_total = 0.0
					    	total_transaction_fee = 0.0
					    %>
					    <% unless @order_reservation_data.blank? %>
							<% @order_reservation_data.each do |order_reservation| %>
								<%
									provider = ""
									discount_amount = order_reservation.discount_amount
									delivery_fee = order_reservation.delivery_fee ? order_reservation.delivery_fee : 0.0
									convenience_charge = order_reservation.convenience_charge
									if !JSON.parse(order_reservation.taxes).nil? && !JSON.parse(order_reservation.taxes).empty?
										JSON.parse(order_reservation.taxes).each do |tax_data|
											if tax_data[0] == "delivery_tax"
												delivery_tax = tax_data[1]
											elsif tax_data[0] == "convenience_tax"
												convenience_tax = tax_data[1]
											elsif tax_data[0] == "commercial_tax"
												commercial_tax = tax_data[1]
											end
										end
									end
									total_discount_amount += discount_amount.to_f
									total_delivery_fee += delivery_fee.to_f
									total_convenience_charge += convenience_charge.to_f
									total_delivery_tax += delivery_tax.to_f
									total_convenience_tax += convenience_tax.to_f
									total_commercial_tax += commercial_tax.to_f
									total_tax += order_reservation.total_tax.to_f
									total_amount += order_reservation.total_amount.to_f
									grand_total += order_reservation.grand_total.to_f

									total_transaction_fee += order_reservation.transaction_fee.to_f

									if order_reservation.provider == 'pick_up'
										provider = "Pick-Up"
									elsif order_reservation.provider == 'direct_delivery'
										provider = "Direct Delivery"
									elsif order_reservation.provider == 'turbo'
										provider = "TURBO"
									else
										provider = order_reservation.provider
									end
									payment_type = ""
									if order_reservation.payment_type == "cash_on_delivery"
										payment_type = "COD"
									elsif order_reservation.payment_type == "dinga"
										payment_type = "DINGA"
									end
								%>
								<tr>
									<td><%= order_reservation.receipt_date.strftime("%Y-%m-%d") %></td>
									<td><%= order_reservation.receipt_no %></td>
									<td><%= order_reservation.transaction_ref %></td>
									<td><%= order_reservation.name %></td>
									<!-- <td><%= order_reservation.order_reservation_type %></td> -->
									<td><%= provider%></td>
									<td><%= payment_type%></td>
									<td><%= order_reservation.payment_status%></td>
									<td><%= number_format(order_reservation.total_amount,  precision:precision.to_i, delimiter:delimiter) rescue '0.0'%></td>
									<td><%= number_format(discount_amount ,  precision:precision.to_i, delimiter:delimiter) rescue '0.0'%></td>
									<td><%= number_format(delivery_fee ,  precision:precision.to_i, delimiter:delimiter) rescue '0.0'%></td>
									<td><%= number_format(convenience_charge ,  precision:precision.to_i, delimiter:delimiter) rescue '0.0'%></td>
									<!-- <td><%= number_format(delivery_tax ,  precision:precision.to_i, delimiter:delimiter) rescue '0.0'%></td>
									<td><%= number_format(convenience_tax ,  precision:precision.to_i, delimiter:delimiter) rescue '0.0'%></td>
									<td><%= number_format(commercial_tax ,  precision:precision.to_i, delimiter:delimiter) rescue '0.0'%></td> -->
									<td><%= number_format(order_reservation.total_tax ,  precision:precision.to_i, delimiter:delimiter) rescue '0.0'%></td>
									<td><%= number_format(order_reservation.grand_total ,  precision:precision.to_i, delimiter:delimiter) rescue '0.0' %></td>
									<td><%= number_format(order_reservation.transaction_fee ,  precision:precision.to_i, delimiter:delimiter) rescue '0.0' %></td>
								</tr>
							<% end
						end %>

						<tr>
							<td colspan="7"><b>Total</b></td>
							<td><b><%= number_format(total_amount ,  precision:precision.to_i, delimiter:delimiter) rescue '0.0'%></b></td>
							<td><b><%= number_format(total_discount_amount ,  precision:precision.to_i, delimiter:delimiter) rescue '0.0'%></b></td>
							<td><b><%= number_format(total_delivery_fee ,  precision:precision.to_i, delimiter:delimiter) rescue '0.0'%></b></td>
							<td><b><%= number_format(total_convenience_charge ,  precision:precision.to_i, delimiter:delimiter) rescue '0.0'%></b></td>
							<!-- <td><b><%= number_format(total_delivery_tax ,  precision:precision.to_i, delimiter:delimiter) rescue '0.0'%></b></td>
							<td><b><%= number_format(total_convenience_tax ,  precision:precision.to_i, delimiter:delimiter) rescue '0.0'%></b></td>
							<td><b><%= number_format(total_commercial_tax ,  precision:precision.to_i, delimiter:delimiter) rescue '0.0'%></b></td>-->
							<td><b><%= number_format(total_tax ,  precision:precision.to_i, delimiter:delimiter) rescue '0.0'%></b></td>
							<td><b><%= number_format(grand_total ,  precision:precision.to_i, delimiter:delimiter) rescue '0.0'%></b></td>
							<td><b><%= number_format(total_transaction_fee ,  precision:precision.to_i, delimiter:delimiter) rescue '0.0'%></b></td>
						</tr>
					</tbody>
				</table>
			</div>
		</div>
	</div>
</div>

<script>
	$(function(){

		var check_arr = [];
		search_by_period();
		$('#sel_period').change(function(){
			search_by_period();
		});

		function search_by_period(){
			var period = $('#sel_period').val();
			var period_type = 0;
			var from = "";
			var to = "";

			show_shift_name(period,period_type,from,to,'shift_item');
		}

		// OK button is clicked
		$('#from').bootstrapMaterialDatePicker().on('beforeChange', function(e, date){
			new_date = new Date(date) ;
			month    = parseInt(new_date.getMonth()+1)
			from = new_date.getDate() + "-" + month + "-" + new_date.getFullYear();
			$('#from').val(from)
			search_by_date();
		});
		$('#to').bootstrapMaterialDatePicker().on('beforeChange', function(e, date){
			new_date = new Date(date) ;
			month    = parseInt(new_date.getMonth()+1)
			to = new_date.getDate() + "-" + month + "-" + new_date.getFullYear();
			$('#to').val(to)
			search_by_date();
		});

		function search_by_date(){

	  		from = $("#from").val();
	  		to   = $("#to").val();

			var period = 0;
			var period_type = 1;

			if(to != '' && from != ''){
				shift_name = from + ',' + to;

				check_arr.push(to);

				if(check_arr.length == 1){
					show_shift_name(period,period_type,from,to,'shift_item');
				}
				if(check_arr.length == 3){
					check_arr = [];
				}
			}

		}

		function show_shift_name(period,period_type,from,to,shift_item){
			var shift = $('#shift_name');

			shift.empty();

			var str = '';
			var param_shift = '';
			var param_shift = '<%= params[:shift_name] rescue '-'%>';
			if (from == '' && to == '') {
				from = $("#from").val();
	  		    to   = $("#to").val();
			}
			url = '<%= reports_get_shift_by_order_reservation_path %>';

			$.get(url, {period :period, period_type :period_type, from :from, to :to, report_type :shift_item} , function(data){

				str = '<option value="0">--- All Shift ---</option>';
				$(data.message).each(function(index){

					var local_date = data.message[index].local_opening_date + ' - ' + data.message[index].local_closing_date;
					var sh_date = data.message[index].opening_date + ' - ' + data.message[index].closing_date;
					var shift_id = data.message[index].shift_id ;
					if(param_shift != ''){
						if(shift_id == param_shift){
							selected = 'selected = "selected"';
						}
						else{
							selected = '';
						}
					}else{
						selected = '';
					}
					str += '<option value="'+ shift_id +'" '+ selected +'>' + local_date + '</option>';

			   // console.log(sh_date)
			})
				shift.append(str);
			});
		}

	});
</script>
