<?php
namespace App\Http\Controllers;
use Illuminate\Support\Collection;
use Validator;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use App\Helper\MailHelper as MailHelper;
use Auth;
use App\Models\User;
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 Request as req;

class FacilityController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    //////////user////////////////////////////
    public function facility()
    {
        $page_title = 'Facility';
        $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__;
        
        $user_id = Auth::user()->id;
        $user_organisation_id = Auth::user()->organisation_id;
        if($user_organisation_id!="")
        {
            $faclities = Facility::where('organisation_id',$user_organisation_id)->with('organisation')->get();  
            $organisations = Organisation::where('id',$user_organisation_id)->get();
        }
        else
        {
            $faclities = Facility::with('organisation')->get();  
            $organisations = Organisation::all();
        }
        
        $slugrolename= "";
        $userarr = UserRoles::where('user_id',$user_id)->where('status',1)->with('role')->first();
        if(isset($userarr))
        {
            $slugrolename = $userarr->role->guard_name;
        }   
        return view('Facility/facility', compact('page_title','page_description','action','logo','logoText','active','event_class','button_class','organisations','faclities','slugrolename'));
    }
    
    public function facility_post(Request $request)
    {
        $messsages = array(
            'organisation_id.required'=>'Please enter organisation',
            'facility_name.required'=>'Please enter facility name',
            );
        $rules = array(
            'organisation_id'=>'required',
            'facility_name'=>'required|max:250',
            );
        Validator::make($request->all(), $rules,$messsages)->validate();
        $insert_id="";
        $organisation_id=$request->organisation_id;
        $facility_name=$request->facility_name;
        $timedshowflag=$request->timedshowflag;
        if(!$timedshowflag)
        {
            $timedshowflag = 0;
        }
        $noofseats=$request->noofseats;
        $fixedseatflag = $request->fixedseatflag;
        if(!$fixedseatflag)
        {
            $fixedseatflag = 0;
        }
        
        $maxticketperperson = $request->maxticketperperson;
        $short_code= $request->short_code;
        $order_by = $request->order_by;
        //dd($request);
        $id = Auth::user()->id;
        $facility_count = Facility::where('organisation_id',$organisation_id)->where('facility_name',$facility_name)->count();
        if($facility_count==0)
        {
                $insert_id = Facility::create([
                    'organisation_id' => $organisation_id,
                    'facility_name' => $facility_name,
                    'timedshowflag' => $timedshowflag,
                    'noofseats' => $noofseats,
                    'fixedseatflag' => $fixedseatflag,
                    'maxticketperperson' => $maxticketperperson,
                    'active_flag' => 1,
                    'short_code' => $short_code,
                    'order_by' => $order_by,
                ]);
        }
        if($insert_id)
        {
            return redirect()->back()->with('success', 'Success! Facility has been added');
        }
        else
        {
            return redirect()->back()->with('error', 'Failed! Facility already exist in the selected organisation');
        }
    }   
    public function show_facility($id)
    {
        $page_title = 'Facility';
        $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__;
        $facilities = Facility::where('id',$id)->with('organisation')->first();
        $organisations = Organisation::all();
        return view('Facility/show_facility', compact('page_title','page_description','action','logo','logoText','active','event_class','button_class','organisations','facilities'));
    }
    public function show_facility_post(Request $request)
    {
        $messsages = array(
            'organisation_id.required'=>'Please enter organisation',
            'facility_name.required'=>'Please enter facility name',
            );
        $rules = array(
            'organisation_id'=>'required',
            'facility_name'=>'required|max:250',
            );
        Validator::make($request->all(), $rules,$messsages)->validate();
        $insert_id="";
        $organisation_id=$request->organisation_id;
        $facility_name=$request->facility_name;
        $timedshowflag=$request->timedshowflag;
        if(!$timedshowflag)
        {
            $timedshowflag = 0;
        }
        $noofseats=$request->noofseats;
        $fixedseatflag = $request->fixedseatflag;
        if(!$fixedseatflag)
        {
            $fixedseatflag = 0;
        }
        $maxticketperperson = $request->maxticketperperson;
        $facility_id = $request->facility_id;
        $short_code = $request->short_code;
        $active_flag = $request->active_flag;
        if(!$active_flag)
        {
            $active_flag = 0;
        }
        $order_by = $request->order_by;
        //dd($request);
        $id = Auth::user()->id;
        $facilityarr = Facility::where('id',$facility_id)->first();
        if(isset($facilityarr))
        {
            $facilityarr->organisation_id = $organisation_id;
            $facilityarr->facility_name = $facility_name;
            $facilityarr->timedshowflag = $timedshowflag;
            $facilityarr->noofseats = $noofseats;
            $facilityarr->fixedseatflag = $fixedseatflag;
            $facilityarr->maxticketperperson = $maxticketperperson;
            $facilityarr->active_flag = $active_flag;
            $facilityarr->short_code = $short_code;
            $facilityarr->order_by = $order_by;
            $facilityarr->save();
            $upd_flg = 1;
        }
        if($upd_flg)
        {
            return redirect()->back()->with('success', 'Success! Faciltiy has been updated');
        }
        else
        {
            return redirect()->back()->with('error', 'Failed!');
        }
        return redirect()->back();
    }
    ///////////////////////////////////////////////////////

    public function showtimings_create($id)
    {
        $page_title = 'Show Timings';
        $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__;
        $facilities = Facility::where('id',$id)->with('organisation')->first();
        $showtimings = ShowTimings::where('facility_id',$id)->get();
        return view('Facility/showtimings_create', compact('page_title','page_description','action','logo','logoText','active','event_class','button_class','showtimings','facilities'));
    }
    public function showtimings_create_post(Request $request)
    {
        $insert_id="";
        $facility_id=$request->facility_id;
        $show_date=$request->show_date;
        $show_start_time=$request->show_start_time;
        $show_end_time=$request->show_end_time;
        $show_flag = $request->show_flag;
        if(!$show_flag)
        {
            $show_flag = 0;
        }
        $remark = $request->remark;
        $id = Auth::user()->id;
        $showtiming_count = ShowTimings::where('facility_id',$facility_id)->where('date',$show_date)->where('from_time',$show_start_time)->where('to_time',$show_end_time)->where('active_flag',1)->count();
        if($showtiming_count==0)
        {
                $insert_id = ShowTimings::create([
                    'facility_id' => $facility_id,
                    'date' => $show_date,
                    'from_time' => $show_start_time,
                    'to_time' => $show_end_time,
                    'active_flag' => 1,
                    'show_flag' => $show_flag,
                    'remark' => $remark,
                ]);
        }
        if($insert_id)
        {
            return redirect()->back()->with('success', 'Success! Show Timings has been added');
        }
        else
        {
            return redirect()->back()->with('error', 'Failed! Selected Show Timings already exist in the selected facility');
        }
    }

    public function showtimings_update($showtimeid)
    {
        $page_title = 'Show Timings';
        $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__;
        $showtimings = ShowTimings::where('id',$showtimeid)->with('facility')->first();
        return view('Facility/showtimings_update', compact('page_title','page_description','action','logo','logoText','active','event_class','button_class','showtimings'));
    }
    public function showtimings_update_post(Request $request)
    {
        $insert_id="";
        $facility_id = $request->facility_id;
        $showtimings_id=$request->showtimings_id;
        $show_date=$request->show_date;
        $show_start_time=$request->show_start_time;
        $show_end_time=$request->show_end_time;
        $show_flag = $request->show_flag;
        if(!$show_flag)
        {
            $show_flag = 0;
        }
        $active_flag = $request->active_flag;
        if(!$active_flag)
        {
            $active_flag = 0;
        }
        $remark = $request->remark;
        $upd_flg = 0;
        $id = Auth::user()->id;
        $showtimings = ShowTimings::where('id',$showtimings_id)->first();
        if(isset($showtimings))
        {
            $showtimings->date = $show_date;
            $showtimings->from_time = $show_start_time;
            $showtimings->to_time = $show_end_time;
            $showtimings->show_flag = $show_flag;
            $showtimings->active_flag = $active_flag;
            $showtimings->remark = $remark;
            $showtimings->save();
            $upd_flg = 1;   
        }
        if($upd_flg)
        {
            return redirect('/showtimings_create/'.$facility_id)->with('success', 'Success! Show Timings has been updated');
        }
        else
        {
            return redirect()->back()->with('error', 'Failed!');
        }
    }
    
    ////////////////layout
    public function layout_create($id)
    {
        $page_title = 'Layout';
        $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__;
        $facilities = Facility::where('id',$id)->with('organisation')->first();
        $layouts = Layout::where('facility_id',$id)->get();
        return view('Facility/layout_create', compact('page_title','page_description','action','logo','logoText','active','event_class','button_class','layouts','facilities'));
    }
    public function layout_create_post(Request $request)
    {
        $insert_id="";
        $facility_id=$request->facility_id;
        $total_rows=$request->total_rows;
        $maxinarow=$request->maxinarow;
        $maxseats=$request->maxseats;
        $id = Auth::user()->id;
        $layout_count = Layout::where('facility_id',$facility_id)->count();
        if($layout_count==0)
        {
                $insert_id = Layout::create([
                    'facility_id' => $facility_id,
                    'total_row' => $total_rows,
                    'maxinarow' => $maxinarow,
                    'maxseats' => $maxseats,
                    'active_flag' => 1,
                ]);
        }
        if($insert_id)
        {
            return redirect()->back()->with('success', 'Success! Layout has been added');
        }
        else
        {
            return redirect()->back()->with('error', 'Failed! Layout already exist in the selected facility');
        }
    }
    
    ////////////////seat layout
    public function seat_layout($id,$layout_id)
    {
        $page_title = 'Seat Layout';
        $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__;
        $facilities = Facility::where('id',$id)->with('organisation','showtimings','layout')->first();
        $layouts = Layout::where('id',$layout_id)->first();
        if(isset($layouts))
        {
            $total_row= $layouts->total_row;
            $maxinarow = $layouts->maxinarow;
        }
        $seatname = [];
        $rowname =[];
        for($i=1;$i<=$total_row;$i++)
        {
            $rowname_layouts = Seats::where('facility_id',$id)->where('layout_id',$layout_id)->where('row_no',$i)->select('row_name')->groupBy('row_name')->get();
            foreach($rowname_layouts as $rowname_layout)
            {
                $rowname[$i] = $rowname_layout->row_name;
            }
            
            for($seat=1;$seat<=$maxinarow;$seat++)
            {
                $seatname[$i][$seat] =  "";
                $seatno[$i][$seat] = "";
                $special_flag[$i][$seat] = "";
                $seat_layout_id[$i][$seat] ="";
                //echo "seat=$seat..<br>";
                if(isset($rowname[$i]))
                {
                    $seatname_layouts = Seats::where('facility_id',$id)->where('layout_id',$layout_id)->where('row_no',$i)->where('seat_no',$seat)->first();
                    if(isset($seatname_layouts))
                    {
                        $seat_layout_id[$i][$seat] = $seatname_layouts->id;
                        $seatname[$i][$seat] = $seatname_layouts->seat_name;
                        $seatno[$i][$seat] = $seatname_layouts->seat_no;
                        $special_flag[$i][$seat] = $seatname_layouts->special_flag;
                    }
                }
            }
        }

        //dd($seatno);
        return view('Facility/seat_layout', compact('page_title','page_description','action','logo','logoText','active','event_class','button_class','facilities','layouts','layout_id','rowname','seatname','seatno','special_flag','seat_layout_id'));
    }
    public function seat_layout_post(Request $request)
    {
        $insert_id="";
        $facility_id=$request->facility_id;
        $layout_id = $request->layout_id;
        $total_row=$request->total_row;
        $maxinarow =$request->maxinarow;
        $id = Auth::user()->id;
        for($i=1;$i<=$total_row;$i++)
        {
            $row_name = "row_name".$i;
            $row_name_save = $request->$row_name;
            $seat_layout_id_save ="";
            for($seat=1;$seat<=$maxinarow;$seat++)
            {
                $seat_number = $i."seat_number".$seat;
                $seat_number_save = $request->$seat_number;
                $seat_name = $i."seat_name".$seat;
                $seat_name_save = $request->$seat_name;
                $special_flag = $i."special_flag".$seat;
                $special_flag_save = $request->$special_flag;
                if(!$special_flag_save)
                    $special_flag_save=0;
                $seat_layout_id = $i."seat_layout_id".$seat;
                $seat_layout_id_save = $request->$seat_layout_id;

                if($seat_name_save)
                {
                    $seats_count = Seats::where('facility_id',$facility_id)->where('layout_id',$layout_id)->where('row_no',$i)->where('seat_no',$seat_number_save)->count();
                    if($seats_count==0)
                    {
                        $insert_id = Seats::create([
                            'facility_id' => $facility_id,
                            'layout_id' => $layout_id,
                            'row_no' => $i,
                            'row_name' => $row_name_save,
                            'seat_name' => $seat_name_save,
                            'seat_no' => $seat_number_save,
                            'special_flag' => $special_flag_save,
                        ]);
                    }
                    else
                    {
                        $seat_layout_update = Seats::where('id',$seat_layout_id_save)->where('facility_id',$facility_id)->where('layout_id',$layout_id)->where('row_no',$i)->where('seat_no',$seat_number_save)->first();
                        $seat_layout_update->row_name = $row_name_save;
                        $seat_layout_update->seat_name = $seat_name_save;
                        $seat_layout_update->seat_no = $seat_number_save;
                        $seat_layout_update->special_flag = $special_flag_save;
                        $seat_layout_update->save();
                    }
                }
            }
        }
        if($insert_id)
        {
            return redirect()->back()->with('success', 'Success! Seat Layout has been added');
        }
        else
        {
            return redirect()->back()->with('error', 'Failed! Selected Seat Layout already exist in the selected facility');
        }
    }

    ///////rates/////////////
    public function show_rates()
    {
        $page_title = 'Rates';
        $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__;
        $organisations = Organisation::all();
        return view('Facility/rates', compact('page_title','page_description','action','logo','logoText','active','event_class','button_class','organisations'));
    }
    public function org_facility_list(Request $request)
    {
        $page_title = 'Rates';
        $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__;
        $organisation_id = $request->organisation_id;
        $facility_id ="";
        $rates =[];
        $facilities = Facility::where('organisation_id',$organisation_id)->with('organisation')->where('active_flag',1)->get();
        $organisations = Organisation::where('id',$organisation_id)->first();
        foreach($facilities as $facility)
        {
            $facility_id = $facility->id;
            $rates[$facility_id] = Rates::where('facility_id',$facility_id)->where('active_flag',1)->first();
        }
        
        return view('Facility/org_facility_list', compact('page_title','page_description','action','logo','logoText','active','event_class','button_class','facilities','organisation_id','organisations','rates'));
    }
    public function facility_rates($facility_id)
    {
        $page_title = 'Rates';
        $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__;
        $facilities = Facility::where('id',$facility_id)->with('organisation')->first();
        $rates = Rates::where('facility_id',$facility_id)->get();
        $currently_active_rates = Rates::where('facility_id',$facility_id)->where('active_flag',1)->first();
        return view('Facility/facility_rates', compact('page_title','page_description','action','logo','logoText','active','event_class','button_class','facilities','rates','currently_active_rates'));
    }
    public function rates_post(Request $request)
    {
        $insert_id="";
        $facility_id=$request->facility_id;
        $adult_rate=$request->adult_rate;
        $child_rate=$request->child_rate;
        $active_date=$request->active_date;
        $active_flag = $request->active_flag;
        if(!$active_flag)
            $active_flag =0;
        $id = Auth::user()->id;
        $rate_count = Rates::where('facility_id',$facility_id)->where('active_flag',1)->count();
        if($rate_count==0)
        {
                $insert_id = Rates::create([
                    'facility_id' => $facility_id,
                    'adult_rate' => $adult_rate,
                    'child_rate' => $child_rate,
                    'active_date' => $active_date,
                    'active_flag' => $active_flag,
                ]);
        }
        else
        {
            $rates_already = Rates::where('facility_id',$facility_id)->where('active_flag',1)->first();
            $rates_already->active_flag= 0;
            $rates_already->save();

            $insert_id = Rates::create([
                    'facility_id' => $facility_id,
                    'adult_rate' => $adult_rate,
                    'child_rate' => $child_rate,
                    'active_date' => $active_date,
                    'active_flag' => $active_flag,
                ]);

        }
        if($insert_id)
        {
            return redirect()->back()->with('success', 'Success! Rates has been added');
            $message = 'Rate has been added';
        }
        else
        {
            return redirect()->back()->with('error', 'Failed! Active Rate already exist in the selected facility');
        }
    }
}
