<?php

error_reporting(E_ALL);
ini_set('display_errors',1);

require '../config.php';
session_start();

$userRole =
strtolower($_SESSION['role']);

if(
    isset($_GET['approve'])
){

    $id =
    intval($_GET['approve']);

    if($userRole=='manager'){

        $conn->query("
        UPDATE overtime_requests
        SET manager_status='Approved'
        WHERE id=$id
        ");

    }

    if(
        in_array(
            $userRole,
            ['hr','admin','superadmin']
        )
    ){

        $conn->query("
        UPDATE overtime_requests
        SET hr_status='Approved'
        WHERE id=$id
        ");
    }

    header(
        "Location:overtime_approval.php"
    );
    exit;
}

$res = $conn->query("
SELECT

o.*,
e.name employee_name,
d.name department

FROM overtime_requests o

LEFT JOIN employees e
ON e.id=o.employee_id

LEFT JOIN departments d
ON d.id=e.department_id

ORDER BY o.created_at DESC
");

$requests=[];

while($row=$res->fetch_assoc()){

    $requests[]=$row;
}