<?php require_once("../../classes/DBConnection.php"); $DB = new DBConnection();

$modulecodes = [
	'A' => 'Pref',
	'B' => 'Supp',
	'C' => 'Mo08',
	'D' => 'Mo09',
	'E' => 'Mo10',
	'F' => 'Ma08',
	'G' => 'Ma09',
	'H' => 'Ma10',
	'I' => 'Macd',
	'J' => 'Shcs',
	'K' => 'Mocn',
	'L' => 'Mbs',
	'ME' => 'Middle East',
	'EU' => 'Europe',
	'O' => 'N/A'
];

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

if($t1_name != ''){
	echo "<br />'References Filling' STARTED<br />";
	$res = $DB->execute("select vfam from `{$t1_name}` where vfam != '' group by vfam");
	if(!is_null($res)){
		$res = $res[0];
		while($W = $DB->nextRow($res)){
			$ref = getFamCodeConstructively('merb', $W['vfam']);
			
			if($DB->simpleQuery("update `{$t1_name}` set ref = '{$ref}' where vfam = '{$W['vfam']}'")){
				echo "<br />Done for #{$W['vfam']}";
			}else{
				echo "<br />Error for #{$W['vfam']}";
			}
		}
		echo "<br />ENDED<br />";
	}
}else{
	echo "<br />ERROR!<br />Table not specified<br />";
}

function getFamCode($fam){
	$carr = str_split(strtoupper($fam));
	$coding = [
		"A" => "01", "B" => "02", "C" => "03", "D" => "04", "E" => "05", "F" => "06", "G" => "07", "H" => "08", "I" => "09", "J" => "10", "K" => "11", "L" => "12", "M" => "13", "N" => "14", "O" => "15", "P" => "16", "Q" => "17", "R" => "18", "S" => "19", "T" => "20", "U" => "21", "V" => "22", "W" => "23", "X" => "24", "Y" => "25", "Z" => "26"
	];
	$ret = "";
	foreach($carr as $c){ if(ctype_digit($c)){ $ret .= $c; }else{ $ret .= $coding[$c]; } }
	return $ret;
}

function getFamCodeConstructively($table, $vin){
	$fc = '';
	switch($table){
		case "pref":
		$fc = getFamCode('A'.substr($vin, 0,8));
		break;

		case "supp":
		$fc = getFamCode('B'.substr($vin, 0,8));
		break;

		case "mo08":
		$fc = getFamCode('C'.substr($vin, 0,8));
		break;

		case "mo09":
		$fc = getFamCode('D'.substr($vin, 0,9));
		break;

		case "mo10":
		$fc = getFamCode('E'.substr($vin, 0,10));
		break;

		case "ma08":
		$fc = getFamCode('F'.substr($vin, 0,8));
		break;

		case "ma09":
		$fc = getFamCode('G'.substr($vin, 0,9));
		break;

		case "merb":
		$fc = getFamCode('L'.substr($vin, 0,9));
		break;

		case "ma10":
		$fc = getFamCode('H'.substr($vin, 0,10));
		break;

		/*case "macd":
		$fc = $this->getFamCode('I'.substr($vin, 0,0));
		break;*/

		case "shcs":
		$fc = getFamCode('J'.$this->modbun['vfam']);
		break;

		case "mocn":
		$fc = getFamCode('K'.substr($vin, 0,8));
		break;
	}
	return $fc;
}

?>