<?php
require_once($_SERVER['DOCUMENT_ROOT'] . "/aipma2025/include/config.php");
require_once(INCLUDE_PATH . "startup.php");



$chapter =  intval($_POST['chapeter']);

$toCountry = sanitize($_POST['toCountry'],SQL+HTML);
// $toCountry = $_POST['toCountry'];

$wherecondition = '';

if($chapter && $toCountry)
{
    $wherecondition .=" Chapter = '$chapter' AND import_to_country = '$toCountry' ";
}
else if($chapter)
{
    $wherecondition .=" Chapter = '$chapter'";
}
else if($toCountry)
{
    $wherecondition .=" import_to_country = '$toCountry' ";
}
  
if($wherecondition)
{
    $sqlhsncode = "SELECT DISTINCT(HSNcode), name FROM " . MAIN_IMPORT_TABLE . "  WHERE $wherecondition AND CHAR_LENGTH(HSNcode) > 6 ORDER BY HSNcode ASC";
}
else
{
    $sqlhsncode = "SELECT DISTINCT(HSNcode), name FROM " . MAIN_IMPORT_TABLE . "  WHERE CHAR_LENGTH(HSNcode) > 6 ORDER BY HSNcode ASC";
}


$alltables = new AllTables();

$hsncodedata = $alltables->getInfo("", $sqlhsncode);
$hsncodecount = count($hsncodedata);         

for ($i = 0; $i < $hsncodecount; $i++) {
    $hsncode = $hsncodedata[$i]["HSNcode"];
    $name = isset($hsncodedata[$i]["name"]) ? $hsncodedata[$i]["name"] : '';
    
    $value = $hsncode . "-" . $name;
    $escapedValue = htmlspecialchars($value,ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); // Escapes <, >, &, etc.
    
    $options .= "<option value='" . htmlspecialchars($hsncode) . "' data-search='" . $escapedValue . "'>" . $escapedValue . "</option>";
} 
header('Content-Type: text/html; charset=utf-8');
mb_internal_encoding("UTF-8");
echo $options;
exit;