<?php

namespace App\Http\Controllers;
use Illuminate\Support\Collection;
use Validator;
use Auth;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use App\Helper\MailHelper as MailHelper;
use Mail;
use DatePeriod;
use DateTime;
use DateInterval;
use Session;
use App\Mail\ForgotPassword;
use App\Models\User;
use App\Models\PublicUser;
use App\Models\Sales\Invoice;
use App\Models\Sales\Ticket;
use App\Models\Settings\Organisation;
use App\Models\Settings\Department;
use App\Models\Settings\Section;
use App\Models\Settings\Roles;
use App\Models\Settings\Permissions;
use App\Models\Settings\RolePermission;
use App\Models\Settings\UserRoles;
use App\Models\Settings\MenuSettings;
use App\Models\Settings\MenuRolePermission;
use App\Models\Settings\State;
use App\Models\Facility\Facility;
use App\Models\Facility\ShowTimings;
use App\Models\Facility\Layout;
use App\Models\Facility\Seats;
use App\Models\Facility\Rates;
use App\Helpers\SMSClass;

use App\Models\Settings\Science_Trivia;
use App\Models\Sales\TicketSeat;

class GeneralController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function generateOTP()
    {
        $otp = mt_rand(1000,9999);
        $otp = 1234;
        return $otp;
    }

    public function getTicket_general()
    {
        $page_title = 'Get Ticket';
        $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__;
        //////////////////////////////////////
        Session::forget('OTP');
        $timenow = now();
        return view('getTicket_general', compact('page_title', 'page_description','action','logo','logoText','active','event_class','button_class','timenow'));  
    }
    public function getTicket_public()
    {
        $page_title = 'Get Ticket';
        $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__;
        //////////////////////////////////////
        Session::forget('OTP');
        $timenow = now();
        return view('getTicket_public', compact('page_title', 'page_description','action','logo','logoText','active','event_class','button_class','timenow'));  
    }
    public function getTicket_general_post(Request $request)
    {
        $otp = $this->generateOTP();
        $phone = $request->phone;
        $publicuser_count = PublicUser::where('phone',$phone)->count();
        if($publicuser_count>0)
        {
            //send SMS the generated OTP
            $sms = new SMSClass;
            Session::put('OTP', $otp);
            $otpsession = $request->session()->get('OTP');

            if($otpsession)
                $smsresponse = $sms->sendSMS($otp,$phone);
            
            return json_encode(array('statusCode'=>200,'msg'=>'otp sent successfully'));
        }
        else
        {
            return json_encode(array('statusCode'=>400,'msg'=>'Invalid phone number'));
        }
    }
    public function phoneotpverify(Request $request)
    {
        $phone = $request->phone;
        $otpentered = $request->otpentered;
        $OTP = $request->session()->get('OTP');
        if($OTP==$otpentered) //otp matches
        {
            $response['statusCode'] = 200;
            $response['otp'] = $OTP;
            $response['message'] = "Your Number is Verified.";
        }
        else
        {
            $response['statusCode'] = 400;
             $response['otp'] = $OTP;
            $response['message'] = "OTP doesnot match";
        }
        return json_encode($response);
    }

    //////resend otp
    public function otpresend(Request $request)
    {
        $phone = $request->phone;
        $publicuser_count = PublicUser::where('phone',$phone)->count();
        if($publicuser_count>0)
        {
            $otp = $request->session()->get('OTP');
            //send SMS the generated OTP
            $sms = new SMSClass;
            if($otp)
                $smsresponse = $sms->sendSMS($otp,$phone);
            Session::put('OTP', $otp);
            return json_encode(array('statusCode'=>200,'msg'=>'otp sent successfully'));
        }
        else
        {
            return json_encode(array('statusCode'=>400,'msg'=>'Invalid phone number'));
        }
    }

    public function getTickets(Request $request)
    {
        $page_title = 'Get Ticket';
        $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__;

        $phone = $request->phone;
        $todate = date("Y-m-d");
        $timenow = now();
        $invoices = Invoice::where('show_date','>=',$todate)->where('status',1)->with('organisation','tickets','publicuser')->whereHas('publicuser', function($inv) use($phone){ $inv->where('phone',$phone)->where('status',1); })->get();
        return view('getTickets_public', compact('page_title', 'page_description','action','logo','logoText','active','event_class','button_class','invoices','phone','timenow'));  
    }

    public function print_tickets_general($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();
        $counter_user_name = "";
        $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_name = "Online Booking ";
        $trivia = Science_Trivia::all();
        //==dd($seatname_ticket);
        return view('tickets.tickets_print_general', compact('page_title', 'page_description','action','logo','logoText','active','event_class','button_class','tickets','invoice','totalrows','seat_name_row','seatname_ticket','counter_user_name','trivia'));
    }
}
