 <div class="row">
  <!-- Column One -->
  <div class="col-lg-6 col-md-6 col-sm-6">
    <!-- Order Details -->
    <div class="card"  >
        <!-- <div class="card-header">
          <div><strong id="order-title">ORDER DETAILS</strong></div>
        </div> -->
        <div class="card-block">
          <div class="card-title row">
            <div class="col-lg-6 col-md-6 col-sm-6">
              <p id="sale-id" class="hidden"><%=@sale_data.sale_id %></p>
              <p>Receipt No: <span id="receipt_no"><%=@sale_data.receipt_no rescue ' '%></span></p>
              <!-- <p>Cashier:  <span id="cashier"><%=@sale_data.cashier_name rescue ' '%></span></p> -->
            </div>
            <div class="col-lg-6 col-md-6 col-sm-6 text-right">
              <p>Date:  <span id="receipt_date"> <%=@sale_data.receipt_date.strftime("%d/%m/%Y - %I:%M %p") rescue '-'%></span></p>
            </div>
          </div>
          <div class="card-text">
            <table class="table table-default" id="order-items-table">
              <thead>
                <tr>
                  <th class="item-name">Items</th>
                  <th class="item-attr">QTY</td>
                  <th class="item-attr">Price</td>
                </tr>
              </thead>
              <tbody>
                <% sub_total = 0 %>
	            <% @sale_data.sale_items.each do |sale_item| %>
	          		<% 
		          		sub_total += sale_item.qty*sale_item.unit_price 
		          		unless sale_item.price == 0
	          		%>
	            	<tr class="item-row" id=<%= sale_item.sale_item_id %> >
	                    <td style="width:60%; text-align:left">
	                       <span id="item-account-type" class="hidden"><%=sale_item.account_id%></span>
	                       <span id="item-name-price"><%=sale_item.product_name%>@<%=sale_item.unit_price%></span>
	                    </td>
	                    <td style="width:20%; text-align:right">
	                      <span id="item-qty"><%=sale_item.qty%></span>
	                    </td>
	                    <td style="width:20%; text-align:right">
	                       <span id="item-total-price"><%=(sale_item.qty*sale_item.unit_price)%></span>
	                    </td>
	                  </tr>
	            <%
	            	   	end
	            	end 
	            %>
              </tbody>
            </table>
          </div>
          <div class="card-footer">
            <table class="table" id="order-charges-table" border="0">
              <tr>
                <td class="charges-name"><strong>Sub Total:</strong></td>
                <td class="item-attr"><strong id="order-sub-total"><%=sub_total%></strong></td>
              </tr>             
              <tr>
                <td class="charges-name"><strong>Discount:</strong></td>
                <td class="item-attr">(<strong id="order-discount"><%=@sale_data.total_discount rescue 0%></strong>)</td>
              </tr>
              <tr class="hidden">
                <td class="charges-name"><strong>Tax:</strong></td>
                <td class="item-attr"><strong id="order-Tax"><%=@sale_data.total_tax rescue 0%></strong></td>
              </tr>
              <tr class="hidden">
                <td class="charges-name"><strong>Grand Total:</strong></td>
                <td class="item-attr"><strong id="order-grand-total"><%=@sale_data.grand_total rescue 0%></strong></td>
              </tr>
            </table>
          </div>
        </div>
    </div>
  </div>
  <!-- Column One -->

  <!-- Column Two -->
  <div class="col-lg-5 col-md-5 col-sm-5">
	<!-- Discount Amount -->
    <div class="card">
        <div class="card-header">
          <div><strong id="order-title">Other Charges Control</strong></div>
        </div>
        <div class="card-block">
			<div class="card-title">
				<div class="form-horizontal">
					<div class="col-md-12">	
						<div class="form-group">										
							<input type="text" id="other-charges-amount" name="other-charges-amount" class="form-control" placeholder="Amount" />							
						</div>						
						<div class="form-group">
							<textarea id="reasons" name="reasons" rows="2" class="form-control" placeholder="Reasons"></textarea>
						</div>	
					</div>					
				</div>
			</div>

			<hr />

	      	<div class="row">
	      		<div class="col-md-9">
		         <div class="row bottom">
		           <div class="col-md-3">
		              <div class="fluid cashier_number" data-value="3000" data-type="add">3000</div>
		           </div>
		           <div class="col-md-9">
		                 <div class="col-md-4 cashier_number" data-value="1" data-type="num">1</div>
		                 <div class="col-md-4 left cashier_number" data-value="2" data-type="num">2</div>
		                 <div class="col-md-4 left cashier_number" data-value="3" data-type="num">3</div>
		            </div>
		         </div>

		         <div class="row bottom">
		           <div class="col-md-3">
		             <div class="fluid cashier_number" data-value="5000" data-type="add">5000</div>
		           </div>
		           <div class="col-md-9">
		                <div class="col-md-4 cashier_number" data-value="4" data-type="num">4</div>
		                <div class="col-md-4 left cashier_number" data-value="5" data-type="num">5</div>
		                <div class="col-md-4 left cashier_number" data-value="6" data-type="num">6</div>
		           </div>
		         </div>

		         <div class="row bottom">
		            <div class="col-md-3">
		                <div class="fluid cashier_number" data-value="10000" data-type="add">10000</div>
		            </div>
		            <div class="col-md-9">
		                 <div class="col-md-4 cashier_number" data-value="7" data-type="num">7</div>
		                 <div class="col-md-4 left cashier_number" data-value="8" data-type="num">8</div>
		                 <div class="col-md-4 left cashier_number" data-value="9" data-type="num">9</div>
		            </div>
		         </div>

		          <div class="row bottom">
		          	 <div class="col-md-3">
		                <div class="fluid cashier_number" data-value="15000" data-type="add">15000</div>
		             </div>
		             <div class="col-md-9">
		                  <div class="col-md-4 cashier_number" data-value="0" data-type="num">0</div>
		                  <div class="col-md-4 left cashier_number" data-value="." data-type="num">.</div>
		                  <div class="col-md-4 left cashier_number" data-value="00" data-type="num">00</div>
		             </div>
		          </div>

		          <div class="row">
		          	 <div class="col-md-3">
		                <div class="fluid cashier_number" data-value="20000" data-type="add">20000</div>
		             </div>
		             <div class="col-md-9">
		                 <div class="col-md-4 cashier_number"></div>
		                 <div class="col-md-4 left cashier_number red" data-type="del">DEL</div>
		                 <div class="col-md-4 left cashier_number green" data-type="clr">CLR</div>
		             </div>
		          </div>		          
		      	</div>

		      	<div class="col-md-3">
		      		<div class="form-group">
						<button id="add_charges" class="btn btn-info fluid action-btn">Add</button>						
						<button id="remove-item" class="btn btn-default fluid action-btn">Remove</button>
		      		</div>
	        	  </div>
	      	</div>	
        </div>
    </div>
  </div>

  <!-- Column Three -->
  <div class="col-lg-1 col-md-1 col-sm-1">
	<!-- Action Panel -->
    <div>
	    <button type="button" class="btn btn-primary btn-block" onclick="window.location.href = '/origami';"><i class="fa fa-arrow-left"></i> Back </button>
		<button id="charge_other" class="btn btn-danger btn-block action-btn">Enter</button>
    </div>
  </div>
