<?php
include('../include/header.php');
if(!isset($_POST['save'])){
 $file = 'count.txt';

//get the number from the file
$uniq = file_get_contents($file);

//add +1
$id = $uniq + 1 ;

// add that new value to text file again for next use
file_put_contents($file, $id);

// your unique id ready
 $emp_num = 'EMP00'.$id;
 }
 
 
 
 if(isset($_POST['save'])){
 $file = 'count2.txt';

//get the number from the file
$uniq = file_get_contents($file);

//add +1
$id = $uniq + 1 ;

// add that new value to text file again for next use
file_put_contents($file, $id);

// your unique id ready
 $emp_num = 'EMP00'.$id;
 }
?>
<style>
.table > tbody > tr > td, .table > tbody > tr > th, .table > tfoot > tr > td, .table > tfoot > tr > th, .table > thead > tr > td, .table > thead > tr > th { border-top:0px;}
.tb > tbody > tr > td { border:0px;}

</style>
 <!-- Page wrapper  -->
        <div class="page-wrapper">
            <!-- Bread crumb -->
            <div class="row page-titles">
                <div class="col-md-5 align-self-center">
                   </div>
                <div class="col-md-7 align-self-center">
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item"><a href="javascript:void(0)">Home</a></li>
                        <li class="breadcrumb-item active">Employee Display</li>
                    </ol>
                </div>
            </div>
            <!-- End Bread crumb -->
            <!-- Container fluid  -->
            <div class="container-fluid">
                <!-- Start Page Content -->
				<form method="post" enctype="multipart/form-data">
                <div class="row">
                    <div class="col-12">
                        <div class="card">
                            <div class="card-body"> 
							 <div class="card card-outline-primary">
						 <div class="card-header" style="cursor:pointer;" id="land_details_click">
                                <h4 class="m-b-0 text-white">Employee Detail</h4>
                            						
	
		
	</div>
		<div class="box-body" id="land_details_contain" style="overflow-y:hidden; display:none;">
		  <h3 class="card-title m-t-15">Person Info</h3>
		<table class="table" style="width:100%">
			<thead>
				
			</thead>
			<tbody>
			  	<tr>
					<td>Name :</td><td><b><?php if(isset($_POST['emp_name'])){echo $_POST['emp_name'];} ?></b><input type="hidden" id="emp_name" name="emp_name" value="<?php if(isset($_POST['emp_name'])){echo $_POST['emp_name'];} ?>"  /></td>
					<td>DOB :</td><td><b><?php if(isset($_POST['dob'])){echo $_POST['dob'];} ?></b><input type="hidden" id="dob" name="dob" value="<?php if(isset($_POST['dob'])){echo $_POST['dob'];} ?>"/></td>
					<td>Gender :</td><td><b><?php if(isset($_POST['gender'])){echo $_POST['gender'];} ?></b><input type="hidden" name="gender" id="gender"  value="<?php if(isset($_POST['gender'])){echo $_POST['gender'];} ?>"/></td>
					<td>Phone :</td><td><b><?php if(isset($_POST['phone'])){echo $_POST['phone'];} ?></b><input type="hidden" name="phone" id="phone"  value="<?php if(isset($_POST['phone'])){echo $_POST['phone'];} ?>" /></td>							
				</tr>  
				
				<tr>
					<td>Aadhar No :</td><td><b><?php if(isset($_POST['aadhar'])){echo $_POST['aadhar'];} ?></b><input type="hidden" id="aadhar" name="aadhar"  value="<?php if(isset($_POST['aadhar'])){echo $_POST['aadhar'];} ?>"/></td>
					<td>Father Name :</td><td><b><?php if(isset($_POST['f_name'])){echo $_POST['f_name'];} ?></b><input type="hidden" id="f_name" name="f_name" value="<?php if(isset($_POST['f_name'])){echo $_POST['f_name'];} ?>"/></td>
					<td>Mother Name :</td><td><b><?php if(isset($_POST['m_name'])){echo $_POST['m_name'];} ?></b><input type="hidden" name="m_name" id="m_name"  value="<?php if(isset($_POST['m_name'])){echo $_POST['m_name'];} ?>"/></td>
					<td>Marital Status :</td><td><b><?php if(isset($_POST['m_status'])){echo $_POST['m_status'];} ?></b><input type="hidden" name="m_status" id="m_status"   value="<?php if(isset($_POST['m_status'])){echo $_POST['m_status'];} ?>"/></td>							
				</tr>   
				
				
				<tr>
					<td colspan="1">Permanent Address :</td><td colspan="2"><b><?php if(isset($_POST['p_address'])){echo $_POST['p_address'];} ?></b><input type="hidden" id="p_address" name="p_address"  value="<?php if(isset($_POST['p_address'])){echo $_POST['p_address'];} ?>"/></td>
					<td colspan="1">Correspondence Address :</td><td colspan="2"><b><?php if(isset($_POST['c_address'])){echo $_POST['c_address'];} ?></b><input type="hidden" id="c_address" name="c_address"value="<?php if(isset($_POST['c_address'])){echo $_POST['c_address'];} ?>" /></td>
					<td colspan="1">date of joining</td><td colspan="2"><b><?php if(isset($_POST['doj'])){echo $_POST['doj'];} ?></b><input type="hidden" name="doj" id="doj"  value="<?php if(isset($_POST['doj'])){echo $_POST['doj'];} ?>"/></td>
											
				</tr>      
			</tbody>
		</table>
		
		
		  <h3 class="card-title m-t-15">Qualification</h3>
		<table class="table" style="width:100%">
			<thead>
				
			</thead>
			<tbody>
			<?php
			
	 $counter = count($_POST["course"]);
  for($x=0; $x<=$counter; $x++){
if(!empty($_POST["course"][$x])){
     
	  $course = mysql_real_escape_string($_REQUEST['course'][$x]);
	   $univ = mysql_real_escape_string($_REQUEST['univ'][$x]);
	    $pcnt = mysql_real_escape_string($_REQUEST['pcnt'][$x]);
		  $year = mysql_real_escape_string($_REQUEST['year'][$x]);
			?>
			  	<tr>
					<td>Course</td><td><b><?php echo $course; ?></b><input type="hidden" id="course" name="course[]" value="<?php echo $course;  ?>" /></td>
					<td>University / Board</td><td><b><?php echo $univ; ?></b><input type="hidden" id="univ" name="univ[]"  value="<?php echo $univ; ?>" /></td>
					<td>Percent</td><td><b><?php echo $pcnt; ?></b><input type="hidden" name="pcnt[]" id="pcnt"   value="<?php echo $pcnt; ?>"/></td>
					<td>Year Of Passing</td><td><b><?php echo $year; ?></b><input type="hidden" name="year[]" id="year"    value="<?php echo $year; ?>"/></td>							
				</tr>  
				<?php }} ?>
				
			</tbody>
		</table>
		
		
		
		  <h3 class="card-title m-t-15">Work Experiance</h3>
		<table class="table" style="width:100%">
			<thead>
				
			</thead>
			<tbody>
			<?php
			
	 $counter2 = count($_POST["company"]);
  for($x=0; $x<=$counter2; $x++){
if(!empty($_POST["company"][$x])){
     
	  $company = mysql_real_escape_string($_REQUEST['company'][$x]);
	   $dept = mysql_real_escape_string($_REQUEST['dept'][$x]);
	    $designation = mysql_real_escape_string($_REQUEST['designation'][$x]);
		  $date_join = mysql_real_escape_string($_REQUEST['date_join'][$x]);
		  $date_leave = mysql_real_escape_string($_REQUEST['date_leave'][$x]);
			?>
			  	<tr>
					<td>Company Name</td><td><b><?php echo $company; ?></b><input type="hidden" id="company" name="company[]"  value="<?php echo $company;  ?>"/></td>
					<td>Department</td><td><b><?php echo $dept; ?></b><input type="hidden" id="dept" name="dept[]" value="<?php echo $dept;  ?>"/></td>
					<td>Designation</td><td><b><?php echo $designation; ?></b><input type="hidden" name="designation[]" id="designation"  value="<?php echo $designation;  ?>"/></td>
					<td>Date of joining</td><td><b><?php echo $date_join; ?></b><input type="hidden" name="date_join[]" id="date_join"  value="<?php echo $date_join;  ?>" /></td>	
					<td>Date of Leaving</td><td><b><?php echo $date_leave; ?></b><input type="hidden" name="date_leave[]" id="date_leave"  value="<?php echo $date_leave;  ?>" /></td>							
				</tr>  
				<?php }} ?>
				
			</tbody>
		</table>
		
		
		 <h3 class="card-title m-t-15">Skill</h3>
		<table class="table" style="width:100%">
			<thead>
				
			</thead>
			<tbody>
			<?php
			
	 $counter3 = count($_POST["skill"]);
  for($x=0; $x<=$counter3; $x++){
if(!empty($_POST["skill"][$x])){
     
	  $skill = mysql_real_escape_string($_REQUEST['skill'][$x]);
	   $s_level = mysql_real_escape_string($_REQUEST['s_level'][$x]);
	   
			?>
			  	<tr>
					<td>Name</td><td><b><?php echo $skill; ?></b><input type="hidden" id="skill" name="skill[]"  value="<?php echo $skill;  ?>"/></td>
					<td>Level</td><td><b><?php echo $s_level; ?></b><input type="hidden" id="s_level" name="s_level[]" value="<?php echo $s_level;  ?>"/></td>
											
				</tr>  
				<?php }} ?>
				
			</tbody>
		</table>
		
		<h3 class="card-title m-t-15">Document</h3>
		<table class="table" style="width:100%">
			<thead>
				
			</thead>
			<tbody>
			<?php 
			/*foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){*/
			 $counter4 = count($_POST["idty_id"]);
  for($x=0; $x<=$counter4; $x++){
  if(!empty($_POST["idty_id"][$x])){
		$file_name =  mysql_real_escape_string($_FILES['files']['name'][$x]);
		$random_digit=$emp_num;
		//$file_name= $random_digit.$file_name;
		  $ext = pathinfo($file_name , PATHINFO_EXTENSION);
		 $rand = mt_rand();
		  $file_name=$random_digit."_".$rand.".".$ext;
		//$file_size =$_FILES['files']['size'][$key];
		$file_tmp =$_FILES['files']['tmp_name'][$x];
		$file_type=$_FILES['files']['type'][$x];	
       // if($file_size > 2000097152){
//			$errors[]='File size must be less than 2 MB';
//        }	
		
		
		$doc_name =  mysql_real_escape_string($_REQUEST['doc_name'][$x]);
			$idty_id = mysql_real_escape_string($_REQUEST['idty_id'][$x]);
	
     
	
        $desired_dir="../photo";
        if(empty($errors)==true){
            if(is_dir($desired_dir)==false){
                mkdir("$desired_dir", 0700);		// Create directory if it does not exist
            }
            if(is_dir("$desired_dir/".$file_name)==false){
                move_uploaded_file($file_tmp,"$desired_dir/".$file_name);
            }else{									// rename the file if another one exist
                $new_dir="$desired_dir/".$file_name.time();
                 rename($file_tmp,$new_dir) ;				
            }
			}
			  $imageURL = "../photo/".$file_name;
			  ?>
			  	<tr>
					<td>Name</td><td><b><?php echo $doc_name;?></b><input type="hidden" id="idty_id" name="idty_id[]" value="<?php echo $idty_id;?>" />
					<td>image</td><td align="left"> <img src="<?php echo $imageURL;?>" style="width:50px; height:50px;" /><input type="hidden" id="files" name="files[]" value="<?php echo $file_name; ?>" /></td>
											
				</tr>  
				
				<?php }}?>
			</tbody>
		</table>
		</div>
					</div>
					
					<table class="table tb table-hover table-bordered table-condensed">
			
			<tbody>
			  	<tr>
					<td> <label class="col-form-label" for="val-username">User Type <span class="text-danger">*</span></label></td>
                                          <td>  
                                                <select  class="form-control" id="type" name="type" required>
												<option>Select</option>
												<?php
												 $sql2 = "select *from tbl_user_type where id!='1' ";
						$result2 = $objhr->viewdata($sql2);
							
							while($rows2 = $result2->fetch_assoc()){
												?>
												<option value="<?php echo $rows2['id']; ?>" ><?php echo strtoupper($rows2['user_type']); ?></option>
												<?php } ?>
												</select>
                                         </td>
											
											<td> <label class="col-form-label" for="val-username">Department <span class="text-danger">*</span></label></td>
                                          <td> 
                                                <select class="form-control" onchange="dpt()" id="depart" name="depart" required>
												<option>Select</option>
												<?php
												 $sqldpt = "select *from tbl_department ";
						$resultdpt = $objhr->viewdata($sqldpt);
							
							while($rowsdpt = $resultdpt->fetch_assoc()){
												?>
												<option value="<?php echo $rowsdpt['id']; ?>"><?php echo strtoupper($rowsdpt['department']); ?></option>
												<?php } ?>
												</select>
                                            </td>
			  	</tr>     
				
				
					<tr>
					<td> <label class=" col-form-label" for="val-username">Designation <span class="text-danger">*</span></label></td>
                                          <td>  
                                                <select  class="form-control" id="desig" name="desig" required>
												
												</select>
                                         </td>
											
											<td> <label class="col-form-label" for="val-username">Leader<span class="text-danger"></span></label></td>
                                          <td> 
                                                <select  class="form-control" id="leader" name="leader" >
												
												<option value="">None</option>
												<?php
												 $sql2 = "select *from tbl_emp_master ";
						$result2 = $objhr->viewdata($sql2);
							
							while($rows2 = $result2->fetch_assoc()){
												?>
												<option value="<?php echo $rows2['id']; ?>"><?php echo strtoupper($rows2['emp_name']); ?></option>
												<?php } ?>
												</select>
                                            </td>
			  	</tr>      
			</tbody>
		</table><br/>
		<input type="submit" name="edit" value="Edit" class="btn btn-primary" /> <input type="submit" name="save" value="Save" class="btn btn-primary" />
		
								
							 </div>
                        </div>
						
                    </div>
					
                </div>
				
				</form>
                <!-- End PAge Content -->
            </div>
            <!-- End Container fluid  -->
