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

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

echo "<br />'PREFIX SELECTOR' STARTED<hr /><br />Will say 'ENDED' when done";

//$res = $DB->execute("SET group_concat_max_len = 2048");//use silent execute to avoid warning message
$res = $DB->execute("select id, GROUP_CONCAT(year order by year ASC SEPARATOR '|') as yearlist, prefix, min(year) as minyear, max(year) as maxyear, count(*) as rowcount from `{$t1_name}` group by prefix order by year ASC");
if(!is_null($res)){
	$res = $res[0];
	while($row = $DB->nextRow($res)){
		$id = $row['id'];
		$prefix = $row['prefix'];
		$minyear = $row['minyear'];
		$maxyear = $row['maxyear'];
		$rowcount = $row['rowcount'];// if($prefix!='JN8HD17Y') continue;

		$yearlist = explode("|", $row['yearlist']);
		$lenoyearlist = sizeof($yearlist);

		$midyear = (int) (($minyear + $maxyear) / 2);

		$halfway = (int) (($minyear + $midyear) / 2);

		$distotop = $maxyear - $midyear;
		$distobot = $midyear - $minyear;

		/*if($rowcount==3){
			$qry = "update `{$t1_name}` set tag = '*' where prefix='{$prefix}' and (year='{$midyear}' or year>'{$minyear}') order by year ASC limit 1";
		}else{
			$qry = "update `{$t1_name}` set tag = '*' where prefix='{$prefix}' and (year='{$midyear}' or year<'{$midyear}') order by year DESC limit 1";
		}*/
		/*if($rowcount==3){
			$qry = "update `{$t1_name}` set tag = '*' where prefix='{$prefix}' and (year='{$midyear}' or year>'{$minyear}') order by year ASC limit 1";
		}else if($rowcount>3){
			$qry = "update `{$t1_name}` set tag = '*' where prefix='{$prefix}' and (year='{$midyear}' or (year>'{$midyear}' and year<'{$maxyear}') or year<'{$maxyear}') order by year DESC limit 1";
			//if($prefix=='JN1BF5MD') echo "<br />".$qry;
		}else{
			$qry = "update `{$t1_name}` set tag = '*' where prefix='{$prefix}' and (year='{$midyear}' or year<'{$midyear}') order by year DESC limit 1";
		}*/
		if($rowcount==3){
			$qry = "update `{$t1_name}` set tag = '*' where prefix='{$prefix}' and (year='{$midyear}' or year>'{$minyear}') order by year ASC limit 1";
		}else if($rowcount>3){
			/*$qry = "update `{$t1_name}` set tag = '*' where prefix='{$prefix}' and (year='{$midyear}' or (year>'{$midyear}' and year<'{$maxyear}') 
			or (year>'{$halfway}' and year='".($minyear+2)."') 
			or (year>'{$halfway}' and year='".($minyear+3)."') 
			or (year>'{$halfway}' and year='".($minyear+4)."') 
			or (year>'{$halfway}' and year='".($minyear+5)."') 
			or (year>'{$halfway}' and year='".($minyear+6)."') 
			or (year>'{$halfway}' and year='".($minyear+7)."') 
			or (year>'{$halfway}' and year='".($minyear+8)."') 
			or (year>'{$halfway}' and year='".($minyear+9)."') 
			or (year>'{$halfway}' and year='".($minyear+10)."') 
			or (year>'{$halfway}' and year='".($minyear+11)."') 
			or (year>'{$halfway}' and year='".($minyear+12)."') 
			or (year>'{$halfway}' and year='".($minyear+13)."') 
			or (year>'{$halfway}' and year='".($minyear+14)."') 
			or (year>'{$halfway}' and year='".($minyear+15)."') 
			or (year>'{$halfway}' and year='".($minyear+16)."') 
			or (year>'{$halfway}' and year='".($minyear+17)."') 
			or (year>'{$halfway}' and year='".($minyear+18)."') 
			or (year>'{$halfway}' and year='".($minyear+19)."')
			or (year>'{$halfway}' and year='".($minyear+20)."')) order by year DESC limit 1";*/

			/*if($distotop>=$distobot){
				if(($midyear + $distotop) == $maxyear){
					$qry = "update `{$t1_name}` set tag = '*' where prefix='{$prefix}' and (year='{$midyear}' or year<'{$midyear}') order by year DESC limit 1";
				}else{
					$qry = "update `{$t1_name}` set tag = '*' where prefix='{$prefix}' and (year='{$midyear}' or year>'{$midyear}') order by year DESC limit 1";
				}
			}else{
				if(($midyear - $distotop) == $minyear){
					$qry = "update `{$t1_name}` set tag = '*' where prefix='{$prefix}' and (year='{$midyear}' or year>'{$midyear}') order by year DESC limit 1";
				}else{
					$qry = "update `{$t1_name}` set tag = '*' where prefix='{$prefix}' and (year='{$midyear}' or year<'{$midyear}') order by year DESC limit 1";
				}
			}*/

			/*if($distotop>=$distobot){
				if($midyear <= $yearlist[$lenoyearlist-2]){
					$qry = "update `{$t1_name}` set tag = '*' where prefix='{$prefix}' and (year='{$midyear}' or year>'{$midyear}') order by year DESC limit 1";
				}else{
					$qry = "update `{$t1_name}` set tag = '*' where prefix='{$prefix}' and (year='{$midyear}' or year<'{$midyear}') order by year DESC limit 1";
				}
			}else{
				if($midyear >= $yearlist[1]){
					$qry = "update `{$t1_name}` set tag = '*' where prefix='{$prefix}' and (year='{$midyear}' or year<'{$midyear}') order by year DESC limit 1";
				}else{
					$qry = "update `{$t1_name}` set tag = '*' where prefix='{$prefix}' and (year='{$midyear}' or year>'{$midyear}') order by year DESC limit 1";
				}
			}*/
			$nebs = getNeighboursOf($midyear, $yearlist);
			if($nebs[0]>0){//green light
				if($nebs[0] == $nebs[1]){//the year we want is the midyear
					$year = $midyear;
					//echo "_1_";
				}else if($distotop > $distobot){//we want the year after the midpoint upwards ($nebs[2])
					//but wait...
					if($nebs[2] < $maxyear){//the norm
						$year = $nebs[2];//echo "_11_";
					}else{
						$year = $nebs[0];//echo "_12_";
					}
				}else{//we want the year after the midpoint downwards ($nebs[0])
					//but wait...
					if($nebs[0] > $minyear){//the norm
						$year = $nebs[0];//echo "_21_";
					}else{
						$year = $nebs[2];//echo "_22_";
					}
				}
				$qry = "update `{$t1_name}` set tag = '*' where prefix='{$prefix}' and year='{$year}'";
			}
		}else{
			$qry = "update `{$t1_name}` set tag = '*' where prefix='{$prefix}' and (year='{$midyear}' or year<'{$midyear}') order by year DESC limit 1";
		}
		//echo "<br />".$qry;

		if($DB->simpleQuery($qry)){
			echo "";
		}else{
			echo "<br />Error at #{$id}";
		}
	}
	echo "<br /><br />ENDED<hr />";
}

function getNeighboursOf($val, $arr){
	$t = array(); $t[0]=0; $t[1]=$val; $t[2]=0;
    for($i=0; $i<sizeof($arr); $i++){
    	if($arr[$i]<=$val) $t[0]=$arr[$i];
    	if($arr[$i]>=$val){ $t[2]=$arr[$i]; break; }
    }
    return $t;
}
?>