<?php
ini_set('display_errors', 0);
//error_reporting(E_ALL);
//ini_set('display_errors', 1);

session_start();
ob_start();

require_once('configuration/server_connect.php');
require_once('configuration/all_function.php');

$active_id=$_REQUEST['active_id'];
$menu_id=$_REQUEST['menu_id'];

// Common Information

$page_title="Employee Information";
$page="71_employee_02.php?active_id=".$active_id."&menu_id=".$menu_id."";
$table='employee_info';	
$unique='emp_code';

$request_id=$_REQUEST['emp_code'];

$ms_data=mysqli_fetch_object(mysqli_query($conn,'select * from '.$table.' where emp_code="'.$request_id.'"'));

// End Common Information


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

{		

$entry_by = $_SESSION['user']['id'];
		 
$entry_at = $now=date('Y-m-d H:i:s');

$emp_type =1;

$cy_id=mysqli_fetch_object(mysqli_query($conn,'select max(emp_sl) as emp_sl from '.$table.' where emp_type="'.$emp_type.'"'));
$emp_sl_id=$cy_id->emp_sl+1;
$emp_sl_no = sprintf("%07d", $emp_sl_id);

$emp_code = $emp_type.''.$emp_sl_no;

$emp_name=$_POST['title_name'].' '.$_POST['first_name'].' '.$_POST['last_name'];


//$grp_sql="SELECT * FROM ledger_group  WHERE group_id='".$ledger_group_id."'";
//$grp_data = find_all_field_sql($conn,$grp_sql);


$ins_sql = 'INSERT INTO  employee_info (emp_code, emp_sl, emp_type, title_name, first_name, last_name, emp_name, department, designation, father_name, mother_name, blood_group, gender, marital_status, religion, nid_no, emp_dob, emp_doj, job_status, mobile_no, email, alternative_mobile, address, city, state, postal_code, country, gross_salary, basic_salary, house_rent, conveyance, medical, others, entry_by, entry_at, emergency_contact_name, emergency_contact_relation, emergency_mobile_no, emergency_email, employee_type, schedule_type, tds_amt) VALUES
("'.$emp_code.'", "'.$emp_sl_no.'", "'.$emp_type.'", "'.$_POST['title_name'].'", "'.$_POST['first_name'].'", "'.$_POST['last_name'].'", "'.$emp_name.'", "'.$_POST['department'].'", "'.$_POST['designation'].'", "'.$_POST['father_name'].'", "'.$_POST['mother_name'].'", "'.$_POST['blood_group'].'", "'.$_POST['gender'].'", "'.$_POST['marital_status'].'", "'.$_POST['religion'].'", "'.$_POST['nid_no'].'", "'.$_POST['emp_dob'].'", "'.$_POST['emp_doj'].'", "'.$_POST['job_status'].'", "'.$_POST['mobile_no'].'", "'.$_POST['email'].'", "'.$_POST['alternative_mobile'].'", "'.$_POST['address'].'", "'.$_POST['city'].'", "'.$_POST['state'].'", "'.$_POST['postal_code'].'", "'.$_POST['country'].'", "'.$_POST['gross_salary'].'", "'.$_POST['basic_salary'].'", "'.$_POST['house_rent'].'", "'.$_POST['conveyance'].'", "'.$_POST['medical'].'", "'.$_POST['others'].'", "'.$entry_by.'", "'.$entry_at.'", "'.$_POST['emergency_contact_name'].'", "'.$_POST['emergency_contact_relation'].'", "'.$_POST['emergency_mobile_no'].'", "'.$_POST['emergency_email'].'", "'.$_POST['employee_type'].'", "'.$_POST['schedule_type'].'", "'.$_POST['tds_amt'].'")';
	
mysqli_query($conn,$ins_sql);




$display_name=$emp_name;

$ins_acc_sql = 'INSERT INTO  accounts_reference (ref_no, ref_id, ref_type, title_name, first_name, last_name, full_name, display_name, entry_by, entry_at) VALUES
("'.$emp_code.'", "'.$emp_sl_no.'", "'.$emp_type.'", "'.$_POST['title_name'].'", "'.$_POST['first_name'].'", "'.$_POST['last_name'].'", "'.$emp_name.'",
 "'.$display_name.'",  "'.$entry_by.'", "'.$entry_at.'")';
	
mysqli_query($conn,$ins_acc_sql);



?>

<script language="javascript">
window.location.href = "<?php echo $page;?>&emp_code=<?=$emp_code?>";
</script>

<?php

}


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

