<!-- <div class="page-header">
  <ol class="breadcrumb">
    <span class="float-right">
      <a href="" id="back">Back</a>
    </span>
  </ol>
</div> -->
<div class="row">
  	<div class="col-md-12 col-sm-12 col-lg-8">
	    <div class="card show_all_commissioner_card">
	      <div class="card-block row" style="margin:0.5rem;">
	      	<% @commissioners.each do |com| %>
	      	 	<div class="col-md-6 col-sm-6 col-lg-3 mt-3 touch_commissioner" commissionerId='<%= com["id"] %>' commissionerName='<%= com["name"] %>'>
	      	 		<div class="card custom-card testimonial-card fadeInRight" style="height:100%;background-image:url(<%=com["image_path"] %>);background-repeat: no-repeat; background-size: cover;">
	      	 			<div class="custom-card-head card-head row" style="line-height:14px;margin:0px;">
	      	 				<div class="col-8" style="padding:0px !important;"><%=com["name"] %></div>
	      	 				<% if com["check_in_count"] >0%>
	      	 					<div class="col-4 row" style="margin:auto;"><span class="badge badge-primary" style="margin:auto;font-size: 0.8rem;">A :<%=com["check_in_count"] %></span></div>
	      	 				<% end %>
	      	 				
	      	 			</div>
	      	 			<div class="add_icon p-t-65 p-b-65 m-r-">
	      	 				
	      	 			</div>
	      	 		</div>
	      	 	</div>
	      	 <% end %>
	      	 <div class="col-md-6 col-sm-6 col-lg-3 mt-3 add_more_commissioner">
      	 		<div class="card custom-card testimonial-card fadeInRight" style="height:100%;width: 100%;">
      	 			<div class="row" style="margin: auto;">
      	 				<i class="material-icons" style="color: gray;font-size: 9rem;">add</i>
      	 			</div>
      	 		</div>
	      	 </div>
	      </div>
	  	</div>
	</div>
  	<div class="col-md-12 col-sm-12 col-lg-4" id="show_list_induty_card">
	  	<div class="card" >
	  		<div class="alert success-msg" role="alert">
	  			<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
			  <i class="fa fa-check"></i>
			  Successed!
			</div>
	  		<div class="card-header row" style="margin-right: 0;margin-left: 0">
	  			<div class="col-7 commissionerName"><label>Create In Duty</label></div>
	  			<div class="col-5"><button type="button" class="btn btn-block btn-default waves-effect" id="back"><i class="material-icons">reply</i>Back to Order</button></div>
	      	</div>
	      	<input type="hidden" name="booking_id" value="<%=@booking_id %>" id="booking_id">
	      	<input type="hidden" name="commissionerId" value="" id="commissionerId">
	      	<div class="row create_induty_card" style="margin-left:1rem;margin-right: 1rem;">
		      		<table class="table table-striped" id="show_in_duty_table">
		      			<thead >
		      				<tr>
		      					<th>Dining</th>
		      					<th>Check In At</th>
		      					<th>Action</th>
		      				</tr>
		      			</thead>
		      			<tbody class="occupide_dining">
		      			</tbody>
		      		</table>
		    </div>
			<div class="row" >
	    		<div class="col-8 add_to_order" style="margin: auto;">
	    			
	    		</div>
			</div>
			<div class="row form-inputs form-group" style="margin: auto;">
		    	<hr style="width :100%;margin-bottom: 1rem;">
			      	<div class="col-12 " style="">
			      		<div class="row form-inputs form-group" style="margin: auto;">
			      			<div class="col-12" >
				      			<label class="radio-room"><input type="radio" name="radio" id="radio-room" value="room" checked="checked" /> Room</label>
				      			<label class="radio-table"><input type="radio" name="radio" id="radio-table"  value="table" /> Table</label>
			      			</div>
			      		</div>
			      		<div class="row form-inputs form-group room_collection" style="margin: auto;">
					      	<div class="col-12 ">
						      	<%= simple_form_for 'zone', :method => :get do |f| %>
							      <%= f.collection_select :dinning_id, Room.all, :id, :name, {prompt: 'Select Room'},:class=>'form-control',:required => true %>
							    <%end %>
							</div>
						 </div>
						 <div class="row form-inputs form-group table_collection" style="margin: auto;">
					      	<div class="col-12 ">
						      	<%= simple_form_for 'zone_t', :method => :get do |f| %>
							      <%= f.collection_select :dinning_id, Table.all, :id, :name, {prompt: 'Select Table'},:class=>'form-control',:required => true %>
							    <%end %>
							</div>
						 </div>
			      	</div>
			</div>
		    <div class="row form-inputs form-group" style="margin: auto;">
				<div class="col-12 ">
					<%= simple_form_for 'save_induties', :url => induties_induties_create_path, :method => :post do |f| %>
						<%= f.check_box :by_name,:class=>'form-control'%> 
					    <label class="checkboxLabel" for="save_induties_by_name">By Name</label>
					<% end %>
				</div>
			</div>
			<button type="button" class=" btn bg-blue form-actions btn_create_induty" id="btn_create_induty" disabled="">Create In Duty</button>
	  	</div>
	</div>
	
