
<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">Branch</li>
					</ul>
				</div>
			<!-- main -->
			<div class="content">
				<div class="main-content">
					<div class="row">
                    	
                        <div class="col-md-4">
							<!-- ADVANCED VALIDATION -->
							<div class="widget">
								<div class="widget-header">
									<h3><i class="fa fa-check-circle"></i> Branch</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 Branch 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">Branch Name*</label>
							  <input type="hidden" class="form-control" id="br_id" name="br_id" >
                            <input type="text" name="branchname" id="branchname" class="form-control" >
                            <small class="text-danger" id="errbranchname"></small>
                        </div>
						<div class="form-group">
                            <label for="text-input1">Branch Type*</label>
                             <select name="branchtype" id="branchtype" class="form-control" required>
									 <option value="">-- Select --</option>
                                    <option value="Main Branch">Main Branch</option>
                                    <option value="Sub Branch">Sub Branch</option>
                                </select>
                            <small class="text-danger" id="errbranchtype"></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>
                    
						<div class="col-md-8">
							<!-- BASIC VALIDATION -->
								<div class="widget widget-table">
						<div class="widget-header">
							<h3><i class="fa fa-table"></i> View Branch</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>Branch Name</th>
										<th>Branch type</th>
										<th>Description</th>
										<th>Status</th>
										<th>Action</th>
									</tr>
								</thead>
								<tbody>
									<?php $__currentLoopData = $data['Branch']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $Branch): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
									<tr>
										<td><?php echo e($Branch->br_name); ?></td>
										<td><?php echo e($Branch->br_type); ?></td>
										<td><?php echo e($Branch->br_description); ?></td>
										<td><?php echo e($Branch->br_status); ?></td>
										
										<td> <span class="btn btn-default btn-xs editlink" onclick="Edit(<?php echo e($Branch->br_id); ?>)">Edit</span>
												 <span class="btn btn-default btn-xs" onclick="Delete(<?php echo e($Branch->br_id); ?>)">Delete </span></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()
                       {
						 		$(".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 br_id = $('#br_id').val();
								var branchname = $('#branchname').val();
								var branchtype = $('#branchtype').val();
								var description = $('#description').val();
								var status = $('#status').val();
								
								
								document.getElementById("errbranchname").innerHTML = "";
								document.getElementById("errbranchtype").innerHTML = "";
						   
						   if(branchname == "")
						   {
								document.getElementById("errbranchname").innerHTML = "Branch 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(branchtype == "")
						   {
								document.getElementById("errbranchtype").innerHTML = "Branch 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
						   {
							//alert(i);							   
							if(i==0)
                             { 
						
								   i++;
									$.ajax({
									url: "/branch",
									type:'POST',
									data: {_token:_token, br_id:br_id, branchname:branchname, branchtype:branchtype, description:description, status:status},
									success: function(data) {
										
										if(data.status == 1)
										{
											printSuccessMsg(data.message);
											$('#sideMenu').load('branch');
										}
										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>

function Edit(id)
{
$.ajax({
		type: "GET",
		url:"/editbranch",
		dataType: "json",
		data: {
			'_token': $('input[name=_token]').val(),
			'id': id
		},
		success: function (data) {
		 $.each(data,function(i,obj)
			{
				 $('#br_id').val(obj.br_id);
				 $('#branchname').val(obj.br_name);
				 $('#branchtype').val(obj.br_type);
				 $('#description').val(obj.br_description);
				 $('#status').val(obj.br_status);
				// $('#btnadd').val("UPDATE"); 
			 });  
			}
	  });
}
function Delete(id)
{
	//alert(id);
	k = 0;
	if(k == 0)
	{
		k++;
			$.ajax({
            type: "GET",
            url:"/delbranch",
            dataType: "json",
			data: {
                '_token': $('input[name=_token]').val(),
                'id': id
            },
            success: function (sdata) {
			if(sdata.status == 1)
			{
				printSuccessMsg(sdata.message);
				$('#sideMenu').load('branch');
			}
			else
			{
				printErrorMsg(sdata.message)
			}
           }
          });
	}

}
</script>					  