</div>

<script type="text/javascript">
	$(document).ready(function(){
		$(".cashier_number").on('click', function(event){
			if(event.handled !== true) {
		      	var original_value=0;
	        	original_value = $('#other-charges-amount').val();

	        	var input_type = $(this).attr("data-type");

		        switch (input_type) {
		          case 'num':
						var input_value = $(this).attr("data-value");
						if (original_value == "0.0"){
							$('#other-charges-amount').val(input_value);						
						}
						else{
							$('#other-charges-amount').val(original_value + '' + input_value);						
						}
						break;

		          case 'add':
						var input_value = $(this).attr("data-value");
						amount = parseInt(input_value);
						$('#other-charges-amount').val(amount);										
						break;

				  case 'del' :
				  		var discount_text=$('#other-charges-amount').val();
				  		$('#other-charges-amount').val(discount_text.substr(0,discount_text.length-1));		
						break;

		          case 'clr':
		             $('#other-charges-amount').val("0.0");	            
		          break;
			    }
		      	event.handled = true;
		    } else {
		        return false;
		    }
		});

		// Select discount-item
		$('#order-items-table tbody').on('click', '.other-item-row',function(){
			if($(this).hasClass('selected-item') == true){
				$(this).removeClass('selected-item');
			}
			else {
		    	$(this).addClass('selected-item');
		    }
		});

		// Remove selected discount items
		$("#remove-item").on('click', function(e){
			e.preventDefault();			

			$('.other-item-row.selected-item').each(function(i){
				var sub_total = $('#order-sub-total').text();
				var charge_amount = $(this).children('td').find("#item-total-price").text();

				// Update sub total
				$('#order-sub-total').text(parseFloat(sub_total) - parseFloat(charge_amount));	
				$(this).remove();
			});
		});

		// Calculate Net Discount for Payment
		$("#add_charges").on('click', function(e){
			e.preventDefault();
			var sale_id = $('#sale-id').text();					
			var sub_total = $('#order-sub-total').text();
			var charge_amount = $("#other-charges-amount").val();
			var reasons = $("#reasons").val();	

			// Update sub total
			$('#order-sub-total').text(parseFloat(sub_total) + parseFloat(charge_amount));		

			var item_row = item_row_template(sale_id, charge_amount, reasons);
			$("#order-items-table tbody").append(item_row);								
		});

		// Calculate Other Charges for Payment
		$("#charge_other").on('click', function(e){
			e.preventDefault();
			var sale_id = $('#sale-id').text();					
			var sub_total = $('#order-sub-total').text();
			var other_charges_items = JSON.stringify(get_other_item_rows());
			var ajax_url = "/origami/" + sale_id + "/other_charges";	

			var params = { 'sale_id': sale_id, 'sub_total': sub_total, 'other_charges_items': other_charges_items };

			$.ajax({
				type: "POST",
			    url: ajax_url,
			    data: params,
			    success:function(result){			    		    			    
			    	alert("Success!");
			    	if(result.table_type == "Table"){
			    		window.location.href = "/origami/table/" + result.table_id
			    	}
			    	else {
			    		window.location.href = "/origami/room/" + result.table_id
			    	}		    	
			    }
			});
		});
	});

