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

$system_id='2222';

$company_id_sell = $_POST["company_id_sell"];
$dept_idd = $_POST["dept_idd"];
$employ_idd = $_POST["employ_idd"];
$add_user_id = $_POST["add_user_id"];
$accept_stat = $_POST["accept_stat"];
if($accept_stat=="moveToAnotherLevell"){
  $level_accept = $_POST["level_accept"];
}else{
  $level_accept = "final_accept";
}
$refuse_statt = $_POST["refuse_statt"];
if($refuse_statt=="moveToAnotherLevel"){
  $level_refuse = $_POST["level_refuse"];
}else{
  $level_refuse = "final_refuse";
}

$promo_id = $_POST["promo_id"];

$level_acceptdetailsar = $_POST["level_acceptdetailsar"];
$level_acceptdetailsen = $_POST["level_acceptdetailsen"];
$level_refusedetailsar = $_POST["level_refusedetailsar"];
$level_refusedetailsen = $_POST["level_refusedetailsen"];

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"];

 $promo = "INSERT INTO promo_tb(id,company_id,dept_id,level_accept,level_refuse,level_acceptdetailsar,level_acceptdetailsen,level_refusedetailsar,level_refusedetailsen,type,createdDate,add_user_id)VALUES('$promo_id','$company_id_sell','$dept_idd','".$level_accept."','".$level_refuse."','$level_acceptdetailsar','$level_acceptdetailsen','$level_refusedetailsar','$level_refusedetailsen','salf','$created_date','$add_user_id')" ; 
 
 if ($conn->query($promo) === TRUE ) {
  for($i=0;$i<count($employ_idd);$i++){
    if($_POST["employ_idd"][$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_idd."'";
      $result_em = $conn->query($sql_em);
       if ($result_em->num_rows > 0) { 
        while($row_em = $result_em->fetch_assoc()) {
    
          $level_employ_promo = "INSERT INTO hr_promo_employ(promo_id,company_id,dept_id,employ_id,level_accept,level_refuse,level_acceptdetailsar,level_acceptdetailsen,level_refusedetailsar,level_refusedetailsen,type,createdDate,add_user_id)VALUES('$promo_id','$company_id_sell','$dept_idd','".$row_em["employ_id"]."','$level_accept','$level_refuse','$level_acceptdetailsar','$level_acceptdetailsen','$level_refusedetailsar','$level_refusedetailsen','salf','$created_date','$add_user_id')" ; 
          if ($conn->query($level_employ_promo) === TRUE ) {
      
          }else {
            echo "Error: " . $level_employ_promo . "<br>" . $conn->error;
          }
      }}
    }else{
      $level_employ_promo = "INSERT INTO hr_promo_employ(promo_id,company_id,dept_id,employ_id,level_accept,level_refuse,level_acceptdetailsar,level_acceptdetailsen,level_refusedetailsar,level_refusedetailsen,type,createdDate,add_user_id)VALUES('$promo_id','$company_id_sell','$dept_idd','".$_POST["employ_idd"][$i]."','$level_accept','$level_refuse','$level_acceptdetailsar','$level_acceptdetailsen','$level_refusedetailsar','$level_refusedetailsen','salf','$created_date','$add_user_id')" ; 
    if ($conn->query($level_employ_promo) === TRUE ) {

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

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

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


}

?>

