<?php
session_start();
header('Content-Type: application/json');
include_once('../../includes/config.php');

// Check authentication
if (!isset($_SESSION['uid'])) {
    echo json_encode(['success' => false, 'message' => 'Not authenticated']);
    exit();
}

$response = ['success' => false, 'message' => ''];

try {
    // Get all active suppliers
    $query = "SELECT ID, SupplierCode, SupplierName, PhoneNumber, GSTNumber, OutstandingBalance 
              FROM tblsuppliers 
              WHERE Status = 'Active' 
              ORDER BY SupplierName ASC";
    
    $result = mysqli_query($con, $query);
    
    if ($result) {
        $suppliers = [];
        while ($row = mysqli_fetch_assoc($result)) {
            $suppliers[] = $row;
        }
        
        $response['success'] = true;
        $response['suppliers'] = $suppliers;
        $response['count'] = count($suppliers);
    } else {
        $response['message'] = 'Database error: ' . mysqli_error($con);
    }
    
} catch (Exception $e) {
    $response['message'] = 'Error: ' . $e->getMessage();
}

echo json_encode($response);
?>
