<?php
include "../db.php";

if(isset($_POST['wa_id']) && isset($_POST['current'])){
    $wa_id = mysqli_real_escape_string($con, $_POST['wa_id']);
    $current = (int) $_POST['current'];

    $newStatus = $current == 1 ? 0 : 1;

    $update = mysqli_query($con, "UPDATE work_attendance 
                                  SET wa_attendance='$newStatus' 
                                  WHERE wa_id='$wa_id'");

    if($update){
        echo json_encode([
            "success" => true,
            "newStatus" => $newStatus,
            "text" => $newStatus == 1 ? "Present" : "Absent"
        ]);
    } else {
        echo json_encode(["success" => false]);
    }
}
?>