<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Neysi\DirectPrint\DirectPrint;
use App\Models\Sales\Invoice;
use App\Models\Sales\Transaction;
use App\Models\Sales\Ticket;
use App\Models\Facility\Layout;
use App\Models\Facility\Seats;
use App\Models\Facility\Rates;
use App\Models\Sales\TicketSeat;
use App\Models\Sales\PaymentMethod;
use App\Models\Sales\PaymentType;
use App\Models\Sales\FeeWaiverApproval;
use Auth;
use App\Models\User;
use App\Models\PublicUser;
use PDF;

class PrintController extends Controller
{
    function print_tickets_pdf($invoice_id,$invoicemd5)
    {
        $page_title = 'KSSTM Ticketing Application';
        $page_description = 'Some description for the page';
        $logo = "images/logo.png";
        $logoText = "images/logo-text.png";
        $active="active";
        $event_class="schedule-event";
        $button_class="btn-primary";
        $action = __FUNCTION__;

        $checkmd5 = md5($invoice_id);
        if($checkmd5!=$invoicemd5)
        {
            dd('Error');
            //need to point to an error view
        }
        /////update verify status in ticket table//////
        $tickets = Ticket::where('invoice_id',$invoice_id)->get();
        foreach($tickets as $ticket)
        {
            $ticket_id = $ticket->id;
            $ticket_update = Ticket::where('id',$ticket_id)->first();
            if(isset($ticket_update))
            {
                $ticket_update->verify_status = 1;
                $ticket_update->save();
            }
        }
        ////////////////////////////////////////////////
        $invoice = Invoice::where('id',$invoice_id)->where('status',1)->with('organisation','tickets')->first();
        $seatid =[];
        $seatname_ticket = [];
        $seat_count=0;
        $seat_name_row= [];
        $totalrows = "";
        $tickets = Ticket::where('invoice_id',$invoice_id)->where('status',1)->with('showtime','facility','invoice')->get();
        foreach($tickets as $ticket)
        {
            if($ticket->facility->fixedseatflag)
            {
                $ticket_id = $ticket->id;
                $facility_id = $ticket->facility_id;
                $layouts[$facility_id] = Layout::where('facility_id', $facility_id)->first();
                if(isset($layouts[$facility_id]))
                {
                    $totalrows = $layouts[$facility_id]->total_row;
                    $layout_id = $layouts[$facility_id]->id;
                    //get seats
                    for($i=1;$i<=$totalrows;$i++)
                    {
                        $seats[$facility_id][$i] = Seats::where('facility_id',$facility_id)->where('layout_id',$layout_id)->where('row_no',$i)->get();
                        foreach($seats[$facility_id][$i] as $seatobj)
                        {
                            $seat_name_row[$i][] = $seatobj->seat_name;
                        }
                    }
                }
                $ticket_seats = TicketSeat::where('ticket_id',$ticket_id)->where('status',1)->with('seat')->get();
                foreach($ticket_seats as $ticket_seat)
                {
                    $seat_count++;
                    $seatid[] = $ticket_seat->seat_id;
                    $seatname_ticket[] = $ticket_seat->seat->seat_name;
                }
            }
        }
        $counter_user = Auth::user()->id; 
        $users = User::where('id',$counter_user)->where('status',1)->first();
        if(isset($users))
        {
            $counter_user_name= $users->name;
        }
        //==dd($seatname_ticket);
        //return view('tickets.tickets_print', compact('page_title', 'page_description','action','logo','logoText','active','event_class','button_class','tickets','invoice','totalrows','seat_name_row','seatname_ticket','counter_user_name'));

        //return view('print.tickets_print', compact('page_title', 'page_description','action','logo','logoText','active','event_class','button_class','tickets','invoice','totalrows','seat_name_row','seatname_ticket','counter_user_name'));

        $pdf = PDF::loadView('print.tickets_print', compact('page_title', 'page_description','action','logo','logoText','active','event_class','button_class','tickets','invoice','totalrows','seat_name_row','seatname_ticket','counter_user_name'))->setOptions(['defaultFont' => 'sans-serif']);
    
        return $pdf->stream('ticket.pdf');
    }
    function print_tickets_html($invoice_id,$invoicemd5)
    {
        $page_title = 'KSSTM Ticketing Application';
        $page_description = 'Some description for the page';
        $logo = "images/logo.png";
        $logoText = "images/logo-text.png";
        $active="active";
        $event_class="schedule-event";
        $button_class="btn-primary";
        $action = __FUNCTION__;

        $checkmd5 = md5($invoice_id);
        if($checkmd5!=$invoicemd5)
        {
            dd('Error');
            //need to point to an error view
        }
        /////update verify status in ticket table//////
        $tickets = Ticket::where('invoice_id',$invoice_id)->get();
        foreach($tickets as $ticket)
        {
            $ticket_id = $ticket->id;
            $ticket_update = Ticket::where('id',$ticket_id)->first();
            if(isset($ticket_update))
            {
                $ticket_update->verify_status = 1;
                $ticket_update->save();
            }
        }
        ////////////////////////////////////////////////
        $invoice = Invoice::where('id',$invoice_id)->where('status',1)->with('organisation','tickets')->first();
        $seatid =[];
        $seatname_ticket = [];
        $seat_count=0;
        $seat_name_row= [];
        $totalrows = "";
        $tickets = Ticket::where('invoice_id',$invoice_id)->where('status',1)->with('showtime','facility','invoice')->get();
        foreach($tickets as $ticket)
        {
            if($ticket->facility->fixedseatflag)
            {
                $ticket_id = $ticket->id;
                $facility_id = $ticket->facility_id;
                $layouts[$facility_id] = Layout::where('facility_id', $facility_id)->first();
                if(isset($layouts[$facility_id]))
                {
                    $totalrows = $layouts[$facility_id]->total_row;
                    $layout_id = $layouts[$facility_id]->id;
                    //get seats
                    for($i=1;$i<=$totalrows;$i++)
                    {
                        $seats[$facility_id][$i] = Seats::where('facility_id',$facility_id)->where('layout_id',$layout_id)->where('row_no',$i)->get();
                        foreach($seats[$facility_id][$i] as $seatobj)
                        {
                            $seat_name_row[$i][] = $seatobj->seat_name;
                        }
                    }
                }
                $ticket_seats = TicketSeat::where('ticket_id',$ticket_id)->where('status',1)->with('seat')->get();
                foreach($ticket_seats as $ticket_seat)
                {
                    $seat_count++;
                    $seatid[] = $ticket_seat->seat_id;
                    $seatname_ticket[] = $ticket_seat->seat->seat_name;
                }
            }
        }
        $counter_user = Auth::user()->id; 
        $users = User::where('id',$counter_user)->where('status',1)->first();
        if(isset($users))
        {
            $counter_user_name= $users->name;
        }
        //==dd($seatname_ticket);
        return view('print.tickets_print2', compact('page_title', 'page_description','action','logo','logoText','active','event_class','button_class','tickets','invoice','totalrows','seat_name_row','seatname_ticket','counter_user_name'));

       
    }
    function printtest()
    {
        //echo $printerName =  DirectPrint::getDefaultPrinter() ;
       
        //$this->ticket_print(83,'fe9fc289c3ff0af142b6d3bead98a923');
        $invoice_id = 91;
        $invoicemd5 = '54229abfcfa5649e7003b83dd4755294';

               $page_title = 'KSSTM Ticketing Application';
        $page_description = 'Some description for the page';
        $logo = "images/logo.png";
        $logoText = "images/logo-text.png";
        $active="active";
        $event_class="schedule-event";
        $button_class="btn-primary";
        $action = __FUNCTION__;

        $checkmd5 = md5($invoice_id);
        if($checkmd5!=$invoicemd5)
        {
            dd('Error');
            //need to point to an error view
        }
        /////update verify status in ticket table//////
        $tickets = Ticket::where('invoice_id',$invoice_id)->get();
        foreach($tickets as $ticket)
        {
            $ticket_id = $ticket->id;
            $ticket_update = Ticket::where('id',$ticket_id)->first();
            if(isset($ticket_update))
            {
                $ticket_update->verify_status = 1;
                $ticket_update->save();
            }
        }
        ////////////////////////////////////////////////
        $invoice = Invoice::where('id',$invoice_id)->where('status',1)->with('organisation','tickets')->first();
        $seatid =[];
        $seatname_ticket = [];
        $seat_count=0;
        $seat_name_row= [];
        $totalrows = "";
        $tickets = Ticket::where('invoice_id',$invoice_id)->where('status',1)->with('showtime','facility','invoice')->get();
        foreach($tickets as $ticket)
        {
            if($ticket->facility->fixedseatflag)
            {
                $ticket_id = $ticket->id;
                $facility_id = $ticket->facility_id;
                $layouts[$facility_id] = Layout::where('facility_id', $facility_id)->first();
                if(isset($layouts[$facility_id]))
                {
                    $totalrows = $layouts[$facility_id]->total_row;
                    $layout_id = $layouts[$facility_id]->id;
                    //get seats
                    for($i=1;$i<=$totalrows;$i++)
                    {
                        $seats[$facility_id][$i] = Seats::where('facility_id',$facility_id)->where('layout_id',$layout_id)->where('row_no',$i)->get();
                        foreach($seats[$facility_id][$i] as $seatobj)
                        {
                            $seat_name_row[$i][] = $seatobj->seat_name;
                        }
                    }
                }
                $ticket_seats = TicketSeat::where('ticket_id',$ticket_id)->where('status',1)->with('seat')->get();
                foreach($ticket_seats as $ticket_seat)
                {
                    $seat_count++;
                    $seatid[] = $ticket_seat->seat_id;
                    $seatname_ticket[] = $ticket_seat->seat->seat_name;
                }
            }
        }
        $counter_user = Auth::user()->id; 
        $users = User::where('id',$counter_user)->where('status',1)->first();
        if(isset($users))
        {
            $counter_user_name= $users->name;
        }
        //==dd($seatname_ticket);
        //return view('tickets.tickets_print', compact('page_title', 'page_description','action','logo','logoText','active','event_class','button_class','tickets','invoice','totalrows','seat_name_row','seatname_ticket','counter_user_name'));

        //return view('print.tickets_print', compact('page_title', 'page_description','action','logo','logoText','active','event_class','button_class','tickets','invoice','totalrows','seat_name_row','seatname_ticket','counter_user_name'));

        $pdf = PDF::loadView('print.tickets_print', compact('page_title', 'page_description','action','logo','logoText','active','event_class','button_class','tickets','invoice','totalrows','seat_name_row','seatname_ticket','counter_user_name'))->setOptions(['defaultFont' => 'sans-serif']);
    
        return $pdf->stream('ticket.pdf');
    }
    function printtest2()
    {
        //echo $printerName =  DirectPrint::getDefaultPrinter() ;
       
        //$this->ticket_print(83,'fe9fc289c3ff0af142b6d3bead98a923');
        $invoice_id = 83;
        $invoicemd5 = 'fe9fc289c3ff0af142b6d3bead98a923';

               $page_title = 'KSSTM Ticketing Application';
        $page_description = 'Some description for the page';
        $logo = "images/logo.png";
        $logoText = "images/logo-text.png";
        $active="active";
        $event_class="schedule-event";
        $button_class="btn-primary";
        $action = __FUNCTION__;

        $checkmd5 = md5($invoice_id);
        if($checkmd5!=$invoicemd5)
        {
            dd('Error');
            //need to point to an error view
        }
        /////update verify status in ticket table//////
        $tickets = Ticket::where('invoice_id',$invoice_id)->get();
        foreach($tickets as $ticket)
        {
            $ticket_id = $ticket->id;
            $ticket_update = Ticket::where('id',$ticket_id)->first();
            if(isset($ticket_update))
            {
                $ticket_update->verify_status = 1;
                $ticket_update->save();
            }
        }
        ////////////////////////////////////////////////
        $invoice = Invoice::where('id',$invoice_id)->where('status',1)->with('organisation','tickets')->first();
        $seatid =[];
        $seatname_ticket = [];
        $seat_count=0;
        $seat_name_row= [];
        $totalrows = "";
        $tickets = Ticket::where('invoice_id',$invoice_id)->where('status',1)->with('showtime','facility','invoice')->get();
        foreach($tickets as $ticket)
        {
            if($ticket->facility->fixedseatflag)
            {
                $ticket_id = $ticket->id;
                $facility_id = $ticket->facility_id;
                $layouts[$facility_id] = Layout::where('facility_id', $facility_id)->first();
                if(isset($layouts[$facility_id]))
                {
                    $totalrows = $layouts[$facility_id]->total_row;
                    $layout_id = $layouts[$facility_id]->id;
                    //get seats
                    for($i=1;$i<=$totalrows;$i++)
                    {
                        $seats[$facility_id][$i] = Seats::where('facility_id',$facility_id)->where('layout_id',$layout_id)->where('row_no',$i)->get();
                        foreach($seats[$facility_id][$i] as $seatobj)
                        {
                            $seat_name_row[$i][] = $seatobj->seat_name;
                        }
                    }
                }
                $ticket_seats = TicketSeat::where('ticket_id',$ticket_id)->where('status',1)->with('seat')->get();
                foreach($ticket_seats as $ticket_seat)
                {
                    $seat_count++;
                    $seatid[] = $ticket_seat->seat_id;
                    $seatname_ticket[] = $ticket_seat->seat->seat_name;
                }
            }
        }
        $counter_user = Auth::user()->id; 
        $users = User::where('id',$counter_user)->where('status',1)->first();
        if(isset($users))
        {
            $counter_user_name= $users->name;
        }
        //==dd($seatname_ticket);
        return view('print.tickets_print2', compact('page_title', 'page_description','action','logo','logoText','active','event_class','button_class','tickets','invoice','totalrows','seat_name_row','seatname_ticket','counter_user_name'));

       
    }
    public function printtest_landing()
    {
        return view('print.printtest');
    }
    public function ticket_print($invoice_id,$invoicemd5)
    {
        $page_title = 'KSSTM Ticketing Application';
        $page_description = 'Some description for the page';
        $logo = "images/logo.png";
        $logoText = "images/logo-text.png";
        $active="active";
        $event_class="schedule-event";
        $button_class="btn-primary";
        $action = __FUNCTION__;

        $checkmd5 = md5($invoice_id);
        if($checkmd5!=$invoicemd5)
        {
            //dd('Error');
            //need to point to an error view
            return view('tickets.error', compact('page_title', 'page_description','action','logo','logoText','active','event_class','button_class'));
        }

        $invoice = Invoice::where('id',$invoice_id)->where('status',1)->with('organisation','tickets')->first();

        $tickets = Ticket::where('invoice_id',$invoice_id)->where('status',1)->with('showtime','facility','invoice')->get();

        return view('tickets.tickets_print', compact('page_title', 'page_description','action','logo','logoText','active','event_class','button_class','tickets','invoice'));

    }
    
}
