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

if (isset($_POST['id'])) {
    $clienteId = $_POST['id'];

    try {
        $dbh = new PDO("firebird:host=$host;dbname=$database;charset=UTF8", $username, $password);
        
        $sqlCliente = "SELECT * FROM PROV01 WHERE CLAVE = ?";
        $stmtCliente = $dbh->prepare($sqlCliente);
        $stmtCliente->execute([$clienteId]);
        $cliente = $stmtCliente->fetch(PDO::FETCH_ASSOC);
        
        $datosGenerales ="<form>
            <div class='form-row'>
                <div class='form-group col-md-6'>
                    <label for='nombre'>Nombre</label>
                    <input type='text' class='form-control' id='nombre' value='" . htmlspecialchars($cliente['NOMBRE'], ENT_QUOTES, 'UTF-8') . "' readonly>
                </div>
                <div class='form-group col-md-3'>
                    <label for='rfc'>RFC</label>
                    <input type='text' class='form-control' id='rfc' value='" . htmlspecialchars($cliente['RFC'], ENT_QUOTES, 'UTF-8') . "' readonly>
                </div>
            </div>
            <div class='form-row'>
                <div class='form-group col-md-3'>
                    <label for='calle'>Calle</label>
                    <input type='text' class='form-control' id='calle' value='" . htmlspecialchars($cliente['CALLE'], ENT_QUOTES, 'UTF-8') . "' readonly>
                </div>
                <div class='form-group col-md-3'>
                    <label for='telefono'>Teléfono</label>
                    <input type='text' class='form-control' id='telefono' value='" . htmlspecialchars($cliente['TELEFONO'], ENT_QUOTES, 'UTF-8') . "' readonly>
                </div>
                <div class='form-group col-md-3'>
                    <label for='saldo'>Saldo</label>
                    <input type='text' class='form-control' id='saldo' value='" . number_format($cliente['SALDO'], 2, '.', ',') . "' readonly>
                </div>
            </div>
            <div class='form-row'>
                <div class='form-group col-md-6'>
                    <label for='correo'>Correo</label>
                    <input type='text' class='form-control' id='correo' value='" . htmlspecialchars($cliente['EMAILPRED'], ENT_QUOTES, 'UTF-8') . "' readonly>
                </div>
            </div>
        </form>";

        

        $sqlFacturas = "SELECT * FROM FACTF01 WHERE CVE_CLPV = ?";
        $stmtFacturas = $dbh->prepare($sqlFacturas);
        $stmtFacturas->execute([$clienteId]);
        
        $facturas = "<table class='table table-hover'>";
        $facturas .= "<thead class='thead-dark'><tr><th>Factura</th><th>Fecha</th><th>Subtotal</th><th>I.V.A</th><th>Total</th><th>Acción</th></tr></thead><tbody>";
        
        while ($row = $stmtFacturas->fetch(PDO::FETCH_ASSOC)) {
            $facturas .= "<tr data-id='" . htmlspecialchars($row['CVE_DOC'], ENT_QUOTES, 'UTF-8') . "'>";
            $facturas .= "<td>" . htmlspecialchars($row['CVE_DOC'], ENT_QUOTES, 'UTF-8') . "</td>";
            $facturas .= "<td>" . date('d/m/Y', strtotime($row['FECHA_DOC'])) . "</td>";
            $facturas .= "<td>" . number_format($row['CAN_TOT'], 2, '.', ',') . "</td>";
            $facturas .= "<td>" . number_format($row['IMP_TOT4'], 2, '.', ',') . "</td>";
            $facturas .= "<td>" . number_format($row['IMPORTE'], 2, '.', ',') . "</td>";
            $facturas .= "<td><i class='fas fa-chevron-right toggle-detalles' style='cursor: pointer;' data-cve_doc='" . htmlspecialchars($row['CVE_DOC'], ENT_QUOTES, 'UTF-8') . "'></i></td>";
            $facturas .= "</tr>";
            $facturas .= "<tr class='detalles-factura' style='display: none;'>";
            $facturas .= "<td colspan='6'></td>";
            $facturas .= "</tr>";
        }
        $facturas .= "</tbody></table>";
        

        $sqlCotizaciones = "SELECT * FROM FACTC01 WHERE CVE_CLPV = ?";
        $stmtCotizaciones = $dbh->prepare($sqlCotizaciones);
        $stmtCotizaciones->execute([$clienteId]);
        
        $coticaciones = "<table class='table table-hover'>";
        $coticaciones .= "<thead class='thead-dark'><tr><th>Cotización</th><th>Fecha</th><th>Subtotal</th><th>I.V.A</th><th>Total</th><th>Acción</th></tr></thead><tbody>";
        
        while ($row = $stmtCotizaciones->fetch(PDO::FETCH_ASSOC)) {
            $coticaciones .= "<tr data-id='" . htmlspecialchars($row['CVE_DOC'], ENT_QUOTES, 'UTF-8') . "'>";
            $coticaciones .= "<td>" . htmlspecialchars($row['CVE_DOC'], ENT_QUOTES, 'UTF-8') . "</td>";
            $coticaciones .= "<td>" . date('d/m/Y', strtotime($row['FECHA_DOC'])) . "</td>";
            $coticaciones .= "<td>" . number_format($row['CAN_TOT'], 2, '.', ',') . "</td>";
            $coticaciones .= "<td>" . number_format($row['IMP_TOT4'], 2, '.', ',') . "</td>";
            $coticaciones .= "<td>" . number_format($row['IMPORTE'], 2, '.', ',') . "</td>";
            $coticaciones .= "<td><i class='fas fa-chevron-right toggle-detalles-cotizacion' style='cursor: pointer; color: #37465A;' data-cve_doc='" . htmlspecialchars($row['CVE_DOC'], ENT_QUOTES, 'UTF-8') . "'></i></td>";
            $coticaciones .= "</tr>";
            $coticaciones .= "<tr class='detalles-cotizacion' style='display: none;'>";
            $coticaciones .= "<td colspan='6'></td>";
            $coticaciones .= "</tr>";
        }
        $coticaciones .= "</tbody></table>";


        
        $sqlEtiquetas = "SELECT CAMPO, ETIQUETA FROM PARAM_CAMPOSLIBRES01 WHERE IDTABLA = 'CLIE_CLIB'";
        $stmtEtiquetas = $dbh->prepare($sqlEtiquetas);
        $stmtEtiquetas->execute();

        $etiquetas = array();
        while ($row = $stmtEtiquetas->fetch(PDO::FETCH_ASSOC)) {
            $etiquetas[$row['CAMPO']] = $row['ETIQUETA'];
        }
        
        
        $sqlDgenerales = "SELECT * FROM CLIE_CLIB01 WHERE CVE_CLIE = ?";
        $stmtDgenerales = $dbh->prepare($sqlDgenerales);
        $stmtDgenerales->execute([$clienteId]);
        
        $dg = "<table id='tablaCamposLibres' class='table table-hover'>";
        $dg .= "<thead class='thead-dark'><tr><th>Etiqueta</th><th>Valor</th><th>Acción</th></tr></thead><tbody>";
        
        while ($row = $stmtDgenerales->fetch(PDO::FETCH_ASSOC)) {
            for ($i = 1; $i <= 22; $i++) { 
                $campoLibre = 'CAMPLIB' . $i;
                $etiqueta = isset($etiquetas[$campoLibre]) ? $etiquetas[$campoLibre] : 'Campo Libre ' . $i;
                $dg .= "<tr data-cve_clie='" . $row['CVE_CLIE'] . "' data-camplib='" . $campoLibre . "'>";
                $dg .= "<td>" . $etiqueta . "</td>";
                $dg .= "<td><span class='valor-camplib'>" . htmlspecialchars($row[$campoLibre], ENT_QUOTES, 'UTF-8') . "</span></td>";
                $dg .= "<td><button class='btn btn-sm btn-primary btn-editar' data-toggle='modal' data-target='#editarModal'>Editar</button></td>";
                $dg .= "</tr>";
            }
        }
        $dg .= "</tbody></table>";

        $sqlEnvio = "SELECT * FROM CLIE01 WHERE CLAVE = ?";
        $stmtEnvio = $dbh->prepare($sqlEnvio);
        $stmtEnvio->execute([$clienteId]);
        $direccion = $stmtEnvio->fetch(PDO::FETCH_ASSOC);

        $datosenvio = "<table id='tablaDatosEnvio' class='table table-hover'>";
        $datosenvio .= "<thead class='thead-dark'><tr><th>Campo</th><th>Datos</th><th>Acción</th></tr></thead><tbody>";

        $datosenvio .= "<tr><td><strong>Calle:</strong></td><td>" . htmlspecialchars($direccion['CALLE_ENVIO'], ENT_QUOTES, 'UTF-8') . "</td><td><button class='btn btn-primary btn-sm btn-editar-datosenvio' data-campo='CALLE_ENVIO' data-valor='" . htmlspecialchars($direccion['CALLE_ENVIO'], ENT_QUOTES, 'UTF-8') . "' data-clienteid='" . $clienteId . "' data-toggle='modal' data-target='#editarModalDatosEnvio'>Editar</button></td></tr>";
        $datosenvio .= "<tr><td><strong>Núm. Ext.:</strong></td><td>" . htmlspecialchars($direccion['NUMEXT_ENVIO'], ENT_QUOTES, 'UTF-8') . "</td><td><button class='btn btn-primary btn-sm btn-editar-datosenvio' data-campo='NUMEXT_ENVIO' data-valor='" . htmlspecialchars($direccion['NUMEXT_ENVIO'], ENT_QUOTES, 'UTF-8') . "' data-clienteid='" . $clienteId . "' data-toggle='modal' data-target='#editarModalDatosEnvio'>Editar</button></td></tr>";
        $datosenvio .= "<tr><td><strong>Núm. Int.:</strong></td><td>" . htmlspecialchars($direccion['NUMINT_ENVIO'], ENT_QUOTES, 'UTF-8') . "</td><td><button class='btn btn-primary btn-sm btn-editar-datosenvio' data-campo='NUMINT_ENVIO' data-valor='" . htmlspecialchars($direccion['NUMINT_ENVIO'], ENT_QUOTES, 'UTF-8') . "' data-clienteid='" . $clienteId . "' data-toggle='modal' data-target='#editarModalDatosEnvio'>Editar</button></td></tr>";
        $datosenvio .= "<tr><td><strong>Cruzamientos:</strong></td><td>" . htmlspecialchars($direccion['CRUZAMIENTOS_ENVIO'], ENT_QUOTES, 'UTF-8') . "</td><td><button class='btn btn-primary btn-sm btn-editar-datosenvio' data-campo='CRUZAMIENTOS_ENVIO' data-valor='" . htmlspecialchars($direccion['CRUZAMIENTOS_ENVIO'], ENT_QUOTES, 'UTF-8') . "' data-clienteid='" . $clienteId . "' data-toggle='modal' data-target='#editarModalDatosEnvio'>Editar</button></td></tr>";

        $datosenvio .= "</tbody></table>";



        echo json_encode(['datosGenerales' => $datosGenerales, 'facturas' => $facturas, 'cotizaciones' => $coticaciones, 'dg' => $dg, 'datosenvio' => $datosenvio]);
    } catch (PDOException $e) {
        echo json_encode(['error' => $e->getMessage()]);
    }
}
?>
 