<?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($splink,"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 ".$targetschoolyear."_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";
			}
		}
	}
//讀取學生組別


	require "templates/header.php"; 

	//個人項目報名
	if (isset($applycompetition)){
			?>
			<h2><?=$txtapplycompetition?></h2>
			<center><?=$msg?></center>
			<?php
		//check apply date
		$sql = mysqli_query($splink,"Select Start_date, End_date from moduleenable where module=\"competitionapply\"");
		while ($row = mysqli_fetch_row($sql)) {
			if ($today >= $row[0] && $today <= $row[1]) {
				$allowapp = 1;
			} else {
				$allowapp = 0;
			}
		}
		//finish check apply date
		if ($allowapp == 1) {
			//php saveapplycompetition
			if (isset($saveapplycompetition)){
				if ($CHOICE1 =="" ) {
					$sql = mysqli_query($splink,"Delete from APPLICATION_COMPETITION where SCHYEAR=\"".$todayschyr."\" AND STUID=\"".$stuid."\" AND CHOICE=\"1\"");
				} else {
					$sql = mysqli_query($splink,"Select * from APPLICATION_COMPETITION where SCHYEAR=\"".$todayschyr."\" AND STUID=\"".$stuid."\" AND CHOICE=\"1\"");
					if (mysqli_num_rows($sql) > 0){
						$usql = mysqli_query($splink,"Update APPLICATION_COMPETITION SET COMPETITION_ID=\"".$CHOICE1."\" WHERE SCHYEAR=\"".$todayschyr."\" AND STUID=\"".$stuid."\" AND CHOICE=\"1\"");
					} else {
						$isql = mysqli_query($splink,"Insert into APPLICATION_COMPETITION ( SCHYEAR, STUID, CHOICE, COMPETITION_ID,ATTENDANCE) VALUES (\"".$todayschyr."\",\"".$stuid."\",\"1\",\"".$CHOICE1."\",\"\")");
					}
				}
				if ($CHOICE2 =="" ) {
					$sql = mysqli_query($splink,"Delete from APPLICATION_COMPETITION where SCHYEAR=\"".$todayschyr."\" AND STUID=\"".$stuid."\" AND CHOICE=\"2\"");
				} else {
					$sql = mysqli_query($splink,"Select * from APPLICATION_COMPETITION where SCHYEAR=\"".$todayschyr."\" AND STUID=\"".$stuid."\" AND CHOICE=\"2\"");
					if (mysqli_num_rows($sql) > 0){
						$usql = mysqli_query($splink,"Update APPLICATION_COMPETITION SET COMPETITION_ID=\"".$CHOICE2."\" WHERE SCHYEAR=\"".$todayschyr."\" AND STUID=\"".$stuid."\" AND CHOICE=\"2\"");
					} else {
						$isql = mysqli_query($splink,"Insert into APPLICATION_COMPETITION ( SCHYEAR, STUID, CHOICE, COMPETITION_ID,ATTENDANCE) VALUES (\"".$todayschyr."\",\"".$stuid."\",\"2\",\"".$CHOICE2."\",\"\")");
					}
				}
				if ($CHOICE3 =="" ) {
					$sql = mysqli_query($splink,"Delete from APPLICATION_COMPETITION where SCHYEAR=\"".$todayschyr."\" AND STUID=\"".$stuid."\" AND CHOICE=\"3\"");
				} else {
					$sql = mysqli_query($splink,"Select * from APPLICATION_COMPETITION where SCHYEAR=\"".$todayschyr."\" AND STUID=\"".$stuid."\" AND CHOICE=\"3\"");
					if (mysqli_num_rows($sql) > 0){
						$usql = mysqli_query($splink,"Update APPLICATION_COMPETITION SET COMPETITION_ID=\"".$CHOICE3."\" WHERE SCHYEAR=\"".$todayschyr."\" AND STUID=\"".$stuid."\" AND CHOICE=\"3\"");
					} else {
						$isql = mysqli_query($splink,"Insert into APPLICATION_COMPETITION ( SCHYEAR, STUID, CHOICE, COMPETITION_ID,ATTENDANCE) VALUES (\"".$todayschyr."\",\"".$stuid."\",\"3\",\"".$CHOICE3."\",\"\")");
					}
				}
				$msg = $txtsuccesssave;
			}
			//finish php saveapplycompetition
			//檢查學生已報項目
			$sql = mysqli_query($splink,"Select COMPETITION_ID from APPLICATION_COMPETITION where SCHYEAR=\"".$todayschyr."\" AND STUID=\"".$stuid."\" AND CHOICE=\"1\"");
			while ($row = mysqli_fetch_row($sql)){
				$sCHOICE1 = $row[0];
			}
			$sql = mysqli_query($splink,"Select COMPETITION_ID from APPLICATION_COMPETITION where SCHYEAR=\"".$todayschyr."\" AND STUID=\"".$stuid."\" AND CHOICE=\"2\"");
			while ($row = mysqli_fetch_row($sql)){
				$sCHOICE2 = $row[0];
			}
			$sql = mysqli_query($splink,"Select COMPETITION_ID from APPLICATION_COMPETITION where SCHYEAR=\"".$todayschyr."\" AND STUID=\"".$stuid."\" AND CHOICE=\"3\"");
			while ($row = mysqli_fetch_row($sql)){
				$sCHOICE3 = $row[0];
			}
			
			//完成檢查學生已報項目
			//比賽項目選單
			$choice1list="";
			$sql = mysqli_query($splink,"Select keyid, CH_DES, EN_DES, TRACKFIELD from COMPETITIONITEM where GRADE=\"".$STUGRADE."\" AND SEX=\"".$sexb1g2."\" AND TRACKFIELD = \"1\" AND INTERHOUSE=\"0\" AND INTERCLASS=\"0\" ORDER BY TRACKFIELD, EVENT_CODE");
			while ($row = mysqli_fetch_row($sql)){
				if (isset($sCHOICE1) && $sCHOICE1 == $row[0]) {
					$selected = " selected=\"selected\"" ;
				} else {
					$selected = "";
				}
				if ($lang=="chi"){
					$eventname = $row[1];
				} else {
					$eventname = $row[2];
				}
				$choice1list .= "<option value=\"".$row[0]."\" ".$selected." >".$eventname."</option>";
			}
			$choice2list="";
			$sql = mysqli_query($splink,"Select keyid, CH_DES, EN_DES, TRACKFIELD from COMPETITIONITEM where GRADE=\"".$STUGRADE."\" AND SEX=\"".$sexb1g2."\" AND INTERHOUSE=\"0\" AND INTERCLASS=\"0\"  ORDER BY TRACKFIELD, EVENT_CODE");
			while ($row = mysqli_fetch_row($sql)){
				if (isset($sCHOICE2) && $sCHOICE2 == $row[0]) {
					$selected = " selected=\"selected\"" ;
				} else {
					$selected = "";
				}
				if ($lang=="chi"){
					$eventname = $row[1];
				} else {
					$eventname = $row[2];
				}
				$choice2list .= "<option value=\"".$row[0]."\" ".$selected." >".$eventname."</option>";
			}
			$choice3list="";
			$sql = mysqli_query($splink,"Select keyid, CH_DES, EN_DES, TRACKFIELD from COMPETITIONITEM where GRADE=\"".$STUGRADE."\" AND SEX=\"".$sexb1g2."\" AND TRACKFIELD=\"2\" AND INTERHOUSE=\"0\" AND INTERCLASS=\"0\"  ORDER BY TRACKFIELD, EVENT_CODE");
			while ($row = mysqli_fetch_row($sql)){
				if (isset($sCHOICE3) && $sCHOICE3 == $row[0]) {
					$selected = " selected=\"selected\"" ;
				} else {
					$selected = "";
				}
				if ($lang=="chi"){
					$eventname = $row[1];
				} else {
					$eventname = $row[2];
				}
				$choice3list .= "<option value=\"".$row[0]."\" ".$selected." >".$eventname."</option>";
			}
			//完成比賽項目選單
			?>
            <?=$txtcompetitionapplicationremark?><br />
			<form action="competition.php" method="post" onsubmit="return validation();" id="applyform"><input type="hidden" name="applycompetition" />
			<div class="row uniform">
				<div class="4u 12u$(xsmall)">
					<label for="CHOICE1"><?=$txtchoice1?>:<?=$txtistrack?></label>
					<div class="select-wrapper">
						<select name="CHOICE1" id="CHOICE1"><option value="">--<?=$txtistrack?>--</option><?=$choice1list?></select>
					</div>
				</div>
				<div class="4u 12u$(xsmall)">
					<label for="CHOICE2"><?=$txtchoice2?>:<?=$txtisfield?></label>
					<div class="select-wrapper">
						<select name="CHOICE2" id="CHOICE2"><option value="">--<?=$txttrackorfield?>--</option><?=$choice2list?></select>
					</div>
				</div>
				<div class="4u 12u$(xsmall)">
					<label for="CHOICE3"><?=$txtchoice3?>:<?=$txtisfield?></label>
					<div class="select-wrapper">
						<select name="CHOICE3" id="CHOICE3"><option value="">--<?=$txtisfield?>--</option><?=$choice3list?></select>
					</div>
				</div>
			</div>
			<br />
			<div class="12u$">
				<ul class="actions">
					<li><input type="submit" value="<?=$txtsave?>" name="saveapplycompetition" class="special" /></li>
				</ul>
			</div>
			</form>
			
			
			<?php
		} else {
			echo $txtnowisnottheapplyperiod;
		} 
	}
	//完成個人項目報名
	//查閱個人項目報名結果
	if (isset($applycompetitionresult)){
		?>
        <h2><?=$txtapplycompetitionresult?></h2>
        <?php
		//檢查報名期是否已完結
		$sql = mysqli_query($splink,"Select MAX(CASE WHEN module = \"competitionapply\" then End_date END ), MAX(CASE WHEN module=\"sportday\" then End_date END ) from moduleenable");
		while ($row = mysqli_fetch_row($sql)) {
			if ($today > $row[0] && $today <= $row[1] ) {
				$appre= 1;
			} else {
				$appre = 0;
			}
		}
		//完成檢查報名期是否已完結
		if ($appre ==1 ) {
			?>
			<div class="row 200%">
				<?php
                $sql = mysqli_query($splink,"Select  B.keyid, B.CH_DES, B.EN_DES from APPLICATION_COMPETITION AS A INNER JOIN COMPETITIONITEM AS B ON A.COMPETITION_ID=B.keyid GROUP BY A.COMPETITION_ID ORDER BY B.TRACKFIELD , B.GRADE, B.SEX, B.keyid");
                while ($row = mysqli_fetch_row($sql) ) {
                    if ($lang=="chi"){
                        $showname = $row[1];
                    } else {
                        $showname = $row[2];
                    }
                    ?>
                    <div class="4u 12u$(medium)">
                        <h3><?=$showname?></h3>
                        <table class="alt"><thead><tr><th><?=$txtname?></th><th><?=$txtclass?></th><th><?=$txtclassno?></th>
                        <th><?=$txthouse?></th></tr></thead>
                        <tbody>
						<?php
                        $csql = mysqli_query($splink, "Select B.CHNAME, B.ENNAME, B.CLASSCODE, B.CLASSNO, B.SCHHOUSE from APPLICATION_COMPETITION AS A inner join studentinfo AS B ON A.STUID = B.STUID where A.COMPETITION_ID = \"".$row[0]."\" ORDER BY B.CLASSCODE, B.CLASSNO");
                        while ($crow = mysqli_fetch_row($csql)){
                            if ($lang == "chi"){
								$name = $crow[0];
							} else {
								$name = $crow[1];
							}
							?>
                            <tr><td><?=$name?></td><td><?=$crow[2]?></td><td><?=$crow[3]?></td><td><?=$crow[4]?></td></tr>
                            <?php
                        }
                        ?>
                        </tbody></table>
                    </div>
                  <!-- Break -->
                    <?php
                }
                ?>
            </div>
            <?php
		} else {
			echo $txtapplicationperiodisnotfinishorsportdayisfinsih;
		}
	}
	//查閱個人項目報名結果
	//社際或班際項目報名
	if (isset($teameventapplication)){
		if ($role == 2 ){
			$sql = mysqli_query($splink,"Select * from HOUSE_MEMBER where STUID=\"".$stuid."\"");
			if (mysqli_num_rows($sql)){
				$ishousecom = 1;
			} else {
				$ishousecom = 0;
			}
			
		} else {
			$ishousecom = 0;
		}
		?>
        <h2><?=$txtteameventapplication?></h2>
        <center><?=$msg?></center><br />
		<?php
		if ($ishousecom ==1 || $role ==1 ) {
			//社際比賽清單
			$houseeventlist = "";
			if ($role == 1) {
				$housearray[] = "R";
				$housearray[] = "Y";
				$housearray[] = "B";
				$housearray[] = "G";
				$x = 0;
				while ($x < sizeof($housearray)){
					$housename = "txthouse".$housearray[$x];
					$sql = mysqli_query($splink,"Select keyid, CH_DES, EN_DES, TRACKFIELD from COMPETITIONITEM where TRACKFIELD = \"1\" AND INTERHOUSE=\"1\" AND INTERCLASS=\"0\" ORDER BY SEX, GRADE,keyid");
					while ($row = mysqli_fetch_row($sql)){
						$csql = mysqli_query($splink,"Select A.STUID from INTERHOUSE_COMPETITION AS A inner join studentinfo AS B ON A.STUID=B.STUID where A.COMPETITION_ID=\"".$row[0]."\" AND B.SCHHOUSE=\"".$housearray[$x]."\"");
						$noapp = mysqli_num_rows($csql);
						if (isset($COMPETITION_ID) && $COMPETITION_ID==$row[0]."-".$housearray[$x]) {
							$selected = " selected=\"selected\" ";
						} else {
							$selected = "";
						}
						if ($lang=="chi"){
							$eventname = $row[1];
						} else {
							$eventname = $row[2];
						}
						if ($noapp == 0 ) {
							$showno = $txtnoapplicant;
						} else {
							$showno = $noapp." ".$txtapplied;
						}
						$houseeventlist .= "<option value=\"".$row[0]."-".$housearray[$x]."\" ".$selected.">".$$housename." - ".$eventname." (".$showno.")</option>";
					}
					$x++;
				}
			} else {  //student
				$sql = mysqli_query($splink,"Select keyid, CH_DES, EN_DES, TRACKFIELD from COMPETITIONITEM where TRACKFIELD = \"1\" AND INTERHOUSE=\"1\" AND INTERCLASS=\"0\" ORDER BY SEX, GRADE,keyid");
				while ($row = mysqli_fetch_row($sql)){
					$csql = mysqli_query($splink,"Select A.STUID from INTERHOUSE_COMPETITION AS A inner join studentinfo AS B ON A.STUID=B.STUID where A.COMPETITION_ID=\"".$row[0]."\" AND B.SCHHOUSE=\"".$SCHHOUSE."\"");
					$noapp = mysqli_num_rows($csql);
					if (isset($COMPETITION_ID) && $COMPETITION_ID==$row[0]) {
						$selected = " selected=\"selected\" ";
					} else {
						$selected = "";
					}
					if ($lang=="chi"){
						$eventname = $row[1];
					} else {
						$eventname = $row[2];
					}
					if ($noapp == 0 ) {
						$showno = $txtnoapplicant;
					} else {
						$showno = $noapp." ".$txtapplied;
					}
					$houseeventlist .= "<option value=\"".$row[0]."\" ".$selected.">".$eventname." (".$showno.")</option>";
				}
			}
			//完成社際比賽清單
			//班際比賽
			$classeventlist = "";
			$sql = mysqli_query($splink,"Select keyid, CH_DES, EN_DES, TRACKFIELD from COMPETITIONITEM where TRACKFIELD = \"1\" AND INTERHOUSE=\"0\" AND INTERCLASS=\"1\" ORDER BY SEX, GRADE,keyid");
			while ($row = mysqli_fetch_row($sql)){
				if ($lang=="chi"){
					$eventname = $row[1];
				} else {
					$eventname = $row[2];
				}
				$classeventlist .= "<option value=\"".$row[0]."\" >".$eventname."</option>";
			}
			//完成班際比賽清單
			?>
			<form action="competition.php" method="POST">
			<input type="hidden" name="teameventapplication"  />
			<div class="select-wrapper">
			<select name="COMPETITION_ID" ><option disabled="disabled"><--<?=$txtselect.$txtcompetitionitem?>--></option>
			<?=$houseeventlist?>
			</select></div><br />
			<input type="submit" name="search" value="<?=$txtsearch?>"  class="special"/>
			</form>
			<?php
			//顯示已報名項目參賽者名單及報名表
			if (isset($COMPETITION_ID)){
				if ($role == 1) {
					$teaCOMPETITION_ID=$COMPETITION_ID;
					$input = explode("-",$COMPETITION_ID);
					$COMPETITION_ID= $input[0];
					$SCHHOUSE=$input[1];
				}
				//php save gosaveteameventapplication
				if (isset($gosaveteameventapplication)){
					if (isset($CHOICE)){
						$x = 0;
						while ($x < sizeof($targetSTUID) ) {
							$sql= mysqli_query($splink,"Select CHNAME, ENNAME, CLASSCODE, CLASSNO from studentinfo where STUID=\"".$targetSTUID[$x]."\"");
							while ($row= mysqli_fetch_row($sql)){
								if ($lang == "chi") {
									$sname= $row[0];
								} else {
									$sname = $row[1];
								}
								$sclass = $row[2];
								$sclassno = $row[3];
							}
							
							$sql = mysqli_query($splink,"Select * from INTERHOUSE_COMPETITION where STUID=\"".$targetSTUID[$x]."\" AND COMPETITION_ID=\"".$COMPETITION_ID."\" AND TEAM=\"".$CHOICE."\" AND SCHHOUSE=\"".$SCHHOUSE."\"");
							if (mysqli_num_rows($sql) > 0 ) {
								//do nothing if already exists
								$msg .= $sclass.$sclassno." ".$sname." ".$txtalreadyexistinteam."<br>";
							} else {
								$csql = mysqli_query($splink,"Select * from INTERHOUSE_COMPETITION where COMPETITION_ID=\"".$COMPETITION_ID."\" AND TEAM=\"".$CHOICE."\" AND SCHHOUSE=\"".$SCHHOUSE."\"");
								if (mysqli_num_rows($csql) < 6  ) {
								//未夠6人就可加
									//check is exist in other team
									if ($CHOICE == "A") {
										$checkchoice = "B";
									} else {
										$checkchoice = "A";
									}
									$csql = mysqli_query($splink,"Select * from INTERHOUSE_COMPETITION where COMPETITION_ID=\"".$COMPETITION_ID."\" AND TEAM=\"".$checkchoice."\" AND STUID=\"".$targetSTUID[$x]."\"");
									if (mysqli_num_rows($csql) > 0 ) {
										$msg .= $sclass.$sclassno." ".$sname." ".$txtalreadyexistinotherteam."<br>";
									} else {
										if ($SCHHOUSE=="R") {
											if ($CHOICE == "A") {
												$LANE_NO =1;
											} else {
												$LANE_NO =5;
											}
										} else if ($SCHHOUSE == "Y") {
											if ($CHOICE == "A") {
												$LANE_NO =2;
											} else {
												$LANE_NO =6;
											}
										} else if ($SCHHOUSE == "B") {
											if ($CHOICE == "A") {
												$LANE_NO =3;
											} else {
												$LANE_NO =7;
											}
										} else if ($SCHHOUSE == "G") {
											if ($CHOICE == "A") {
												$LANE_NO =4;
											} else {
												$LANE_NO =8;
											}
										} 
										$isql = mysqli_query($splink,"Insert into INTERHOUSE_COMPETITION (SCHHOUSE, TEAM, STUID, COMPETITION_ID, LANE_NO) VALUES (\"".$SCHHOUSE."\",\"".$CHOICE."\", \"".$targetSTUID[$x]."\", \"".$COMPETITION_ID."\",\"".$LANE_NO."\")");
										$msg .= $sclass.$sclassno." ".$sname." ".$txtsuccessaddrecord."<br>";
									}
								} else {
									$msg .= $txtteam." ".$CHOICE." ".$txtexistlimitednumber."<br>";
								}
							}
							$x++;
						}
					} else {
						$msg = $txtdidnotselectteam;
					}
				}
				//finish php save gosaveteameventapplication
				//php deleteteameventplayer
				if (isset($deleteteameventplayer)){
					$sql = mysqli_query($splink,"Delete from INTERHOUSE_COMPETITION where keyid=\"".$targetkeyid."\"");
					$msg =  $txtsuccessdelete;
				}
				//finish php deleteteameventplayer
				//顯示已報名項目參賽者名單
				$sql = mysqli_query($splink,"Select CH_DES, EN_DES, GRADE,SEX from COMPETITIONITEM where  keyid=\"".$COMPETITION_ID."\"");
				while ($row = mysqli_fetch_row($sql)){
					if ($lang == "chi") {
						$eventname = $row[0];
					} else {
						$eventname = $row[1];
					}
					$eventgrade = $row[2];
					$eventsex = $row[3] ;
				}
				?><br />
                <h3><?=$eventname?></h3>
                <center><font color="#FF0000"><?=$msg?></font></center><br />
                <?php
				$sql = mysqli_query($splink,"Select A.keyid,A.TEAM, B.CHNAME, B.ENNAME, B.CLASSCODE, B.CLASSNO, B.SCHHOUSE from INTERHOUSE_COMPETITION AS A inner join studentinfo AS B ON A.STUID=B.STUID where A.COMPETITION_ID=\"".$COMPETITION_ID."\" AND B.SCHHOUSE=\"".$SCHHOUSE."\" ORDER BY A.TEAM, B.CLASSCODE, B.CLASSNO");
				if (mysqli_num_rows($sql) > 0) {
					?>
                    <table><thead><tr><th><?=$txtteam?></th><th><?=$txtname?></th><th><?=$txtclass?></th><th><?=$txtclassno?></th>
                    <th><?=$txthouse?></th><th><?=$txtfunction?></th></tr></thead><tbody>
                    <?php
					while ($row = mysqli_fetch_row($sql)){
						if ($lang =="chi") {
							$name = $row[2];
						} else {
							$name = $row[3];
						}
						?>
                        <tr><td><?=$row[1]?></td><td><?=$name?></td><td><?=$row[4]?></td><td><?=$row[5]?></td><td><?=$row[6]?></td>
                        <td><form action="competition.php" method="POST"><input name="teameventapplication" type="hidden" />
                		<input type="hidden" name="COMPETITION_ID" value="<?php if ($role == "1") { echo $teaCOMPETITION_ID; } else { echo $COMPETITION_ID; }?>" />
                        <input type="hidden" name="targetkeyid" value="<?=$row[0]?>" />
                		<input type="submit" name="deleteteameventplayer" value="<?=$txtdelete?>" />
                        </form></td>
                        </tr>
                        
                        <?php
					}
					?>
                    </tbody></table>
                    <?php
				} else {
					echo $txtnoapplicant."<br>";
				}
				//完成顯示已報名項目參賽者名單
				//報名表
				if ($eventgrade == "A") {
					$gradeyr = " LEFT(DOB,4) < \"".$gradeAyr."\" ";
				} else if (	$eventgrade == "B") {
					$gradeyr = " LEFT(DOB,4 ) >= \"".$gradeAyr."\" AND LEFT(DOB,4) < \"".$gradeByr."\" ";
				} else {
					$gradeyr = " LEFT (DOB,4) >=\"".$gradeByr."\" ";
				}
				if ($eventsex ==1) {
					$sexis = " AND SEX=\"M\"";
				} else if ($eventsex == 2 ) {
					$sexis = " AND SEX=\"F\"";
				} else {
					$sexis = "";
				}
				$sql = mysqli_query($splink,"Select STUID, CHNAME, ENNAME, CLASSCODE, CLASSNO from studentinfo where ".$gradeyr.$sexis." AND SCHHOUSE=\"".$SCHHOUSE."\" ORDER BY CLASSCODE, CLASSNO");
				$namelist = "";
				while ($row = mysqli_fetch_row($sql)){
					if ($lang == "chi") {
						$name = $row[1];
					} else {
						$name = $row[2];
					}
					$namelist .="<option value=\"".$row[0]."\">".$row[3].$row[4]." ".$name."</option>";
				}
				?>
                
                <h3><?=$txtapplicationform?></h3>
                <?=$txtteameventapplicationremarker?>
                <form action="competition.php" method="POST"><input name="teameventapplication" type="hidden" />
                <input type="hidden" name="COMPETITION_ID" value="<?php if ($role == "1") { echo $teaCOMPETITION_ID; } else { echo $COMPETITION_ID; }?>" />
                <input type="radio" name="CHOICE" value="A" id="targetteamA" /><label for="targetteamA">Team A</label>&nbsp;
                <input type="radio" name="CHOICE" value="B" id="targetteamB" /><label for="targetteamB">Team B</label>&nbsp;
                <select name="targetSTUID[]" multiple="multiple" style="height:200px"><?=$namelist?></select>
                <input type="submit" name="gosaveteameventapplication" value="<?=$txtsave?>" />
                </form>
				<?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>';
}
?>