<?php
// --- API pour récupérer les tarifs d'abonnement par SiteID ---
ini_set('zlib.output_compression', 0);
while (@ob_end_clean());
ob_start();
header('Content-Type: text/plain; charset=utf-8');
header("HTTP/1.0 200 OK");
register_shutdown_function(function() {
    $size = ob_get_length();
    header('Content-Length: ' . $size);
    ob_end_flush();
});

require_once 'config/database.php';

$site_id = isset($_REQUEST['site_id']) ? trim($_REQUEST['site_id']) : '';

if (empty($site_id)) {
    die("ERR|SiteID manquant");
}

try {
    // Récupérer les tarifs d'abonnement pour le SiteID spécifié
    $stmt = $pdo->prepare("SELECT Id, SiteID, Designation, DesignationAR, Type, TypeAR, Prix FROM Tarifs_Abonnement WHERE SiteID = ? ORDER BY Id");
    $stmt->execute([$site_id]);
    $tarifs = $stmt->fetchAll(PDO::FETCH_ASSOC);

    if (empty($tarifs)) {
        die("ERR|Aucun tarif d'abonnement pour ce site");
    }

    // Format de réponse pour Java ME: 
    // Id;Designation;DesignationAR;Type;TypeAR;Prix (une ligne par tarif)
    foreach ($tarifs as $t) {
        $id = str_replace([";", "\n", "\r"], "", $t['Id']);
        $designation = str_replace([";", "\n", "\r"], " ", $t['Designation']);
        $designationAR = str_replace([";", "\n", "\r"], " ", $t['DesignationAR']);
        $type = str_replace([";", "\n", "\r"], " ", $t['Type']);
        $typeAR = str_replace([";", "\n", "\r"], " ", $t['TypeAR']);
        $prix = str_replace([";", "\n", "\r"], "", $t['Prix']);
        
        echo $id . ";" . $designation . ";" . $designationAR . ";" . $type . ";" . $typeAR . ";" . $prix . "\n";
    }

} catch (Exception $e) {
    die("ERR|Erreur serveur: " . $e->getMessage());
}
?>
