<?php
session_start();
include("dbConfig.php");
if(ISSET($_POST['sav_btn'])){
    
$company_id = $_POST["company_id"];

$system_id='2222';

$company_id_sel = $_POST["company_id_sel"];
$dept_id = $_POST["dept_id"];
$employ_id = $_POST["employ_id"];
$add_user_id = $_POST["add_user_id"];
$salf_id = $_POST["salf_id"];
$level_stat_details_ar = $_POST["level_stat_details_ar"];
$level_stat_details_en = $_POST["level_stat_details_en"];



date_default_timezone_set("Asia/Riyadh");
  $created_date = date("Y-m-d");
  $createdDate_time = date("Y-m-d h:i:sa");
  
 $link_lang = $_POST["link_lang"];
 $messag_add = $_POST["messag_add"];

 $level = $_POST["level_num"];
 $level_refuse = $_POST["level_refuse"];

 $refuse_stat = $_POST["refuse_stat"];

 if($refuse_stat=="final_reject"){
 $level_refuse_stat_details_ar = "رفض نهائي";
  $level_refuse_stat_details_en = "Final Reject";
 }elseif($refuse_stat=="returnto_employee"){

  $level_refuse_stat_details_ar = "تم الرفض ويمكنك الترقيه";
  $level_refuse_stat_details_en = "Rejected and you can upgrade";
 }else{
  $level_refuse_stat_details_ar = $_POST["level_refuse_stat_details_ar"];
  $level_refuse_stat_details_en = $_POST["level_refuse_stat_details_en"];
 }


 ////
 $dept_manager_approval = $_POST["dept_manager_approval"];
 if(isset($dept_manager_approval)){// في حال تم اختيار مدير القسم 
  if($level=="at_the_begning"){

    // insert row at the begning of table
    $sql = "UPDATE hr_advance_level_tbl SET level = level + 1 WHERE level >=1 and company_id='$company_id_sel'";
    $conn->query($sql);
    $sqll = "UPDATE hr_level_employ_approval SET level = level + 1 WHERE level >=1 and company_id='$company_id_sel'";
    $conn->query($sqll);
    // Insert a new row with the incremented position
    
    $level1 = 1;
    if($refuse_stat=="moveToAnotherLevel"){
      if($level_refuse=="next_level"){
        $refuse_level = $level1 + 1;
      }else{
        $refuse_level = $level_refuse;
      }
     
     }else{
      $refuse_level = $refuse_stat;
     }
     //
    $salf_level = "INSERT INTO hr_advance_level_tbl(id,company_id,dept_id,employ_id,level,level_refuse,level_stat_details_ar,level_stat_details_en,level_refuse_stat_details_ar,level_refuse_stat_details_en,type,createdDate,add_user_id)VALUES('$salf_id','$company_id_sel','','','$level1','$refuse_level','$level_stat_details_ar','$level_stat_details_en','$level_refuse_stat_details_ar','$level_refuse_stat_details_en','salf','$created_date','$add_user_id')" ;
    }else{
    // Increment the position of all rows after the specific row
    $sql = "UPDATE hr_advance_level_tbl SET level = level + 1 WHERE level > $level and company_id='$company_id_sel'";
    $conn->query($sql);
    
    // Insert a new row with the incremented position
    
    $level1 = $level + 1;
    if($refuse_stat=="moveToAnotherLevel"){
      if($level_refuse=="next_level"){
        $refuse_level = $level1 + 1;
      }else{
        $refuse_level = $level_refuse;
      }
     
     }else{
      $refuse_level = $refuse_stat;
     }
     //
    $salf_level = "INSERT INTO hr_advance_level_tbl(id,company_id,dept_id,employ_id,level,level_refuse,level_stat_details_ar,level_stat_details_en,level_refuse_stat_details_ar,level_refuse_stat_details_en,type,createdDate,add_user_id)VALUES('$salf_id','$company_id_sel','','','$level1','$refuse_level','$level_stat_details_ar','$level_stat_details_en','$level_refuse_stat_details_ar','$level_refuse_stat_details_en','salf','$created_date','$add_user_id')" ;
    $sqll = "UPDATE hr_level_employ_approval SET level = level + 1 WHERE level >$level and company_id='$company_id_sel'";
    $conn->query($sqll);
    }
 }else{


if($level=="at_the_begning"){

// insert row at the begning of table
$sql = "UPDATE hr_advance_level_tbl SET level = level + 1 WHERE level >=1 and company_id='$company_id_sel'";
$conn->query($sql);
$sqll = "UPDATE hr_level_employ_approval SET level = level + 1 WHERE level >=1 and company_id='$company_id_sel'";
$conn->query($sqll);
// Insert a new row with the incremented position

$level1 = 1;
if($refuse_stat=="moveToAnotherLevel"){
  if($level_refuse=="next_level"){
    $refuse_level = $level1 + 1;
  }else{
    $refuse_level = $level_refuse;
  }
 
 }else{
  $refuse_level = $refuse_stat;
 }
 //
$salf_level = "INSERT INTO hr_advance_level_tbl(id,company_id,dept_id,employ_id,level,level_refuse,level_stat_details_ar,level_stat_details_en,level_refuse_stat_details_ar,level_refuse_stat_details_en,type,createdDate,add_user_id)VALUES('$salf_id','$company_id_sel','$dept_id','','$level1','$refuse_level','$level_stat_details_ar','$level_stat_details_en','$level_refuse_stat_details_ar','$level_refuse_stat_details_en','salf','$created_date','$add_user_id')" ;
}else{
// Increment the position of all rows after the specific row
$sql = "UPDATE hr_advance_level_tbl SET level = level + 1 WHERE level > $level and company_id='$company_id_sel'";
$conn->query($sql);

// Insert a new row with the incremented position

$level1 = $level + 1;
if($refuse_stat=="moveToAnotherLevel"){
  if($level_refuse=="next_level"){
    $refuse_level = $level1 + 1;
  }else{
    $refuse_level = $level_refuse;
  }
 
 }else{
  $refuse_level = $refuse_stat;
 }
 //
$salf_level = "INSERT INTO hr_advance_level_tbl(id,company_id,dept_id,employ_id,level,level_refuse,level_stat_details_ar,level_stat_details_en,level_refuse_stat_details_ar,level_refuse_stat_details_en,type,createdDate,add_user_id)VALUES('$salf_id','$company_id_sel','$dept_id','','$level1','$refuse_level','$level_stat_details_ar','$level_stat_details_en','$level_refuse_stat_details_ar','$level_refuse_stat_details_en','salf','$created_date','$add_user_id')" ;
$sqll = "UPDATE hr_level_employ_approval SET level = level + 1 WHERE level >$level and company_id='$company_id_sel'";
$conn->query($sqll);
}

 }
////
//==================العملايات==================================

$browserAgent = $_SERVER['HTTP_USER_AGENT'];
 $user_id =$_SESSION['user_id'];
 $system_type='hr_marsa_system';
  $proccess_type='add_salf_level';
  $proccess_details_en='add salf level'.$add_user_id;
  $proccess_details='إضافة مستوى للسلف من قبل الموظف'.$add_user_id;
 $table_name='hr_advance_level_tbl';   
  date_default_timezone_set("Asia/Riyadh");
  $createdDate = date("Y-m-d h:i:sa");
   $sql_req = "INSERT INTO process_systems_tbl(system_type,user_id,user_ip,proccess_type,proccess_details,proccess_details_en,table_name,proccess_id,proccess_date,process_time,process_datetime,browser)values('$system_type','$add_user_id','$user_ip','$proccess_type','$proccess_details','$proccess_details_en','$table_name','$user_id',NOW(),NOW(),'$createdDate','$browserAgent')";
 //==================العملايات==================================
  
if ($conn->query($salf_level) === TRUE  ) {
   //
   if(isset($dept_manager_approval)){

   }else{
  for($i=0;$i<count($employ_id);$i++){
    if($_POST["employ_id"][$i]=="all_employees"){
      
      $sql_em = "SELECT * FROM hr_employ_tbl where employ_stat='active_employ' AND company_id!='0'AND deleted='0'AND dept_id='".$dept_id."'";
      $result_em = $conn->query($sql_em);
       if ($result_em->num_rows > 0) { 
        while($row_em = $result_em->fetch_assoc()) {
    
          $level_employ_approval = "INSERT INTO hr_level_employ_approval(salf_id,company_id,dept_id,level,employ_id,type,createdDate,add_user_id)VALUES('$salf_id','$company_id_sel','$dept_id','$level1','".$row_em["employ_id"]."','salf','$created_date','$add_user_id')" ; 
          if ($conn->query($level_employ_approval) === TRUE ) {
      
          }else {
            echo "Error: " . $level_employ_approval . "<br>" . $conn->error;
          }
      }}
    }else{
    $level_employ_approval = "INSERT INTO hr_level_employ_approval(salf_id,company_id,dept_id,level,employ_id,type,createdDate,add_user_id)VALUES('$salf_id','$company_id_sel','$dept_id','$level1','".$_POST["employ_id"][$i]."','salf','$created_date','$add_user_id')" ; 
    if ($conn->query($level_employ_approval) === TRUE ) {

    }else {
      echo "Error: " . $level_employ_approval . "<br>" . $conn->error;
    }
  }
}
   }
//
  $_SESSION['message'] =$messag_add;

   header("Refresh:0; url=hr_advance_Level.php?$link_lang&company_id=$company_id");


} else {
  echo "Error: " . $sql_req . "<br>" . $conn->error;
}

}

?>

