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

$employ_id = $_GET["employ_id"];
$todayMonth = date("n");
$todayYear = date("Y");

$sql_topmanager = "SELECT * FROM hr_employ_tbl WHERE employ_id='$employ_id'";
$result_topmanager = $conn->query($sql_topmanager);

if ($result_topmanager->num_rows > 0) {
    $row_topmanager = $result_topmanager->fetch_assoc();
    $user_id_top_manager = $row_topmanager["user_id_top_manager"];

    $sql_employ_ids = "SELECT employ_id FROM hr_employ_tbl WHERE user_id_top_manager='$user_id_top_manager' AND employ_id NOT IN ('271562', '568714', '961547') and office_employ='office_employee'";
    $result_employ_ids = $conn->query($sql_employ_ids);
    
    if ($result_employ_ids->num_rows > 0) {
        $em_array = [];
        while ($row_employ_ids = $result_employ_ids->fetch_assoc()) {
            $em_array[] = $row_employ_ids["employ_id"];
        }

        $ids_uss = "'" . implode("','", $em_array) . "'";
        
        $sql = "SELECT user_id, COUNT(process_systems_id) AS count_occurrences
                FROM process_systems_tbl
                WHERE system_type='hr_marsa_system' AND user_id IN ($ids_uss) 
                AND YEAR(proccess_date)='$todayYear' AND MONTH(proccess_date)='$todayMonth' AND deleted='0'
                GROUP BY user_id
                ORDER BY count_occurrences DESC
                LIMIT 10";
        
        $result = $conn->query($sql);
        
        if ($result->num_rows > 0) {
            $response = ['success' => true, 'data' => []];
            
            $stmt_employ_name = $conn->prepare("SELECT employ_name_ar, employ_name_en, employ_sex, job_id FROM hr_employ_tbl WHERE employ_id=?");
            $stmt_job = $conn->prepare("SELECT job_name FROM job_tb WHERE job_id=?");
            $stmt_attachments = $conn->prepare("SELECT attach_link FROM hr_employ_attachments_tbl WHERE employ_id=? AND att_name='employ_photo' AND stop='0' AND deleted='0'");

            while ($row = $result->fetch_assoc()) {
                $stmt_employ_name->bind_param("s", $row["user_id"]);
                $stmt_employ_name->execute();
                $stmt_employ_name->bind_result($employ_name_ar, $employ_name_en, $employ_sex, $job_id);
                $stmt_employ_name->fetch();
                $stmt_employ_name->reset();

                $stmt_job->bind_param("s", $job_id);
                $stmt_job->execute();
                $stmt_job->bind_result($job_name_ar);
                $stmt_job->fetch();
                $stmt_job->reset();

                $stmt_attachments->bind_param("s", $row["user_id"]);
                $stmt_attachments->execute();
                $stmt_attachments->bind_result($attach_link);
                
                $attachments = '';
                while ($stmt_attachments->fetch()) {
                    $attachments = 'https://www.marsalogistics.com/new/hr_marsa_system/ar/hr_files/' . $attach_link;
                }
                $stmt_attachments->reset();

                $response['data'][] = [
                    'success' => true,
                    'count_occurrences' => $row["count_occurrences"],
                    'employ_name_ar' => $employ_name_ar,
                    'employ_name_en' => $employ_name_en,
                    'employ_sex' => $employ_sex,
                    'job_name_ar' => $job_name_ar,
                    'attachments' => $attachments,
                ];
            }

            $response['success'] = true;
        } else {
            $response = [
                'success' => true,
                'msg' => "not found",
                'data' => [],
            ];
        }
   } else {
        $response = [
            'success' => true,
            'msg' => "not found",
            'data' => [],
        ];
    }
}
    echo json_encode($response, JSON_UNESCAPED_UNICODE);
?>