<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">Company</li>
	</ul>
</div>
<!-- main -->
<div class="content">
	<div class="main-content">
		<div class="row">
			<?php 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> Category</h3></div>
					<div class="widget-content">
						<form id="frmmain" name="frmmain" data-parsley-validate novalidate>
							<input type="hidden" name="_token" value="<?php echo e(csrf_token()); ?>">
							<?php echo csrf_field(); ?>           
							<div class="alert  alert-info " >
								<ul><li class="fa fa-cog  fa-1x fa-fw"></li>Kindly enter Category 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-input1">Category Name*</label>
								<input type="hidden" class="form-control" id="cat_id" name="cat_id" >
								<input type="text" name="categoryname" id="categoryname" class="form-control" >
								<small class="text-danger" id="errcategoryname"></small>
							</div>
							<div class="form-group">
								<label for="text-input2">Main Category*</label>
								<select name="maincategory" id="maincategory" class="form-control" required>
									<option value="0">-- Select --</option>
								</select>
								<small class="text-danger" id="errmaincategory"></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>
			<?php endif; ?>
			<?php if($data['create']=='1'): ?>
			<div class="col-md-8">
				<?php else: ?>
				<div class="col-md-12">
					<?php endif; ?>
					<!-- BASIC VALIDATION -->
					<div class="widget widget-table">
						<div class="widget-header">
							<h3><i class="fa fa-table"></i> View Category</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>Category Name</th>
										<th>Main Category</th>
										<th>Description</th>
										<th>Status</th>
										<?php if($data['edit']=='1' || $data['delete']=='1'){echo'<th>Action</th>';}?>
									</tr>
								</thead>
								<tbody>
									<?php $__currentLoopData = $data['Category']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $Category): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
									<tr>
										<td><?php echo e($Category->cat_name); ?></td>
										<td><?php echo e($Category->maincat); ?></td>
										<td><?php echo e($Category->cat_description); ?></td>
										<td><?php echo e($Category->cat_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('.$Category->cat_id.')">Edit</button>'; } if($data['delete']=='1'){echo' <button class="btn btn-default btn-xs print" onclick="Delete('.$Category->cat_id.')">Delete</button>';} echo '</td>';?>
									</tr>
									<?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
								</tbody>
							</table>
						</div>
					</div>
					<!-- END BASIC VALIDATION -->
				</div>
				
			</div>
		</div>
	</div>
	<!-- /main -->
	<script type="application/javascript">
		i=0;
		$(document).ready(function()
		{
			$(document).on('submit', '#frmmain', function(e)
			{
				$(".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 cat_id = $('#cat_id').val();
				var categoryname = $("input[name='categoryname']").val();
				var maincategory =  $('#maincategory').val();
				var description = $('#description').val();
				var status = $('#status').val();
				
				
				document.getElementById("errcategoryname").innerHTML = "";
				
				if(categoryname == "")
				{
					document.getElementById("errcategoryname").innerHTML = "Category Name 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(i==0)
					{
						
						i++;
						e.preventDefault();
						$.ajax({
							url: "/category",
							type:'POST',
							data: {_token:_token, 
								'cat_id':cat_id,
								'categoryname':categoryname, 
								'maincategory':maincategory, 
								'description':description, 
								'status':status
							},
							success: function(data) {
								
								if(data.status == 1)
								{
									printSuccessMsg(data.message);
									$('#sideMenu').load('category');
								}
								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);
			
		}						
	</script>	
	<script>
		$(document).ready(function(){
			$.ajax({
				type: "GET",
				url:"/maincategory",
				dataType: "json",
				success: function (data) {
					$.each(data,function(i,obj)
					{
						var div_data="<option value="+obj.cat_id+">"+obj.cat_name+"</option>";	
						$(div_data).appendTo('#maincategory'); 
					});  
				}
			});
		});
		
		
		function Edit(id)
		{
			$.ajax({
				type: "GET",
				url:"/editcategory",
				dataType: "json",
				data: {
					'_token': $('input[name=_token]').val(),
					'id': id
				},
				success: function (data) {
					$.each(data,function(i,obj)
					{
						$('#cat_id').val(obj.cat_id);
						$('#categoryname').val(obj.cat_name);
						$('#maincategory').val(obj.maincat_id);
						$('#description').val(obj.cat_description);
						$('#status').val(obj.cat_status);
						// $('#btnadd').val("UPDATE"); 
					});  
				}
			});
		}
		function Delete(id)
		{
			//alert(id);
			k = 0;
			if(k == 0)
			{
				k++;
				$.ajax({
					type: "GET",
					url:"/delcategory",
					dataType: "json",
					data: {
						'_token': $('input[name=_token]').val(),
						'id': id
					},
					success: function (sdata) {
						if(sdata.status == 1)
						{
							printSuccessMsg(sdata.message);
							$('#sideMenu').load('category');
						}
						else
						{
							printErrorMsg(sdata.message)
						}
					}
				});
			}
			
		}		
	</script>				  		