{		

$entry_by = $_SESSION['user']['id'];
		 
$entry_at = $now=date('Y-m-d H:i:s');

$emp_code=$request_id;

$emp_name=$_POST['title_name'].' '.$_POST['first_name'].' '.$_POST['last_name'];

 $up_sql = 'update employee_info set title_name="'.$_POST['title_name'].'", first_name="'.$_POST['first_name'].'", last_name="'.$_POST['last_name'].'", emp_name="'.$emp_name.'", 
department="'.$_POST['department'].'", designation="'.$_POST['designation'].'", father_name="'.$_POST['father_name'].'", mother_name="'.$_POST['mother_name'].'", blood_group="'.$_POST['blood_group'].'", gender="'.$_POST['gender'].'", marital_status="'.$_POST['marital_status'].'", religion="'.$_POST['religion'].'", nid_no="'.$_POST['nid_no'].'", emp_dob="'.$_POST['emp_dob'].'", emp_doj="'.$_POST['emp_doj'].'",  job_status="'.$_POST['job_status'].'", mobile_no="'.$_POST['mobile_no'].'" , email="'.$_POST['email'].'" , alternative_mobile="'.$_POST['alternative_mobile'].'" , address="'.$_POST['address'].'" , city="'.$_POST['city'].'" , state="'.$_POST['state'].'" , postal_code="'.$_POST['postal_code'].'" , country="'.$_POST['country'].'" , gross_salary="'.$_POST['gross_salary'].'" , basic_salary="'.$_POST['basic_salary'].'" , house_rent="'.$_POST['house_rent'].'" , conveyance="'.$_POST['conveyance'].'" , medical="'.$_POST['medical'].'" , others="'.$_POST['others'].'" , edit_by="'.$entry_by.'" , edit_at="'.$entry_at.'" , emergency_contact_name="'.$_POST['emergency_contact_name'].'" , emergency_contact_relation="'.$_POST['emergency_contact_relation'].'" , emergency_mobile_no="'.$_POST['emergency_mobile_no'].'" , emergency_email="'.$_POST['emergency_email'].'" , employee_type="'.$_POST['employee_type'].'" , schedule_type="'.$_POST['schedule_type'].'" , tds_amt="'.$_POST['tds_amt'].'" 
 where emp_code='.$emp_code;
mysqli_query($conn,$up_sql);



$display_name=$emp_name;

 $up_sql_user = 'update accounts_reference set title_name="'.$_POST['title_name'].'", first_name="'.$_POST['first_name'].'", last_name="'.$_POST['last_name'].'", full_name="'.$emp_name.'", display_name="'.$display_name.'", edit_by="'.$entry_by.'" , edit_at="'.$entry_at.'"
 where ref_no='.$emp_code;
mysqli_query($conn,$up_sql_user);





?>

<script language="javascript">

window.location.href = "<?php echo $page;?>&emp_code=<?=$emp_code?>";
</script>

<?php

}

?>



<?php
if (isset($_POST['submit'])) {

$emp_code=$request_id;

  $name = $_FILES['image']['name'];
  $type = $_FILES['image']['type'];
  $size = $_FILES['image']['size'];
  $tmp_name = $_FILES['image']['tmp_name'];
  
  // Check if file type is allowed
  $allowed_extensions = array('jpg', 'jpeg', 'png', 'gif');
  $allowed_types = array('image/jpeg', 'image/png', 'image/gif');
  $extension = pathinfo($name, PATHINFO_EXTENSION);
  if (!in_array($extension, $allowed_extensions) || !in_array($type, $allowed_types)) {
    die("Error: File type not allowed.");
  }
  
  // Generate unique filename
  $timestamp = $emp_code;
  $random_string = bin2hex(random_bytes(5)); // generate 10-character random string
  //$new_name = $timestamp . '.' . $extension;
  $new_name = $timestamp . '.jpg';
  
  // Move uploaded file to destination folder
  $destination = 'uploads/' . $new_name;
  if (!move_uploaded_file($tmp_name, $destination)) {
    die("Error: Failed to move file.");
  }
  
  echo "File uploaded successfully.";
}
?>



