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

// Retrieve the employ_id parameter from the request
$employ_id = $_GET['employ_id'];

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

if ($result->num_rows > 0) {
    $response = array();
    while ($row = $result->fetch_assoc()) {
        $b = array();
        $sql_ico = "SELECT * FROM hr_employ_attachments_tbl WHERE employ_id='$employ_id' AND attach_type='169315' 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,
                    'attach_id' => $row_ico["attach_id"],
                    'att_name' => $row_ico["att_name"],
                );
            }
        }

        $a[] = array(
            'success' => true,
            'request_per_type' => $row["request_per_type"],
            'request_type' => $row["request_type"],
            'request_per_details' => $row["request_per_details"],
            'request_per_date' => $row["request_per_date"],
            'request_per_time_from' => $row["request_per_time_from"],
            'request_per_time_to' => $row["request_per_time_to"],
            'request_per_stat' => $row["request_per_stat"],
            'updateDate_Time' => $row["updateDate_Time"],
            'update_user_id' => $row["update_user_id"],
            'the_reason' => $row["the_reason"],
            'add_user_id' => $row["add_user_id"],
            'createdDate_Time' => $row["createdDate_Time"],
            'attatchement' => $b,
        );
    }
    
    $response = [
        'success' => true,
        'data' => $a,
    ];
} else {
    $a = array();
    $response = [
        'success' => true,
        'msg' => "not found",
        'data' => $a,
    ];
}

echo json_encode($response, JSON_UNESCAPED_UNICODE);
?>