<?php

namespace App\Http\Controllers\Auth;

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

use App\Staff;
use App\Model\StaffDetail;

Use Hash;
use Carbon\Carbon;
use Gate;

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

	public function showFormAdmin(){
		return view('login_cs_co');
	}

	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 requestLoginAdmin(Request $request) {
		$this->validate($request, [
			'email' => 'required|email'
			// 'password' => 'required|min:6'
		]);
		
		$user = '';
		$roles_access = '';
		if($request->roles == 'contractor'){
			$roles_access = 'Contractor';
		}else if($request->roles == 'customer-service'){
			$roles_access = 'Customer Service';
		}else if($request->roles == 'marketing'){
			$roles_access = 'Marketing';
		}

		try {
            $message = trans('messages.invalid_login_credentials');
            $rememberMe = false;
           
            $user = Staff::where('email', $request->email)->where('roles_access',$roles_access)->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->save();

					if($user->roles_access == "Contractor"){
						Auth::guard('contractor')->loginUsingId($user->_id, $rememberMe);
						if (Carbon::now()->diffInDays($user->created_at) >= 90) {
							return $this->expired($user->_id);
						}
						else{
						return redirect('/contractor');
						}
					}
					else if($user->roles_access == "Customer Service"){
						Auth::guard('cs')->loginUsingId($user->_id, $rememberMe);
						if (Carbon::now()->diffInDays($user->created_at) >= 90) {
							return $this->expired($user->_id);
						}else{
							return redirect('/customer-service');
						}
					}else if($user->roles_access == "Developer"){
						Auth::guard('cs')->loginUsingId($user->_id, $rememberMe);
						if (Carbon::now()->diffInDays($user->created_at) >= 90) {
							return $this->expired($user->_id);
						}else{
							return redirect('/customer-service');
						}
					}else if($user->roles_access == "Marketing"){
						Auth::guard('ms')->loginUsingId($user->_id, $rememberMe);
						if (Carbon::now()->diffInDays($user->created_at) >= 90) {
							return $this->expired($user->_id);
						}else{
							return redirect('/marketing');
						}
					}
				}
			}
        } catch (\Exception $e) {
            Log::error(__CLASS__ . "::" . __METHOD__ . "  " . $e->getMessage() . "on line" . $e->getLine());
        }
        return redirect('/login/dashboard/admin')->with('error_msg', $message);
	}

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

		if (Auth::guard($this->get_guard())->check()) {
		    $user = Staff::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('/');
		}
	}
	public function expired($_id)
    {
		$user = Staff::where('_id',$_id)->first();
        return view('expiredPassword', compact ('user'));
    }

    public function postExpired(Request $request)
    {
		$user = Staff::where('_id',$request->_id)->first();
		
		$user->password = $this->encryptPassword('encrypt', $request->password);
		$user->created_at = Carbon::now()->toDateTimeString();
		$user->save();

        if($user->roles_access == "Contractor"){
			return redirect('/contractor');
		}
		else if($user->roles_access == "Customer Service"){
			return redirect('/customer-service');
		}else if($user->roles_access == "Developer"){
			return redirect('/customer-service');
		}else if($user->roles_access == "Marketing"){
			return redirect('/marketing');
		}
    }
	
}
