<?php
/**
 * Get Print Settings AJAX Handler
 * WAMP Environment Compatible
 */

header('Content-Type: application/json');
include '../db.php';

try {
    $code = $_GET['code'] ?? null;
    
    if ($code) {
        // Get specific setting by code
        $stmt = $conn->prepare("SELECT value FROM settings WHERE code = ? AND is_active = 1 LIMIT 1");
        $stmt->bind_param("s", $code);
        $stmt->execute();
        $result = $stmt->get_result();
        
        if ($row = $result->fetch_assoc()) {
            echo json_encode([
                'success' => true,
                'value' => $row['value']
            ]);
        } else {
            echo json_encode([
                'success' => false,
                'message' => 'Setting not found',
                'value' => 'popup' // Default value
            ]);
        }
        $stmt->close();
    } else {
        // Get all print settings
        $result = $conn->query("SELECT code, value FROM settings WHERE category_id = 1 AND is_active = 1");
        $settings = [];
        while ($row = $result->fetch_assoc()) {
            $settings[$row['code']] = $row['value'];
        }
        
        echo json_encode([
            'success' => true,
            'settings' => $settings
        ]);
    }
    
} catch (Exception $e) {
    echo json_encode([
        'success' => false,
        'message' => $e->getMessage(),
        'value' => 'popup' // Default value
    ]);
}
?>
