<?php
session_start();
include "koneksi.php";

$nama = $_SESSION['user']['nama'] ?? '';

if ($nama === '') {
    echo json_encode(['status' => 'ERROR', 'message' => 'User tidak ditemukan']);
    exit;
}

$query = "SELECT status, sift FROM absen WHERE nama = '$nama' ORDER BY created_at DESC LIMIT 1";
$result = mysqli_query($koneksi, $query);

$last_status = '';
$last_sift = '';

if ($result && mysqli_num_rows($result) > 0) {
    $row = mysqli_fetch_assoc($result);
    $last_status = $row['status'];
    $last_sift = $row['sift'];
}

$_SESSION['absen_status'] = $last_status;
$_SESSION['absen_sift'] = $last_sift;

echo json_encode([
    'status' => 'OK',
    'last_status' => $last_status,
    'last_sift' => $last_sift
]);
