<?php session_start(); 

require "common.php";
if (isset($_SESSION['role'])){
	$role =$_SESSION['role'];
}
if (isset($_GET['lang'])){
	$_SESSION['lang'] = $_GET['lang'];
}
if (isset($_SESSION['lang'])){
	if ($_SESSION['lang'] == "chi"){
		require "language/chi.php";
	} else {
		require "language/eng.php";
	}
$lang = $_SESSION['lang'];
}

if(isset($_SESSION['UID']) != null){ 
	$UID = $_SESSION["UID"];  
		if (isset($_SESSION['STAFFCODE'])) {
		$staffcode = $_SESSION['STAFFCODE'];
		if ($_SESSION['lang'] == "chi") {
			$name = $_SESSION['CH_NAME'];
		} else {
			$name = $_SESSION['EN_NAME'];
		}
	} else {
		$stuid = $_SESSION['STUID'];
		$classcode = $_SESSION['CLASSCODE'];
		$classno = $_SESSION['CLASSNO'];
		if ($_SESSION['lang'] == "chi") {
			$name = $_SESSION['CH_NAME'];
		} else {
			$name = $_SESSION['EN_NAME'];
		}
		
	}
	foreach ($_POST as $key => $value) 	{ 
			if ($key !== "role") {
				$$key = $value;    //2個$$代表轉為VARIABLE
			}
	}
	foreach ($_GET as $key => $value) 	{ 
			if ($key !== "role") {
				$$key = $value;    //2個$$代表轉為VARIABLE
			}
	}


//設定學年
		if (!isset($targetschoolyear)){
			$targetschoolyear = $todayschyr;
		}

//完成設定學年
//學年選擇表		
			$sql=mysqli_query($link,"Select SCHYEAR from SCHYEAR ORDER BY SCHYEAR DESC");
			$yearlist = "";
			while ($row = mysqli_fetch_row($sql)){
				if (isset($targetschoolyear)){
					if ($targetschoolyear == $row[0]) {
						$selected = "selected";
					} else {
						$selected = "";
					}
				} else {
					$selected = "";
				}
				$pp = $row[0]+1;
				$yearlist .="<option value=\"".$row[0]."\" ".$selected.">".$row[0]."-".substr($pp,2)."</option>";
			//	$yearlist .="<option value=\"".$row[0]."\" ".$selected.">".$row[0]."</option>";
			}
//完成學年選擇表

//讀取學生組別
	if ($role == 2) {
		
		$sql = mysqli_query($splink,"Select DOB , SCHHOUSE,SEX from studentinfo where STUID=\"".$stuid."\"");
		while ($row= mysqli_fetch_row($sql)){
			$yearofbirth = substr($row[0],0,4);
			$SCHHOUSE= $row[1];
			if ($row[2] =="M") {
				$sexb1g2 = 1;
			} else {
				$sexb1g2 = 2;
			}
		}
		$sql = mysqli_query($splink, "Select MAX( CASE WHEN grade=\"A\" THEN gradeyear END ) ,MAX( CASE WHEN grade=\"B\" THEN gradeyear END) , MAX(CASE WHEN grade=\"C\" THEN gradeyear END ) from gradeyear where SCHYEAR = \"".$targetschoolyear."\"");
		while ($row=mysqli_fetch_row($sql)){
			if ($yearofbirth <= $row[2]) {
				$STUGRADE = "C";
			} else if ( $yearofbirth <= $row[1]){
				$STUGRADE="B";
			} else {
				$STUGRADE="A";
			}
		}
	}
//讀取學生組別
//讀取學生組別年齡
		$sql = mysqli_query($splink, "Select MAX( CASE WHEN grade=\"A\" THEN gradeyear END ) ,MAX( CASE WHEN grade=\"B\" THEN gradeyear END) , MAX(CASE WHEN grade=\"C\" THEN gradeyear END ) from gradeyear where SCHYEAR = \"".$targetschoolyear."\"");
		while ($row = mysqli_fetch_row($sql)){
			$gradeAyr= $row[0];
			$gradeByr = $row[1];
			$gradeCyr = $row[2];
		}
//完成讀取學生組別年齡


	require "templates/header.php"; 

	
	//徑項分組
	if (isset($eventapplynumber)){
		$sql = mysqli_query($splink,"Select A.COMPETITION_ID, COUNT(A.COMPETITION_ID),B.CH_DES, B.EN_DES,B.GRADE, B.SEX, B.TRACKFIELD from APPLICATION_COMPETITION AS A inner join COMPETITIONITEM AS B ON A.COMPETITION_ID=B.keyid where A.SCHYEAR=\"".$todayschyr."\" AND B.INTERHOUSE=\"0\" AND B.INTERCLASS=\"0\" GROUP BY A.COMPETITION_ID ORDER BY B.TRACKFIELD, B.SEX, B.GRADE, A.COMPETITION_ID");
		?>
               <h2><?=$txteventapplynumber?></h2>
                <table><thead><tr><th><?=$txtitem?></th><th><?=$txtgrade?></th><th><?=$txtsex?></th><th><?=$txttrackorfield?></th><th><?=$txttotalnum?></th></tr></thead><tbody>
                <?php
                while ($row = mysqli_fetch_row($sql)){
                    if ($lang == "chi" ) {
                        $eventname = $row[2];
                    } else {
                        $eventname = $row[3];
                    }
                    if ($row[6] == 1) {
                        $isevent = $txtistrack;
                    } else {
                        $isevent = $txtisfield;
                    }
                    ?>
                    <tr><td><?=$eventname?></td><td><?=$row[4]?></td><td><?=$row[5]?></td><td><?=$isevent?></td><td><?=$row[1]?></td></tr>
                    <?php
                }
                ?>
                </tbody></table>
        <?php
	}
	//徑項分組


	require "templates/footer.php";
	mysqli_close($splink);
} else {
	require "language/eng.php";
	echo $txtnoright;
	echo '<meta charset="utf-8"';
	echo '<meta http-equiv=REFRESH CONTENT=2;url=index.php>';
}
?>