/* Remove Selection */
function selection_remove(){
	$(".item-row").removeClass("selected-item");
	$(".discount_item_row").removeClass("selected-item");
	$(".accounts-type").removeClass("selected-account");
}	

/* Get discount Item rows */
function get_other_item_rows(){
	var sale_items = [];	
	$('.other-item-row').each(function(i){
		var sale_item = {};
		sale_item.id = $(this).attr('id');
		sale_item.name = $(this).find('#item-name-price').text();
		sale_item.price = $(this).find('#item-total-price').text();
		sale_items.push(sale_item);
	});	
	return sale_items;
}	

function item_row_template(sale_id, charge_amount, reasons){
	var item_row = "<tr class='other-item-row' id='SLI-000000000000'>" +
        						"<td style='width: 60%; text-align: left;'>" + 
        						"<span id='item_account_type' class='hidden'>" + 
        							0 +
        						"</span>" + 
        						"<span id='item-name-price'>" + 
        							reasons + 
        						"</span>" + 
        						"</td>" +
        						"<td style='width: 20%; text-align: right;'>" + 
        						"<span id='item-qty'>1</span>" +
        						"</td>" +
        						"<td style='width: 20%; text-align: right;'>" + 
        						"<span id='item-total-price'>" + 
        							charge_amount +
        						"</span>" + 
        						"</td>" +
    					   		"</tr>";
	return  item_row; 					   		
}

</script>
