<?php

include("dbConfig.php");
include("../Function/level_salf.php"); 
//header('Content-Type: application/json; charset=utf-8');
$data = json_decode(file_get_contents('php://input'), true);
$add_user_id = $data["employ_id"];

$company_id = $data["company_id"];

$system_id='2222';

$request_type_id = $data["request_type_id"]; 
$value_type_checkif = $data["value_type_checkif"];

$sql_top="SELECT * FROM waseet_company_user_tbl where stop='0' AND deleted='0' AND company_id='$company_id'AND user_type='Top_Manager' ";
                   $result_top = $conn->query($sql_top);
                   if ($result_top->num_rows > 0) {
                   while($row_top = $result_top->fetch_assoc()) {
                    $user_id_top_manager=$row_top['user_id'];
}}
 
$details = $data["details"];
$type = 'all_requests';
$vacation = $data["request_type_id"];
$checkifleveldepttype = checkifleveldepttype('1',$company_id,$type,$vacation);

$em_dept = employ_dept($add_user_id);
$dept_manager = dept_manager($em_dept);                     
$br=  "\r\n";
date_default_timezone_set("Asia/Riyadh");
  $created_date = date("Y-m-d");
  $createdDate_time = date("Y-m-d h:i:sa");
  $lang = $_POST["lang"];

 function GetSeriaotherrr() {	
    $strings = '123456789';	
    $stringLength = strlen($strings);
    $newStrings = '';
    for ($i = 0; $i < 6; $i++) {
        $newStrings .= $strings[rand(0, $stringLength - 1)];
    }
    return $newStrings;
 }
 $salf_id = GetSeriaotherrr();
 
 $stat_ar = "قيد الانتظار";
 $stat_en = "appending";
 
  $sql_level_att_type_id="SELECT * FROM hr_advance_level_tbl where company_id='$company_id' and level='1' and type='$type' and vacation_id='$vacation'";
                       $result_level_att_type_id = $conn->query($sql_level_att_type_id);
                        if ($result_level_att_type_id->num_rows > 0) {
                         while($row_level_att_type_id = $result_level_att_type_id->fetch_assoc()) {
                          
                           $levell_id = $row_level_att_type_id["id"];
                         }}
 
 $sql_typ="SELECT * FROM hr_request_type_main_tbl where request_type_id ='$vacation' ";
                        $result_typ = $conn->query($sql_typ);
                        if ($result_typ->num_rows > 0) {
                          while($row_typ = $result_typ->fetch_assoc()) {
                              $type_title_ar = $row_typ["type_name_ar"];
                              $type_title_en = $row_typ["type_name_en"];
                          }}
 
//$add_request = "INSERT INTO hr_request_tbl(id,company_id,employ_id,details,maden,daen,request_stat,stat_ar,stat_en,detailsar,detailsen,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','$details','$amount','0','appending','$stat_ar','$stat_en','".getstatdetailsAR(1,$company_id_sel,$type,$vacation)."','".getstatdetailsEN(1,$company_id_sel,$type,$vacation)."','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,dept_id,maden,daen,detailsar,detailsen,employ_approval_id,level,op_type,stat_ar,stat_en,type,vacation_id,createdDate,createdDate_time,add_user_id)VALUES('$salf_id','$checkifleveldepttype','$amount','0','".getstatdetailsAR(1,$company_id_sel,$type,$vacation)."','".getstatdetailsEN(1,$company_id_sel,$type,$vacation)."','0','1','appending','$stat_ar','$stat_en','$type','$vacation','$created_date','$createdDate_time','$add_user_id')" ;
  
   //====================================================================
   $add_request = "INSERT INTO hr_request_tbl(id,company_id,employ_id,details,maden,daen,request_stat,stat_ar,stat_en,detailsar,detailsen,employ_approval_id,level,type,type_title_ar,type_title_en,vacation_id,createdDate,createdDate_time,add_user_id)VALUES('$salf_id','$company_id','$add_user_id','$details','$amount','0','appending','$stat_ar','$stat_en','".getstatdetailsAR(1,$company_id,$type,$vacation)."','".getstatdetailsEN(1,$company_id,$type,$vacation)."','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,dept_id,maden,daen,detailsar,detailsen,employ_approval_id,level,level_id,op_type,stat_ar,stat_en,type,vacation_id,createdDate,createdDate_time,add_user_id)VALUES('$salf_id','$checkifleveldepttype','$amount','0','".getstatdetailsAR(1,$company_id,$type,$vacation)."','".getstatdetailsEN(1,$company_id,$type,$vacation)."','0','1','$levell_id','appending','$stat_ar','$stat_en','$type','$vacation','$created_date','$createdDate_time','$add_user_id')" ;
  
   //====================================================================
  
  
  
  //==================options==================================================================


