<?php
header('Content-Type: application/json');
require_once '../config.php';

try {
    $id = $_POST['id'] ?? $_GET['id'] ?? '';
    $today = date('Y-m-d'); // Use format for MySQL DATE comparison

    // Count created today
    $sql = "SELECT COUNT(id) AS created 
            FROM tblissuedbookdetails 
            WHERE RetrunStatus = 0";
    $query = $dbh->prepare($sql);
    $query->execute();
    $createdCount = $query->fetch(PDO::FETCH_ASSOC)['created'] ?? 0;

    // Count modified today
    $sql = "SELECT COUNT(id) AS modified 
            FROM tblissuedbookdetails 
            WHERE DATE(ReturnDate) = :today";
    $query = $dbh->prepare($sql);
    $query->bindParam(':today', $today);
    $query->execute();
    $modifiedCount = $query->fetch(PDO::FETCH_ASSOC)['modified'] ?? 0;

    $sql = "SELECT logo FROM theme LIMIT 1";
    $query = $dbh->prepare($sql);
    $query->execute();
    $logo = $query->fetch(PDO::FETCH_ASSOC)['logo'] ?? null;

    $sql = "SELECT logoutTime FROM tbladminlogs WHERE applogin = :adminId AND adminId = :adminId AND logoutTime IS NOT NULL ORDER BY id DESC LIMIT 1";
    $query = $dbh->prepare($sql);
    $query->bindParam(":adminId", $id);
    $query->execute();
    $logoutTime = $query->fetch(PDO::FETCH_ASSOC)['logoutTime'] ?? null;

    echo json_encode(["created" => $createdCount, "modified" => $modifiedCount, "logoutTime" => $logoutTime, "logo" => $logo]);
} catch (Exception $e) {
    json_encode($e);
}
