<?php
use Mike42\Escpos\Printer;
use Mike42\Escpos\EscposImage;
use Mike42\Escpos\CapabilityProfile;
use Mike42\Escpos\PrintConnectors\FilePrintConnector;
use Mike42\Escpos\PrintConnectors\WindowsPrintConnector;
use Mike42\Escpos\PrintConnectors\NetworkPrintConnector;

function print_receipt($data) {
    //load printer
    if ($data->type == 'network') {
        $connector = new NetworkPrintConnector($data->printer_ip_address, $data->printer_port);
    } elseif ($data->type == 'linux') {
        $connector = new FilePrintConnector($data->path);
    } else {
        $connector = new WindowsPrintConnector($data->path);
    }
    $profile = CapabilityProfile::load($data->profile_);
    $printer = new Printer($connector, $profile);

    $printer->setJustification(Printer::JUSTIFY_CENTER);
    $printer->setEmphasis(true);
    $printer->setTextSize(2, 2);
    $printer->text(printText("iRestora PLUS",$data->characters_per_line)."\n");
    $printer->setEmphasis(false);
    $logo = EscposImage::load(dirname(__FILE__)."/print_content.png", true);
    $printer->bitImage($logo);
    $printer->setTextSize(1, 1);
    $printer->text(printLine( "Total: " ."33", $data->characters_per_line)."\n");
    $printer->text(drawLine($data->characters_per_line));
    $printer-> cut();
    $printer-> close();
    //end printing
}
function print_receipt_bill($data) {
    //load printer

    if ($data->type == 'network') {
        $connector = new NetworkPrintConnector($data->printer_ip_address, $data->printer_port);
    } elseif ($data->type == 'linux') {
        $connector = new FilePrintConnector($data->path);
    } else {
        $connector = new WindowsPrintConnector($data->path);
    }

    

    $profile = CapabilityProfile::load($data->profile_);
    $printer = new Printer($connector, $profile);




    // Ensure UTF-8 encoding for Arabic text
    // $printer->setCharacterTable(255); // Use the UTF-8 character table  oregenal Code

    $printer->selectCharacterTable(255);


    // Start printing
    $printer->setJustification(Printer::JUSTIFY_CENTER);
    $printer->setEmphasis(true);
    $printer->setTextSize(2, 2);

    
    
    // Print store name
    $printer->text(printText(mb_convert_encoding($data->store_name, 'UTF-8'), $data->characters_per_line) . "\n");
    $printer->setEmphasis(false);
    $printer->setTextSize(1, 1);

    // Print order details
    $printer->setJustification(Printer::JUSTIFY_CENTER);
    $printer->setEmphasis(true);
    $printer->text("Order Type: " . ($data->sale_type) . "\n");
    $printer->text("Bill No: " . $data->sale_no_p . "\n");
    $printer->feed();
    $printer->setEmphasis(false);
    $printer->setJustification(Printer::JUSTIFY_LEFT);
    $printer->text("Date: " . ($data->date) . " " . ($data->time_inv) . "\n");
    $printer->text("Sales Associate: " . ($data->sales_associate) . "\n");
    $printer->text("Customer: " . mb_convert_encoding($data->customer_name, 'UTF-8') . "\n");

    if ($data->customer_address != NULL && $data->customer_address != "") {
        $printer->text("Address: " . mb_convert_encoding($data->customer_address, 'UTF-8') . "\n");
    }

    if ($data->waiter_name != '') {
        $printer->text("Waiter: " . mb_convert_encoding($data->waiter_name, 'UTF-8') . "\n");
    }

    if ($data->customer_table) {
        $printer->text("Table: " . $data->customer_table . "\n");
    }

    // Print items and totals
    $printer->text(mb_convert_encoding($data->items, 'UTF-8'));
    $printer->text(drawLine($data->characters_per_line));
    $printer->text(mb_convert_encoding($data->totals, 'UTF-8'));
    $printer->text(drawLine($data->characters_per_line));

    // Print footer
    $printer->setJustification(Printer::JUSTIFY_CENTER);
    $printer->setEmphasis(true);
    $printer->text(printText(mb_convert_encoding($data->invoice_footer, 'UTF-8'), $data->characters_per_line) . "\n");
    $printer->setEmphasis(false);

    // Finalize
    $printer->cut();
    $printer->close();
}


