<?php
/**
 * API: Get Sales Type Configuration
 * Returns all sales types and their configurations
 */

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

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

try {
    $query = "SELECT ID, SalesType, InvoicePrefix, Description, IsActive 
              FROM tblsales_type_config 
              WHERE IsActive = 1
              ORDER BY ID";
    
    $result = mysqli_query($con, $query);
    
    if (!$result) {
        echo json_encode(['success' => false, 'message' => 'Database error']);
        exit();
    }
    
    $salesTypes = [];
    while ($row = mysqli_fetch_assoc($result)) {
        $salesTypes[] = [
            'id' => $row['ID'],
            'type' => $row['SalesType'],
            'prefix' => $row['InvoicePrefix'],
            'description' => $row['Description'],
            'isActive' => $row['IsActive'] == 1
        ];
    }
    
    echo json_encode([
        'success' => true,
        'salesTypes' => $salesTypes
    ]);
    
} catch (Exception $e) {
    echo json_encode([
        'success' => false,
        'message' => 'Error fetching sales types: ' . $e->getMessage()
    ]);
}
?>
