<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models;

class LoginController extends Controller
{
    public function getLoginPage(){
        $designation = Models\Designation::pluck('name', 'id');
        $data = [
            'designation' => $designation
        ];

    	return view('login.index', $data);
    }

    public function handleLogin(Request $request){
    	$this->validate($request, ['email' => 'required', 'password' => 'required']);

    	$logged_in = Models\SystemUser::with('designation_info')->where('email', $request->email)->where('password', md5($request->password))->first();
    	
        if(empty($logged_in)){
    	  return redirect("/")->withErrors(array("Incorrect Email/Password. Check and try again!"))->withInput();
    	}

    	$request->session()->regenerate(true);
    	$request->session()->put('current_user.id', $logged_in->id);
    	$request->session()->put('current_user.name', $logged_in->name);
    	$request->session()->put('current_user.email', $logged_in->email);
    	$request->session()->put('current_user.phone', $logged_in->phone);
        $request->session()->put('current_user.designation', $logged_in->designation_info->name);

        \Log::info($logged_in->name . ' successfully logged in at : ' . date('Y-m-d H:i:s'));
    	// return redirect(url('dashboard'));

        $activity_arr = [
            'type' => 'staff',
            'content' => "User ID : " . $logged_in->id . " (" . $logged_in->name . ") Logged In",
            'user_id' => $logged_in->id,
            'ip_address' => \Request::ip(),
            'device' => $request->header('User-Agent')
        ];
        $retval = Models\UserActivity::create($activity_arr);
        return redirect(url('/'));
    }

    public function handle_logout(Request $request) {
        $user_id = session('current_user.id');
        $username = session('current_user.name');
        $content  = "User ID : " . $user_id . " (" . $username . ") Logged Out";

        $request->session()->forget('current_user');
        $request->session()->flush();
        $request->session()->regenerate(true);

        $this->logUsersActivity($type = 'staff', $content);
        return redirect("/");
    }

    public function registerAccount(Request $request){
        dd($request->all());
        $request->validate([
            'name' => 'required',
            'designation' => 'required',
            'email' => 'required',
            'phone' => 'required',
            'password' => 'required',
            'confirm_password' => 'same:password',            
        ]);

        $make_account = [
            'name' => $request->name,
            'designation' => $request->designation,
            'email' => $request->email,
            'phone' => $request->phone,
            'password' => md5($request->password)
        ];

        $inserted = Models\Account::create($make_account);
        Session::flash('success_message', 'Account successfully added');
        return redirect(url('/'));
    }
}
