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

//---------------------------arr---------------------------------------------
  $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);
          
//---------------------/arr------------------------------------------------------



if(ISSET($_POST['sav_btn'])){
//$shift_id =$_POST["shift_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"];

$shift_name_ar=$_POST["shift_name_ar"];
$shift_name_en = $_POST["shift_name_en"];
$min_start_time = $_POST["min_start_time"];
$start_time	= $_POST["start_time"];
$max_start_time = $_POST["max_start_time"];

$min_end_time=$_POST["min_end_time"];
$end_time = $_POST["end_time"];
$max_end_time = $_POST["max_end_time"];
$number_hours	= $_POST["number_hours"];
$number_hours_calculated= $_POST["number_hours_calculated"];
$allowed_minutes= $_POST["allowed_minutes"];



$break_time = $_POST["break_time"];

$flexible_working=$_POST["flexible_working"];
$only_presence_and_absence = $_POST["only_presence_and_absence"];

$billed_for_absence = $_POST["billed_for_absence"];


$sat = $_POST["sat"];
$sun = $_POST["sun"];
$mon	= $_POST["mon"];
$tue = $_POST["tue"];
$we = $_POST["we"];
$thu = $_POST["thu"];
$fr = $_POST["fr"];


$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"];
 
     function GetSeriaotherr() {	
		$strings = '123456789';	
		$stringLength = strlen($strings);
		$newStrings = '';
		for ($i = 0; $i < 6; $i++) {
			$newStrings .= $strings[rand(0, $stringLength - 1)];
		}
		return $newStrings;
	}
 
 $shift_id=GetSeriaotherr();

  $sql1 = "INSERT INTO hr_shift_tbl(shift_id,system_id,company_id,user_id_top_manager,shift_name_ar,shift_name_en,min_start_time,start_time,max_start_time,min_end_time,end_time,max_end_time,number_hours,number_hours_calculated,allowed_minutes,break_time,flexible_working,hodoor_ghyab,billed_for_absence,sat,sun,mon,tue,we,thu,fr,notes,createdDate,createdDate_time,add_user_id)values('$shift_id','$system_id','$company_id_sel',$user_id_top_manager,'$shift_name_ar','$shift_name_en','$min_start_time','$start_time','$max_start_time','$min_end_time','$end_time','$max_end_time','$number_hours','$number_hours_calculated','$allowed_minutes','$break_time','$flexible_working','$only_presence_and_absence','$billed_for_absence','$sat','$sun','$mon','$tue','$we','$thu','$fr','$notes','$created_date','$createdDate_time','$add_user_id')";


 //$sql1 = "INSERT INTO hr_shift_tbl(shift_id,system_id,company_id,user_id_top_manager,shift_name_ar,shift_name_en)values('$shift_id','$system_id','$company_id_sel',$user_id_top_manager,'$shift_name_ar','$shift_name_en')";
 

  //----------------------------------------------------
 if($company_id_sel=='all_company'){
foreach ($arr as $value) {
  $sql_w = "INSERT INTO hr_shift_waseet_tbl(shift_id,system_id,company_id,createdDate_time,add_user_id)values('$shift_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 hr_shift_waseet_tbl(shift_id,system_id,company_id,createdDate_time,add_user_id)values('$shift_id','$system_id','$company_id_sel','$createdDate_time','$add_user_id')";  
   $run_w = $conn->query($sql_w) 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_shift';
  $proccess_details_en='Add New shift'.$shift_id;
   $proccess_details=' إضافة شفت جديد رقم  '.$shift_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','$shift_id',NOW(),NOW(),'$createdDate','$browserAgent')";
 //==================العملايات==================================
  
if ($conn->query($sql1) === TRUE  && $conn->query($sql2) === TRUE) {
    //$_SESSION['message'] = "تم إضافة القسم بنجاح";
   
     $_SESSION['message'] =$messag_add;
   
 // header("Refresh:0; url=add_dpet.php?company_id=$company_id");
   
   //header("Refresh:0; url=hr_shiftlist.php?$link_lang&company_id=$company_id");
   ?>
   
    <script>window.location="https://www.marsalogistics.com/new/hr_marsa_system/ar/att_dept/hr_shiftlist.php?<?php echo $link_lang; ?>&company_id=<?php echo $company_id;?>"</script>;
   </script>
   
 <?php

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

}

?>

