<?php
    session_start();
    require '../../../vendor/autoload.php';
    require('functions_ticket.php');
    use Mike42\Escpos\PrintConnectors\WindowsPrintConnector;
    use Mike42\Escpos\CapabilityProfile;
    // use Mike42\Escpos\PrintConnectors\NetworkPrintConnector;
    use Mike42\Escpos\Printer;

    /* use Mike42\Escpos\EscposImage; */
    #========================================================= #
    #=================== Recibiendo datos ==================== #
    $id_nota = $_POST['id'];
    $nota = get_nota($id_nota);

    $campos = get_ultimos_productos_nota3($id_nota);
    $camposBar = get_ultimos_productos_bar($id_nota);

    $clasificaciones3 = clasificaciones3($id_nota);
    $no_nota = $nota[0]['CLAVE'];
    $tipo_venta = $nota[2]['ZONA'];
    $mesero = $nota[1]['NOMBRE'];
    $m=$nota[3]['MESA']; // Numero de mesa
    $comensal1 = $campos[0]['COMENSAL'];
    
    date_default_timezone_set('America/Mexico_City');
    $hoy=new DateTime();

#========================================================= #


#=================== Datos de la impresora =============== #

$name = $_POST['opcionImprComanda'];

$profile = CapabilityProfile::load("simple");
$connector = new WindowsPrintConnector($name);
$printer = new Printer($connector, $profile);

/* $connector = new NetworkPrintConnector("10.x.x.x", 9100);
$printer = new Printer($connector); */

#========================================================= #

// IMPRIMIR TODOS LOS PRODUCTOS A COCINA

$printer->setJustification(Printer::JUSTIFY_CENTER);
$printer->setEmphasis(true);
$printer->setTextSize(3, 3);
$printer->text( "M:". $m . "\n");

$printer->text("\n"); 
$printer->setEmphasis(false);
$printer->setTextSize(1, 1);
$printer->text(date("Y-m-d") ."  ". "NOTA:". $no_nota . "\n");
$printer->setEmphasis(false);
$printer->setTextSize(2, 1);
$printer->text(date("H:i:s") . "\n");
$printer->setEmphasis(false);
$printer->setTextSize(1, 1);
$printer->text("Tipo Venta: " . $tipo_venta . "\n");

$printer->setEmphasis(false);
$printer->setTextSize(1, 1);
$printer->text("MESERO: " . $mesero . "\n");


$printer->setEmphasis(true);
$printer->setTextSize(2,2);
$printer->text("C: " . $comensal1 . "\n");
$printer->text("\n");   
$printer->setEmphasis(false);
$printer->setTextSize(1, 1);
    


#========================================================= #

    
$printer->setJustification(Printer::JUSTIFY_CENTER);
$printer->setEmphasis(false);
$printer->setTextSize(1, 1);

$printer->text("------------------------------------------"); // podria quitarse
$printer->text("\n");
if ($clasificaciones3[0]!= 0) {
    /*----------  Detalles de la tabla  ----------*/
    foreach ($campos as $campo) {
    
        $printer->setEmphasis(true);
        $printer->setTextSize(2, 1);
        $tiempo = $campo['TIEMPO'];
        $comentario = $campo['COMENTARIO'];
        $comensal = $campo['COMENSAL'];

        $printer->setJustification(Printer::JUSTIFY_RIGHT); //
        $printer->text($tiempo . "\n");
        $printer->setJustification(Printer::JUSTIFY_CENTER);
        $printer->text($campo['CANTIDAD'] . "   " . $campo['PRO_NOMBRE'] . "   ");
        $printer->text("\n");

        $printer->text($comentario .  "\n " );
        
        $printer->text("\n");
        $id_pro_nota = $campo['ID'];
        $query2 = query("UPDATE productos_nota SET impreso='C', pdn_guardado=pdn_guardado+1 WHERE id='$id_pro_nota'");

    }
}
// Cortar el papel
$printer->cut();

// Enviar un pulso de apertura del cajón de efectivo
$printer->pulse();

// Esperar un segundo para que la impresora procese la orden
sleep(1);
// IMPRIMIR TODOS LOS PRODUCTOS A BAR

$printer->setJustification(Printer::JUSTIFY_CENTER);
$printer->setEmphasis(true);
$printer->setTextSize(3, 3);
$printer->text( "M:". $m . "\n");

$printer->text("\n"); 
$printer->setEmphasis(false);
$printer->setTextSize(1, 1);
$printer->text(date("Y-m-d") ."  ". "NOTA:". $no_nota . "\n");
$printer->setEmphasis(false);
$printer->setTextSize(2, 1);
$printer->text(date("H:i:s") . "\n");
$printer->setEmphasis(false);
$printer->setTextSize(1, 1);
$printer->text("Tipo Venta: " . $tipo_venta . "\n");

$printer->setEmphasis(false);
$printer->setTextSize(1, 1);
$printer->text("MESERO: " . $mesero . "\n");


$printer->setEmphasis(true);
$printer->setTextSize(2,2);
$printer->text("C: " . $comensal1 . "\n");
$printer->text("\n");   
$printer->setEmphasis(false);
$printer->setTextSize(1, 1);

$printer->text("------------------------------------------"); 
$printer->text("\n");

if ($camposBar[0]["ID"]!= 0) {
    foreach ($camposBar as $campoBar) {
    
        $printer->setEmphasis(true);
        $printer->setTextSize(2, 1);
        $tiempo = $campoBar['TIEMPO'];
        $comentario = $campoBar['COMENTARIO'];
    
        $printer->setJustification(Printer::JUSTIFY_RIGHT); //
        $printer->text($tiempo . "\n");
        $printer->setJustification(Printer::JUSTIFY_CENTER);
        $printer->text($campoBar['CANTIDAD'] . "   " . $campoBar['PRO_NOMBRE'] . "   ");
        $printer->text("\n");
    
        $printer->text($comentario .  "\n " );
        
        $printer->text("\n");
        $id_pro_nota = $campoBar['ID'];
        $query2 = query("UPDATE productos_nota SET impreso='C', pdn_guardado=pdn_guardado+1 WHERE id='$id_pro_nota'");
    
    }
}
/*----------  Fin Detalles de la tabla  ----------*/

// Cortar el papel
$printer->cut();

// Enviar un pulso de apertura del cajón de efectivo
$printer->pulse();

// Esperar un segundo para que la impresora procese la orden
sleep(1);

// }


// Cerrar la impresora
$printer->close();
?>