<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\User;
use App\Rules\Pesel;

class userProfileController extends Controller
{
	public function create(){
	    if(auth()->user() != null && auth()->user()->fireStationID != null ){
	            $userData = DB::table('users')->where("users.id", '=', auth()->user()->id)
	            	->leftJoin('ranks', 'users.degreeID', '=', 'ranks.id')
					->leftJoin('unitFunctions', 'users.functionID', '=', 'unitFunctions.id')
					->select('users.id','users.name', 'users.surname', 'users.PESEL','users.phoneNumber', 'users.email', 'ranks.rank', 'unitFunctions.unitFunction', 'users.number')
					->first();
	            return view("userProfile", ["userData" => $userData]);
	        } else{
	            return redirect()->to('/login');
	        }
    }

     public function editForm()
    {
        if(auth()->user() != null && auth()->user()->fireStationID != null )
        {

            $userData = DB::table('users')->where("id", '=', auth()->user()->id)
	                ->first();

                return view("userProfileEdit", ["userData" => $userData]);
        }
        else
            return redirect()->to('/login');

    }

    public function update(){
        $this->validate(request(), [
            'name' =>'required|min:2|max:45|regex:/^[A-Za-zżźćńółęąśŻŹĆĄŚĘŁÓŃ\040\x27-]+$/',
            'surname' =>'required|min:2|max:45|regex:/^[A-Za-zżźćńółęąśŻŹĆĄŚĘŁÓŃ\040\x27-]+$/',
            'PESEL' => new Pesel,
            'phoneNumber' => 'required|digits:9',
            'email' => 'required|email|unique:users,email,'.auth()->user()->id, //wymagaj unikalnego adresu email ale pozwól na zachowanie starego adresu
        ],
            [
                'required' => ':attribute jest wymagany(e).',
                'min' => ':attribute musi mieć przynajmniej :min znaki.',
                'max' => ':attribute musi mieć nie więcej niż :max znaków.',
                'regex' => ':attribute może zawierać tylko litery, spacje, myślniki i apostrofy',
                'digits' => ':attribute musi składać się z :digits cyfr.',
                'unique' =>':attribute jest już zajęty.',
                'confirmed' =>':attribute się nie zgadza.',
                'email' => 'Niepoprawny adres e-mail.'
            ]);

        $request = request();
        $user = User::find( $request->userID);
        $user-> name = $request->name;
        $user-> surname = $request->surname;
        $user-> PESEL = $request->PESEL;
        $user-> phoneNumber = $request->phoneNumber;
        $user-> email = $request->email;
        $user-> changingID = auth()->user()-> id;
        $user->save();

        return redirect()->to('/userprofile');;
    }

    public function userTrainings(){

        if(auth()->user() != null && auth()->user()->fireStationID != null ){
            $userTrainings = DB::table('trainingsFirefighters')->where("trainingsFirefighters.firefighterID", '=', auth()->user()->id)
            ->leftJoin('trainings', 'trainingsFirefighters.trainingID', '=', 'trainings.id')
            ->whereNull('deleted_at')
            ->select('trainingsFirefighters.id','trainings.trainingName','trainingsFirefighters.dateOfComplete', 'trainingsFirefighters.dateOfExpiry')
            ->get();
            return view("userTrainings", ["userTrainings" => $userTrainings]);
        }
        else{
            return redirect()->to('/login');;
        }
    }

    public function userDecorations(){
        if(auth()->user() != null && auth()->user()->fireStationID != null ){
            $userDecorations = DB::table('decorationsFirefighters')->where("decorationsFirefighters.firefighterID", '=', auth()->user()->id)
                ->whereNull('decorationsFirefighters.deleted_at')
                ->leftJoin('decorations', 'decorationsFirefighters.decorationID', '=', 'decorations.id')
                ->get();
            return view("userDecorations", ["userDecorations" => $userDecorations]);
        } else{
            return "Brak dostepu";
        }
    }
}
