<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use App\Models\User;
use App\Models\Degree;
use App\Models\Patient;
use App\Models\Award;
use App\Models\Experience;
use App\Models\Setting;
use App\Models\Department;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Mail;
use Carbon\Carbon;
use App\Mail\sendmail;
use Browser;
use Session;
use Image;
use Redirect;

class RegisterController extends Controller
{
    public function register() {
        $data['set'] = Setting::first();
        $data['department'] = Department::orderBy('id', 'DESC')->get();
        return view('/auth/register', $data);
    }
    public function patientregister() {
        $data['set'] = Setting::first();
        $data['department'] = Department::orderBy('id', 'DESC')->get();
        return view('/auth/patient-register', $data);
    }
    public function referral($id) {
        return redirect()->away('https://google.com/'.$id);
    }

    public function registersubmit(Request $request) {
        $set = Setting::first();
        $validate = Validator::make($request->all(), [
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'password' => ['min:6', 'required', 'string'],
            'phone' => ['required', 'string', 'max:255'],
            'first_name' => ['required', 'string', 'max:255'],
            'last_name' => ['required', 'string', 'max:255'],
            'gender' => ['required', 'string', 'max:255'],
            'username' => ['required', 'string', 'max:255'],
        ]);
        if ($validate->fails()) {
            return back()->with('alert', $validate->errors());
        }
        else {
            // if(!$request->accept_terms) {
            //     return back()->with('alert', 'You must accept terms before you register');
            // }
            $ch = User::whereemail($request->email)->count();
            if($ch >0) {
                return back()->with('alert', 'Email already exists');
            }
            $user = new User();
                $user->email = $request->email;
                $user->phone = $request->phone;
                $user->first_name = $request->first_name;
                $user->last_name = $request->last_name;
                $user->gender = $request->gender;
                $user->department = $request->department;
                $user->username = $request->username;
                $user->about = $request->about;
                $user->mpd = $request->mpd;
                $user->certify = $request->certify;
                $user->dob = $request->dob;
                $user->license = $request->license;
                $user->password = Hash::make($request->password);
                $fer = mt_rand(100000,999999);
                $user->email_verify_code = $fer;
                $user->email_verify_status = 0;
                $user->balance = 0;
                $user->code = Str::random(8);
                if ($request->hasFile('image')) {
                    if($request->file('image')->getClientOriginalExtension() == 'jpg' || $request->file('image')->getClientOriginalExtension() == 'png' || $request->file('image')->getClientOriginalExtension() == 'jpeg') {
                    $image = $request->file('image');
                    $filename = time() . '_profile.png';
                    $location = 'images/user/' . $filename;
                    $image->move('images/user', $filename);
                    $user->image = $location;
                    }
                }
                $ty = 10;
                $user->expires_at = Carbon::now()->addMinutes($ty);
                $user->save();

                if($request->deg_num > 0) {
                    $tot = $request->deg_num;
                    $r = 1;
                    $ty = 0;
                    $der = '';
                    while($r<=$tot) {
                    if($r == '1') {
                        $tr = new Degree();
                        $tr->school = $request->deg_ins_1;
                        $tr->title = $request->deg_title_1;
                        $tr->user_id = $user->id;
                        $tr->save();
                        
                    }
                    else if($r == '2') {
                        $tr = new Degree();
                        $tr->school = $request->deg_ins_2;
                        $tr->title = $request->deg_title_2;
                        $tr->user_id = $user->id;
                        $tr->save();
                    }
                    else if($r == '3') {
                        $tr = new Degree();
                        $tr->school = $request->deg_ins_3;
                        $tr->title = $request->deg_title_3;
                        $tr->user_id = $user->id;
                        $tr->save();
                    }
                    else if($r == '4') {
                        $tr = new Degree();
                        $tr->school = $request->deg_ins_4;
                        $tr->title = $request->deg_title_4;
                        $tr->user_id = $user->id;
                        $tr->save();
                    }
                    else if($r == '5') {
                        $tr = new Degree();
                        $tr->school = $request->deg_ins_5;
                        $tr->title = $request->deg_title_5;
                        $tr->user_id = $user->id;
                        $tr->save();
                    }
                    else if($r == '6') {
                        $tr = new Degree();
                        $tr->school = $request->deg_ins_6;
                        $tr->title = $request->deg_title_6;
                        $tr->user_id = $user->id;
                        $tr->save();
                    }
                    
                    $r++;
                }
            }

                if($request->exp_num > 0) {
                    $tot = $request->exp_num;
                    $r = 1;
                    $ty = 0;
                    $der = '';
                    while($r<=$tot) {
                    if($r == '1') {
                        $tr = new Experience();
                        $tr->details = $request->exp_title_1;
                        $tr->user_id = $user->id;
                        $tr->save();
                        
                    }
                    else if($r == '2') {
                        $tr = new Experience();
                        $tr->details = $request->exp_title_2;
                        $tr->user_id = $user->id;
                        $tr->save();
                    }
                    else if($r == '3') {
                        $tr = new Experience();
                        $tr->details = $request->exp_title_3;
                        $tr->user_id = $user->id;
                        $tr->save();
                    }
                    else if($r == '4') {
                        $tr = new Experience();
                        $tr->details = $request->exp_title_4;
                        $tr->user_id = $user->id;
                        $tr->save();
                    }
                    else if($r == '5') {
                        $tr = new Experience();
                        $tr->details = $request->exp_title_5;
                        $tr->user_id = $user->id;
                        $tr->save();
                    }
                    else if($r == '6') {
                        $tr = new Experience();
                        $tr->details = $request->exp_title_6;
                        $tr->user_id = $user->id;
                        $tr->save();
                    }
                    
                    $r++;
                }
            }
                if($request->awd_num > 0) {
                    $tot = $request->awd_num;
                    $r = 1;
                    $ty = 0;
                    $der = '';
                    while($r<=$tot) {
                    if($r == '1') {
                        $tr = new Award();
                        $tr->title = $request->awd_title_1;
                        $tr->user_id = $user->id;
                        $tr->save();
                        
                    }
                    else if($r == '2') {
                        $tr = new Award();
                        $tr->title = $request->awd_title_2;
                        $tr->user_id = $user->id;
                        $tr->save();
                    }
                    else if($r == '3') {
                        $tr = new Award();
                        $tr->title = $request->awd_title_3;
                        $tr->user_id = $user->id;
                        $tr->save();
                    }
                    else if($r == '4') {
                        $tr = new Award();
                        $tr->title = $request->awd_title_4;
                        $tr->user_id = $user->id;
                        $tr->save();
                    }
                    else if($r == '5') {
                        $tr = new Award();
                        $tr->title = $request->awd_title_5;
                        $tr->user_id = $user->id;
                        $tr->save();
                    }
                    else if($r == '6') {
                        $tr = new Award();
                        $tr->title = $request->awd_title_6;
                        $tr->user_id = $user->id;
                        $tr->save();
                    }
                    
                    $r++;
                }
            }
                

                $msg = 'Your OTP code is: <b>'. $fer .'</b>. It expires in 10 Minutes!';
                $subject = 'Email Verification';
                //Mail::to($request->email)->send(new sendmail($msg, $set, $subject));
                // $data['user'] = $user;
                // $data['set'] = Setting::first();
                return redirect()->route('user.verify.lk', $user->email);
       }
    }

