<?php

require_once "config.php";

// Set header to specify content type as JSON
header('Content-Type: application/json');

// Get the HTTP method used
$method = $_SERVER['REQUEST_METHOD'];

// Perform actions based on the HTTP method
switch ($method) {
    case 'GET':
        // Get the value of the "cemail" parameter from the query string
        $cemail = isset($_GET["cemail"]) ? $_GET["cemail"] : '';
        
        // Check if email is provided
        if (!empty($cemail)) {
            // Prepare the SQL query to check for the email
            $stmt = $conn->prepare("SELECT cemail FROM tbmember WHERE cemail = ?");
            $stmt->bind_param("s", $cemail);
            $stmt->execute();
            $result = $stmt->get_result();
            
            // Fetch the result
            if ($result->num_rows > 0) {
                $row = $result->fetch_assoc();
                $response = array("exists" => true, "email" => $row['cemail'], "message" => "Email ditemukan.");
            } else {
                $response = array("exists" => false, "message" => "Email tidak ditemukan.");
            }
            
            // Close the statement
            $stmt->close();
        } else {
            $response = array("error" => "Tidak ada email yang diberikan.");
        }

        // Output the response as JSON
        echo json_encode($response);
        break;
    
    default:
        echo json_encode(array("error" => "Metode permintaan tidak valid."));
        break;
}

// Close the database connection
mysqli_close($conn);
?>
