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


//設定學年
		$findmax = mysqli_query($link,"Select MAX(SCHYEAR) FROM SCHYEAR");
		while ($getmax = mysqli_fetch_row($findmax)){
			$schyr = $getmax[0];
		}
		if (!isset($targetschoolyear)){
			$targetschoolyear = $schyr;
		}

//完成設定學年
//學年選擇表		
			$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>";
			}
//完成學年選擇表

	require "templates/header.php"; 



	//設定陸運會工作人員職位
	if (isset($setupsportdaystudentworker)) {
		//save record
		if (isset($saveduty)){
			if (!isset($allowapply)) {
				$allowapply = 0;
			}
			if (isset($targetkeyid)){
				$sql = mysqli_query($link,"Update ECA_sportdaystudentworker SET CH_DES=\"".$CH_DES."\", EN_DES=\"".$EN_DES."\", NEED_NUM=\"".$NEED_NUM."\", REQUIRE_SEX=\"".$REQUIRE_SEX."\",CH_REMARK=\"".$CH_REMARK."\",EN_REMARK=\"".$EN_REMARK."\",allowapply=\"".$allowapply."\" WHERE keyid=\"".$targetkeyid."\"");	
				unset($targetkeyid);
				echo $txtsuccessupdaterecord;
			} else {
				$sql = mysqli_query($link,"INSERT INTO ECA_sportdaystudentworker (CH_DES,EN_DES,NEED_NUM, REQUIRE_SEX, CH_REMARK, EN_REMARK,allowapply) VALUES (\"".$CH_DES."\",\"".$EN_DES."\",\"".$NEED_NUM."\",\"".$REQUIRE_SEX."\",\"".$CH_REMARK."\",\"".$EN_REMARK."\",\"".$allowapply."\")");
				echo $txtsuccessaddrecord;
			}
		}
		//finish save record
		//delete record
		if (isset($deleteduty)){
			$sql = mysqli_query($link,"Delete from ECA_sportdaystudentworker where keyid=\"".$targetkeyid."\"");
			echo $txtsuccessdelete;
		}
		//finish deltet record
		//save apply date
		if (isset($savemoduleenable)){
			if (!isset($apenable)) {
				$apenable=1;
			}
			$sql = mysqli_query($link, "UPDATE moduleenable SET apenable=\"".$apenable."\" , Start_date=\"".$Start_date."\", End_date=\"".$End_date."\" WHERE  module=\"eca_sportdaystudentworker\"");
			echo $txtsuccesssave;
		}
		//finish save apply date
		?>
        <?=$txtsportdaystudentworkerapplydate?>
        <link rel="stylesheet" href="jscss/jquery.datepick.css">
        <script src="jscss/jquery.plugin.js"></script>
        <script src="jscss/jquery.datepick.js"></script>
        <script>
        $(document).ready(function() {
            $( "#Start_date" ).datepick({
                dateFormat: 'yyyy-mm-dd',
                        minDate: "<?=date("Y-m-d")?>",
                        maxDate: new Date(<?=$targetschoolyear+2?>,8,0),
                monthsToShow: 1, 
                
                });
            $( "#End_date" ).datepick({
                dateFormat: 'yyyy-mm-dd',
                        minDate:"<?=date("Y-m-d")?>",
                        maxDate: new Date(<?=$targetschoolyear+2?>,8,0),
                monthsToShow: 1, 
                });
         });
        </script> 
        <?php
		$sql = mysqli_query($link,"Select * from moduleenable where module=\"eca_sportdaystudentworker\"");
		while ($row= mysqli_fetch_row($sql)){
			?>
            <form action="eca.php" method="post"><input type="hidden" name="setupsportdaystudentworker" />
            <table border="1">
            <tr><td><?=$txtallowapp?></td><td><?=$txtstartdate?></td><td><?=$txtenddate?></td><td><?=$txtfunction?></td></tr>
            <tr><td align="center"><input type="checkbox" value="1" <?php if ($row[2]== 1) { echo "checked";}?> name="apenable" /></td>
            <td><input type="text" value="<?=$row[3]?>" name="Start_date" id="Start_date"/></td>
            <td><input type="text" value="<?=$row[4]?>" name="End_date" id="End_date" /></td>
            <td><input type="submit" value="<?=$txtsave?>" name="savemoduleenable"></td></tr></table></form>
            <?php
		}
		?>
        <hr width="60%" />
        
        <table border="1">
        <tr><td><?=$txtduty?></td><td><?=$txtneed_num?></td><td><?=$txtsex?></td>
        <td><?=$txtremark?></td><td><?=$txtallowapp?></td><td><?=$txtfunction?></td></tr>
        <?php
		$sql = mysqli_query($link,"Select * from ECA_sportdaystudentworker");
		while ($row = mysqli_fetch_row($sql)){
			if ($row[4] == 0 ) {
				$selectsex = "<option value=\"0\" selected>".$txtboy."/".$txtgirl."</option><option value=\"1\">".$txtboy."</option><option value=\"2\">".$txtgirl."</option>";
			} else if ($row[4] == 1) {
				$selectsex = "<option value=\"0\">".$txtboy."/".$txtgirl."</option><option value=\"1\" selected>".$txtboy."</option><option value=\"2\">".$txtgirl."</option>";
			} else {
				$selectsex = "<option value=\"0\">".$txtboy."/".$txtgirl."</option><option value=\"1\">".$txtboy."</option><option value=\"2\" selected>".$txtgirl."</option>";
			}
			
			?>
            <tr><form action="eca.php" method="POST"><input type="hidden" name="setupsportdaystudentworker" /><td><?=$txtchi?>：<input type="text" value="<?=$row[1]?>" name="CH_DES" /><br /><?=$txteng?>：
            <input type="text" value="<?=$row[2]?>" name="EN_DES" /></td>
            <td align="center"><input type="text" value="<?=$row[3]?>" name="NEED_NUM" style="width:30px; text-align:center" /></td>
            <td><select name="REQUIRE_SEX"><?=$selectsex?></select><input type="hidden" name="targetkeyid" value="<?=$row[0]?>" /></td>
            <td><input type="text" value="<?=$row[5]?>" name="CH_REMARK" /><br />
            <input type="text" value="<?=$row[6]?>" name="EN_REMARK" /></td><td align="center"><input type="checkbox" name="allowapply" value="1" <?php if ($row[7] ==1) { echo "checked";}?>/></td>
            <td><input type="submit" name="saveduty" value="<?=$txtsave?>" /><input type="submit" name="deleteduty" value="<?=$txtdelete?>" /></td></form>
            </tr>
            <?php
		}
		?>
                <tr><form action="eca.php" method="POST"><input type="hidden" name="setupsportdaystudentworker" />
        <td><?=$txtchi?>：<input type="text" value="" name="CH_DES" /><br /><?=$txteng?>：
            <input type="text" value="" name="EN_DES" /></td>
            <td align="center"><input type="text" value="" name="NEED_NUM" style="width:30px; text-align:center" /></td>
            <td><select name="REQUIRE_SEX"><option value="0"><?=$txtboy."/".$txtgirl?></option><option value="1"><?=$txtboy?></option><option value="2"><?=$txtgirl?></option></select></td>
            <td><input type="text" value="<?=$row[5]?>" name="CH_REMARK" /><br />
            <input type="text" value="<?=$row[6]?>" name="EN_REMARK" /></td><td align="center"><input type="checkbox" name="allowapply" value="1" <?php if ($row[7] ==1) { echo "checked";}?>/></td>
            <td><input type="submit" name="saveduty" value="<?=$txtadd?>" /></td></form>
        </tr>

        </table>
        <?php
	}
	//完成設定陸運會工作人員職位





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