<?php
if(isset($_POST['save'])){
$dob= $_POST['dob'];
$dob = date('Y-m-d', strtotime($dob));

$doj= $_POST['doj'];
$doj = date('Y-m-d', strtotime($doj));

$sql_ad_emp = "INSERT INTO `tbl_emp_master` (`emp_code`, `emp_name`, `dob`, `aadhar_no`, `phone_no`, `gender`, `father_name`, `mother_name`, `marital_status`, `p_address`, `c_address`, `date_of_join`, `emp_terminate_date`, `termination_remark`, `del_status`, `user_id`, `entry_time`) VALUES ('$emp_num', '".$_POST['emp_name']."', '$dob', '".$_POST['aadhar']."', '".$_POST['phone']."', '".$_POST['gender']."', '".$_POST['f_name']."', '".$_POST['m_name']."', '".$_POST['m_status']."', '".$_POST['p_address']."', '".$_POST['c_address']."', '$doj', '', '', '0', '$user_id', CURRENT_TIMESTAMP);";
						$result_emp_id = $objhr->insertion_last_id_view($sql_ad_emp);
							if($result_emp_id!="")
	{
	$ipaddress = $_SERVER['REMOTE_ADDR'];
	$sql_emp_login = "INSERT INTO `tbl_user_mstr` (`user_type_id`, `emp_id`,`user_name`, `password`,`ip_address`, `user_id`, `active_status`) VALUES ('2', '$result_emp_id', '$emp_num', '1234', '$ipaddress', '$user_id', '1');";
						$result_emp_login = $objhr->insertion($sql_emp_login);
						
						$sql_emp_des = "INSERT INTO `tbl_emp_desg_dept` (`dept_id`, `desig_id`, `emp_id`, `leader_id`) VALUES ('".$_POST['depart']."', '".$_POST['desig']."', '$result_emp_id', '".$_POST['leader']."');";
						$result_emp_des = $objhr->insertion($sql_emp_des);
	
	
	$counter = count($_POST["course"]);
  for($x=0; $x<=$counter; $x++){
if(!empty($_POST["course"][$x])){
     
	  $course = mysql_real_escape_string($_REQUEST['course'][$x]);
	   $univ = mysql_real_escape_string($_REQUEST['univ'][$x]);
	    $pcnt = mysql_real_escape_string($_REQUEST['pcnt'][$x]);
		  $year = mysql_real_escape_string($_REQUEST['year'][$x]);
		  $sql_emp_qul = "INSERT INTO `tbl_emp_qualification` (`emp_id`, `course`, `univ_board`, `percentage`, `year_of_pass`) VALUES ('$result_emp_id', '$course', '$univ', '$pcnt', '$year');";
						$result_emp_qul = $objhr->viewdata($sql_emp_qul);
		  
		  }}
	
	 	
		$counter2 = count($_POST["company"]);
  for($x=0; $x<=$counter2; $x++){
if(!empty($_POST["company"][$x])){
     
	  $company = mysql_real_escape_string($_REQUEST['company'][$x]);
	   $dept = mysql_real_escape_string($_REQUEST['dept'][$x]);
	    $designation = mysql_real_escape_string($_REQUEST['designation'][$x]);
		  $date_join = mysql_real_escape_string($_REQUEST['date_join'][$x]);
		  $date_join = date('Y-m-d', strtotime($date_join));
		  $date_leave = mysql_real_escape_string($_REQUEST['date_leave'][$x]);
		  $date_leave = date('Y-m-d', strtotime($date_leave));
		  
		  
		  
		   $sql_emp_wrk = "INSERT INTO `tbl_emp_work_exp` (`emp_id`, `company`, `department`, `designation`, `date_join`, `date_leave`, `cntct_prson1_name`, `cntct_prson1_phone`, `cntct_prson2_name`, `cntct_prson2_phone`) VALUES ('$result_emp_id', '$company', '$dept', '$designation', '$date_join', '$date_leave', '', '', '', '');";
						$result_emp_wrk = $objhr->viewdata($sql_emp_wrk);
		  
		  }}
		
		
		$counter3 = count($_POST["skill"]);
  for($x=0; $x<=$counter3; $x++){
if(!empty($_POST["skill"][$x])){
     
	  $skill = mysql_real_escape_string($_REQUEST['skill'][$x]);
	   $s_level = mysql_real_escape_string($_REQUEST['s_level'][$x]);
	   
	    $sql_emp_skl = "INSERT INTO `tbl_emp_skill` (`emp_id`, `skill`, `skill_level`) VALUES ('$result_emp_id', '$skill', '$s_level');";
						$result_emp_skl = $objhr->viewdata($sql_emp_skl);
	   }}
		
		 $counter4 = count($_POST["idty_id"]);
  for($x=0; $x<=$counter4; $x++){
  if(!empty($_POST["idty_id"][$x])){
 
			$idty_id = mysql_real_escape_string($_REQUEST['idty_id'][$x]);
			 $file =  mysql_real_escape_string($_REQUEST['files'][$x]);
			
	    $sql_emp_doc = "INSERT INTO `tbl_emp_identity` (`emp_id`, `identity_id`, `image`) VALUES ('$result_emp_id', '$idty_id', '$file');";
						$result_emp_doc = $objhr->viewdata($sql_emp_doc);
  
  }}
		?>
		<script>
		alert('Success');
		location='login_detail.php?emp_id=<?php echo md5($result_emp_id); ?>';
		</script>
		
		<?php
		
		}

}
?>

<?php
include('../include/footer.php');
?>

<script>
function dpt(){

var dprt = $('#depart').val();


 
	
	$.ajax({
		type: "POST",
		url: "fetch_dept.php",
		data: "department="+dprt,
		 //dataType: 'json', 
		cache: false,
		success: function(data)
			{
			
			document.getElementById("desig").innerHTML=data;
            }
			});
				
			}
		


</script>
<script>

$(document).ready(function(){
    
      
	$("#land_details_click").click(function(){
        $("#land_details_contain").slideToggle("slow");
		$("#land_details_fa").toggleClass('fa-long-arrow-up');
    });
	
});
</script>