<!DOCTYPE html>
<?php 
include("../connection.php");
include("../user.php");
$cur_date=date('Y-m-d H:i:s');
$user=$_SESSION['user'];
$name=$_REQUEST['u_name'];
?>
<html class="no-js" lang=""> 
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title><?php echo TITLE;?></title>
    <meta name="description" content="Ela Admin - HTML5 Admin Template">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="apple-touch-icon" href="../images/bluebase.png">
    <link rel="shortcut icon" href="../images/bluebase.png">
    <!-- <script type="text/javascript" src="https://cdn.jsdelivr.net/html5shiv/3.7.3/html5shiv.min.js"></script> -->
</head>
<body >
<div class="col-lg-8">
<div class="card">
<div class="card-header">
<strong style="float:left;">User Branch Mapping</strong>
<a href="user_master.php"><button class="btn btn-primary" style="width:50px;float:right;"><i class="fa fa-backward"></i></button></a>	
</div>
<div class="card-body card-block">

	<div class="row form-group">
		<label class="control-label">User Name</label>
		<?php
		$sq=mysqli_fetch_array(mysqli_query($con,"select user_name,user_id from user_master where user_id='$name'"));
		?>
		<input type="hidden" name="user" id="user" value="<?php echo $sq['user_id']; ?>">
		<input type="text" id="full_name" name="full_name" class="form-control" value="<?php echo $sq['user_name'];  ?>" readonly>
		<div class="help-block with-errors"></div>
	</div>
	<div class="row form-group">
		<label class="control-label">Center</label>
		<select name="branch_name[]" id="branch_name" class="form-control" style="padding:5px;"  required>
		<?php 
		$que=mysqli_fetch_array(mysqli_query($con,"select m.branch_id,r.branch_name from branch_mapping m join branch_master r on r.id=m.branch_id  where m.user_id='$name' and m.to_date is null"));
		?> 
		<?php
		if($que)
		{
			$br=$que['branch_name'];
			?>
		<option value="<?php echo $que['branch_id'];?>"><?php echo $que['branch_name'];?></option>
		<?php
		$sql=mysqli_query($con,"select * from branch_master where status='0' and branch_name<>'$br'");
		while($query=mysqli_fetch_array($sql))
		{
		?>
		<option value="<?php echo $query['id'];?>"><?php echo $query['branch_name'];?></option>
		<?php
		}	
		?>	
		<?php }
		else{ ?>
		<option disabled selected>SELECT</option>
		<?php
		$sql=mysqli_query($con,"select * from branch_master where status='0'");
		while($query=mysqli_fetch_array($sql))
		{
		?>
		<option value="<?php echo $query['id'];?>"><?php echo $query['branch_name'];?></option>
		<?php
		}	
		?>	
		<?php }
		?>
		
		</select>     <div class="help-block with-errors"></div>
	</div>
	<div style="">
		<button type="submit" name="submit" id="submit" onclick="branchmapping()" class="btn btn-success" ><i class="fa fa-dot-circle-o"></i> Submit</button>
		</div>
	
	<input type="hidden" name="formsent1">
</div>	
</div>
</div>
</div>	
</form>

<div style="margin-top:20px;">		
	<link rel="stylesheet" href="/asset_temp/datatable/css/jquery.dataTables.min.css">
	<script src="/asset_temp/datatable/js/jquery.datatables.min.js"></script>
	<!-- button --><link rel="stylesheet" type="text/css" href="/asset_temp/button/css/buttons.css" /><!-- button -->
	<script>
	$(document).ready(function() {
	$('#example').DataTable();
	} );
	</script>
	<?php
	$sql=mysqli_query($con,"select bm.branch_name,u.full_name,b.*,case when b.status='0' then 'ACTIVE' else 'INACTIVE' end as status from branch_mapping b left join branch_master bm on bm.id=b.branch_id left join user_master u on u.user_id=b.user_id where u.user_id='$name'");
	?>
		<table id="example" class="table table-striped table-bordered" >
			<thead>
			<tr>
			<th>#</th>
			<th>User Name</th>
			<th>Branch Name</th>
			<th>From Date</th>
			<th>To Date</th>
			<th>Modified By</th>
			<th>Status</th>
			</tr>
			</thead>
			<tbody>
			<?php
			$i=1;
			while($result=mysqli_fetch_array($sql))
			{
			?>
			<tr >
			<td><?php echo $i++;?></td>
			<td><?php echo $result['full_name'];?></td>
			<td><?php echo $result['branch_name'];?></td>
			<td><?php echo $result['from_date'];?></td>
			<td><?php echo $result['to_date'];?></td>
			<td><?php echo $result['modified_by'];?></td>
			<td><?php echo $result['status'];?></td>
			</tr>
			<?php
			}
			?>
			</tbody>
		</table>
</div>
</body>
</html>
<script>
function branchmapping()
{
	//alert("hai");
	 var user_name=$('#full_name').val();
	 var user_id=$('#user').val();
	var branch_name=$('#branch_name').val();
	//alert(branch_name);
	
	//alert(user_id);
	
	 $.post("branchinsert.php?user="+user_id+"&branch="+branch_name,
	 function(data)
	
	{
		if(data>0)
		{
			 setTimeout(function() {
	
					swal({
						title: "Successfully",
						text: "User Mapping is Applied..!!!!",
						type: "success"
					}, function() {
							
					  window.location = "user_master.php";
					});
				}, 100);
		}
	else
	{
		setTimeout(function() {

					swal({
						title: "Successfully",
						text: "User Mapping is Applied..!!!!",
						type: "success"
					}, function() {
							
					  window.location = "user_master.php";
					});
				}, 100);
	}
		//window.location="user_master.php";
		
	});
	 
}
</script>

