<?php
require_once __DIR__ . "/headers.php";
require_once __DIR__ . "/config.php";

// Only GET allowed
if ($_SERVER["REQUEST_METHOD"] !== "GET") {
    echo json_encode([
        "status" => false,
        "message" => "Invalid Request Method. Use GET."
    ]);
    exit;
}

$id = isset($_GET["id"]) ? intval($_GET["id"]) : 0;

if ($id <= 0) {
    echo json_encode([
        "status" => false,
        "message" => "Party ID is required"
    ]);
    exit;
}

$stmt = $conn->prepare("SELECT id, party_type, name, mobile, address, opening_balance, status, created_at 
                        FROM parties 
                        WHERE id = ?");
$stmt->bind_param("i", $id);
$stmt->execute();

$res = $stmt->get_result();

if ($res->num_rows == 0) {
    echo json_encode([
        "status" => false,
        "message" => "Party not found"
    ]);
    exit;
}

$party = $res->fetch_assoc();

echo json_encode([
    "status" => true,
    "message" => "Party loaded successfully",
    "data" => $party
]);
exit;
