<?php
error_reporting(0); //hide error
header("Content-Type:text/html; charset=utf-8");
$servername = "localhost";
$username = "id21593489_id17615458_tangb";
$password = "c5t9QN19L(W6EPRq";
$database = "id21593489_id17615458_zadmin_website";

$conn = mysqli_connect($servername,$username,$password)or die("cannot connect to the db");//connect server
mysqli_select_db($conn,$database)or die("cannot open the selected db");//select db
mysqli_query($conn,"set names utf8");//utf8 to select data, allow show chinese
$data=mysqli_query($conn,"SELECT nickName, duty FROM `altar` WHERE id<2000") or die("no data");//all table in contact db
$data2=mysqli_query($conn,"SELECT * FROM `absenceApp` WHERE date >= CURDATE() ORDER BY date;") or die("no data");//all table in contact db

function disRemark($disCount,$remark){
	if ($disCount<6)
		return "(".$remark.")";
}	
?>
<html>
<head> <title>Duty list</title> 
<meta name="viewport" content="width=device-width, initial-scale=1">
	<link rel="stylesheet" href="css/style.css">
    <link rel="stylesheet" href="css/w3.css">
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  
<link rel="icon" type="image/png" href="/images/favicon/favicon-32x32.png" sizes="32x32">
<link rel="icon" type="image/png" href="/images/favicon/favicon-16x16.png" sizes="16x16">
</head>
<body>
<div id="wrapper">
  
	<div id="main" class="clearfix">
	<div class="w3-center w3-blue w3-padding-48">
	<h1>Duty list</h1>
	</div>
	<br>
	<div class="container">
		<div class="row">
		<div class="col-sm-12">
		    <div class="col-sm-6">
			<h2>Absence List:</h2>
			<table class="table table-striped">
			  <thead>
			  <tr>
				<th>MM</th>
				<th>DD</th>
				<th>W</th>
				<th>Name</th>
			  </tr></thead>
			  <tbody><?php
			  $timestamp1="";
			  $timestamp2="";
			  $disCount =0;
				if (!$data2)  die ("");
					else {
					while ($row=mysqli_fetch_array($data2,MYSQLI_ASSOC))  {
					   $timestamp2 = strtotime($row["date"]);
					   if($timestamp2!==$timestamp1||!$timestamp1){
							echo "</td></tr><tr><td>" .date("m", $timestamp2). "</td><td>" .date("d", $timestamp2). "</td><td>" 
								.date("N", $timestamp2). "</td><td>" .$row["name"]  //echo $row["date"]. "~~" .$row["name"]. ": " . $row["remark"]. "\n";
								.disRemark($disCount,$row["remark"]);
							$disCount +=1;  }
						   else echo ", " .$row["name"] .disRemark($disCount,$row["remark"]) ;
						$timestamp1 = $timestamp2;
					}
					}
			?>
			</tbody></table>
			</div>

			<div class="col-sm-6">
			<form action="dutyDisplay.php" method="post">
				<div class="btn-group-toggle" data-toggle="buttons">
				<?php
        				if (!$data)  die ("");
        					else {
        					while ($row=mysqli_fetch_array($data,MYSQLI_ASSOC))  {
        					   echo"<label class='btn btn-primary btn-lg", ($row["duty"] == 1 ? " active ":" "); //https://getbootstrap.com/docs/4.0/components/buttons/
							   echo"'>";
        					   echo"<input type='checkbox' name='nickName[]'", ($row["duty"] == 1 ? " checked ":" ");
        					   echo"value=" .$row["nickName"]. " autocomplete='off' >" . $row["nickName"] ;
        					   echo"</label>";
        					}
        					}
        					
        		mysqli_close($conn);
        		?>
				</div>
        	</div>
		</div>
		</div>
	</div>
		
		<div class="container text-center">
		    <input type="submit" class="btn btn-success btn-lg" value="Submit">
    		</form>
    		<button class="btn btn-default btn-lg" onclick="history.go(-1);return true;">Back</button>
    	</div>	
	</div>	
	</div>
	</div>
	
<div id="footer" class="w3-center w3-dark-grey w3-padding-48 w3-hover-text-black">
	 &copy; 2016
	 <script>new Date().getFullYear()>2016&&document.write("-"+new Date().getFullYear()); </script>
	 , <a href="/">tangb.tk</a>, 
	 All Rights Reserved
     &middot; 
</div>

</body>
</html>