<?php
	include("../../connection/Functions.php");
	$operation = new Functions();
	session_start();
//add
	if(isset($_POST['email']) && isset($_POST['fullname']) && isset($_POST['phone']) && isset($_POST['password']) && !empty($_POST['email']) && !empty($_POST['fullname']) && !empty($_POST['phone']) && !empty($_POST['password'])){
		$email = addslashes($_POST['email']);
		$fname = addslashes($_POST['fullname']);
		$phone = addslashes($_POST['phone']);
		$pass = addslashes($_POST['password']);

        $table = 'users';
		//check email if already taken
		$countEmail = $operation->countAll("SELECT *FROM users WHERE email='$email'");
		if($countEmail >0){
			echo json_encode(array("code"=>2,"msg"=>"Email already taken"));
		}else{
			$password=password_hash($pass, PASSWORD_DEFAULT);
			$data = [
				'fullname'=>"$fname",
				'email'=>"$email",
				'phone'=>"$phone",
				'password'=>"$password",
				'account_status'=>"0",
			];
			if($operation->insertData($table,$data) == 1){
				//get user id
				$getUser = $operation->retrieveSingle("SELECT * FROM `users` WHERE email='$email'");
				echo json_encode(array("code"=>1,"msg"=>"Success, refreshing"));
			}else{
				echo json_encode(array("code"=>2,"msg"=>"An error occured, please try again later"));
			}
		}
        
        
    //edit    
}elseif(isset($_POST['efullname']) && isset($_POST['eUid']) && isset($_POST['eemail']) &&  isset($_POST['ephone']) && !empty($_POST['eUid']) && !empty($_POST['eemail']) && !empty($_POST['ephone']) && !empty($_POST['efullname']) ){
        
    $email = addslashes($_POST['eemail']);
    $fullname = addslashes($_POST['efullname']);
    $phone = addslashes($_POST['ephone']);
    $uid = addslashes($_POST['eUid']);
        
      
    
    
    $query = "SELECT * FROM `users` WHERE user_id = '$uid'";
    $count = $operation->countAll($query);

    if($count == 1){
        $table = "users";
        $where  = "user_id = '$uid'";
        
       if(isset($_POST['epassword']) && !empty($_POST['epassword'])){
         $password = password_hash(addslashes($_POST['epassword']), PASSWORD_DEFAULT);
           $data = [
				'fullname'=>"$fullname",
				'email'=>"$email",
				'phone'=>"$phone",
				'password'=>"$password",
			];
           if($operation->updateData($table,$data,$where) == 1){
               echo json_encode(array("code"=>1,"msg"=>"record saved!"));
           }else{
               echo json_encode(array("code"=>2,"msg"=>"Something went wrong while saving!"));
           }
       }else{
         $data = [
				'fullname'=>"$fullname",
				'email'=>"$email",
				'phone'=>"$phone",
			];
           if($operation->updateData($table,$data,$where) == 1){
               echo json_encode(array("code"=>1,"msg"=>"record saved!"));
           }else{
               echo json_encode(array("code"=>2,"msg"=>"Something went wrong while saving!"));
           }
       }



    }else{
        echo json_encode(array("code" => 2,"msg"=>"User seem not to exist, try again!"));
    }
//delete
}elseif(isset($_POST['uid']) && !empty($_POST['uid'])){
    $id = addslashes($_POST['uid']);
    //check if exists in DB
    if($operation->countAll("SELECT *FROM users WHERE user_id = '$id'") > 0 ){
        //delete heheh
        $where = "user_id = '$id'";
        $table= 'users';
        if($operation->deleteData($table,$where) == 1){
           echo json_encode(array("code"=>1,"msg"=>"record deleted!")); 
        }else{
            echo json_encode(array("code"=>2,"msg"=>"Record deleting failed, try again later!"));
        }
    }else{
        echo json_encode(array("code" => 2,"msg"=>"User seem not to exist, try again!"));
    }
}

?>