function print_receipt_bot($data) {
    //load printer
    if ($data->type == 'network') {
        $connector = new NetworkPrintConnector($data->printer_ip_address, $data->printer_port);
    } elseif ($data->type == 'linux') {
        $connector = new FilePrintConnector($data->path);
    } else {
        $connector = new WindowsPrintConnector($data->path);
    }
    $profile = CapabilityProfile::load($data->profile_);
    $printer = new Printer($connector, $profile);


    $printer->setJustification(Printer::JUSTIFY_CENTER);
    $printer->setEmphasis(true);
    $printer->setTextSize(2, 2);
    $printer->text(printText($data->store_name,$data->characters_per_line)."\n");
    $printer->setEmphasis(false);
    $printer->setTextSize(1, 1);

    $printer->setJustification(Printer::JUSTIFY_CENTER);
    $printer->setEmphasis(true);
    $printer->text("Order Type: ".($data->sale_type)."\n");
    $printer->text("BOT: ".$data->sale_no_p."\n");
    $printer->feed();
    $printer->setEmphasis(false);
    $printer->setJustification(Printer::JUSTIFY_LEFT);
    $printer->text("Date: ".($data->date)." ".($data->time_inv)."\n");
    $printer->text("Customer: ".($data->customer_name)."\n");
    if($data->customer_address!=NULL  && $data->customer_address!=""){
        $printer->text("Address: ".$data->customer_address."\n");
    }
    if($data->waiter_name!=''){
        $printer->text("Waiter: ".$data->waiter_name."\n");
    }

    if($data->customer_table){
        $printer->text("Table: ".$data->customer_table."\n");
    }

    $printer->text($data->items);
    $printer->text(drawLine($data->characters_per_line));
    $printer->text($data->totals);
    $printer->text(drawLine($data->characters_per_line));

    $printer->setJustification(Printer::JUSTIFY_CENTER);
    $printer->setEmphasis(true);
    $printer->text(printText($data->invoice_footer,$data->characters_per_line)."\n");
    $printer->setEmphasis(false);
    //$printer->feed(1);
    $printer->cut();
    $printer->close();

}
function print_receipt_kot($data) {
    //load printer
    if ($data->type == 'network') {
        $connector = new NetworkPrintConnector($data->printer_ip_address, $data->printer_port);
    } elseif ($data->type == 'linux') {
        $connector = new FilePrintConnector($data->path);
    } else {
        $connector = new WindowsPrintConnector($data->path);
    }
    $profile = CapabilityProfile::load($data->profile_);
    $printer = new Printer($connector, $profile);

    $printer->setJustification(Printer::JUSTIFY_CENTER);
    $printer->setEmphasis(true);
    $printer->setTextSize(2, 2);
    $printer->text(printText($data->store_name,$data->characters_per_line)."\n");
    $printer->setEmphasis(false);
    $printer->setTextSize(1, 1);

    $printer->setJustification(Printer::JUSTIFY_CENTER);
    $printer->setEmphasis(true);
    $printer->text("Order Type: ".($data->sale_type)."\n");
    $printer->text("KOT: ".$data->sale_no_p."\n");
    $printer->feed();
    $printer->setEmphasis(false);
    $printer->setJustification(Printer::JUSTIFY_LEFT);
    $printer->text("Date: ".($data->date)." ".($data->time_inv)."\n");
    $printer->text("Customer: ".($data->customer_name)."\n");
    if($data->customer_address!=NULL  && $data->customer_address!=""){
        $printer->text("Address: ".$data->customer_address."\n");
    }
    if($data->waiter_name!=''){
        $printer->text("Waiter: ".$data->waiter_name."\n");
    }

    if($data->customer_table){
        $printer->text("Table: ".$data->customer_table."\n");
    }

    $printer->text($data->items);
    $printer->text(drawLine($data->characters_per_line));
    $printer->text($data->totals);
    $printer->text(drawLine($data->characters_per_line));

    $printer->setJustification(Printer::JUSTIFY_CENTER);
    $printer->setEmphasis(true);
    $printer->text(printText($data->invoice_footer,$data->characters_per_line)."\n");
    $printer->setEmphasis(false);
    //$printer->feed(1);
    $printer->cut();
    $printer->close();

}



