<?php
include $_SERVER['DOCUMENT_ROOT'] . "/classes/autoload.php";

// var_dump($_POST);

if (!isset($_POST['supplier_code']) || empty($_POST['supplier_code'])) {
    echo 'supplier_code is empty';
    exit;
}

$supplier_code = trim($_POST['supplier_code']);

$db = new db();

$res = $db->query("supplier_orders","SELECT * FROM supplier_orders WHERE supplier_order_no LIKE '%{$supplier_code}-%'");

// echo "SELECT * FROM supplier_orders WHERE supplier_order_no LIKE '%{$supplier_code}-%'";

if ($res->num_rows == 0) {
    echo $supplier_code . '_1';
    exit;
} else {
    $current_total = $res->num_rows;
    $nom_orders = $current_total + 1;

    echo $supplier_code . '_' . $nom_orders;
}