    public function resendotp($id) {
        
        $data['set'] = $set = Setting::first();
        $user = User::whereemail($id)->first();
        $fer = mt_rand(100000,999999);
        $user->email_verify_code = $fer;
        $ty = 10;
        $user->expires_at = Carbon::now()->addMinutes($ty);        
        $user->save();
        $msg = 'Your OTP code is: '. $fer;
        $subject = 'Email Verification';
        Mail::to($id)->send(new sendmail($msg, $set, $subject));
        $data['user'] = $user;
        return redirect()->route('user.verify.lk', $id);
        
        
    }
    public function redverify($id) {
        $data['set'] = $set = Setting::first();
        $user = User::whereemail($id)->first();
        $data['user'] = $user;
        return view('/auth/verify', $data);
    }
    public function verifyotp(Request $request) {
        $userp = User::whereemail($request->email)->count();
        if($userp < 1) {
            return back()->with('alert', 'Invalid Email');
        }
        else {
            $user = User::whereemail($request->email)->first();
            
            if($user->email_verify_code == $request->otp) {
                
                if(Carbon::now() < $user->expires_at) {
                    $user->email_verify_status = 1;
                    $user->save();
                    
                
                    
                    return redirect()->route('login')->with('success', 'Email verified successfully!');
                    
                }
                else {
                    return back()->with('alert', 'Code is no longer valid'); 
                }
            }
            else {
                return back()->with('alert', 'Invalid OTP code');
            }
        }
    }
    
    
    
