<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\http\Request;
use Illuminate\http\Response;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Input;
use Hash;


use DB;


class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;


    public function postInsertData(Request $request)
    {

        $EmailAddress=$request['EmailAddress'];
        $Password=$request['Password'];
		$inserteed = DB::insert('insert into Person (EmailAddress,Password) values (?,?)',[$EmailAddress,$Password]);
        if($inserteed)
        {
			return response()->json(['status'=>'success']);   
        }
        else
        {
             return response()->json(['status'=>'Email Already Exist']);
        }
            
    }

    public function PersonSignup (Request $request)
    { 
		if(isset($request['EmailAddress']) && !empty($request['EmailAddress']) AND isset($request['Password']) && !empty($request['Password']) AND isset($request['Type']) && !empty($request['Type']) ) 
			{
				$EmailAddress=$request['EmailAddress'];
				$Password=$request['Password'];
				$Type=$request['Type'];
				
				try 
				{ 
					$inserteed = DB::insert('insert into Person (EmailAddress,Password,Type) values (?,?,?)',[$EmailAddress,$Password,$Type]);
					return response()->json(['status'=>'success']);   
				} 
				catch(\Illuminate\Database\QueryException $ex)
				{ 		
					return response()->json(['status'=>'failed','error'=>'Email Already Exist']);
				}


			}

		else 
			{
				return response()->json(['status'=>'failed','error'=>'Incomplete Parameters']);
			}
      
    }
	
	
	public function PersonSignin (Request $request)
    { 
		if(isset($request['EmailAddress']) && !empty($request['EmailAddress']) AND isset($request['Password']) && !empty($request['Password'])  ) 
			{
				$EmailAddress=$request['EmailAddress'];
				$Password=$request['Password'];
				
				try 
				{ 

                                        $users = DB::select('select * from Person where EmailAddress = ? AND Password = ?', [$EmailAddress, $Password]);
					//$users = DB::select("select * from Person  where EmailAddress ='$EmailAddress' AND Password='$Password'  ");
					if($users)
					{
                                                //$users->posts->pluck('person_id')
                                                 //$person_id = $users['person_id'];
                                                // echo $users ->person_id;
                                                //return $users ->person_id; 
						return response()->json(['status'=>'success','data'=>$users]);
					}
					else
					{
						return response()->json(['status'=>'failed','error'=>'Wrong Username or Password']);
					}	
					  
				} 
				catch(\Illuminate\Database\QueryException $ex)
				{ 		
					return response()->json(['status'=>'failed','error'=>'Sql Error']);
				}
			
			}

		else 
			{
				return response()->json(['status'=>'failed','error'=>'Incomplete Parameters']);
			}
      
    }
	
	
	public function PersonProfile (Request $request)
    { 
		if(isset($request['EmailAddress']) && !empty($request['EmailAddress']) AND isset($request['FirstName']) && !empty($request['FirstName']) 
			AND isset($request['LastName']) && !empty($request['LastName'])AND isset($request['CountryID']) && !empty($request['CountryID'])AND isset($request['MobileNumber']) && !empty($request['MobileNumber'])AND isset($request['Sex']) && !empty($request['Sex'])  ) 
			{
				$EmailAddress=$request['EmailAddress'];
				$FirstName=$request['FirstName'];
				$LastName=$request['LastName'];
				$CountryID=$request['CountryID'];
				$MobileNumber=$request['MobileNumber'];
				$Sex=$request['Sex'];
                                $progress= 100;
				$users = DB::select('select * from Person where EmailAddress = ?', [$EmailAddress]);
				if($users)
				{
					try 
					{ 
						$user_new = DB::update('update Person SET progress = ? , FirstName = ? , LastName = ?, CountryID = ? , MobileNumber= ? , Sex= ?  where EmailAddress = ?', [$progress,$FirstName,$LastName,$CountryID, $MobileNumber , $Sex ,$EmailAddress]);
						if ( $user_new)
						{
							return response()->json(['status'=>'success']);
						}
						else 
						{
							return response()->json(['status'=>'failed','error'=>'Sql Error']);
						}
					
					}
					
					catch(\Illuminate\Database\QueryException $ex)
					{ 		
						return response()->json(['status'=>'failed','error'=>'Sql Error']);
					}
					
									  
					//return response()->json(['status'=>'success','data'=>$users ]);
				}
				else
				{
					return response()->json(['status'=>'failed','error'=>'Wrong Email']);
				}	
					  
				
			
			}

		else 
			{
				return response()->json(['status'=>'failed','error'=>'Incomplete Parameters']);
			}
      
    }

public function PersonView(Request $request)
    {

		if(isset($request['EmailAddress']) && !empty($request['EmailAddress']))
		{
//echo "yahaa";
			$EmailAddress=$request['EmailAddress'];
			$Type=$request['Type'];
			$users = DB::select('select * from Person where EmailAddress = ? AND  Type = ?', [$EmailAddress,$Type]);
				if($users)
				{
					return response()->json(['status'=>'success','data'=>$users]);
				}
				else 
				{
					return response()->json(['status'=>'failed','error'=>'No Information Found']);
				}
			
		}
		else 
		{
			$Type=$request['Type'];
			$users = DB::select('select * from Person where  Type = ?', [$Type]);
			
				if($users)
				{
					return response()->json(['status'=>'success','data'=>$users]);
				}
				else 
				{
					return response()->json(['status'=>'failed','error'=>'No Information Found']);
				}
			
		}
        
    }
	
	
	public function PersonDelete(Request $request)
    {

		if(isset($request['EmailAddress']) && !empty($request['EmailAddress']))
		{
//echo "yahaa";
			$EmailAddress=$request['EmailAddress'];
			
			$users = DB::select('select * from Person where EmailAddress = ?', [$EmailAddress]);
				if($users)
				{
					try 
					{ 
						$flag =1;
						$user_new = DB::update('update Person SET DeletedFlag = ?  where EmailAddress = ?', [$flag,$EmailAddress]);
						if ( $user_new)
						{
							return response()->json(['status'=>'success']);
						}
						else 
						{
							return response()->json(['status'=>'failed','error'=>'Sql Error']);
						}
					
					}
					
					catch(\Illuminate\Database\QueryException $ex)
					{ 		
						return response()->json(['status'=>'failed','error'=>'Sql Error']);
					}
				}
				else 
				{
					return response()->json(['status'=>'failed','error'=>'Wrong Email']);
				}
			
		}
		else 
		{
			return response()->json(['status'=>'failed','error'=>'Incomplete Parameters']);
			
		}
        
    }

}

