<?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') {
        setlocale(LC_ALL, 'pl', 'pl_PL', 'pl_PL.ISO8859-2', 'plk', 'polish', 'Polish');
        $user_id = Auth::id();
        $subjects = Subject::where('user_id', $user_id)->orderBy('name','ASC')->get();
        $subjects_grouped = $subjects->groupBy($groupBy);
        $rooms = Room::all();
        $weekdays = ['Poniedziałek', 'Wtorek', 'Środa', 'Czwartek', 'Piątek', 'Sobota', 'Niedziela'];
//        $types = ['Lecture', 'Excercises', 'Labs', 'Other'];
        $defaultTime = date("H:i");
        if(date('w')-1 < 0 ) {
            $defaultWeekday = $weekdays[6];
        } else {
            $defaultWeekday = "Poniedziałek";//$weekdays[date('w') - 1];
        }
        return view('user.user_subjects', ['subjects' => $subjects, 'weekdays' => $weekdays, 'subjects_grouped' => $subjects_grouped, 'grouped_by' => $groupBy, 'defaultTime' => $defaultTime, 'defaultWeekday' => $defaultWeekday, 'rooms' => $rooms]);
    }

    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()->back();
    }

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

    public function edit_subject($subject_id)
    {
        Subject::find($subject_id);
        return redirect()->back();
    }
}