<?php
require_once '../auth.php';
ensureAdminAjaxAuth();
// ajax/get_current_status.php
require_once '../../config/database.php';

header('Content-Type: application/json');

if(!isset($_GET['id'])) {
    echo json_encode(['success' => false, 'message' => 'Request ID required']);
    exit;
}

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

try {
    $database = new Database();
    $db = $database->getConnection();
    
    $query = "SELECT sr.job_status_id, j.status_name, j.color_code 
              FROM service_requests sr
              JOIN job_statuses j ON sr.job_status_id = j.id
              WHERE sr.id = :id";
    
    $stmt = $db->prepare($query);
    $stmt->execute([':id' => $id]);
    $status = $stmt->fetch(PDO::FETCH_ASSOC);
    
    if($status) {
        echo json_encode([
            'success' => true,
            'status_id' => $status['job_status_id'],
            'name' => $status['status_name'],
            'color' => $status['color_code']
        ]);
    } else {
        echo json_encode(['success' => false, 'message' => 'Status not found']);
    }
    
} catch(Exception $e) {
    echo json_encode(['success' => false, 'message' => 'Error: ' . $e->getMessage()]);
}
?>