<script src="assets/js/king-table.js"></script>
<div class="col-lg-12">
	<ul class="breadcrumb">
		<li><i class="fa fa-home"></i><a href="/home">Home</a></li>
		<!--<li><a href="#">Forms</a></li>-->
		<li class="active">Shed</li>
	</ul>
</div>			<!-- main -->
<div class="content">
	<div class="main-content">
		<div class="row">
			@if($data['create']=='1')
			<div class="col-md-4">
				<!-- ADVANCED VALIDATION -->
				<div class="widget">
					<div class="widget-header">
					<h3><i class="fa fa-check-circle"></i> Shed</h3></div>
					<div class="widget-content">
						<form id="frmmain" name="frmmain" data-parsley-validate novalidate>
							<input type="hidden" name="_token" value="{{ csrf_token() }}">
							{!! csrf_field() !!}           
							<div class="alert  alert-info " >
								<ul><li class="fa fa-cog  fa-1x fa-fw"></li>Kindly enter Shed Details..</ul>
							</div>						
							<div class="alert alert-success print-success-msg" style="display:none">
								<ul></ul>
							</div>	
							<div class="alert alert-danger print-error-msg" style="display:none">
								<ul></ul>
							</div>
							
							
							<div class="form-group">
								<label for="text-input2">Branch*</label>
								<select name="branch" id="branch" class="form-control" required onchange="LoadUser(this.value,0)">
									<option value="0">-- Select --</option>
									
								</select>
								<small class="text-danger" id="errbranch"></small>
							</div>
							<div class="form-group">
								<label for="text-input2">User*</label>
								<select name="user" id="user" class="form-control" required>
									<option value="0">-- Select --</option>
								</select>
								<small class="text-danger" id="erruser"></small>
							</div>
							<div class="form-group">
								<label for="text-input1">Shed Name*</label>
								<input type="hidden" class="form-control" id="shd_id" name="shd_id" >
								<input type="text" name="shedname" id="shedname" class="form-control" >
								<small class="text-danger" id="errshedname"></small>
							</div>
							<div class="form-group">
								<label>Description</label>
								<textarea class="form-control" name="description" id="description" rows="5" cols="30" ></textarea>
								<small class="text-danger" id="errdescription" ></small>
							</div>
							<div class="form-group">
								<label for="multiselect1">Status*</label>
								<br/>
                                <select name="status" id="status" class="form-control" required>
                                    <option value="Active">Active</option>
                                    <option value="Inactive">Inactive</option>
								</select>
								<small class="text-danger" id="errstatus"></small>
							</div>
							<br/>
							<button type="submit" id="submit" name="submit" class="btn btn-primary">Submit</button>
						</form>
					</div>
				</div>
				<!-- END ADVANCED VALIDATION -->
			</div>
			@endif
			@if($data['create']=='1')
			<div class="col-md-8">
				@else
				<div class="col-md-12">
					@endif
					<!-- BASIC VALIDATION -->
					<div class="widget widget-table">
						<div class="widget-header">
							<h3><i class="fa fa-table"></i> View Shed</h3> 
						</div>
						<div class="widget-content">
							<table id="datatable-data-export" class="table table-sorting table-striped table-hover table-bordered datatable">
								<thead>
									<tr>
										<th>Shed Name</th>
										<th>Branch</th>
										<th>User</th>
										<th>Description</th>
										<th>Status</th>
										<?php if($data['edit']=='1' || $data['delete']=='1'){echo'<th>Action</th>';}?>
									</tr>
								</thead>
								<tbody>
									@foreach($data['Shed'] as $Shed)
									<tr>
										<td>{{$Shed->shd_name}}</td>
										<td>{{$Shed->br_name}}</td>
										<td>{{$Shed->usr_name}}</td>
										<td>{{$Shed->shd_description}}</td>
										<td>{{$Shed->shd_status}}</td>
										
										<?php if($data['edit']=='1' || $data['delete']=='1'){ echo'<td>';}?>	
										
										<?php if($data['edit']=='1'){echo'<button class="btn btn-default btn-xs print" onclick="Edit('.$Shed->shd_id.')">Edit</button>'; } if($data['delete']=='1'){echo' <button class="btn btn-default btn-xs print" onclick="Delete('.$Shed->shd_id.')">Delete</button>';} echo '</td>';?>
									</tr>
									@endforeach
								</tbody>
							</table>
						</div>
					</div>
					<!-- END BASIC VALIDATION -->
				</div>
				
			</div>
		</div>
	</div>
	<!-- /main -->
	
	<script type="application/javascript">
		i=0;
		$(document).ready(function()
		{
			$('#frmmain').on('submit',function()
			{
				$(".alert-info").find("ul").html('<li class="fa fa-cog fa-spin fa-1x fa-fw"></li>Inserting record...');
				
				var _token = $("input[name='_token']").val();
				var shd_id = $("#shd_id").val();
				var branch = $("#branch").val();
				var user =  $('#user').val();
				var shedname =  $('#shedname').val();
				var description = $('#description').val();
				var status = $('#status').val();
				
				
				document.getElementById("errbranch").innerHTML = "";
				document.getElementById("erruser").innerHTML = "";
				document.getElementById("errshedname").innerHTML = "";
				
				if(branch == "0")
				{
					document.getElementById("errbranch").innerHTML = "Branch should not be empty...";
					$(".alert-info").find("ul").html('<li class="fa fa-cog fa-1x fa-fw"></li>Kindly check the error...');
				}
				else if(user == "0")
				{
					document.getElementById("erruser").innerHTML = "User should not be empty...";
					$(".alert-info").find("ul").html('<li class="fa fa-cog fa-1x fa-fw"></li>Kindly check the error...');
				}
				else if(shedname == "")
				{
					document.getElementById("errshedname").innerHTML = "Shed Name not be empty...";
					$(".alert-info").find("ul").html('<li class="fa fa-cog fa-1x fa-fw"></li>Kindly check the error...');
				}						   
				else
				{						   
					if(i == 0)
					{
						i++;
						$.ajax({
							url: "/shed",
							type:'POST',
							data: {_token:_token, 
								'shd_id':shd_id,
								'branch':branch, 
								'user':user, 
								'shedname':shedname, 
								'description':description,
								'status':status
							},
							success: function(data) {
								
								if(data.status == 1)
								{							
									printSuccessMsg(data.message);
									$('#sideMenu').load('shed');
								}
								else
								{
									printErrorMsg(data.message)
								}
								
							}
						});	
					}
					
				}
				return false;
			});
			
		});
		
		function printErrorMsg (msg) {				
			$(".alert-info").css('display','none');
			$(".print-success-msg").css('display','none');
			$(".print-error-msg").find("ul").html('');
			$(".print-error-msg").css('display','block');
			$(".print-error-msg").find("ul").append('<li class="fa fa-times-circle fa-1x fa-fw"></li>'+msg);
		}
		
		function printSuccessMsg (msg) {
			$(".alert-info").css('display','none');
			$(".print-error-msg").css('display','none');
			$(".print-success-msg").find("ul").html('');
			$(".print-success-msg").css('display','block');
			$(".print-success-msg").find("ul").append('<li class="fa fa-check  fa-1x fa-fw"></li>'+msg);
		}	
		
		$(document).ready(function(){
			$.ajax({
				type: "GET",
				url:"/loadbranch",
				dataType: "json",
				success: function (data) {
					$.each(data,function(i,obj)
					{
						var div_data="<option value="+obj.br_id+">"+obj.br_name+"</option>";	
						$(div_data).appendTo('#branch'); 
					});  
				}
			});
		});
		
		function LoadUser(brid,userid)
		{
			$('#user').empty();
			var div_data=" <option value='0'>-- Select --</option>";
			$(div_data).appendTo('#user'); 
			var _token = $("input[name='_token']").val();
			$.ajax({
				type: "GET",
				url:"/loaduser",
				dataType: "json",
				data: {_token:_token, 
					'branch':brid
				},
				success: function (data) {
					$.each(data,function(i,obj)
					{
						if(userid == 0)
						{
							var div_data="<option value="+obj.usr_id+">"+obj.usr_name+"</option>";	
						}
						else{
							if(userid == obj.usr_id)
							{
								var div_data="<option value="+obj.usr_id+" selected>"+obj.usr_name+"</option>";	
							}
							else
							{
								var div_data="<option value="+obj.usr_id+">"+obj.usr_name+"</option>";	
							}
						}
						$(div_data).appendTo('#user'); 
					});  
				}
			});
		}		
		
		function Edit(id)
		{
			$.ajax({
				type: "GET",
				url:"/editshed",
				dataType: "json",
				data: {
					'_token': $('input[name=_token]').val(),
					'id': id
				},
				success: function (data) {
					$.each(data,function(i,obj)
					{ 
						LoadUser(obj.br_id,obj.usr_id);
						$('#shd_id').val(obj.shd_id);
						$('#shedname').val(obj.shd_name);
						$('#branch').val(obj.br_id);
						
						$('#user').val(obj.usr_id);
						$('#description').val(obj.shd_description);
						$('#status').val(obj.shd_status);
						// $('#btnadd').val("UPDATE"); 
					});  
				}
			});
		}
		function Delete(id)
		{
			//alert(id);
			k = 0;
			if(k == 0)
			{
				k++;
				$.ajax({
					type: "GET",
					url:"/delshed",
					dataType: "json",
					data: {
						'_token': $('input[name=_token]').val(),
						'id': id
					},
					success: function (sdata) {
						if(sdata.status == 1)
						{
							printSuccessMsg(sdata.message);
							$('#sideMenu').load('shed');
						}
						else
						{
							printErrorMsg(sdata.message)
						}
					}
				});
			}
			
		}	
		
	</script>				  		