    //Patient Registration
    public function patientregistersubmit(Request $request) {
        $set = Setting::first();
        $validate = Validator::make($request->all(), [
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'password' => ['min:6', 'required', 'string'],
            'phone' => ['required', 'string', 'max:255'],
            'first_name' => ['required', 'string', 'max:255'],
            'last_name' => ['required', 'string', 'max:255'],
            'gender' => ['required', 'string', 'max:255'],
            'username' => ['required', 'string', 'max:255'],
        ]);
        if ($validate->fails()) {
            return back()->with('alert', $validate->errors());
        }
        else {
            $ch = Patient::whereemail($request->email)->count();
            if($ch >0) {
                return back()->with('alert', 'Email already exists');
            }
            $user = new Patient();
                $user->email = $request->email;
                $user->phone = $request->phone;
                $user->first_name = $request->first_name;
                $user->last_name = $request->last_name;
                $user->gender = $request->gender;
                $user->username = $request->username;
                $user->about = $request->about;
                $user->dob = $request->dob;
                $user->password = Hash::make($request->password);
                $fer = mt_rand(100000,999999);
                $user->email_verify_code = $fer;
                $user->email_verify_status = 0;
                $user->balance = 0;
                $user->code = Str::random(8);
                if ($request->hasFile('image')) {
                    if($request->file('image')->getClientOriginalExtension() == 'jpg' || $request->file('image')->getClientOriginalExtension() == 'png' || $request->file('image')->getClientOriginalExtension() == 'jpeg') {
                    $image = $request->file('image');
                    $filename = time() . '_profile.png';
                    $location = 'images/patient/' . $filename;
                    $image->move('images/patient', $filename);
                    $user->image = $location;
                    }
                }
                $ty = 10;
                $user->expires_at = Carbon::now()->addMinutes($ty);
                $user->save();


                $msg = 'Your OTP code is: <b>'. $fer .'</b>. It expires in 10 Minutes!';
                $subject = 'Email Verification';
                //Mail::to($request->email)->send(new sendmail($msg, $set, $subject));
                // $data['user'] = $user;
                // $data['set'] = Setting::first();
                return redirect()->route('patient.user.verify.lk', $user->email);
       }
    }

    public function patientresendotp($id) {
        
        $data['set'] = $set = Setting::first();
        $user = Patient::whereemail($id)->first();
        $fer = mt_rand(100000,999999);
        $user->email_verify_code = $fer;
        $ty = 10;
        $user->expires_at = Carbon::now()->addMinutes($ty);        
        $user->save();
        $msg = 'Your OTP code is: '. $fer;
        $subject = 'Email Verification';
        Mail::to($id)->send(new sendmail($msg, $set, $subject));
        $data['user'] = $user;
        return redirect()->route('patient.user.verify.lk', $id);
        
        
    }
    public function patientredverify($id) {
        $data['set'] = $set = Setting::first();
        $user = Patient::whereemail($id)->first();
        $data['user'] = $user;
        return view('/auth/patient-verify', $data);
    }
    public function patientverifyotp(Request $request) {
        $userp = Patient::whereemail($request->email)->count();
        if($userp < 1) {
            return back()->with('alert', 'Invalid Email');
        }
        else {
            $user = Patient::whereemail($request->email)->first();
            
            if($user->email_verify_code == $request->otp) {
                
                if(Carbon::now() < $user->expires_at) {
                    $user->email_verify_status = 1;
                    $user->save();
                    
                
                    
                    return redirect()->route('patient.login')->with('success', 'Email verified successfully!');
                    
                }
                else {
                    return back()->with('alert', 'Code is no longer valid'); 
                }
            }
            else {
                return back()->with('alert', 'Invalid OTP code');
            }
        }
    }
}
