<?php
require_once "../../includes/db.php";
session_start();

header('Content-Type: application/json');

$comp_id = $_GET['sel_co'] ?? '';
$t_type  = $_GET['trans_type'] ?? '';

if (!$t_type || !$comp_id) {
    echo json_encode([
        "success" => false,
        "highestNumber" => 1
    ]);
    exit;
}

$stmt = $pdo->prepare("
    SELECT COALESCE(MAX(CAST(t_doc_no AS UNSIGNED)), 0) + 1 AS highestNumber
    FROM transactions
    WHERE co_id = ?
    AND t_type = ?
    AND t_status <> 'Deleted'
");

$stmt->execute([$comp_id, $t_type]);

$result = $stmt->fetch(PDO::FETCH_ASSOC);

echo json_encode([
    "success" => true,
    "highestNumber" => $result['highestNumber']
]);
?>