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

$employ_id = $_GET['employ_id'];

// Prepare the SQL query with the parameter
$sql = "SELECT * FROM hr_request_tbl WHERE employ_id='$employ_id' AND deleted='0' ORDER BY createdDate_Time DESC";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    $response = array();
    while ($row = $result->fetch_assoc()) {
        //
        $date1 = new DateTime($row["start_date_vacation"]);
$date2 = new DateTime($row["end_date_vacation"]);
$interval = $date1->diff($date2);
$dayss = $interval->days;
$days = $dayss+1;

        //
        $b = array();
        $sql_ico = "SELECT * FROM hr_employ_attachments_tbl WHERE employ_id='$employ_id' AND attach_type='777' AND connect_id='".$row['request_per_id']."' AND stop='0' AND deleted='0'";
        $result_ico = $conn->query($sql_ico);
        if ($result_ico->num_rows > 0) {
            while ($row_ico = $result_ico->fetch_assoc()) {
                $attach_link = $row_ico['attach_link'];
                $attach_id = $row_ico['attach_id'];
                $att_name = $row_ico['att_name'];
                $b[] = array(
                    'attach_link' => 'https://www.marsalogistics.com/new/hr_marsa_system/ar/hr_files/'.$attach_link,
                    
                );
            }
        }

        $a[] = array(
            'success' => true,
            'request_id' => $row["request_id"],
            'company_id' => $row["company_id"],
            'employ_id' => $row["employ_id"],
            'where_travel' => $row["where_travel"],
            'vacation_place' => $row["vacation_place"],
            'details' => $row["details"],
            'stat' => $row["stat"],
            'request_stat' => $row["request_stat"],
            'stat_ar' => $row["stat_ar"],
            'stat_en' => $row["stat_en"],
            'detailsar' => $row["detailsar"],
            'detailsen' => $row["detailsen"],
            'start_date_vacation' => $row["start_date_vacation"],
            'end_date_vacation' => $row["end_date_vacation"],
            'vac_num' => strval($days),
            'exit_date' => $row["exit_date"],
            'return_date' => $row["return_date"],
            'level' => $row["level"],
            'type' => $row["type"],
            'type_title_ar' => $row["type_title_ar"],
            'type_title_en' => $row["type_title_en"],
            'vacation_id' => $row["vacation_id"],
            'refuse_reason' => $row["refuse_reason"],
            'createdDate_time' => $row["createdDate_time"],
            'attach_link' => $b,
        );
    }
    
    $response = [
        'success' => true,
        'data' => $a,
    ];
} else {
    $a = array();
    $response = [
        'success' => true,
        'msg' => "not found",
        'data' => $a,
    ];
}

echo json_encode($response, JSON_UNESCAPED_UNICODE);
?>