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

if (!$tabla || strpos($tabla, 'ACTIVOS') !== 0) {
    echo json_encode(['error' => 'Tabla no válida']);
    exit;
}

try {
    $dbh = new PDO("firebird:host=$host;dbname=$database;charset=UTF8", $username, $password);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $sqlTotales = "SELECT TIPO, COUNT(*) AS TOTAL FROM $tabla GROUP BY TIPO";
    $stmtTotales = $dbh->query($sqlTotales);

    $totalesTipos = [
        1 => 0,
        2 => 0,
        3 => 0,
        4 => 0,
        5 => 0,
        6 => 0,
    ];

    while ($row = $stmtTotales->fetch(PDO::FETCH_ASSOC)) {
        $tipo = (int)$row['TIPO'];
        if (isset($totalesTipos[$tipo])) {
            $totalesTipos[$tipo] += (int)$row['TOTAL'];
        }
    }

    echo json_encode($totalesTipos);

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