<?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;
		}

//完成設定學年

//讀取學生組別
	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";
			}
		}
	}
//讀取學生組別
	require "templates/header.php"; 

//報名工作人員
	if (isset($applystudentworker)){
		?>
        <h2><?=$txtapplystudentworker?></h2>
        <?php
		$sql = mysqli_query($splink,"Select Start_date, End_date from moduleenable where module=\"workerapply\"");
		while ($row = mysqli_fetch_row($sql)){
			$startdate=$row[0];
			$enddate=$row[1];
		}
		
		if ( $startdate <= date("Y-m-d") && $enddate >= date("Y-m-d")) {
			//check have approved or not
			$csql = mysqli_query($splink,"select MAX(Case choice when 1 then approved END), MAX(Case choice when 2 then approved END), MAX(Case choice when 3 then approved END) from APPLICATION_STUDENTWORKER where SCHYEAR=\"".$targetschoolyear."\" AND STUID=\"".$stuid."\"");
			while ($crow = mysqli_fetch_row($csql)){
				$sachoice1= $crow[0];
				$sachoice2 = $crow[1];
				$sachoice3 = $crow[2];
			}
			//finish check have approved or not
			//php save choice
			if (isset($submitapplicationform)){
				if (isset($sachoice1)){
					if ($sachoice1 == 1  ) {
						$choice1 ="approved";
					}
				} else {
					if (!isset($choice1)) {
						$choice1 = "clear";
					}
				}
				if (isset($sachoice2)){
					if ($sachoice2 == 1  ) {
						$choice2 ="approved";
					}
				} else {
					if (!isset($choice2)) {
						$choice2 = "clear";
					}
				}
				if (isset($sachoice3)){
					if ($sachoice3 == 1  ) {
						$choice3 ="approved";
					}
				} else {
					if (!isset($choice3)) {
						$choice3 = "clear";
					}
				}
				if ( $choice1 == "clear" && $choice2 == "clear" && $choice3 =="clear") {
					$sql = mysqli_query($splink,"Delect from APPLICATION_STUDENTWORKER where SCHYEAR=\"".$targetschoolyear."\" AND STUID=\"".$STUID."\"");
				} else {
					if (($choice1 == $choice2 && $choice1 !== "clear" )|| ($choice1 == $choice3 && $choice3 !== "clear") || ($choice2 == $choice3 && $choice2 !=="clear"))  {
						$msg .= $txtsamechoiceerror;
					} else {
						if ($choice1 == "clear") {
							$sql = mysqli_query($splink,"Delete from APPLICATION_STUDENTWORKER where SCHYEAR=\"".$targetschoolyear."\" AND STUID=\"".$STUID."\" AND choice=\"1\"");
						} else if ($choice1 == "approved") {
							//do nothing
						} else {
							$csql = mysqli_query($splink,"Select * from APPLICATION_STUDENTWORKER where SCHYEAR=\"".$targetschoolyear."\" AND STUID=\"".$STUID."\" AND sw_id=\"".$choice1."\"");
							if (mysqli_num_rows($csql) > 0) {
								while ($row=mysqli_fetch_row($csql)){
									if (($row[3]== "2" && $row[4] !== $choice2 ) || ($row[3] == "3" && $row[4] !== $choice3)) {
										$sql= mysqli_query($splink,"Update APPLICATION_STUDENTWORKER SET sw_id=\"".$choice1."\" WHERE  SCHYEAR=\"".$targetschoolyear."\" AND STUID=\"".$STUID."\" AND choice=\"1\"");
									} else {
										$msg .= $txtchoice1.$txtapplypostalreadyexists."<br>";
									}
								}
							} else {
								$c2sql = mysqli_query($splink,"Select * from APPLICATION_STUDENTWORKER where SCHYEAR=\"".$targetschoolyear."\" AND STUID=\"".$STUID."\" AND choice=\"1\"");
								if (mysqli_num_rows($c2sql) > 0 ) {
									$sql= mysqli_query($splink,"Update APPLICATION_STUDENTWORKER SET sw_id=\"".$choice1."\" WHERE  SCHYEAR=\"".$targetschoolyear."\" AND STUID=\"".$STUID."\" AND choice=\"1\"");
								} else {
									$sql = mysqli_query($splink,"Insert into APPLICATION_STUDENTWORKER (SCHYEAR, STUID, choice, sw_id) VALUES (\"".$targetschoolyear."\" , \"".$STUID."\" , \"1\" , \"".$choice1."\")");
								}
							}
						}
						if ($choice2 == "clear") {
							$sql = mysqli_query($splink,"Delete from APPLICATION_STUDENTWORKER where SCHYEAR=\"".$targetschoolyear."\" AND STUID=\"".$STUID."\" AND choice=\"2\"");
						} else if ($choice2 == "approved") {
							//do nothing
						} else {
							$csql = mysqli_query($splink,"Select * from APPLICATION_STUDENTWORKER where SCHYEAR=\"".$targetschoolyear."\" AND STUID=\"".$STUID."\" AND sw_id=\"".$choice2."\" AND NOT choice=\"1\"");
							if (mysqli_num_rows($csql) > 0) {
								while ($row=mysqli_fetch_row($csql)){
									if ($row[3] == "3" && $row[4] !== $choice3) {
										$sql= mysqli_query($splink,"Update APPLICATION_STUDENTWORKER SET sw_id=\"".$choice2."\" WHERE  SCHYEAR=\"".$targetschoolyear."\" AND STUID=\"".$STUID."\" AND choice=\"2\"");
									} else {
										$msg .= $txtchoice2.$txtapplypostalreadyexists."<br>";
									}
								}
							} else {
								$c2sql = mysqli_query($splink,"Select * from APPLICATION_STUDENTWORKER where SCHYEAR=\"".$targetschoolyear."\" AND STUID=\"".$STUID."\" AND choice=\"2\"");
								if (mysqli_num_rows($c2sql) > 0 ) {
									$sql= mysqli_query($splink,"Update APPLICATION_STUDENTWORKER SET sw_id=\"".$choice2."\" WHERE  SCHYEAR=\"".$targetschoolyear."\" AND STUID=\"".$STUID."\" AND choice=\"2\"");
								} else {
									$sql = mysqli_query($splink,"Insert into APPLICATION_STUDENTWORKER (SCHYEAR, STUID, choice, sw_id) VALUES (\"".$targetschoolyear."\" , \"".$STUID."\" , \"2\" , \"".$choice2."\")");
								}
							}
						}
						if ($choice3 == "clear") {
							$sql = mysqli_query($splink,"Delete from APPLICATION_STUDENTWORKER where SCHYEAR=\"".$targetschoolyear."\" AND STUID=\"".$STUID."\" AND choice=\"3\"");
						} else if ($choice3 == "approved") {
							//do nothing
						} else {
							$csql = mysqli_query($splink,"Select * from APPLICATION_STUDENTWORKER where SCHYEAR=\"".$targetschoolyear."\" AND STUID=\"".$STUID."\" AND sw_id=\"".$choice3."\"");
							if (mysqli_num_rows($csql) > 0) {
								$msg .= $txtchoice3.$txtapplypostalreadyexists."<br>";
							} else {
								$c2sql = mysqli_query($splink,"Select * from APPLICATION_STUDENTWORKER where SCHYEAR=\"".$targetschoolyear."\" AND STUID=\"".$STUID."\" AND choice=\"3\"");
								if (mysqli_num_rows($c2sql) > 0 ) {
									$sql= mysqli_query($splink,"Update APPLICATION_STUDENTWORKER SET sw_id=\"".$choice3."\" WHERE  SCHYEAR=\"".$targetschoolyear."\" AND STUID=\"".$STUID."\" AND choice=\"3\"");
								} else {
									$sql = mysqli_query($splink,"Insert into APPLICATION_STUDENTWORKER (SCHYEAR, STUID, choice, sw_id) VALUES (\"".$targetschoolyear."\" , \"".$STUID."\" , \"3\" , \"".$choice3."\")");
								}
							}
						}
					}
				}
			}
			//finish save choice
			//workerlist
			if ($sexb1g2 == 1) {
				$filter = "NOT REQUIRE_SEX=\"2\"";
			} else {
				$filter = "NOT REQUIRE_SEX=\"1\"";
			}
			//check have selected or not
			$csql = mysqli_query($splink,"select MAX(Case choice when 1 then sw_id END), MAX(Case choice when 2 then sw_id END), MAX(Case choice when 3 then sw_id END) from APPLICATION_STUDENTWORKER where SCHYEAR=\"".$targetschoolyear."\" AND STUID=\"".$stuid."\"");
			while ($crow = mysqli_fetch_row($csql)){
				$schoice1= $crow[0];
				$schoice2 = $crow[1];
				$schoice3 = $crow[2];
			}
			//finish check have selected or not
			$sql= mysqli_query($splink,"Select CH_DES,EN_DES,NEED_NUM,CH_REMARK,EN_REMARK,keyid from studentworker where allowapply=\"1\" AND ".$filter); 
			if (mysqli_num_rows($sql) > 0 ){
				?>
                <form action="studentworker.php" method="POST"><input type="hidden" name="applystudentworker" />
                <table class="alt"><thead>
                <tr><th><?=$txtitem?></th><th width="100px" style="text-align:center"><?=$txtneed_num?></th>
                <th width="200px"><?=$txtremark?></th><th style="text-align:center"><?=$txtchoice1?></th>
                <th style="text-align:center"><?=$txtchoice2?></th><th style="text-align:center"><?=$txtchoice3?></th></tr></thead>
                <tbody>
                <?php
				while ($row = mysqli_fetch_row($sql)) {
					?>
                    <tr><td><?=$row[0]."<br>".$row[1]?></td><td align="center"><?=$row[2]?></td><td><?=$row[3]."<br>".$row[4]?></td>
                    <td align="center"><input type="radio" name="choice1" value="<?=$row[5]?>"  id="choice1<?=$row[5]?>"  <?php if (isset($schoice1) && ($schoice1 == $row[5])) { ?> checked="checked" <?php } if (isset($sachoice1) && ($sachoice1 == 1)) { ?> disabled="disabled" <?php } ?> /><label for="choice1<?=$row[5]?>"></label></td>
                    <td align="center"><input type="radio" name="choice2" value="<?=$row[5]?>" id="choice2<?=$row[5]?>"  <?php if (isset($schoice2) && ($schoice2 == $row[5])) { ?> checked="checked"<?php } if (isset($sachoice2) && ($sachoice2 == 1)) { ?> disabled="disabled" <?php } ?> /><label for="choice2<?=$row[5]?>"></label></td>
                    <td align="center"><input type="radio" name="choice3" value="<?=$row[5]?>" id="choice3<?=$row[5]?>"  <?php if (isset($schoice3) && ($schoice3 == $row[5])) { ?> checked="checked"<?php } if (isset($sachoice3) && ($sachoice3 == 1)) { ?> disabled="disabled" <?php } ?> /><label for="choice3<?=$row[5]?>"></label></td>
                    </tr>
                    <?php
				}
				?>
                <tr><td colspan="3" align="right"><?=$txtcancelchoice?></td><td align="center"><input type="radio" name="choice1" value="clear"  id="choice1no"  <?php if (isset($sachoice1) && ($sachoice1 == 1)) { ?>disabled="disabled"<?php } ?>/><label for="choice1no"></label></td>
                    <td align="center"><input type="radio" name="choice2" value="clear"  id="choice1no" <?php if (isset($sachoice1) && ($sachoice2 == 1)) { ?>disabled="disabled"<?php } ?>/><label for="choice2no"></label></td>
                    <td align="center"><input type="radio" name="choice3" value="clear"  id="choice1no"  <?php if (isset($sachoice1) && ($sachoice3 == 1)) { ?>disabled="disabled"<?php } ?>/><label for="choice3no"></label></td></tr>
                </tbody>
                </table><input type="hidden" name="STUID" value="<?=$stuid?>" />
                <input type="submit" name="submitapplicationform" value="<?=$txtsubmit?>" class="special" />
                
                <?php
			}
			//finish workerlist
			
		} else {
			echo $txtapplicationisnotopen;
		}
	}
