<?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;
$system_id = '2222';
$company_id = $_POST["company_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,system_id,company_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,add_from)
VALUES ('$request_per_id_same','$system_id','$company_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','app')";

// 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($sql_request) === TRUE) {
 //==================العملايات==================================

$browserAgent = $_SERVER['HTTP_USER_AGENT'];
 $user_id =$employ_id;
 $add_user_id = $employ_id;
 $system_type='hr_marsa_system';
  $proccess_type='Add a permission request through the Hr app';
  $proccess_details_en='Add a permission request through the Hr app';
  $proccess_details='إضافة طلب اذن عن طريق التطبيق';
 $table_name='	hr_request_permission_tbl';   
  date_default_timezone_set("Asia/Riyadh");
  $createdDate = date("Y-m-d h:i:sa");
   $sql_req = "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','$user_id',NOW(),NOW(),'$createdDate','$browserAgent')";
  $run = $conn->query($sql_req) or die("Error in saving ".$conn->error); 
 //==================العملايات==================================

    $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);
?>