<?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|alpha|min:3|max:45',
            'surname' => 'required|alpha|min:3|max:45',
            '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.',
                'alpha' => ':attribute może zawierać tylko litery.',
                'alpha_num' => ':attribute może zawierać tylko litery i cyfry.',
                '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')
            ->select('trainingsfirefighters.id','trainings.trainingName','trainingsfirefighters.dateOfComplete', 'trainingsfirefighters.dateOfExpiry')
            ->get();
            //DB::table('trainings')->where("fireStationID", '=', auth()->user()->fireStationID)
                //->whereNull('deleted_at')->get();
            return view("userTrainings", ["userTrainings" => $userTrainings]);
        }
        else{
            return redirect()->to('/login');;
        }
    }
}