<?php 
    header("Access-Control-Allow-Origin: *");
    header("Content-Type: application/json; charset=UTF-8");
    header("Access-Control-Allow-Methods: POST");
    header("Access-Control-Max-Age: 3600");
    header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

    include_once '../config/database.php';
    include_once '../objects/attendance.php'; 
    include_once '../config/_globle.php';

    $database = new Database();
    $db = $database->getConnection();

    $attendance = new Attendance($db);
    $data = json_decode(file_get_contents("php://input")); 

    $attendance->df = isset($_GET['df']) ? $_GET['df'] : null;
    $attendance->dt = isset($_GET['dt']) ? $_GET['dt'] : null;
    $attendance->staff_id = isset($_GET['staff_id']) ? $_GET['staff_id'] : null;

    $stmt = $attendance->search();
    $num = $stmt->rowCount();

    $arr = array();
    $arr["records"] = array();

    if($num>0){
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            extract($row); 
        
            $duty_min = ((strtotime($schedule_out) - strtotime($schedule_in)) / 60) > 0 ? ((strtotime($schedule_out) - strtotime($schedule_in)) / 60) : 0;
        
            $att_min = ((strtotime($duty_out) - strtotime($duty_in)) / 60) > 0 ? ((strtotime($duty_out) - strtotime($duty_in)) / 60) : 0;
            $second_att_min = ((strtotime($second_duty_out) - strtotime($second_duty_in)) / 60) > 0 ? ((strtotime($second_duty_out) - strtotime($second_duty_in)) / 60) : 0;
        
            $total_attendance_time = $att_min + $second_att_min;
        
            $time_difference = $total_attendance_time - $duty_min;

            $detail = array(
                $att_date,
                $name,
                (($schedule_in != "") ? date('h:i A', strtotime($schedule_in)) : "") . 
                (($schedule_out != "") ? " - " . date('h:i A', strtotime($schedule_out)) : "") . " " . $off_day,
                ($duty_in != "") ? date('h:i A', strtotime($duty_in)) : "",
                ($duty_in_photo != "") ? $app_url . 'api/attendance/upload/' . $staff_id . '/' . $duty_in_photo : "",
                ($duty_out != "") ? date('h:i A', strtotime($duty_out)) : "",
                ($duty_out_photo != "") ? $app_url . 'api/attendance/upload/' . $staff_id . '/' . $duty_out_photo : "",
                ($second_duty_in != "") ? date('h:i A', strtotime($second_duty_in)) : "",
                ($second_duty_in_photo != "") ? $app_url . 'api/attendance/upload/' . $staff_id . '/' . $second_duty_in_photo : "",
                ($second_duty_out != "") ? date('h:i A', strtotime($second_duty_out)) : "",
                ($second_duty_out_photo != "") ? $app_url . 'api/attendance/upload/' . $staff_id . '/' . $second_duty_out_photo : "",
                $duty_min,
                $total_attendance_time,
                $time_difference 
            );  
        
            array_push($arr["records"], $detail);
        }
        
    }
    echo json_encode($arr);
?>