<?php

if($_SERVER["REQUEST_METHOD"]=="POST"){
      require 'db_connect.php';
      deleteSignedInAccount();
}

function deleteSignedInAccount(){

    global $conn;
    $response= array();
   
$uid = $_POST["ID"];

// Prepare the statement
$query = "DELETE  users, messages, members, groups
          FROM users
          LEFT JOIN messages ON users.ID = messages.member_ID
          LEFT JOIN members ON users.ID = members.member_ID
          LEFT JOIN groups ON users.ID = groups.ID
          WHERE users.ID = ?";
          
$stmt = mysqli_prepare($conn, $query);
mysqli_stmt_bind_param($stmt, "s", $uid);

$result = mysqli_stmt_execute($stmt);

if ($result) {
	$qry = "DELETE FROM users WHERE ID = '$uid'";
	$result = mysqli_query($conn,$qry);
	 if($result){
	   $response['success'] = true;
       $response['message'] = "Deleted Successfully";
	  }else{
		$response['success'] = false;
        $response['message'] = "Deletion Failed";
	  }
} else {
    $response['success'] = false;
    $response['message'] = "Deletion Failed";
}

echo json_encode($response);
mysqli_close($conn);


}
?>