<?php


namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use App\Models\Admin;
use App\Models\User;
use App\Models\Patient;
use App\Models\Setting;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Mail;
use Carbon\Carbon;
use Browser;
use Session;
use Image;
use Redirect;

class LoginController extends Controller
{
    public function adminlogin() {
        $ch = Admin::orderBy('id', 'DESC')->count();
        if($ch<1) {
            $tr = new Admin();
            $tr->username = 'Admin';
            $tr->email = 'info@admin.com';
            $tr->password = Hash::make('Admin');
            $tr->save();
        }
        $data['set'] = Setting::first();
        return view('/auth/admin-login', $data);
    }
    public function loginsubmit(Request $request) {
        $validate = Validator::make($request->all(), [
            'email' => ['required', 'string', 'max:255'],
            'password' => ['required', 'string'],
            
        ]);
        if ($validate->fails()) {
            return back()->with('alert', $validate->errors()->first());
        }
        else {
        
            if (Auth::guard('user')->attempt([
                'email' => $request->email,
                'password' => $request->password
            ])) {
                $user = User::whereid(Auth::user()->id)->first();
                if($user->email_verify_status == 0) {
                 $data['user'] = $user;
                return redirect()->route('user.verify.lk', $user->email);
                }
                if($user->status == 0) {
                    
                   return back()->with('alert', 'Your account is still under review');
                   }
                   if($user->status == 2) {
                    
                    return back()->with('alert', 'Your account is currently suspended');
                    }
                
               return redirect()->intended('user/dashboard');
            }
            else {
                return back()->with('alert', 'Invalid Credentials');
            }
       }
    }

    public function login() {
        $data['set'] = Setting::first();
        return view('/auth/login', $data);
    }
    
    
    //Patient Login
    public function patientloginsubmit(Request $request) {
        $validate = Validator::make($request->all(), [
            'email' => ['required', 'string', 'max:255'],
            'password' => ['required', 'string'],
            
        ]);
        if ($validate->fails()) {
            return back()->with('alert', $validate->errors()->first());
        }
        else {
        
            if (Auth::guard('patient')->attempt([
                'email' => $request->email,
                'password' => $request->password
            ])) {
                $user = Patient::whereid(Auth::user()->id)->first();
                if($user->email_verify_status == 0) {
                 $data['user'] = $user;
                return redirect()->route('patient.user.verify.lk', $user->email);
                }
                if($user->status == 0) {
                    
                   return back()->with('alert', 'Your account is still under review');
                   }
                   if($user->status == 2) {
                    
                    return back()->with('alert', 'Your account is currently suspended');
                    }
                
               return redirect()->intended('patient/dashboard');
            }
            else {
                return back()->with('alert', 'Invalid Credentials');
            }
       }
    }

    public function patientlogin() {
        $data['set'] = Setting::first();
        return view('/auth/patient-login', $data);
    }
    
    public function adminloginsubmit(Request $request) {
        $validate = Validator::make($request->all(), [
            'username' => ['required', 'string', 'max:255'],
            'password' => ['required', 'string'],
            
        ]);
        if ($validate->fails()) {
            return back()->with('alert', $validate->errors()->first());
        }
        else {
        
            if (Auth::guard('admin')->attempt([
                'username' => $request->username,
                'password' => $request->password
            ])) {
               return redirect()->intended('admin/dashboard');
            }
            else {
                return back()->with('alert', 'Invalid Credentials');
            }
       }
    }

}
