<?php
$host = 'localhost';
$database = 'C:\Program Files (x86)\Common Files\Aspel\Sistemas Aspel\SAE9.00\Empresa01\Datos\SAE90EMPRE01.FDB';
$username = 'SYSDBA';
$password = 'masterkey';

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

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

    $filter = $_GET['filter'] ?? 'all';
    $startDate = $_GET['start_date'] ?? null;
    $endDate = $_GET['end_date'] ?? null;
    $facturado = $_GET['facturado'] ?? null;

    $whereBase = "WHERE TIP_DOC = 'P'";
    $whereEmitidas = $whereBase . " AND TIP_DOC_SIG IN ('F')";
    $whereCanceladas = $whereBase . " AND TIP_DOC_SIG IN ('C', 'V', 'R')";
    $whereTotal = $whereBase;

    // Aplicar filtro de facturación
    if ($facturado === '1') {
        // Solo facturados
        $whereTotal .= " AND TIP_DOC_SIG IN ('F')";
        $whereEmitidas .= " AND 1 = 1"; // se mantiene
        $whereCanceladas = $whereBase . " AND 1 = 0"; // cero no facturados
    } elseif ($facturado === '0') {
        // Solo no facturados
        $whereTotal .= " AND TIP_DOC_SIG IN ('C', 'V', 'R')";
        $whereEmitidas = $whereBase . " AND 1 = 0"; // cero facturados
        $whereCanceladas .= " AND 1 = 1"; // se mantiene
    }

    // Filtro de fechas
    if ($filter === 'today') {
        $today = date('Y-m-d');
        $fecha = "CAST(FACTP01.FECHA_DOC AS DATE) = CAST('$today' AS DATE)";
        $whereTotal .= " AND $fecha";
        $whereEmitidas .= " AND $fecha";
        $whereCanceladas .= " AND $fecha";
    } elseif ($filter === 'this-month') {
        $fecha = "EXTRACT(YEAR FROM FECHA_DOC) = EXTRACT(YEAR FROM CURRENT_DATE) 
                  AND EXTRACT(MONTH FROM FECHA_DOC) = EXTRACT(MONTH FROM CURRENT_DATE)";
        $whereTotal .= " AND $fecha";
        $whereEmitidas .= " AND $fecha";
        $whereCanceladas .= " AND $fecha";
    } elseif ($filter === 'last-month') {
        $year = date('Y', strtotime('first day of last month'));
        $month = date('m', strtotime('first day of last month'));
        $fecha = "EXTRACT(YEAR FROM FECHA_DOC) = $year AND EXTRACT(MONTH FROM FECHA_DOC) = $month";
        $whereTotal .= " AND $fecha";
        $whereEmitidas .= " AND $fecha";
        $whereCanceladas .= " AND $fecha";
    } elseif ($filter === 'custom' && $startDate && $endDate) {
        $fecha = "CAST(FACTP01.FECHA_DOC AS DATE) BETWEEN CAST('$startDate' AS DATE) AND CAST('$endDate' AS DATE)";
        $whereTotal .= " AND $fecha";
        $whereEmitidas .= " AND $fecha";
        $whereCanceladas .= " AND $fecha";
    }

    // Consultas
    $sqlTotal = "SELECT COUNT(*) FROM FACTP01 $whereTotal";
    $sqlEmitidas = "SELECT COUNT(*) FROM FACTP01 $whereEmitidas";
    $sqlCanceladas = "SELECT COUNT(*) FROM FACTP01 $whereCanceladas";

    $totalRegistros = $dbh->query($sqlTotal)->fetchColumn();
    $totalEmitidas = $dbh->query($sqlEmitidas)->fetchColumn();
    $totalCanceladas = $dbh->query($sqlCanceladas)->fetchColumn();

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