<script src="assets/js/king-table.js"></script>

<!-- main -->
<div class="content">
	<div class="main-content">
		<div class="row ">
			<?php if($data['create']=='1'): ?>
			<div class="col-md-4 update">
				<!-- ADVANCED VALIDATION -->
				<div class="widget">
					<div class="widget-header">
					<h3><i class="fa fa-check-circle"></i> Buyer Company</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 Company 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">Company Name*</label>
								<input type="hidden" class="form-control" id="cmp_id" name="cmp_id" >
								<input type="text" name="companyname" id="companyname" class="form-control" >
								<small class="text-danger" id="errcompanyname"></small>
							</div>
							<div class="form-group">
								<label for="text-input1">Business Type*</label>
								<select name="companytype" id="companytype" class="form-control" required>
									<option value="">-- Select --</option>
                                    <option value="Buyer" selected>Buyer</option>
									<option value="Buyer & Seller">Buyer & Seller</option>
								</select>
								<small class="text-danger" id="errcompanytype"></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 Buyer Company</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>Company Name</th>
										<th>Business type</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['Company']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $Company): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
									<tr>
										<td><?php echo e($Company->cmp_name); ?></td>
										<td><?php echo e($Company->cmp_type); ?></td>
										<td><?php echo e($Company->cmp_description); ?></td>
										<td><?php echo e($Company->cmp_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="Editseller('.$Company->cmp_id.')">Edit</button>'; } if($data['delete']=='1'){echo' <button class="btn btn-default btn-xs print" onclick="Delete('.$Company->cmp_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 cmp_id = $('#cmp_id').val();
				var companyname = $('#companyname').val();
				var companytype = $('#companytype').val();
				var description = $('#description').val();
				var status = $('#status').val();
				
				
				document.getElementById("errcompanyname").innerHTML = "";
				document.getElementById("errcompanytype").innerHTML = "";
				
				if(companyname == "")
				{
					document.getElementById("errcompanyname").innerHTML = "Company 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(companytype == "")
				{
					document.getElementById("errcompanytype").innerHTML = "Company type 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: "/salesinsertcompany",
							type:'POST',
							data: {_token:_token, cmp_id:cmp_id, companyname:companyname, companytype:companytype, description:description, status:status},
							success: function(data) {
								if(data.status == 1)
								{
									printSuccessMsg(data.message);
									$('#sideMenu').load('salesloadcompany');
								}
								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);
		}
		
		function Editseller(id)
		{
			$.ajax({
				type: "GET",
				url:"/saleseditcompany",
				data: "&editId="+id,
				success: function (data) 
				{
					$('.update').html(data);
				}
			});
		}
		function Delete(id)
		{
			if (confirm("Are you sure delete!") == true)
			{
				$.ajax({
					type: "GET",
					url:"/salesdelcompany",
					dataType: "json",
					data: {
						'_token': $('input[name=_token]').val(),
						'id': id
					},
					success: function (sdata) {
						
						$('#sideMenu').load('salesloadcompany');
						
					}
				});
			}
		}
		
	</script>					  				