<?php
session_start();

include("dbConfig.php");
header('Content-Type: application/json; charset=utf-8');
include("../Function/level_salf.php");

   $type = 'vacation';
  if($type=="vacation"){
    $type_title_ar = "إجازة";
    $type_title_en = "vacation";
  }elseif($type=="auhd"){
    $type_title_ar = "عهدة مالية";
    $type_title_en = "Covenant";
  }elseif($type=="salf"){
    $type_title_ar = "سلفة";
    $type_title_en = "Advance";
  }elseif($type=="other"){
    $type_title_ar = "طلبات اخرى";
    $type_title_en = "other request";
  }else{
    $type_title_ar = "طلبات اخرى";
    $type_title_en = "other request";
  }

$system_id='2222';

$company_id_sel = $_POST["company_id"];

$amount = $_POST["amount"];

$where_travel = $_POST["where_travel"];
$vacation_place = $_POST["vacation_place"];
$details = $_POST["details"];
$add_user_id = $_POST["employ_id"];

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

 $vacation = $_POST["vacation_typee"];
 $title = $_POST["title"];
 $encodedText = urlencode($title);
 $decodedTitle = urldecode($encodedText);

 $start_date_vacation = $_POST["start_date_vacation"];
 $end_date_vacation = $_POST["end_date_vacation"];
 
  $exit_date = $_POST["exit_date"];
  $return_date = $_POST["return_date"];
  
  
  //=============laws=====================================================================
  
     function GetSeriallaws() {	
          $strings = '123456789';	
          $stringLength = strlen($strings);
          $newStrings = '';
          for ($i = 0; $i < 6; $i++) {
              $newStrings .= $strings[rand(0, $stringLength - 1)];
          }
          return $newStrings;
       }
       
       $fixed_laws_id=GetSeriallaws();
  
   $num_vacation_days = $_POST["num_vacation_days"];
   
    $substitute_employee = $_POST["substitute_employee"];
    $user_id_top_manager = $_POST["user_id_top_manager"];
   $substitute_employee_id = $_POST["employ_id_select"];
   
    $vacation_fatra_type = $_POST["vacation_fatra_type"];
    $vacation_max_fatra = $_POST["vacation_max_fatra"];
    $add_ayam_fatra = $_POST["add_ayam_fatra"];
    
     $taswia_mostahkat = $_POST["taswia_mostahkat"];
     $vacation_gender = $_POST["vacation_gender"];
     $vacation_type_raseed_less = $_POST["vacation_type_raseed_less"];

     
      $vacation_type_other = $_POST["vacation_type_other"];
      
           
      $nesba_month1 = $_POST["nesba_month1"];
      $nesba_month2 = $_POST["nesba_month2"];
      $nesba_month3 = $_POST["nesba_month3"];
      $nesba_month4 = $_POST["nesba_month4"];
     
      $vacation_check_bigger_or_less = $_POST["vacation_check_bigger_or_less"];
      $vacation_add_ayam_less = $_POST["vacation_add_ayam_less"];
      $vacation_add_ayam_reseed = $_POST["vacation_add_ayam_reseed"];
      $vacation_add_ayam_old = $_POST["vacation_add_ayam_old"];
      $vacation_add_ayam_safy_raseed = $_POST["vacation_add_ayam_safy_raseed"];
      $numbe_unpaid_additional_days_less = $_POST["numbe_unpaid_additional_days_less"];
      $additional_days_and_add_ayam_raseed = $_POST["additional_days_and_add_ayam_raseed"];
      
      
        $travel_ticket_type = $_POST["travel_ticket_type"];
        $spent_time_with_company = $_POST["spent_time_with_company"];
        $spent_time_with_company_employ = $_POST["spent_time_with_company_employ"];
        $travel_ticket_period = $_POST["travel_ticket_period"];
        $travel_ticket_period_employ = $_POST["travel_ticket_period_employ"];
        $check_employ_or_dept_manager = $_POST["check_employ_or_dept_manager"];
        $ticket_credit = $_POST["ticket_credit"];
        
         $restart_work_type = $_POST["restart_work_type"];
         $restart_work_type_days = $_POST["restart_work_type_days"];
            
   
  //=============laws==================================================================
  
 

$add_request = "INSERT INTO hr_request_tbl(id,company_id,employ_id,where_travel,vacation_place,details,maden,daen,detailsar,detailsen,start_date_vacation,end_date_vacation,exit_date,return_date,employ_approval_id,level,type,type_title_ar,type_title_en,vacation_id,createdDate,createdDate_time,add_user_id)VALUES('$salf_id','$company_id_sel','$add_user_id','$where_travel','$vacation_place','$details','$amount','0','".getstatdetailsAR(1,$company_id_sel,$type,$vacation)."','".getstatdetailsEN(1,$company_id_sel,$type,$vacation)."','$start_date_vacation','$end_date_vacation','$exit_date','$return_date','0','1','$type','$type_title_ar','$type_title_en','$vacation','$created_date','$createdDate_time','$add_user_id')" ;

