<?php
require_once '../layouts/session.php';
require_once '../layouts/dbconn.php';

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

if (!isset($_SESSION['user_role']) || $_SESSION['user_role'] !== 'HEAD_NURSE') {
    echo json_encode(['success' => false, 'message' => 'Unauthorized']);
    exit();
}

$conn = db_connect();

try {
    $procedure_id = isset($_POST['procedure_id']) ? intval($_POST['procedure_id']) : 0;
    
    if ($procedure_id <= 0) {
        throw new Exception('Invalid procedure ID');
    }
    
    // Get procedure with checklist
    $sql = "SELECT pre_procedure_checklist FROM ent_procedures WHERE procedure_id = ?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param('i', $procedure_id);
    $stmt->execute();
    $result = $stmt->get_result();
    
    if ($result->num_rows === 0) {
        throw new Exception('Procedure not found');
    }
    
    $procedure = $result->fetch_assoc();
    $checklist = json_decode($procedure['pre_procedure_checklist'], true);
    
    echo json_encode([
        'success' => true,
        'checklist' => is_array($checklist) ? $checklist : []
    ]);
    
} catch (Exception $e) {
    echo json_encode([
        'success' => false,
        'message' => $e->getMessage(),
        'checklist' => []
    ]);
}

$conn->close();
?>