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

$employ_id =$_POST["employ_id"];

$hr_question_id =$_POST["hr_question_id"];
$employee_reply=$_POST["employee_reply"];
$hr_question_stat ='Answered_by_the_employee';
$employee_reply_date =$_POST["employee_reply_date"];
$add_user_id = $_POST["employ_id"];
$sql_finger_printer="SELECT * FROM hr_employ_tbl where employ_id='$employ_id'";
$result_finger_printer = $conn->query($sql_finger_printer);
if ($result_finger_printer->num_rows > 0) {
$row_finger_printer = $result_finger_printer->fetch_assoc();
 $employee_number_fingerprint=$row_finger_printer['employee_number_fingerprint'];
}
date_default_timezone_set("Asia/Riyadh");
  $created_date = date("Y-m-d");
  $createdDate_time = date("Y-m-d h:i:sa");
$hr_question_date = $_POST["hr_question_date"];
 
  $sql1 = "UPDATE  hr_question_tbl SET employee_reply='$employee_reply',hr_question_stat='$hr_question_stat',employee_reply_date='$createdDate_time' where hr_question_id ='$hr_question_id' ";

  $sql_w = "UPDATE hr_add_hodor_ensraf_tbl SET hodor_ensraf_stat='$hr_question_stat' WHERE employee_number_fingerprint='$employee_number_fingerprint' AND hodor_ensraf_date='$hr_question_date' ";
 $run_w = $conn->query($sql_w) or die("Error in saving ".$conn->error);

 //======================upload attachments========================================= 

   $main_type='employ_file';
   $attach_type='219231';
   
   $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)) {
            $query = "INSERT INTO hr_employ_attachments_tbl(employ_id,main_type,attach_type,att_name,attach_link,attach_size,file_type,connect_id,createdDate_time,add_user_id) VALUES('$employ_id','$main_type','$attach_type','','$fileName','','','$hr_question_id','$createdDate_time','$add_user_id')" ;
            $run = $conn->query($query) 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;
           }
      // }
   }
//==================العملايات==================================
$browserAgent = $_SERVER['HTTP_USER_AGENT'];
 $user_id =$_SESSION['user_id'];
 $system_type='hr_marsa_system';
  $proccess_type='Answered_by_the_employee';
  $proccess_details_en='The response was given by the employee to Justification No'.$hr_question_id;
   $proccess_details='تم الرد من قبل الموظف على التبرير رقم'.$hr_question_id;
 $table_name='hr_question_tbl';   
  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,device)values('$system_type','$add_user_id','$user_ip','$proccess_type','$proccess_details','$proccess_details_en','$table_name','$hr_question_id',NOW(),NOW(),'$createdDate','$browserAgent','app')";
 //==================العملايات==================================
  
if ($conn->query($sql1) === TRUE  && $conn->query($sql2) === 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);
?>