$add_request_steps = "INSERT INTO hr_request_steps_tbl(salf_id,maden,daen,detailsar,detailsen,employ_approval_id,level,type,vacation_id,createdDate,createdDate_time,add_user_id)VALUES('$salf_id','$amount','0','".getstatdetailsAR(1,$company_id_sel,$type,$vacation)."','".getstatdetailsEN(1,$company_id_sel,$type,$vacation)."','0','1','$type','$vacation','$created_date','$createdDate_time','$add_user_id')" ;
 
 
 
 $add_request_laws = "INSERT INTO vacations_request_fixed_laws_tbl(fixed_laws_id,user_id_top_manager,vacations_type_id,request_id,num_vacation_days,substitute_employee,substitute_employee_id,vacation_fatra_type,vacation_max_fatra,add_ayam_fatra,taswia_mostahkat,vacation_gender,vacation_type_raseed_less,vacation_type_other,nesba_month1,nesba_month2,nesba_month3,nesba_month4,vacation_check_bigger_or_less,vacation_add_ayam_less,vacation_add_ayam_reseed,vacation_add_ayam_old,vacation_add_ayam_safy_raseed,numbe_unpaid_additional_days_less,additional_days_and_add_ayam_raseed,travel_ticket_type,spent_time_with_company,spent_time_with_company_employ,travel_ticket_period,travel_ticket_period_employ,check_employ_or_dept_manager,ticket_credit,restart_work_type,restart_work_type_days,createdDate,createdDate_time,add_user_id)VALUES('$fixed_laws_id','$user_id_top_manager','$vacation','$salf_id','$num_vacation_days','$substitute_employee','$substitute_employee_id','$vacation_fatra_type','$vacation_max_fatra','$add_ayam_fatra','$taswia_mostahkat','$vacation_gender','$vacation_type_raseed_less','$vacation_type_other','$nesba_month1','$nesba_month2','$nesba_month3','$nesba_month4','$vacation_check_bigger_or_less','$vacation_add_ayam_less','$vacation_add_ayam_reseed','$vacation_add_ayam_old','$vacation_add_ayam_safy_raseed','$numbe_unpaid_additional_days_less','$additional_days_and_add_ayam_raseed','$travel_ticket_type','$spent_time_with_company','$spent_time_with_company_employ','$travel_ticket_period','$travel_ticket_period_employ','$check_employ_or_dept_manager','$ticket_credit','$restart_work_type','$restart_work_type_days','$created_date','$createdDate_time','$add_user_id')" ;
 $run2 = $conn->query($add_request_laws) or die("Error in saving Laws".$conn->error);
 
 if($restart_work_type=='yes_need_restart_work'){
 $add_restart_work = "INSERT INTO hr_request_tbl(id,company_id,employ_id,where_travel,vacation_place,details,maden,daen,detailsar,detailsen,start_date_vacation,end_date_vacation,exit_date,return_date,employ_approval_id,level,type,type_title_ar,type_title_en,vacation_id,createdDate,createdDate_time,add_user_id)VALUES('$salf_id','$company_id_sel','$add_user_id','$where_travel','$vacation_place','$details','$amount','0','".getstatdetailsAR(1,$company_id_sel,$type,$vacation)."','".getstatdetailsEN(1,$company_id_sel,$type,$vacation)."','$start_date_vacation','$end_date_vacation','$exit_date','$return_date','0','1','restart_work','$type_title_ar','$type_title_en','$vacation','$created_date','$createdDate_time','$add_user_id')" ;
 $run3 = $conn->query($add_restart_work) or die("Error in saving Laws".$conn->error);
}else{}
 
 
   
$employ_id=$add_user_id;
 $attach_type='777';
$connect_id=$salf_id;
$main_type='employ_file_vacations';
// $attach_type='227285'; 

// upload files
$folderPath = "../hr_files/";

if (isset($_FILES['files'])) {
    $fileCount = count($_FILES['files']['name']);
       $time = time();
    //for ($i = 0; $i < $fileCount; $i++) {
        $file = $_FILES['files'];
        $fileName = $file['name'].$time;
        $fileTmpPath = $file['tmp_name'];
        $fileType = $file['type'];

        $destination = $folderPath . $fileName;

        if (move_uploaded_file($fileTmpPath, $destination)) {
            $sql_request_att = "INSERT INTO hr_employ_attachments_tbl (employ_id, main_type, attach_type, att_name, attach_link, connect_id, createdDate_time, add_user_id) VALUES ('$employ_id', '$main_type', '$attach_type', '', '$fileName', '$request_per_id_same', '$createdDate_Time', '$add_user_id')";
            $run = $conn->query($sql_request_att) or die("Error in saving image: " . $conn->error);
        } else {
            $response = array(
                'success' => false,
                'msg' => "Error uploading file",
                'status' => "unsuccess",
            );
            echo json_encode($response, JSON_UNESCAPED_UNICODE);
            exit;
        }
   // }
}
 
 
//==================العملايات==================================
//==================العملايات==================================
  
if ($conn->query($add_request) === TRUE && $conn->query($add_request_steps) === TRUE) {
  $success = true;
  $response = array(
    'success' => $success,
    'msg' => "تمت الإضافة بنجاح",
    'status' => "success",
);

} else {
  $success = false;
    $response = array(
        'success' => $success,
        'msg' => "هناك خطاء الرجاء اعادة المحاوله",
        'status' => "unsuccess",
    );
}

echo json_encode($response, JSON_UNESCAPED_UNICODE);

?>

