<?php

require_once "config.php";

// Mengatur header untuk menentukan tipe konten sebagai JSON
header('Content-Type: application/json');

// Mendapatkan metode HTTP yang digunakan
$method = $_SERVER['REQUEST_METHOD'];

// Menjalankan aksi berdasarkan metode HTTP
switch ($method) {
    case 'GET':
        $cemail = $_GET["cemail"];
        $query = $conn->query("SELECT * FROM tbmember WHERE ckodecust = '$cemail'");
        $result = $query->fetch_assoc();
        echo json_encode($result);
        break;
        
        case 'PUT':
        $data = json_decode(file_get_contents('php://input'), true);
        $cemail = $data['cemail'];
        $cnama = $data['cnama'];
        $cnohp = $data['cnohp'];
        $dtgllahir = $data['dtgllahir'];
        $ckelamin = $data['ckelamin'];
        $ckodecust = $data['ckodecust'];

        
        $sql = "UPDATE tbmember SET cemail = '$cemail', cnama = '$cnama', cnohp = '$cnohp', dtgllahir = '$dtgllahir', ckelamin = '$ckelamin' WHERE ckodecust = '$ckodecust'";
        
        if ($conn->query($sql) === TRUE) {
            echo json_encode(['message' => 'Update successfully']);
        } else {
            http_response_code(409);
            echo json_encode(['message' => 'Update Failed', "error" => $conn->error]);
        }
        break;
}
// Menutup koneksi database
mysqli_close($conn);
?>
