<?PHP


ob_start();


session_start();


include 'connect.php';


if(isset($_SESSION['adminemail']))


{


$user=$_SESSION['adminemail'];





$gtp=mysqli_query($conn,"select * from admin where username='$user'");


$gtp1=mysqli_fetch_assoc($gtp);





include 'top.php';


?>









<div id="page-wrapper" >
          <div id="page-inner">

			<div class="main-content">


				<div class="main-content-inner">


					<div class="breadcrumbs ace-save-state" id="breadcrumbs">


						<ul class="breadcrumb">


							<li>


								<i class="ace-icon fa fa-home home-icon"></i>


								<a href="#">Home</a>


							</li>


							<li class="active">Team</li>


						</ul><!-- /.breadcrumb -->


					</div>





					<div class="page-content">


			





						<div class="page-header">


                        


<?php





$aca=mysqli_query($conn,"select * from financial where user='$user' order by ID desc");


$aca1=mysqli_fetch_assoc($aca);





$financial=$aca1['financial'];





?>                        


                        


							<table style="width:100%;">


                        


                        <tr><td style="width:50%;">


                        


							<h1>


								Team


							</h1>


                            


                            </td>


                            


                            <td style="width:50%; text-align:right; padding-right:15px; font-size:15px;">


                            


                            <i class="fa fa-calendar"></i>&nbsp; <?php echo $financial; ?>


                            


                            </td></tr></table>


                            


						</div><!-- /.page-header -->





						<div class="row">


							<div class="col-xs-12">


                            


								<!-- PAGE CONTENT BEGINS -->





<script src="assets/js/jquery-2.1.4.min.js"></script>





<script type="text/javascript">





$(document).ready(function() {


    


	$('.teamleader').change(function(){


		


		var tl=$('.teamleader').val();


		


		if(tl!='')


		{


		$('.executive').show();


		}


		else


		{


		$('.executive').hide();


		}


		


	});


	


});





</script>





<script>


	


jQuery(function($){


		   


	// simple jQuery validation script


	$('#course').submit(function(){


		


		var valid = true;


		var errormsg = 'This field is required!';


		var errorcn = 'error';


		


		$('.' + errorcn, this).remove();			


		


		$('#required', this).each(function(){


			var parent = $(this).parent();


			if( $(this).val() == '' ){


				var msg = $(this).attr('title');


				msg = (msg != '') ? msg : errormsg;


				$('<span style="font-size:13px;color:#FF0000; padding-left:3px;" class="'+ errorcn +'">'+ msg +'</span>')


					.appendTo(parent)


					.fadeIn('fast')


					.click(function(){ $(this).remove(); });


				valid = false;


			};


		});


		


		


		var teamleader=$('.teamleader').val();


		


		if(teamleader!='')


		{


			


		var r=0


		$('.check').each(function(i){


		


		if($(this).prop('checked'))


		{


		r++;


		}


		


		});


		//alert(r);


		


		if(r==0)


		{


		alert("Select atleast one checkbox");


		valid = false;


		}


		


		}


		


		


		return valid;


		


		$('input:submit').prop('disabled', true);


		


	});


	


});	





</script>











<?php





	if(isset($_POST['submit']))


	{





		$teamleader=$_POST['teamleader'];


		


		$executive=$_POST['executive'];


		


		$check=$_POST['check'];








		foreach($check as $chk)


		{


			


			$exec=$executive[$chk];





			$tm=mysqli_query($conn,"insert into team_allocation(team_leader,executive) values('$teamleader','$exec')");





		}


		


		if($tm)


		{


			header("location:viewteam.php?s=2");


		}


		else


		{


			echo "<script type='text/javascript'>alert('Field Missing.');</script><META HTTP-EQUIV = 'Refresh' Content = '1; URL =team.php'>";


		}





	}


	else


	{





		echo'<center><form action="team.php" method="post" id="course"><table width="60%">





		<tr><td id="page">Team Leader <span class="red"></span> </td><td></td><td> 


		


		<select style="width: 30ch; margin-bottom:10px; height:34px;border-radius: 5px;border: 1px solid #c0c0c0;" margin-bottom:10px" name="teamleader" class="teamleader" style="width:220px;" id="required" title="*">


		


		<option value="">Select Team Leader</option>';


		


		$tl=mysqli_query($conn,"select employee.name as name,employee.ID as ID,employee.designation as designation from employee left join department on employee.department=department.ID where department.crm='1' order by employee.ID asc");


		while($tl1=mysqli_fetch_assoc($tl))


		{


			$chk=mysqli_query($conn,"select * from team_allocation where team_leader='$tl1[ID]'");


			$chk1=mysqli_num_rows($chk);


			


			$gg=mysqli_query($conn,"select * from employee_designation where ID='$tl1[designation]'");


			$gg1=mysqli_fetch_assoc($gg);


			


			if($chk1==0 && $gg1['designation']=='TEAM LEADER')


			{


			echo"<option value='".$tl1['ID']."'>".$tl1['name']."</option>";


			}


			


		}


		


		echo'</select>


		


		</td></tr>





		<tr><td style="padding-top:15px;" colspan="3"><div class="executive" style="display:none;">


		


		<table style="width:100%;"><tr><td style="padding-bottom:7px;"><b>Executives</b></td></tr>';


		


		$sno=0;


		$split=0;


		$emp=mysqli_query($conn,"select employee.name as name,employee.ID as ID,employee.designation as designation from employee left join department on employee.department=department.ID where department.crm='1' order by employee.ID asc");


		while($ret1=mysqli_fetch_assoc($emp))


		{


		$sno=$sno+1;


		$split++;  


		//echo $split; 


		if ($split%2==1){


		echo "</tr><tr>";   


		}


		


		$chek=mysqli_query($conn,"select * from team_allocation where executive='$ret1[ID]'");


		$chek1=mysqli_num_rows($chek);


		


		$gg=mysqli_query($conn,"select * from employee_designation where ID='$ret1[designation]'");


		$gg1=mysqli_fetch_assoc($gg);


		


		if($chek1==0 && $gg1['designation']!='TEAM LEADER')


		{


		echo'<td> <input type="checkbox" style="margin-top:-3px;" class="check" name="check['.$sno.']" value="'.$sno.'" /> &nbsp; 





		<input type="hidden" name="executive['.$sno.']" value="'.$ret1['ID'].'" /> '.$ret1['name'].' </td>';


		}


			


		}


		


		echo'</table></div></td></tr>





		<tr><td></td><td></td><td style="padding-top:7px;"><input class="btn btn btn-primary" type="submit" name="submit" value="Save" /></td></tr></table></form></center>';











	}











?>














										<!-- PAGE CONTENT ENDS -->


									</div><!-- /.col -->


								</div><!-- /.row -->


							</div><!-- /.page-content -->


						</div>


					</div><!-- /.main-content -->
					</div><!-- /.main-content -->
					</div><!-- /.main-content -->








<?php


include 'bottom.php';


}


else


{





 header("location:login.php");


}


ob_flush();





?>