<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;
use Session;
use DB;
class Login_con extends Controller
{
    public function index()
{
	return view('auth.create_acc');
}
public function login()
{
	return view('auth.login');
}
public function create(Request $r)
{
	
	$name=$r->name;
	$type=$r->type;
	$email=$r->email;
	$phone=$r->phone;
	$password=$r->password;
	$check_email=  DB::table('users')->where('email',$email)->get();

	if(count($check_email)>0) ////this is to check duplicate email insertion
	{
		return redirect('/create_account')->with('msg','Email Exists');

	}else{
       
	$Login= new User;
 
		$Login->usertype= $type;
		$Login->name= $name;
		$Login->email= $email;   	
		$Login->password= $password;  
		$Login->phone= $phone; 
        
		$created=$Login->save();

		if($created){
           
		$Type = $Login->usertype;
			if($Type=='owner'){
				return redirect('/patnerwithus');
			}
			else{

			
			return redirect('/login')->with('msg','Account Created Successfully. Please LogIn !');
			}
		}
        else{
           
            return view('welcome22');
        }

	}
}
public function check_user(Request $r)
		{
			// $r->session()->forget('owner_id');
			// $r->session()->forget('admin_id');
			// $r->session()->forget('owner_email');
			// $r->session()->forget('admin_email');
			// $r->session()->forget('owner_name');
			// $r->session()->forget('admin_name');
			
			$email=$r->email;
			$password=$r->password;

			
			$txtCheck_User=  DB::table('users')->where('email',$email)->where('password',$password)->get();

			if(count($txtCheck_User)>0){
				$Admin_count =DB::table('users')->where('usertype','admin')->where('email',$email)->get() ;
				$Owner_count =DB::table('users')->where('usertype','owner')->where('email',$email)->get() ;
				if(count($Admin_count)>0)
				{
					// $r->session()->forget(['owner_id', 'owner_email'.'owner_name']);
					// $r->session()->forget(['admin_id', 'admin_name'.'admin_email']);
					$r->session()->put('admin_id',$txtCheck_User[0]->id);
					$r->session()->put('admin_name',$txtCheck_User[0]->name);
					$r->session()->put('admin_email',$txtCheck_User[0]->email);
				
					
					return redirect('/admin/dashboard');
				}
				else if(count($Owner_count)>0){
					// $r->session()->forget(['owner_id', 'owner_email'.'owner_name']);
					// $r->session()->forget(['admin_id', 'admin_name'.'admin_email']);
				

					$r->session()->put('owner_id',$txtCheck_User[0]->id);
					$r->session()->put('owner_name',$txtCheck_User[0]->name);
					$r->session()->put('owner_email',$txtCheck_User[0]->email);
				
						$id = Session::get('owner_id');
					// $id = $Owner_count->id;
					// print_r($id);
					// exit;
					$restaurants = DB::table('restaurants')->where('user_id',$id)->first();
				
					$resid = $restaurants->id;
					$restaurants1 = DB::table('restaurants')->where('id',$resid)->first();
					// echo $restaurants1->name;
					// exit;
					session()->put('reslogo',$restaurants1->image);
					session()->put('resname',$restaurants1->name);
                    // return redirect('restaurant-owner/dashboard');
 
					return redirect('restaurant-owner/dashboard');
				}
			
			}
			else{
				return redirect()->back()->with('msg', 'Email or Password does not match');  
			}

            

		}


        public function logout(Request $r)
		{
			// echo "hy";
			// exit;
			$r->session()->forget('user_id');
			$r->session()->forget('user_name');

			return redirect('/login');

		}

		public function patnerwithus()
		{	
		
			return view('auth.partnerwithus');
		}


		public function create_patnerwithus(Request $request){
$email = $request->email;

			$check_email=  DB::table('users')->where('email',$email)->get();

	if(count($check_email)>0) ////this is to check duplicate email insertion
	{
		
		
		DB::table('patnerwithuses')->insert([
			'name'=>$request->name,
			'address'=>$request->address,
			'first_name'=>$request->first_name,
			'sur_name'=>$request->sur_name,  
			'phone'=>$request->phone,
			'res_phone'=>$request->res_phone,
			'email'=>$request->email,
			'no_of_loc'=>$request->no_of_loc, 
			'type_of_cuisine'=>$request->type_of_cuisine, 
			'user_id'=>'1', 

		]);
		return redirect('/login');

	}else{
       
		return redirect('/partnerwithus')->with('msg','Please Enter Registered Email');
		}
}
}