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

if (!isset($_GET['tabla'])) {
    echo json_encode(['error' => 'No se especificó la tabla']);
    exit;
}

$tabla = $_GET['tabla'];

$host = 'localhost';
$database = 'C:\Program Files (x86)\Common Files\Aspel\Sistemas Aspel\COI10.00\Ejemplos\EJEMPLOS.FDB';
$username = 'SYSDBA';
$password = 'masterkey';

try {
    $dbh = new PDO("firebird:host=$host;dbname=$database;charset=UTF8", $username, $password);
    $stmt = $dbh->query("SELECT STATUS, COUNT(*) AS TOTAL FROM $tabla GROUP BY STATUS");

    $totales = [
        'A' => 0, // Alta
        'B' => 0, // Baja
        'S' => 0  // Suspendida
    ];

    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        $status = trim($row['STATUS']);
        if (isset($totales[$status])) {
            $totales[$status] += (int)$row['TOTAL'];
        }
    }

    echo json_encode($totales);

} catch (PDOException $e) {
    echo json_encode(['error' => $e->getMessage()]);
}

// STATUS