//完成報名工作人員

//報名結果
	if  (isset($studentworkerresult)){
		?>
        <h2><?=$txtstudentworkerresult?></h2>
        <?php
		$sql = mysqli_query($splink,"Select B.CH_DES,B.EN_DES from APPLICATION_STUDENTWORKER AS A inner join studentworker AS B ON A.sw_id = B.keyid where A.approved=\"1\" AND A.SCHYEAR=\"".$targetschoolyear."\"");
		if (mysqli_num_rows($sql) > 0){
			while ($row = mysqli_fetch_row($sql)){
				if( $lang == "chi") {
					$swname = $row[0];
				} else {
					$swname = $row[1];
				}
			}
			echo $txtconfirmeddutyis." : " .$swname;
		} else {
			echo $txtnoduty;
		}
	}
//完成報名結果

//工作人員表現
	if (isset($studentworkerperformance)){
		//PHP課外活動表現
		if (isset($submitswscore)) {
			$nostud = sizeof($perfSTUID);
			$x =0;
			while ($x < $nostud) {
				$result= mysqli_query($splink,"UPDATE APPLICATION_STUDENTWORKER SET `PREF_ID`=\"".$PREF_ID[$x]."\" WHERE keyid=\"".$perfSTUID[$x]."\"");
				$x++;
			}
			$msg .= $txtsuccesssave;
		}	
		?>
        <h2><?=$txtstudentworkerperformance?></h2>
        <center><?=$msg?></center><br />
        <?php
		//完成PHP課外活動表現
		//課外活動表現界面
		
		$sql = mysqli_query($splink,"Select A.keyid,B.CHNAME,B.ENNAME,B.CLASSCODE,B.CLASSNO, A.sw_id,A.PREF_ID FROM APPLICATION_STUDENTWORKER AS A inner join studentinfo AS B on A.STUID=B.STUID WHERE A.approved=\"1\" AND A.SCHYEAR=\"".$targetschoolyear."\" AND B.SCHLEAVERSIGN=\"0\" ORDER BY A.sw_id ASC, B.CLASSCODE ASC, B.CLASSNO ASC");
		if (mysqli_num_rows($sql) > 0 ){
			$x =0;
			?>
			<form action="studentworker.php" method="POST"><input type="hidden" name="studentworkerperformance"/>
			<table align="center" border="1"><thead>
			<tr align="center">
				<th width="30"><?=$txtnum?></th>
				<th width="150"><?=$txtname?></th>
				<th width="50"><?=$txtclass?></th>
				<th width="50"><?=$txtclassno?></th>
				<th width="50"><?=$txtduty?></th>
				<th width="100"><?=$txtperformance?></th>
			</tr></thead>
            <tbody>
			<?php
			while ($row = mysqli_fetch_row($sql)) {
				$checkperf = $row[6];
				$ssql = mysqli_query($splink,"Select CH_DES,EN_DES from studentworker where keyid=\"".$row[5]."\"");
				while ($srow = mysqli_fetch_row($ssql)) {
					if ($lang=="chi") {
						$duty= $srow[0];
					} else {
						$duty = $srow[1];
					}
				}
				?>
                <tr align="center">
                <td><?php echo $x+1;?></td>
                <td><?php if ($lang=="chi") { echo $row[1]; } else { echo $row[2]; } ?> </td>
                <td><?=$row[3]?></td>
                <td><?=$row[4]?><input type="hidden" name="perfSTUID[]" value="<?=$row[0]?>" /></td>
                <td><?=$duty?></td>
                <td><div class="select-wrapper"><select name="PREF_ID[]"><option value=""></option><?php
                $ssql=mysqli_query($splink, "Select CH_DES, EN_DES ,CODE_ID FROM SW_PFM WHERE CODE_ST <>\"I\"");
                while ($srow = mysqli_fetch_row($ssql)){ ?>
                    <option value="<?=$srow[2]?>" <?php if($srow[2] == $checkperf) { echo "selected"; }?>><?php if($lang =="chi"){ echo $srow[0]; } else { echo $srow[1];} ?></option><?php
                }
                ?></select></div></td></tr>
                <?php
				$x++;

			}
			?></tbody>
			</table><br />
			<input type="hidden" name="SCHYEAR" value="<?=$targetschoolyear?>"/>
			<input type="submit" name="submitswscore" value="<?=$txtsave?>" class="special" />
			</form>
			<?php
		} else {
		   echo $txtnoapply;
		}
		?>
		</td>
		</tr>
		</table>
		<hr width="700"/>
		<?php

	}