$main_type='all_requests';

$options = $data["options"];
$files = $data["files"];
$dd = 0;

if (count($options) > 0) {
    foreach ($options as $option) {
        $s_o_id = $option["options_id"];
        $update_options_id_title = $option["update_options_id"];
        $value_type = $option["value_type"];
        $var_name = $option["var_name"];

        if ($value_type == "file") {
            // Upload files
            if (in_array($s_o_id, $files)) {
                $time = time();
        $uploadFolder = '../hr_files/';
        $filePath = $var_name;  // File path is provided instead of field name
        $fileName = $time . '_' . basename($filePath);
        $destination = $uploadFolder . $fileName;

        if (copy($filePath, $destination)) {
            // File copied successfully
            //echo "y";
        } else {
            // Error copying file
            //echo "no";
        }

                $sql1 = "INSERT INTO hr_request_type_main_value_tbl (system_id, user_id_top_manager, company_id, main_type, request_id, request_type_id, options_id, update_options_id_title, value_type, steps_values, createdDate_time, add_user_id)
                         VALUES ($system_id, '$user_id_top_manager', '$company_id', '$main_type', '$salf_id', '$request_type_id', '$s_o_id', '$update_options_id_title', '$value_type', '$fileName', '$createdDate_time', '$add_user_id')";

                if ($conn->query($sql1) === TRUE) {
                    $dd++;
                } else {
                    echo "Error: " . $sql1 . "<br>" . $conn->error;
                }
            }
        } else {
            $sql1 = "INSERT INTO hr_request_type_main_value_tbl (system_id, user_id_top_manager, company_id, main_type, request_id, request_type_id, options_id, update_options_id_title, value_type, steps_values, createdDate_time, add_user_id)
                     VALUES ($system_id, '$user_id_top_manager', '$company_id', '$main_type', '$salf_id', '$request_type_id', '$s_o_id', '$update_options_id_title', '$value_type', '" . $_POST["var_name$s_o_id"] . "', '$createdDate_time', '$add_user_id')";

            if ($conn->query($sql1) === TRUE) {
                $dd++;
            } else {
                echo "Error: " . $sql1 . "<br>" . $conn->error;
            }
        }
    }
}
//==================/options================================================================

$employ_id=$add_user_id;
 $attach_type='777';
$connect_id=$salf_id;
$main_type='all_requests';
// $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 && $dd>0) {
  $success = true;
  $response = array(
      'success' => $success,
      'msg' => "تمت الإضافة بنجاح",
      'status' => "success",
      //'m' => $imageName1,
      
  );
} elseif($dd<=0) {
     $success = false;
        $response = array(
            'success' => $success,
            'msg' => "هناك خطاء في الرفع",
            'status' => "unsuccess",
            //'v' => $options,
        );
} else {
  $success = false;
        $response = array(
            'success' => $success,
            'msg' => "هناك خطاء الرجاء اعادة المحاوله",
            'status' => "unsuccess",
        );
}
 echo json_encode($response, JSON_UNESCAPED_UNICODE);

?>

