<?php

require_once "config.php";

header('Content-Type: application/json');

$method = $_SERVER['REQUEST_METHOD'];

switch ($method) {
    case 'GET':
        $ckodekelurahan = isset($_GET['ckodekelurahan']) ? $_GET['ckodekelurahan'] : '';
        $namaLokasi = isset($_GET['namaLokasi']) ? $_GET['namaLokasi'] : '';
        $alamat = isset($_GET['alamat']) ? $_GET['alamat'] : '';
        $note = isset($_GET['note']) ? $_GET['note'] : '';
        $ckodecust = isset($_GET['ckodecust']) ? $_GET['ckodecust'] : '';
        $cnoid = isset($_GET['cnoid']) ? $_GET['cnoid'] : '';

        if (empty($ckodekelurahan) || empty($namaLokasi) || empty($alamat) || empty($note) || empty($ckodecust) || empty($cnoid)) {
            echo json_encode(["success" => "false", "error" => "Missing required parameters", "message" => "Please provide all the required parameters."]);
            exit;
        }

        $query = $conn->prepare("
            SELECT 
                ckodekelurahan, cnamakelurahan, ckodekecamatan, cnamakecamatan, 
                ckodekota, cnamakota, ckodeprovinsi, cnamarovinsi, ckodepos 
            FROM tblokasi 
            WHERE ckodekelurahan = ? 
            ORDER BY cnamakelurahan ASC
        ");
        $query->bind_param("s", $ckodekelurahan);
        $query->execute();
        $result = $query->get_result();

        if ($result->num_rows > 0) {
            $locationData = $result->fetch_assoc();

            $updateQuery = $conn->prepare("
                UPDATE tbalamatkirim 
                SET 
                    clokasi = ?, calamat1 = ?, calamat2 = ?, 
                    ckodeprov = ?, cnamaprov = ?, ckodekota = ?, cnamakota = ?, 
                    ckodekecamatan = ?, cnamakecamatan = ?, ckodekelurahan = ?, 
                    cnamakelurahan = ?, ckodepos = ?, cnote = ?
                WHERE ckodecust = ? AND cnoid = ?
            ");

            $clokasi = $namaLokasi;
            $calamat1 = $alamat;
            $calamat2 = '';
            $ckodeprov = $locationData['ckodeprovinsi'];
            $cnamaprov = $locationData['cnamarovinsi'];
            $ckodekota = $locationData['ckodekota'];
            $cnamakota = $locationData['cnamakota'];
            $ckodekecamatan = $locationData['ckodekecamatan'];
            $cnamakecamatan = $locationData['cnamakecamatan'];
            $ckodekelurahan = $locationData['ckodekelurahan'];
            $cnamakelurahan = $locationData['cnamakelurahan'];
            $ckodepos = $locationData['ckodepos'];
            $cnote = $note;

            $updateQuery->bind_param("sssssssssssssss", 
                $clokasi, $calamat1, $calamat2, 
                $ckodeprov, $cnamaprov, $ckodekota, $cnamakota, 
                $ckodekecamatan, $cnamakecamatan, $ckodekelurahan, 
                $cnamakelurahan, $ckodepos, $cnote, $ckodecust, $cnoid
            );

            if ($updateQuery->execute()) {
                echo json_encode([
                    "success" => "true", 
                    "message" => "Data updated successfully"
                ]);
            } else {
                echo json_encode([
                    "success" => "false", 
                    "error" => "Failed to update data",
                    "message" => "Data update failed. Please try again."
                ]);
            }

            $updateQuery->close();
        } else {
            echo json_encode([
                "success" => "false", 
                "error" => "No matching location found for the provided ckodekelurahan",
                "message" => "The provided ckodekelurahan does not match any locations."
            ]);
        }

        $result->close();
        $query->close();
        break;

    default:
        echo json_encode(["error" => "Invalid request method"]);
        break;
}

mysqli_close($conn);
?>
