<?php
/**
 * Get Customer Address API
 * Retrieves customer billing address details
 */

session_start();
include_once('../../includes/config.php');

// Check if user is logged in
if (!isset($_SESSION['uid'])) {
    echo json_encode(['success' => false, 'message' => 'Unauthorized access']);
    exit();
}

// Get POST data
$customerId = isset($_POST['customer_id']) ? intval($_POST['customer_id']) : 0;

// Validate customer ID
if ($customerId <= 0) {
    echo json_encode(['success' => false, 'message' => 'Invalid customer ID']);
    exit();
}

// Query to get customer address
$query = "SELECT 
            CustomerName, 
            Address, 
            City, 
            State, 
            PinCode 
          FROM tblcustomers 
          WHERE ID = ?";

$stmt = mysqli_prepare($con, $query);
mysqli_stmt_bind_param($stmt, "i", $customerId);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);

if (mysqli_num_rows($result) > 0) {
    $data = mysqli_fetch_assoc($result);
    
    echo json_encode([
        'success' => true,
        'data' => [
            'CustomerName' => $data['CustomerName'] ?? '',
            'Address' => $data['Address'] ?? '',
            'City' => $data['City'] ?? '',
            'State' => $data['State'] ?? '',
            'PinCode' => $data['PinCode'] ?? ''
        ]
    ]);
} else {
    echo json_encode([
        'success' => false,
        'message' => 'Customer not found'
    ]);
}

mysqli_stmt_close($stmt);
mysqli_close($con);
?>
