<?php
include("dbConfig.php");
header('Content-Type: application/json; charset=utf-8');
function GetSeriaother() {	
    $strings = '123456789';	
    $stringLength = strlen($strings);
    $newStrings = '';
    for ($i = 0; $i < 6; $i++) {
        $newStrings .= $strings[rand(0, $stringLength - 1)];
    }
    return $newStrings;
}

$employ_id = $_POST['employ_id'];
$request_per_id = GetSeriaother();
$request_per_id_same = GetSeriaother();
$request_per_type = $_POST['request_per_type'];
$request_type = $_POST["request_type"]; 
$request_per_details = $_POST["request_per_details"]; 
$request_per_date = $_POST["request_per_date"]; 
$request_per_time_from = $_POST["request_per_time_from"]; 
$request_per_time_to = $_POST["request_per_time_to"];
$request_per_stat = "0"; 
$add_user_id = $employ_id; 

$main_type='employ_file';
$attach_type='169315';

$att_name = $_POST["att_name"];
date_default_timezone_set("Asia/Riyadh");
$createdDate = date("Y-m-d");
$createdDate_Time = date("Y-m-d h:i:sa");
$sql_request = "INSERT INTO hr_request_permission_tbl (request_per_id,employ_id,request_per_type,request_type,request_per_details,request_per_date,request_per_time_from,request_per_time_to,request_per_stat,add_user_id,createdDate_Time,createdDate_Timee)
VALUES ('$request_per_id_same','$employ_id','$request_per_type','$request_type','$request_per_details','$request_per_date','$request_per_time_from','$request_per_time_to','$request_per_stat','$add_user_id','$createdDate_Time','$createdDate_Time')";
// upload files
$folderPath = "permation_images/";
if(isset($_FILES['file'])) {
        $file = $_FILES['file'];
        $fileName = $file['name'];
        $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', '".$_POST["att_name"]."', '$fileName', '$request_per_id_same', '$createdDate_Time', '$add_user_id')";
            $run = $conn->query($sql_request_att) or die("Error in saving image: " . $conn->error);
            $mf ="yesimage";
        } else {
            $mf ="noimage";
        }
}else{
     $mf ="notupload";
}
//
if ($conn->query($sql_request) === TRUE ) {
    $success = true;
    $respons = array(
        'success' => $success,
        'msg' => "تمت الإضافة بنجاح".$mf,
        'status' => "succes",
      );
}else{
    $success = false;
    $respons = array(
        'success' => $success,
        'msg' => "هناك خطاء الرجاء اعادة المحاوله",
        'status' => "unsuccess",
      );
}
                      
echo json_encode($respons,JSON_UNESCAPED_UNICODE);

?>