<?php
session_start();
include("dbConfig.php");

  $arr=array();
                $sql_comv="SELECT * FROM waseet_company_user_tbl where  system_id='2222' AND deleted='0'AND stop='0'AND user_id='".$_SESSION['user_id']."' GROUP BY company_id ";
                $result_comv = $conn->query($sql_comv);
                if ($result_comv->num_rows > 0) {
               while($row_comv = $result_comv->fetch_assoc()) {
                $company_id_all=$row_comv['company_id']; //echo $company_id_all.'<br>';
                array_push($arr,$company_id_all);
                  // echo $_SESSION['user_id'].'<br>';
                  }} 
                  // print_r($arr);
                  ?>
				<?php //echo  $company_id;
				//echo $urow['user_type'];
			    $ids_arr = join("','",$arr);

if(ISSET($_POST['sav_btn'])){
$holiday_id=$_POST["holiday_id"];
$company_id = $_POST["company_id"];
$system_id=$_POST["system_id"];

$company_id_sel = $_POST["company_id_sel"];
$user_id_top_manager=$_POST["user_id_top_manager"];

$holiday_name_ar=$_POST["holiday_name_ar"];
$holiday_name_en = $_POST["holiday_name_en"];
$holiday_details = $_POST["holiday_details"];
$holiday_start_date	= $_POST["holiday_start_date"];
$holiday_end_date = $_POST["holiday_end_date"];

$add_user_id = $_POST["add_user_id"];

date_default_timezone_set("Asia/Riyadh");
  $created_date = date("Y-m-d");
  $createdDate_time = date("Y-m-d h:i:sa");
  $subscription_end_date = date('Y-m-d', strtotime($created_date . ' +15 day'));
  
  $link_lang = $_POST["link_lang"];
 $messag_add = $_POST["messag_add"];
 
// $arr_company_id=array_filter($company_id);
  $arr_company_id = $_POST["arr_company_id"];
  
  // get dates between
  function getBetweenDates($startDate, $endDate) {
    $rangArray = [];
 
    $startDate = strtotime($startDate);
    $endDate = strtotime($endDate);
 
    for ($currentDate = $startDate; $currentDate <= $endDate; $currentDate += (86400)) {
        $date = date('Y-m-d', $currentDate);
        $rangArray[] = $date;
    }
 
    return $rangArray;
}
$dates = getBetweenDates($holiday_start_date, $holiday_end_date);

  $sql1 = "INSERT INTO holidays_tbl(holiday_id,user_id_top_manager,holiday_name_ar,holiday_name_en,holiday_details,holiday_start_date,holiday_end_date,CreatedDate,createdDate_time,add_user_id)values('$holiday_id',$user_id_top_manager,'$holiday_name_ar','$holiday_name_en','$holiday_details','$holiday_start_date','$holiday_end_date','$created_date','$createdDate_time','$add_user_id')";

//----------------------------------------------------
 if($company_id_sel=='all_company'){
foreach ($arr as $value) {
  $sql_w = "INSERT INTO holidays_waseet_tbl(holiday_id,system_id,company_id,createdDate_time,add_user_id)values('$holiday_id','$system_id','$value','$createdDate_time','$add_user_id')";
 $run_w = $conn->query($sql_w) or die("Error in saving ".$conn->error);
 }
 }else{
   $sql_w = "INSERT INTO holidays_waseet_tbl(holiday_id,system_id,company_id,createdDate_time,add_user_id)values('$holiday_id','$system_id','$company_id_sel','$createdDate_time','$add_user_id')";  
   $run_w = $conn->query($sql_w) or die("Error in saving ".$conn->error);
  }
 
//-----------------------------------------------------------
foreach ($dates as $dates_value) {
$sql3 = "INSERT INTO holidays_tbll(holiday_id,holiday_name_ar,holiday_name_en,holiday_date,createdDate)values('$holiday_id','$holiday_name_ar','$holiday_name_en','$dates_value','$createdDate_time')";
$run3 = $conn->query($sql3) or die("Error in saving ".$conn->error);
}
//==================العملايات==================================

$browserAgent = $_SERVER['HTTP_USER_AGENT'];
 $user_id =$_SESSION['user_id'];
 $system_type='hr_marsa_system';
  $proccess_type='add_holiday';
  $proccess_details_en='Add New Holiday'.$holiday_id;
   $proccess_details=' إضافة عطله جديده رقم  '.$holiday_id;
 $table_name='dept_tb';   
  date_default_timezone_set("Asia/Riyadh");
  $createdDate = date("Y-m-d h:i:sa");
   $sql2 = "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','$holiday_id',NOW(),NOW(),'$createdDate','$browserAgent')";
 //==================العملايات==================================
  
if ($conn->query($sql1) === TRUE  && $conn->query($sql2) === TRUE) {
    //$_SESSION['message'] = "تم إضافة القسم بنجاح";
   
     $_SESSION['message'] =$messag_add;
   
  // header("Refresh:0; url=hr_holidayslist.php?$link_lang&company_id=$company_id");
   ?>
   <script>window.location="https://www.marsalogistics.com/new/hr_marsa_system/ar/att_dept/hr_holidayslist.php?<?php echo $link_lang; ?>&company_id=<?php echo $company_id;?>"</script>;
   </script>
   
   <?php
   

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

}

?>

