<?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;
}

$sql = "SELECT id, party_type, name, mobile, address, opening_balance, status, created_at 
        FROM parties 
        ORDER BY id DESC";

$res = mysqli_query($conn, $sql);

if (!$res) {
    echo json_encode([
        "status" => false,
        "message" => "Database query failed"
    ]);
    exit;
}

$data = [];
while ($row = mysqli_fetch_assoc($res)) {
    $data[] = $row;
}

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