<?php

namespace App\Http\Controllers;

use App\Event;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

class EventController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return Response
     */
    public function store()
    {
        $event = new Event();

        $event->name = request('name');
        $event->date_begin = request('begin');
        $event->date_end = request('end');
        $event->type = request('type');
        $event->user_id = \Auth::user()->id;
        $event->location = request('location');
        if(request('is_mandatory') != null){
            $event->is_mandatory = 1;
        } else {
            $event->is_mandatory  = 0;
        }
        $event->desc = request('desc');

        $event->save();

        if ($event->type == 1) {
            for ($l=1; $l <= \App\Config::getData('admin_level_in_schedule_nb'); $l++) { 
                for ($p=1; $p <= \App\Config::getData('admin_periode_nb'); $p++) { 
                    
                    $course = new \App\Course();

                    $users = \App\User::all();
                    $instructor = 1;

                    foreach ($users as $user) {
                        if($user->fullname() == request('instruc_n'.$l.'_p'.$p))
                        {
                            $instructor = $user->id;
                        }
                    }

                    $course->name = request('name_n'.$l.'_p'.$p);
                    $course->user_id = $instructor;
                    $course->ocom = request('ocom_n'.$l.'_p'.$p);
                    $course->location = request('loc_n'.$l.'_p'.$p);
                    $course->periode = $p;
                    $course->level = $l;

                    $course->comment = "";
                    $course->event_id = $event->id;

                    $course->save();
                }
            }   
        }

        return redirect('/admin/calendar')->with('success','Événement ajouter à l\'horaire');
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Event  $event
     * @return Response
     */
    public function show(Event $event)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param $id
     * @return Response
     */
    public function edit($id)
    {
        return view('admin.schedule.event.edit',['activity' => \App\Event::find($id)]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Event  $event
     * @return Response
     */
    public function update($id)
    {
        $event = Event::find($id);

        $event->name = request('name');
        $event->date_begin = request('begin');
        $event->date_end = request('end');
        $event->location = request('location');
        if(request('is_mandatory') != null){
            $event->is_mandatory = 1;
        } else {
            $event->is_mandatory  = 0;
        }
        $event->desc = request('desc');

        $event->save();

        if ($event->type == 1) {
            for ($l=1; $l <= \App\Config::getData('admin_level_in_schedule_nb'); $l++) {
                for ($p=1; $p <= \App\Config::getData('admin_periode_nb'); $p++) {

                    $course = new \App\Course();

                    foreach ($event->courses as $c)
                    {
                        if ($c->periode == $p)
                        {
                            if ($c->level == $l)
                            {
                                $course = $c;
                            }
                        }
                    }

                    $users = \App\User::all();
                    $instructor = 1;

                    foreach ($users as $user) {
                        if($user->fullname() == request('instruc_n'.$l.'_p'.$p))
                        {
                            $instructor = $user->id;
                        }
                    }

                    $course->name = request('name_n'.$l.'_p'.$p);
                    $course->user_id = $instructor;
                    $course->ocom = request('ocom_n'.$l.'_p'.$p);
                    $course->location = request('loc_n'.$l.'_p'.$p);
                    $course->periode = $p;
                    $course->level = $l;

                    $course->comment = "";
                    $course->event_id = $event->id;

                    $course->save();
                }
            }
        }

        return redirect('/admin/calendar')->with('success','Modification à l\'événement sauvegarder à l\'horaire');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Event  $event
     * @return Response
     */
    public function destroy(Event $event)
    {
        //
    }
}
