<?php

namespace App\Http\Controllers;
use Illuminate\Support\Collection;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
use Illuminate\Support\Arr;
use Auth;
use Session;
use Mail;
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;

class PublicFacilityController extends Controller
{
    public function about()
    {
        $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__;
        //get organisations
        $organisations = Organisation::all();
        
        return view('tickets.about', compact('page_title', 'page_description','action','logo','logoText','active','event_class','button_class','organisations'));
    }

    public function contact()
    {
        $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__;
        //get organisations
        $organisations = Organisation::all();
        
        return view('tickets.contact', compact('page_title', 'page_description','action','logo','logoText','active','event_class','button_class','organisations'));
    }

    public function rates()
    {
        $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__;
        //get organisations
        $organisations = Organisation::all();
        foreach($organisations as $organisation)
        {
            $organisation_id = $organisation->id;
            $facilities[$organisation_id] = Facility::where('organisation_id',$organisation_id)->where('active_flag',1)->orderBy('order_by')->get();
            foreach($facilities[$organisation_id] as $facility)
            {
                $facility_id = $facility->id;
                $rates[$facility_id] = Rates::where('facility_id',$facility_id)->where('active_flag',1)->orderBy('active_date','DESC')->first();
            }
        }
        return view('tickets.rates', compact('page_title', 'page_description','action','logo','logoText','active','event_class','button_class','organisations','rates','facilities'));
    }

    public function shows()
    {
        $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__;
        //get organisations
        $organisations = Organisation::all();
        foreach($organisations as $organisation)
        {
            $organisation_id = $organisation->id;
            $facilities[$organisation_id] = Facility::where('organisation_id',$organisation_id)->where('active_flag',1)->orderBy('order_by')->get();
            foreach($facilities[$organisation_id] as $facility)
            {
                $facility_id = $facility->id;
                $showtimtings[$facility_id] = ShowTimings::where('facility_id',$facility_id)->where('active_flag',1)->where('show_flag',0)->get();
            }
        }
        return view('tickets.shows', compact('page_title', 'page_description','action','logo','logoText','active','event_class','button_class','organisations','showtimtings','facilities'));
    }

    public function profile()
    {
        $page_title = 'My Profile';
        $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__;
        $public_user_id = Auth::guard('publicuser')->user()->id; 
        //dd($id);
        $users = PublicUser::where('id',$public_user_id)->first();
        return view('tickets.profile', compact('page_title', 'page_description','action','logo','logoText','active','event_class','button_class','users'));
    }
    public function profile_post(Request $request)
    {
        $insert_id="";
        $name=$request->name;
        $email=$request->email;
        $user_id = $request->user_id;
        $organisation_id = $request->organisation_id;
        $department_id = $request->department_id;
        $section_id = $request->section_id;
        $phone_number =$request->phone_number;
        //dd($request);
        $updflg = 0;
        $public_user_id = Auth::guard('publicuser')->user()->id; 
        $users = PublicUser::where('id',$public_user_id)->first();
        if(isset($users))
        {
            $updflg = 1;
            $users->name=$name;
            $users->email=$email;
            $users->phone = $phone_number;
            $users->save();
        }
        $message = 'Profile has been updated succeesfully';

        return redirect('user/profile');
    }
    public function profilepwd_post(Request $request)
    {
        $insert_id="";
        $password=$request->password;
        $cpassword=$request->confirm_password;
        $user_id = $request->user_id;
        $updflg = 0;
        $public_user_id = Auth::guard('publicuser')->user()->id; 
        $users = PublicUser::where('id',$public_user_id)->first();
        if(isset($users))
        {
            $updflg = 1;
            if($password && $cpassword)
            {
                if(!(strcmp($password, $cpassword)) == 0)
                {
                    return redirect()->back()->with('status',"Password and confirm should be same.");
                }
                else
                {
                    if($cpassword)
                    {
                        $newpassword = Hash::make($cpassword);
                        $user_pwd = PublicUser::where('id',$public_user_id)->first();
                        $user_pwd->password = $newpassword;
                        $user_pwd->save();
                    }
                }
            }
        }
        $message = 'Profile has been updated succeesfully';

        return redirect('user/profile');
    }

    public function generateOTP()
    {
        $otp = mt_rand(1000,9999);
        //$otp = 1234;
        return $otp;
    }
    public function forgotpassword()
    {
        $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__;        
        return view('public.forgotpassword', compact('page_title', 'page_description','action','logo','logoText','active','event_class','button_class'));
    }
    public function getphonenumberotp(Request $request)
    {
        $otp = $this->generateOTP();
        $phone = $request->phone;
        $users = PublicUser::where('phone',$phone)->count();
        if($users!=0)
        {
            //send SMS the generated OTP
            $sms = new SMSClass;
            $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'=>0,'msg'=>'Wrong Phone Number. Please Enter Correct Phone Number.'));
        }
    }
    public function otpphoneverify_password(Request $request)
    {
        $phone = $request->phone;
        $otpentered = $request->otpentered;
        $OTP = $request->session()->get('OTP');
        if($OTP==$otpentered) //otp matches
        {
            $response['statusCode'] = 200;
            $response['message'] = "Your Number is Verified.";
        }
        else
        {
            $response['statusCode'] = 400;
            $response['message'] = "OTP doesnot match";
        }
        return json_encode($response);
    }

    public function getpassword(Request $request)
    {
        $phone = $request->phone;
        $password=$request->password;
        $cpassword=$request->confirm_password;
        $todate = date("Y-m-d");
        //$password =  Str::random($length = 8);
        $users = PublicUser::where('phone',$phone)->first();
        if(isset($users))
        {
            $public_user_id = $users->id;
            ////update public user table///////////////////////
            if($password && $cpassword)
            {
                if(!(strcmp($password, $cpassword)) == 0)
                {
                    return redirect()->back()->with('status',"Password and confirm should be same.");
                }
                else
                {
                    if($cpassword)
                    {
                        $newpassword = Hash::make($cpassword);
                        $user_pwd = PublicUser::where('id',$public_user_id)->first();
                        $user_pwd->password = $newpassword;
                        $user_pwd->save();
                    }
                }
            }
            
            ///////////////////////////////////////////////////
            //$smssend = new SMSClass;
            //$smsarr = $smssend->sendSMSpassword($password, $phone, $country_code='+91');
        }
        return redirect('user/login');

    }

}