<!DOCTYPE html>
<html lang="en">

 <?php require_once('inc_files/inc_head.php'); ?>
 
 <?php
  do_calander('#emp_dob');
  do_calander('#emp_doj');
  ?>

<script language="javascript">

function salary_cal(){

var basic_salary=(document.getElementById('basic_salary').value)*1; 
var house_rent=(document.getElementById('house_rent').value)*1;
var conveyance=(document.getElementById('conveyance').value)*1;
var medical=(document.getElementById('medical').value)*1;
var others=(document.getElementById('others').value)*1;

document.getElementById('gross_salary').value=(basic_salary+house_rent+conveyance+medical+others);

}

</script>
<body>

  <!-- ======= Header ======= -->
  <?php 
  require_once('inc_files/inc_header.php');
  ?><!-- End Header -->

  <!-- ======= Sidebar ======= -->
  <?php  require_once('inc_files/inc_menu.php'); ?><!-- End Sidebar-->

  <main id="main" class="main">

    <div class="pagetitle">
      <h1><?php echo $page_title?></h1>
      <nav>
        <ol class="breadcrumb">
          <li class="breadcrumb-item"><a href="home.php">Home</a></li>
          <li class="breadcrumb-item">Employee</li>
          <li class="breadcrumb-item active"><?php echo $page_title?></li>
        </ol>
      </nav>
    </div><!-- End Page Title -->

    <section class="section profile">
      <div class="row">
	  
	  <?php if($ms_data->emp_code>0) {?>
        <div class="col-xl-4">

          <div class="card"> 
		  
		   <form method="post" action="" enctype="multipart/form-data">
            <div class="card-body profile-card pt-4 d-flex flex-column align-items-center">
              <img src="uploads/<?php echo $ms_data->emp_code?>.jpg" alt="Profile" class="rounded-circle">
			
  			<input type="file" name="image">
  			<input type="submit" name="submit" value="Upload">
              <h2><?php echo $ms_data->emp_name?></h2>
              <h3>EMP ID: <?php echo $ms_data->emp_code?></h3>
			  <h3><?php echo find_a_field($conn,'designation','designation','id="'.$ms_data->designation.'"');?></h3>
              <div class="social-links mt-2">
                <a href="#" class="twitter"><i class="bi bi-twitter"></i></a>
                <a href="#" class="facebook"><i class="bi bi-facebook"></i></a>
                <a href="#" class="instagram"><i class="bi bi-instagram"></i></a>
                <a href="#" class="linkedin"><i class="bi bi-linkedin"></i></a>
              </div>
            </div>
			</form>
			
          </div>
        </div>
		<?php }?>

        <div class="col-xl-8">

          <div class="card">
            <div class="card-body">
              <h5 class="card-title"><strong><?php echo $page_title;?></strong></h5>
			  
			<hr>

              <!-- Multi Columns Form -->
              <form class="row g-3" action="" method="post" name="form1" id="form1" onSubmit="return check()" style="font-size:14px;">
			  
			  <div class="col-md-2">
                 <label for="title_name" class="form-label"><strong>Title</strong></label>
                  <input type="text" class="form-control" id="title_name" name="title_name" value="<?php if($request_id>0){ echo $ms_data->title_name; } ?>" >
                </div>
                <div class="col-md-5">
                 <label for="first_name" class="form-label"><strong>First Name</strong></label>
                  <input type="text" class="form-control" id="first_name" name="first_name" value="<?php if($request_id>0){ echo $ms_data->first_name; } ?>" >
                </div>
				
				<div class="col-md-5">
                 <label for="last_name" class="form-label"><strong>Last Name</strong></label>
                  <input type="text" class="form-control" id="last_name" name="last_name" value="<?php if($request_id>0){ echo $ms_data->last_name; } ?>" >
                </div>
			  
			 	<div class="col-md-4">
                 <label for="mobile_no" class="form-label"><strong>Mobile</strong></label>
                  <input type="text" class="form-control" id="mobile_no" name="mobile_no" value="<?php if($request_id>0){ echo $ms_data->mobile_no; } ?>" >
                </div>
                <div class="col-md-4">
                 <label for="alternative_mobile" class="form-label"><strong>Alternative Mobile</strong></label>
                  <input type="text" class="form-control" id="alternative_mobile" name="alternative_mobile" value="<?php if($request_id>0){ echo $ms_data->alternative_mobile; } ?>" >
                </div>
				
				<div class="col-md-4">
                 <label for="email" class="form-label"><strong>Email</strong></label>
                  <input type="text" class="form-control" id="email" name="email" value="<?php if($request_id>0){ echo $ms_data->email; } ?>" >
                </div>
				
				<div class="col-md-6">
                 <label for="father_name" class="form-label"><strong>Father's Name</strong></label>
                  <input type="text" class="form-control" id="father_name" name="father_name" value="<?php if($request_id>0){ echo $ms_data->father_name; } ?>" >
                </div>
                <div class="col-md-6">
                 <label for="mother_name" class="form-label"><strong>Mother's Name</strong></label>
                  <input type="text" class="form-control" id="mother_name" name="mother_name" value="<?php if($request_id>0){ echo $ms_data->mother_name; } ?>" >
                </div>
				
				<div class="col-md-6">
                 <label for="gender" class="form-label"><strong>Gender</strong></label>
                  
			<select id="gender" name="gender"  class="form-select"  required  >
			<option></option>
			<?php foreign_relation($conn,'gender','id','gender',$ms_data->gender,'1 order by id');?>
		
        	</select>
				  
                </div>
                <div class="col-md-6">
                 <label for="marital_status" class="form-label"><strong>Marital Status</strong></label>
             <select id="marital_status" name="marital_status"  class="form-select"  required  >
			<option></option>
			<?php foreign_relation($conn,'marital_status','id','marital_status',$ms_data->marital_status,'1 order by id');?>
		
        	</select>
                </div>
				
				<div class="col-md-6">
                 <label for="blood_group" class="form-label"><strong>Blood Group</strong></label>
                  
			<select id="blood_group" name="blood_group"  class="form-select"  required  >
			<option></option>
			<?php foreign_relation($conn,'blood_group','id','blood_group',$ms_data->blood_group,'1 order by id');?>
		
        	</select>
				  
                </div>
                <div class="col-md-6">
                 <label for="religion" class="form-label"><strong>Religion</strong></label>
            <select id="religion" name="religion"  class="form-select"  required  >
			<option></option>
			<?php foreign_relation($conn,'religion','id','religion',$ms_data->religion,'1 order by id');?>
		
        	</select>
                </div>
				
				<div class="col-md-6">
                 <label for="emp_dob" class="form-label"><strong>Date of Birth</strong></label>
                  <input type="text" class="form-control" id="emp_dob" name="emp_dob" value="<?php if($request_id>0){ echo $ms_data->emp_dob; } ?>" >
                </div>
                <div class="col-md-6">
                 <label for="nid_no" class="form-label"><strong>NID No</strong></label>
                  <input type="text" class="form-control" id="nid_no" name="nid_no" value="<?php if($request_id>0){ echo $ms_data->nid_no; } ?>" >
                </div>
				
				<br>
			   
				<div class="col-md-12">
                 
                </div>
				
				<div class="row mb-3">
                  <label for="address" class="col-sm-3 col-form-label"><strong>Address</strong></label>
                  <div class="col-sm-9">
                 <textarea class="form-control" style="height: 80px" id="address" name="address" ><?php if($request_id>0){ echo $ms_data->address; } ?></textarea>	
                  </div>
                </div>
				
			
				
				<div class="col-md-6">
                 <label for="city" class="form-label"><strong>City/Town</strong></label>
                  <input type="text" class="form-control" id="city" name="city" value="<?php if($request_id>0){ echo $ms_data->city; } ?>" >
                </div>
                <div class="col-md-6">
                 <label for="state" class="form-label"><strong>State/Province</strong></label>
                  <input type="text" class="form-control" id="state" name="state" value="<?php if($request_id>0){ echo $ms_data->state; } ?>" >
                </div>
				
				
				
				<div class="col-md-6">
                 <label for="postal_code" class="form-label"><strong>Postal Code</strong></label>
                  <input type="text" class="form-control" id="postal_code" name="postal_code" value="<?php if($request_id>0){ echo $ms_data->postal_code; } ?>">
                </div>
                <div class="col-md-6">
                 <label for="country" class="form-label"><strong>Country</strong></label>
                  <input type="text" class="form-control" id="country" name="country" value="<?php if($request_id>0){ echo $ms_data->country; } ?>">
                </div>
			  
                <div class="col-md-12">
                  <h5 class="card-title" style="margin:0; padding:0;"><strong>&nbsp;:: Job Status ::</strong></h5>
                </div>
	<hr>
				
				<div class="col-md-6">
                 <label for="department" class="form-label"><strong>Department</strong></label>
                  
			<select id="department" name="department"  class="form-select"  required  >
			<option></option>
			<?php foreign_relation($conn,'department','id','department',$ms_data->department,'1 order by id');?>
		
        	</select>
				  
                </div>
                <div class="col-md-6">
                 <label for="designation" class="form-label"><strong>Designation</strong></label>
             <select id="designation" name="designation"  class="form-select"  required  >
			<option></option>
			<?php foreign_relation($conn,'designation','id','designation',$ms_data->designation,'1 order by id');?>
		
        	</select>
                </div>
				
				
				<div class="col-md-6">
                 <label for="employee_type" class="form-label"><strong>Employee Type</strong></label>
             <select id="employee_type" name="employee_type"  class="form-select"  required  >
			<option></option>
			<?php foreign_relation($conn,'employee_type','id','employee_type',$ms_data->employee_type,'1 order by id');?>
		
        	</select>
                </div>
				
				<div class="col-md-6">
                 <label for="schedule_type" class="form-label"><strong>Schedule Type</strong></label>
             <select id="schedule_type" name="schedule_type"  class="form-select"  required  >
			<option></option>
			<?php foreign_relation($conn,'attendance_schedule','id','schedule_type',$ms_data->schedule_type,'1 order by id');?>
		
        	</select>
                </div>
				
				<div class="col-md-6">
                 <label for="emp_doj" class="form-label"><strong>Joining Date</strong></label>
                  <input type="text" class="form-control" id="emp_doj" name="emp_doj" value="<?php if($request_id>0){ echo $ms_data->emp_doj; } ?>" >
                </div>
				
                <div class="col-md-6">
                 <label for="job_status" class="form-label"><strong>Job Status</strong></label>
             <select id="job_status" name="job_status"  class="form-select"  required  >
			<option></option>
			<?php foreign_relation($conn,'job_status','id','job_status',$ms_data->job_status,'1 order by id');?>
		
        	</select>
                </div>
				
				
				
				
				<div class="col-md-12">
                  <h5 class="card-title" style="margin:0; padding:0;"><strong>&nbsp;:: Emergency Contact ::</strong></h5>
                </div>
			<hr>
				
				<div class="col-md-6">
                 <label for="department" class="form-label"><strong>Emergency Contact</strong></label>
           <input type="text" class="form-control" id="emergency_contact_name" name="emergency_contact_name" value="<?php if($request_id>0){ echo $ms_data->emergency_contact_name; } ?>" >
				  
                </div>
                <div class="col-md-6">
                 <label for="designation" class="form-label"><strong>Relation</strong></label>
            <input type="text" class="form-control" id="emergency_contact_relation" name="emergency_contact_relation" value="<?php if($request_id>0){ echo $ms_data->emergency_contact_relation; } ?>" >
                </div>
				
				<div class="col-md-6">
                 <label for="emp_doj" class="form-label"><strong>Mobile No</strong></label>
 <input type="text" class="form-control" id="emergency_mobile_no" name="emergency_mobile_no" value="<?php if($request_id>0){ echo $ms_data->emergency_mobile_no;}?>" >
                </div>
				
                <div class="col-md-6">
                 <label for="job_status" class="form-label"><strong>Email</strong></label>
    <input type="text" class="form-control" id="emergency_email" name="emergency_email" value="<?php if($request_id>0){ echo $ms_data->emergency_email; } ?>" >
                </div>
				
				
      
				<div class="col-md-12">
                  <h5 class="card-title" style="margin:0; padding:0;"><strong>&nbsp;:: Salary Information ::</strong></h5>
                </div>
				<hr>
				
				<div class="col-md-4">
                 <label for="basic_salary" class="form-label"><strong>Basic Salary</strong></label>
                  <input type="text" class="form-control" id="basic_salary" name="basic_salary" value="<?php if($request_id>0){ echo $ms_data->basic_salary; } ?>" onKeyUp="salary_cal()" >
                </div>
                <div class="col-md-4">
                 <label for="house_rent" class="form-label"><strong>House Rent</strong></label>
                  <input type="text" class="form-control" id="house_rent" name="house_rent" value="<?php if($request_id>0){ echo $ms_data->house_rent; } ?>" onKeyUp="salary_cal()">
                </div>
				
				<div class="col-md-4">
                 <label for="conveyance" class="form-label"><strong>Conveyance</strong></label>
                  <input type="text" class="form-control" id="conveyance" name="conveyance" value="<?php if($request_id>0){ echo $ms_data->conveyance; } ?>" onKeyUp="salary_cal()">
                </div>
				
				<div class="col-md-4">
                 <label for="medical" class="form-label"><strong>Medical</strong></label>
                  <input type="text" class="form-control" id="medical" name="medical" value="<?php if($request_id>0){ echo $ms_data->medical; } ?>" onKeyUp="salary_cal()">
                </div>
                <div class="col-md-4">
                 <label for="others" class="form-label"><strong>Others</strong></label>
                  <input type="text" class="form-control" id="others" name="others" value="<?php if($request_id>0){ echo $ms_data->others; } ?>" onKeyUp="salary_cal()">
                </div>
				
				<div class="col-md-4">
                 <label for="gross_salary" class="form-label"><strong>Gross Salary</strong></label>
                  <input type="text" class="form-control" id="gross_salary" name="gross_salary" value="<?php if($request_id>0){ echo $ms_data->gross_salary; } ?>"  readonly="">
                </div>
				
				<div class="col-md-4">
                 <label for="tds_amt" class="form-label"><strong>TDS</strong></label>
                  <input type="text" class="form-control" id="tds_amt" name="tds_amt" value="<?php if($request_id>0){ echo $ms_data->tds_amt; } ?>"  >
                </div>
				
				
				
				<br>
				<br>
				
			
            
                
                
                <div class="text-center">
				<?php if($request_id>0) {?>
                  <button type="submit" class="btn btn-success btn-sm" name="update" id="update" ><i class="bi bi-check-all"></i> Update &nbsp;</button>
				 <?php }else {?>
				  <button type="submit" class="btn btn-primary btn-sm" name="insert" id="insert" ><i class="bi bi-plus"></i> Save &amp; New &nbsp;</button>
				 <?php }?>
                  <button type="reset" class="btn btn-secondary btn-sm" onClick="parent.location='<?php echo $page?>'" >Reset</button>
                </div>
              </form><!-- End Multi Columns Form -->

            </div>
          </div>

        </div>
      </div>
    </section>

  </main><!-- End #main -->

  <!-- ======= Footer ======= -->
  <?php require_once('inc_files/inc_footer.php'); ?>
 <!-- ======= Footer ======= -->
</body>

</html>