//完成工作人員表現

//工作人員管理
	if (isset($managestudentworker)){
		?>
        <h2><?=$txtmanagestudentworker?></h2>
        <?php
		//php save managestudentworker
		if (isset($savemanagestudentworker)) {
			$x = 0;
			while ($x < sizeof($targetSTUID)){
				$selectpost = "s".$targetSTUID[$x];
				if (isset($$selectpost)){
					$sql = mysqli_query($splink,"Update APPLICATION_STUDENTWORKER SET approved=\"".$$selectpost."\" where keyid=\"".$targetkeyid[$x]."\"");
					if ($$selectpost == 1) {
						$sql = mysqli_query($splink,"Update APPLICATION_STUDENTWORKER SET approved=\"2\" where STUID=\"".$targetSTUID[$x]."\" AND NOT keyid=\"".$targetkeyid[$x]."\""); 
					}
				}
				$x++;
			}
		}
		//finish save managestudentworker
		?>
        <?=$txtaboutmanagestudentworker?><br /><br />
        <?php
		$sql = mysqli_query($splink,"select A.sw_id, B.CH_DES, B.EN_DES, NEED_NUM from APPLICATION_STUDENTWORKER AS A inner join studentworker AS B ON A.sw_id = B.keyid where A.SCHYEAR=\"".$targetschoolyear."\" GROUP BY A.sw_id ORDER BY A.sw_id");
		if (mysqli_num_rows($sql) > 0 ){
			while ($row = mysqli_fetch_row($sql)){
				if ($lang == "chi") {
					$SW = $row[1];
				} else {
					$SW = $row[2];
				}
				?>
                <h3><?=$SW."(".$txtneed_num." ".$row[3].")"?></h3>
                <?php
				$ssql = mysqli_query($splink,"Select A.STUID, A.choice, A.approved , B.CHNAME, B.ENNAME, B.CLASSCODE, B.CLASSNO,A.keyid from APPLICATION_STUDENTWORKER AS A inner join studentinfo AS B on A.STUID=B.STUID where SCHYEAR=\"".$targetschoolyear."\" AND sw_id = \"".$row[0]."\" order by B.CLASSCODE, B.CLASSNO");
				?>
				<form action="studentworker.php" method="POST"><input type="hidden" name="managestudentworker" />
                <input type="hidden" name="sw_id" value="<?=$row[0]?>" />
                <table>
                <thead><tr><th><?=$txtclass?></th><th><?=$txtclassno?></th><th><?=$txtname?></th>
				<th width="20%"><?=$txtfunction?></th><th width="50%"><?=$txtremark?></th></tr></thead>
                <tbody>
				<?php

				while ($srow = mysqli_fetch_row($ssql)){
					if ($lang =="chi"){
						$sname = $srow[3];
					} else {
						$sname = $srow[4];
					}
					$csql = mysqli_query($splink,"Select sw_id, approved from APPLICATION_STUDENTWORKER where SCHYEAR=\"".$targetschoolyear."\" AND STUID=\"".$srow[0]."\" AND NOT sw_id=\"".$row[0]."\"");
					$remark = $txtappliedduty." ： ";
					while ($crow = mysqli_fetch_row($csql)){
						$dsql = mysqli_query($splink,"Select CH_DES, EN_DES from studentworker where keyid=\"".$crow[0]."\"");
						while ($drow = mysqli_fetch_row($dsql)){
							if ($lang == "chi") {
								$swn = $drow[0];
							} else {
								$swn = $drow[1];
							}
							if ($crow[1] == 1) {
								$sta = "(". $txtaccepted." )";
							} else if ($crow[1] == 2) {
								$sta = "(". $txtrejected." )";
							} else {
								$sta = "";
							}
							$remark .= $swn.$sta." ,";
						}
					}
					$remark = substr($remark,0,-1);
					?>
                    <tr><td><?=$srow[5]?></td><td><?=$srow[6]?></td><td><?=$sname?><input type="hidden" name="targetSTUID[]" value="<?=$srow[0]?>" />
                    <input type="hidden" name="targetkeyid[]" value="<?=$srow[7]?>" /></td>
                    <td><input type="radio" name="s<?=$srow[0]?>" value="1" <?php if ($srow[2] == 1) { ?> checked="checked" <?php } ?> id="r<?=$srow[0]?>1<?=$row[0]?>" /><label for="r<?=$srow[0]?>1<?=$row[0]?>"><?=$txtaccept?></label>
                    <input type="radio" name="s<?=$srow[0]?>" value="2"  <?php if ($srow[2] == 2) { ?> checked="checked" <?php } ?> id="r<?=$srow[0]?>2<?=$row[0]?>"/><label for="r<?=$srow[0]?>2<?=$row[0]?>"><?=$txtreject?></label></td>
                    <td><?=$remark?></td></tr>
                    <?php
				}
				?></tbody>
                </table><p><input type="submit" name="savemanagestudentworker" value="<?=$txtsave?>" /></form></p><br /><br />
                <?php
			}
			
			
		}
		?>
        
        <?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>';
}
?>