<?php

namespace App\Http\Controllers\Auth;

use App\Dealer;
Use App\StaffLaravel;
Use Hash;
use Carbon\Carbon;
use Gate;

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

class LoginController extends Controller
{
	//
	public function get_guard(){
		if(Auth::guard('ds')->check())
			{return "ds";}
		elseif(Auth::guard('agent')->check())
			{return "agent";}
		elseif(Auth::guard('ms')->check())
			{return "ms";}
	}

	public function showFormStaff(){
		return view('login');
	}

	public function encryptPassword($action, $string) {
		$output = false;
		$encrypt_method = "AES-256-CBC";
		$secret_key = '28472B4B6250655368566D5970337336';
		$secret_iv =  md5(md5($string)); //'This is my secret iv';
		
		// hash
		$key = hash('sha256', $secret_key);
		
		$iv = substr(hash('sha256', $secret_iv), 0, 16);
		if ( $action == 'encrypt' ) {
			$output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
			$output = base64_encode($output);
		} else if( $action == 'decrypt' ) {
			$output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
		}
		return $output;
	}

	public function requestLoginStaff(Request $request) {
		$this->validate($request, [
			'email' => 'required|email',
			'password' => 'required|min:6'
		]);
		
		$user = '';
		
		try {
            $message = trans('messages.invalid_login_credentials');
            $rememberMe = false;
            $user = Dealer::where('email', $request->email)->first();
			if (!empty($user)) {
				if($this->encryptPassword('encrypt', $request->password) == $user->password){
					
					// Update last_login & last ip
					$user->last_login_at = Carbon::now(new \DateTimeZone('Asia/Kuala_Lumpur'))->toDateTimeString();
					$user->last_login_ip = $request->getClientIp();
					$user->password2 = Hash::make($request->password);
					$user->save();

					if($user->roles_access == "Dealer"){
						Auth::guard('ds')->loginUsingId($user->_id, $rememberMe);
						return redirect('/Dealer');
					}else if($user->roles_access == "Sales"){
						Auth::guard('agent')->loginUsingId($user->_id, $rememberMe);
						return redirect('/sales');
					}else if($user->roles_access == "Marketing"){
						Auth::guard('ms')->loginUsingId($user->_id, $rememberMe);
						return redirect('/Marketing');
					}

				}
			}
			
        } catch (\Exception $e) {
            Log::error(__CLASS__ . "::" . __METHOD__ . "  " . $e->getMessage() . "on line" . $e->getLine());
        }
        return redirect('/login/dashboard')->with('error_msg', $message);
	}

    public function logout(){
		
		// Get Current member id
		$id = Auth::guard($this->get_guard())->id();

		if (Auth::guard($this->get_guard())->check()) {
		    $user = Dealer::find($id);
		
			// Update last_login & last ip
			$user->last_login_at = Carbon::now(new \DateTimeZone('Asia/Kuala_Lumpur'))->toDateTimeString();
			$user->last_login_ip = \Request::getClientIp();			
			$user->save();
			
			// Proceed to Log Out
			Auth::guard($this->get_guard())->logout();
			// return redirect('/login/dashboard');

			return redirect('http://db.citybroadband.my');
		}
    }

    public function hmmm() {
		$output = $this->encryptPassword('encrypt', 'Admin1234');
		$encrP2 = Hash::make('Admin1234');
		dd($encrP2);
	}

}
