<?php

namespace App\Http\Controllers\User;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

use App\Subject;
use App\Room;

class UserSubjectsController extends Controller
{
    public function index($groupBy='weekday')
    {
        $user_id = Auth::id();
        $subjects = Subject::where('user_id', $user_id)->get();
        $subjects_grouped = $subjects->groupBy($groupBy);
        $rooms = Room::all();
        $weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
        $types = ['Lecture', 'Excercises', 'Labs', 'Other'];
        return view('user.user_subjects', ['subjects' => $subjects, 'rooms' => $rooms, 'weekdays' => $weekdays,
            'types' => $types, 'subjects_grouped' => $subjects_grouped, 'grouped_by' => $groupBy]);
    }

    public function add_subject(Request $request)
    {
        $name = $request->input('name');
        $type = $request->input('type');
        $weekday = $request->input('weekday');
        $time = $request->input('time');
        $room_id = $request->input('room_id');
        $user_id_n = Auth::id();
        Subject::create([
            'name' => $name,
            'type' => $type,
            'weekday'=> $weekday,
            'time' => $time,
            'room_id' => $room_id,
            'user_id' => $user_id_n
        ]);
        return redirect(route('user_subjects'));
    }

    public function delete_subject($subject_id)
    {
        Subject::find($subject_id)->delete();
        return redirect(route('user_subjects'));
    }

    public function edit_subject($subject_id)
    {
        Subject::find($subject_id);
        return redirect(route('user_subjects'));
    }
}