</div>
<script type="text/javascript">
	$(".table_collection").hide();
	$('#back').on('click',function(){
		event.preventDefault();
		var dinning_id = localStorage.getItem('dinning_id');
		var dinning_type = localStorage.getItem('dinning_type');
      	window.location.href = '/origami/'+dinning_type+'/'+dinning_id;
	});
	$( document ).ready(function() {
		// document.getElementById("btn_create_induty").disabled = true;
		$('#btn_create_induty').attr("disabled", 'disabled');
		var booking_id=document.getElementById("booking_id").value
		getCurrentBooking(booking_id);
	});
	$(document).on('click', '.btn_create_induty', function(event){
		$('.add_to_order').html("");
		event.preventDefault();
		var str=document.getElementById("zone_dinning_id").value;
		var str_t=document.getElementById("zone_t_dinning_id").value;
			if (str == ""){
				zone_dinning_id =document.getElementById("zone_t_dinning_id").value;
			}
			if (str_t == ""){
				zone_dinning_id =document.getElementById("zone_dinning_id").value;
			}
			if(str == "" && str_t == ""){
				document.getElementById('zone_dinning_id').focus();
				$('#zone_dinning_id').css('border-color', 'red');
				$('#zone_t_dinning_id').css('border-color', 'red');
				return false
			}
			else{
				var booking_id=document.getElementById("booking_id").value
				var commissionerId=document.getElementById("commissionerId").value
				var by_name =document.getElementById("save_induties_by_name").checked
				$.ajax({
		            type: "POST",
		            url: "assign_create",
		            dataType: "json",
		            data: {booking_id:booking_id,commissionerId:commissionerId,dinning_id:zone_dinning_id,by_name:by_name},
		            success: function (data) {
	            		in_time = changDateFormat(data.in_time);
	            		d_type = data.dinning_type.charAt(0);
	            			row ='<tr>'
				                   	+'<td>'+d_type+'-'+data.dinning_id+'</td>'
				                    +'<td>'+in_time+'</td>'
				                    +'<td><a href="" onclick = "checkout_induty(this)"  class="btn checkout_induty" style= "background-color: #3F51B5;"indutyId="'+ data.id +'" >Check Out</a>&nbsp;<a href="" onclick = "remove_induty(this)" class="btn remove_induty" style= "background-color: red;"indutyId="'+ data.id +'" >Remove</a></td>'
			                  +'</tr>';
		                $('.occupide_dining').append(row);
		                $('#zone_dinning_id').val("");
		                document.getElementById("save_induties_by_name").checked = false;
		                var scrollTo = document.getElementById('show_list_induty_card');
      					$("HTML, body").animate({scrollTop: scrollTo.offsetTop - 60 }, 500 );
      					$(".success-msg").css('display','block');
		            }
		        });
			}
	});
	$(document).on('click', '.touch_commissioner', function(event){
		$('#btn_create_induty').attr("disabled", '');
		$(".success-msg").css('display','none');
		$('.add_to_order').html("");
		document.getElementById("btn_create_induty").disabled = false;
        var commissionerId = this.getAttribute("commissionerId");
        var commissionerName = this.getAttribute("commissionerName");
        var url = "get_all_occupied_dining_path" ;
        $('.occupide_dining').html("");
         $('.commissionerName').html("");
         booking_id=document.getElementById("booking_id").value
        $.ajax({
            type: "GET",
            url: "/induties/assign_in_duties/get_all_occupied_dining/"+commissionerId,
            dataType: "json",
            success: function (data) {
            	$('.commissionerName').append('<label>'+commissionerName+' (<a href="" onclick = "getCurrent(this)">'+booking_id+'</a> )</label'); 
            	data.forEach(function(items) {
            		in_time = changDateFormat(items.in_time);
            		d_type = items.dinning_type.charAt(0);
            			row ='<tr>'
		                   	+'<td>'+d_type+'-'+items.dinning_id+'</td>'
		                    +'<td>'+in_time+'</td>'
		                    +'<td><a href="" onclick = "checkout_induty(this)"  class="btn checkout_induty" style= "background-color: #3F51B5;"indutyId="'+ items.id +'" >Check Out</a>&nbsp;<a href="" onclick = "remove_induty(this)" class="btn remove_induty" style= "background-color: red;"indutyId="'+ items.id +'" >Remove</a></td>'
	                    +'</tr>';
                $('.occupide_dining').append(row);

				});
            	$('#commissionerId').val(commissionerId);
            	var scrollTo = document.getElementById('show_list_induty_card');
      			$("HTML, body").animate({scrollTop: scrollTo.offsetTop - 60 }, 500 );
            }
        });
    });
    function changDateFormat(date){
			datetime = new Date(date);
			day = datetime.getDate();
			month = datetime.getMonth() + 1; //month: 0-11
			year = datetime.getFullYear();
			date = year + "-" + day + "-" + month;
			var hours = datetime.getHours();
			var minutes = datetime.getMinutes();
			var ampm = hours >= 12 ? 'PM' : 'AM';
			hours = hours % 12;
			hours = hours ? hours : 12; // the hour '0' should be '12'
			minutes = minutes < 10 ? '0'+minutes : minutes;
			var in_time = date +' '+hours + ':' + minutes + ' ' + ampm;
		return in_time;
    }
    $('input[type=radio]').change(function() {       
    	if(this.value == "room"){
    		$(".table_collection").css('display','none');
    		$(".room_collection").css('display','block');
    	}
    	else{
    		$(".room_collection").css('display','none');
    		$(".table_collection").css('display','block');
    	}
	});
	function checkout_induty(row){
		event.preventDefault();
		var indutyId = row.getAttribute("indutyId");
		var booking_id=document.getElementById("booking_id").value
		datetime = new Date();
		datetime = changDateFormat(datetime);
		 swal({ 
        title: '',
        text: 'Are you sure want to checkout at ' +datetime,
        html:true,
        confirmButtonColor: "#3F51B5",
        confirmButtonText: "OK",
        closeOnConfirm: true,
        showCancelButton: true,
        cancelButtonColor: '#d33',
        }, function(isConfirm){
            if(isConfirm){
		           $.ajax({
			            type: "POST",
			            url: "assign_checkout/"+indutyId,
			            dataType: "json",
			            success: function (data) {
			         		var d = row.parentNode.parentNode.rowIndex;
			      			document.getElementById('show_in_duty_table').deleteRow(d);
			      			var scrollTo = document.getElementById('show_list_induty_card');
			      			$("HTML, body").animate({scrollTop: scrollTo.offsetTop - 60 }, 500 );
			      			$(".success-msg").css('display','block');
			            }
			        });
	            }
	    }); 
		
	}
	function remove_induty(row){
		event.preventDefault();
		var indutyId = row.getAttribute("indutyId");
		var booking_id=document.getElementById("booking_id").value
		swal({ 
        title: '',
        text: 'Are you sure want to remove induties ',
        html:true,
        confirmButtonColor: "#3F51B5",
        confirmButtonText: "OK",
        closeOnConfirm: true,
        showCancelButton: true,
        cancelButtonColor: '#d33',
        }, function(isConfirm){
            if(isConfirm){
		           $.ajax({
			            type: "DELETE",
			            url: "assign_remove/"+indutyId,
			            dataType: "json",
			            success: function (data) {
			            	var d = row.parentNode.parentNode.rowIndex;
			      			document.getElementById('show_in_duty_table').deleteRow(d);
			      			var scrollTo = document.getElementById('show_list_induty_card');
			      			$("HTML, body").animate({scrollTop: scrollTo.offsetTop - 60 }, 500 );
			      			$(".success-msg").css('display','block');

			            }
			        });
	            }
	    });
		
	}
    $(document).on('click', '.add_more_commissioner', function(event){
    	localStorage.setItem('from_induty', true);
    	localStorage.setItem('booking_id', document.getElementById("booking_id").value);
    	window.location.href ='/settings/commissioners/new';
    });
    localStorage.setItem('from_induty', false);
    
    function getCurrentBooking(booking_id){
    	document.getElementById("btn_create_induty").disabled = true;
    	$('.commissionerName').html("");
    	$('.add_to_order').html("");
    	$('.occupide_dining').html("");
    	$.ajax({
	            type: "GET",
	            url: "get_current_booking",
	            dataType: "json",
	            success: function (data) {
	            	dinning_id = localStorage.getItem('dinning_id');
	            	dinning_type = localStorage.getItem('dinning_type');
	            	booking_id=document.getElementById("booking_id").value
	            	$('.commissionerName').append('<label><a href="" onclick = "getCurrent(this)">'+booking_id+'</a></label');
	            	$('.add_to_order').append('<input type="hidden" name="current_checkin_count" value='+data.length+'><input type="submit" class="btn bg-blue form-actions add_to_charges" value="Charges to Invoice" onclick = "addToOrder(this)" current_checkin_induties_count ='+data.length+'>');
	            	data.forEach(function(items) {
            		in_time = changDateFormat(items.in_time);
            		d_type = items.dinning_type.charAt(0);
            			row ='<tr>'
		                   	+'<td>'+d_type+'-'+items.dinning_id+'</td>'
		                    +'<td>'+in_time+'</td>'
		                    +'<td><a href="" onclick = "checkout_induty(this)"  class="btn checkout_induty" style= "background-color: #3F51B5;"indutyId="'+ items.id +'" >Check Out</a>&nbsp;<a href="" onclick = "remove_induty(this)" class="btn remove_induty" style= "background-color: red;"indutyId="'+ items.id +'" >Remove</a></td>'
	                    +'</tr>';
	            
                $('.occupide_dining').append(row);
				});
	            	
	            var scrollTo = document.getElementById('show_list_induty_card');
      			$("HTML, body").animate({scrollTop: scrollTo.offsetTop - 60 }, 500 );
	            }
	        });
    }
    function getCurrent(row){
    	$(".success-msg").css('display','none');
    	booking_id=document.getElementById("booking_id").value
		event.preventDefault();
		getCurrentBooking(booking_id);
	}
	function addToOrder(row){
		event.preventDefault();
    	$(".success-msg").css('display','none');
    	booking_id=document.getElementById("booking_id").value
    	var current_checkin_induties_count = row.getAttribute("current_checkin_induties_count");
    	localStorage.setItem("current_checkin_induties_count",current_checkin_induties_count);
    	var dinning_id = localStorage.getItem('dinning_id');
		var dinning_type = localStorage.getItem('dinning_type');
      	window.location.href = '/origami/'+dinning_type+'/'+dinning_id;
	}
</script>
	 
