<?php
session_start();
// ============
include("../php_crud/my_class.php");
$obj=new my_class();
// ============


if(isset($_SESSION['LOGIN_USER'])) {

	$institute_ids=explode(",",$_SESSION["LOGIN_INSTITUTE"]);
	$branch_ids=explode(",",$_SESSION["LOGIN_BRANCHES"]);	
	$number_of_institute = count($institute_ids);
	$number_of_branch = count($branch_ids);			
	
		  
	if( $number_of_branch==1 && $number_of_branch==1){

		
		 $where_cond=" BRANCH_ID=".$branch_ids[0]."";		
		$branch_list = $obj->View_All_By_Cond("INT_INSTITUTE_SETUP_VW",$where_cond);
		
		$ins_id = $branch_list[0]['INSTITUTE_ID'];		
		$_SESSION["LOGIN_INSTITUTE_ID"] = $branch_list[0]['INSTITUTE_ID'];
		$_SESSION["LOGIN_INSTITUTE_NAME"] = $branch_list[0]['INSTITUTE'];				
	
		$_SESSION["LOGIN_BRANCH"] = $branch_list[0]['BRANCH_ID'];
		$_SESSION["LOGIN_BRANCH_NAME"] = $branch_list[0]['BRANCH'];

		/*
		$ins_obj = $obj->View_Institute_details_By_ID($ins_id);			
		$ins_logo = $ins_obj->_logo_location;			
		if($ins_logo && file_exists($ins_logo)){
			 $_SESSION["LOGIN_INSTITUTE_LOGO"] = $ins_logo;
		}
		*/


		//header('Location: ./dashboard/dashboard.php');
		header('Location: ../dashboard/dashboard.php');
	}
?>

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8" />
		<title>Branch Selection </title>
		
		<?php
		include('../include/link.php');
		?>
		
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<!--webfonts
		<link href='http://fonts.googleapis.com/css?family=Open+Sans:600italic,400,300,600,700' rel='stylesheet' type='text/css'>
		//webfonts-->
		<link href="../files/css/style.css" rel='stylesheet' type='text/css' />
		<style>
		body::before {
			background: rgba(0, 0, 0, 0) none repeat scroll 0 0;
		}
		body {
			background-color: rgba(0, 0, 0, 0);
		}

		select {
			background: #d3d3d3 none repeat scroll 0 0;
			border: 1px solid #ded6d6;
			border-radius: 5px;
			color: #676767;
			font-family: "Open Sans",sans-serif;
			font-size: 16px;
			margin-bottom: 6%;
			outline: medium none;
			padding: 2%;
			position: relative;
			text-align: center;
			width: 99%;
			height:40px;
		}
		
		.login-form {
			margin: 5% auto 4%;
		}
		</style>
</head>
<body>
	<?php	
		include("include/header.php");	
    ?>	
	
			 
	<div class="main">
		<div class="login-form">
			<h1>Please Select Branch</h1>
					<div class="head">
						<img src="../files/images/user.png" alt=""/>
					</div>
				<form>
						
																					<?php
															$institute_ids=explode(",",$_SESSION["LOGIN_INSTITUTE"]);
																$j=0;
																$inids='(';
																foreach($institute_ids AS $m=>$n){
																	if($j==0){
																		$inids.=trim($n);
																	}
																	else{
																		$inids.=",".trim($n);
																	}
																	$j++;
																}
																$inids.=")";
																	
															?>
															<select id="institute_select_box" onchange="call_branch(this.value);" required="required" placeholder="Select Branch" class="text" >
                                                            	<?php
																		
																		
																		$table_name='_int_institute_setup';
																		$where_cond=" _type='I' AND _status='A' AND _id IN ".$inids." ";
																		$institute_list=$obj->View_All_By_Cond($table_name,$where_cond);
											                            $inst_selected = '';
																		$inst_multiple = true;
																		$inst_id = '';
																		if(count($institute_list)==1){
																			  $inst_selected = 'selected="selected"';
																			  $inst_multiple = false; 	 
																			 // if there is single institute available. it will be selected and 'Select Any Institute' will be hidden, and if theres is multiple institute then 'Select Any Institute' will be display 
																			  
																			} 
																		 
																		if($inst_multiple) 
																		 		echo '<option value="">Select Any Institute...</option>'; 
											                             foreach($institute_list AS $index2=>$value2){
											                                   $inst_id = $value2['_id']; 
																			 ?>
											                             <option value="<?php echo $value2['_id'];?>" <?php echo  $inst_selected;  ?> ><?php echo $value2['_name'];?></option>
											                              <?php } ?>
										                    </select>
						
						
															<select id="branch_select_box" required="required" placeholder="Select Branch" class="text" >
															      <option value="">- - - - - -</option>
                                                                  <?php 
																  	if(!$inst_multiple){
																		 
																		 $branch_ids=explode(",",$_SESSION['LOGIN_BRANCHES']);
																			$i=0;
																			$brids='(';
																			foreach($branch_ids AS $x=>$y){
																			if($i==0){
																			$brids.=trim($y);
																			}
																			else{
																			$brids.=",".trim($y);
																			}
																			$i++;
																			}
																			$brids.=")";
																		 
																		    $table_name='_int_institute_setup';
																			$where_cond=" _type='B' AND _status='A' AND _pid='$inst_id' AND _id IN ".$brids." ";
																			$branch_list=$obj->View_All_By_Cond($table_name,$where_cond);
																			
																			foreach($branch_list AS $index1=>$value1){
																			?>
																			
																			<option value="<?php echo $value1['_id'];?>"><?php echo $value1['_name'];?></option>
																			<?php }
																		
																		}
																  ?>
                                                                  
										                    </select>
						
						<div class="submit">
							<input type="submit" onclick="return false;" id="btn_branch_submit" value="Enter" >
					</div>	
					
				</form>
		</div>
		<!--//End-login-form-->
	</div>			 
		
<script type="text/javascript">

function call_branch(id){
	var datastr="institute_select_box="+id;
	$("#branch_select_box").load("get_branch.php",datastr);
}

						$(document).ready(function(){


							$("#branch_select_box").focus();
							$("#branch_select_box").val('<?php echo $selected_branch_id;?>')
							$("#branch_select_box").change(function () {
								if($(this).val() == "") $(this).addClass("branch_select_box_empty");
								else $(this).removeClass("branch_select_box_empty")
							});
							$("#branch_select_box").change();
							$("#btn_branch_submit").click(function(){


								var institute_id =$("#institute_select_box").val();
								var institute_name = $("#institute_select_box option:selected").text(); 
								var branch_id = $("#branch_select_box").val();
								var branch_name = $("#branch_select_box option:selected").text();
								var dataStr = 'branch_id='+branch_id+'&branch_name='+branch_name+'&institute_id='+institute_id+'&institute_name='+institute_name;
								
								if(branch_id=="") {
									alert("Please Select Branch");
									$("#branch_select_box").focus();
									return false;
								}
								else
								{
									
									$.ajax({
										type:"post" ,
										url:"set_branch_session.php" ,
										data:dataStr ,
										cache:false ,
										success:function(st){					      
											//alert(st);
											if(st==1) {
											location.href = "./dashboard/dashboard.php"
											}else{
												alert("Please check your username or password");
												$("#password").focus();
											}
										}
									});	
								}
								
							});		  
						});	



</script>
</body>
</html>
<?php 
}
else{ 
	?>
	<script type="text/javascript">location.href="./logout" </script>
	<?php
}
?>