<?php
$groupCode=$_GET['groupCode'];	
$form=$_GET['form'];	
$id=$_GET['id'];	
$show=$_GET['show'];
$studentStatusCode=$_GET['studentStatusCode'];
?>
<style>
	li{
		list-style-type:none;
	}
</style>
<link href="css/bootstrap2-toggle.css" rel="stylesheet">
<script src="js/bootstrap2-toggle.js"></script> 
<h4>ข้อมูลนักศึกษา </h4>
<div align="right">
<?php
if($form=="summarize" and $show==""){?>	

	<a href="?op=summarize&module=groups&detail=list&id=<?=$id?>#<?=$groupCode?>" class="btn btn-secondary" role="button"> <i class="fa fa-reply" aria-hidden="true"></i> ย้อนกลับ</a>
<?php }elseif($form=="advisor" and $show==""){	 ?>
         <a href="pages/qrscan.php" class="btn btn-primary" role="button"><i class="fa fa-qrcode fa-1x" aria-hidden="true"></i> สแกน QR Code จากบัตรนักศึกษา</a>
  <a href="?" class="btn btn-secondary" role="button"> <i class="fa fa-reply" aria-hidden="true"></i> ย้อนกลับ</a>
<?php }elseif($form=="summarize" and $show=="all"){	 ?>
		<a href="?op=summarize&module=groups-list&show=all" class="btn btn-secondary" role="button"> <i class="fa fa-reply" aria-hidden="true"></i> ย้อนกลับ</a>	
<?php }else{ ?>
	<a href="?op=student&module=groups-list&show=all#<?=$groupCode?>" class="btn btn-secondary" role="button"> <i class="fa fa-reply" aria-hidden="true"></i> ย้อนกลับ</a>
<?php } ?>	
</div><br>
<?php
if($_GET['groupCode']!="search"){	
//global $connect;		
//ดึงข้อมูลกลุ่มเรียน	
$result_std2018=select("std2018_studentgroup","where groupCode='$groupCode'");
	$grade=$result_std2018['grade'];
	$groupCode=$result_std2018['groupCode'];
	$groupName=$result_std2018['groupName'];
	$teacherFirstname=$result_std2018['teacherFirstname'];
	$teacherLastname=$result_std2018['teacherLastname'];	
	  

	
	 

?>

<p>ระดับ          <b><?=$grade?></b><br>
	  รหัสกลุ่ม      <b><?=$groupCode?></b>  <b><?=$groupName?></b>  <br>
	  ครูที่ปรึกษา <b><?php if($teacherFirstname!=""){ echo "ครู$teacherFirstname    $teacherLastname"; }?></b>

</p>
<?php }else{ ?>
  <div class="card">
    <div class="card-body">
<form action="" method="post" class="form-inline">
  <label for="email">ค้นหาจาก ชื่อ, นามสกุล, รหัสนักศึกษา&nbsp;</label>
  <input name="fsearch" type="text" required="required" class="form-control" id="fsearch" placeholder="คำที่ค้นหา">&nbsp;&nbsp;
    <button type="submit" class="btn btn-primary" id="btnSearch" ><i class="fa fa-search fa-fw"></i> ค้นหา</button>
    <input name="action" type="hidden" id="action" value="search">
</form> 

    </div>
  </div>


<?php } ?>

<?php
//บันทึกค่ากลุ่มเรียนที่เลือก
if($_POST['action']=='add'){
    $groupCode=$_GET['groupCode'];
    //ลบข้อมูลกลุ่มเรียนที่มีทั้งหมด
	global $connect;	
   
	$sql="Select * From checking_logs where groupCode='$groupCode'  AND checking_date='$today'  AND checking_note='Select' ";
	$db_query=mysqli_query($connect,$sql);
	$num_rows=mysqli_num_rows($db_query); 
    //echo "num_rows=$num_rows<br>";
	$a=0;
	while($a < $num_rows)
	{
		$result = mysqli_fetch_array($db_query);
		$clear_ip=$result['checking_id'];
        delete("checking_logs","where checking_id='$clear_ip'");
        //echo "clear_ip=$clear_ip<br>";
   
		$a++;
	}

    

	
	//เพิ่มกลุ่มเรียนที่เลือก
	$studentCode_Original_array=$_POST['studentCode_Original_array'];
	$studentCode_array=$_POST['studentCode_array'];
	$level_add=$_GET['serachCode'];
	if($studentCode_array!=""){
				for ($i=0;$i<count($studentCode_array);$i++) {
					$code_no = $studentCode_array[$i];
					//echo "$code_no<br>";
					//ตรวจสอบว่ามีอยู่หรือเปล่า 
					$num8=num_record("checking_logs","where studentCode='$code_no' AND checking_date='$today' ");
					if($num8==0){
                        insert("NULL,'$academicYear','$semester','$groupCode','$code_no','$today','$time','$teacherIdcard','Select' ","checking_logs");    
					}
				}
		echo "<div class=\"alert alert-success\"><strong>Success!</strong> บันทึกรายการที่เลือก <b>$i</b> ราย เรียบร้อยแล้ว.</div>";
	}
	
	
	
}		
?>

<?php if($_GET['groupCode']!="search"){ ?>
<div align="right">
วันที่ <?=thai22($day_log)?>


</div><?php } ?><br>
<?php
//$sql="Select * From std2018_student,prefix where std2018_student.groupCode='$groupCode' AND std2018_student.studentStatusCode='03'  AND std2018_student.prefixID=prefix.prefix_id  order by std2018_student.studentCode ASC";
if($_POST['action']=="search"){
	$fsearch=$_POST['fsearch'];
	echo "<br>fsearch=$fsearch";
	$sql="Select * From std2018_student where firstname LIKE '%$fsearch%' OR  surname LIKE '%$fsearch%'  OR  studentCode LIKE '%$fsearch%'  order by studentCode ASC"; 
}else{
	if($_GET['studentStatusCode']=='03'){
	$sql="Select * From std2018_student,prefix where std2018_student.groupCode='$groupCode' AND std2018_student.prefixID=prefix.prefix_id AND std2018_student.studentStatusCode='03'  order by std2018_student.studentCode ASC"; 	
	}else{
	$sql="Select * From std2018_student,prefix where std2018_student.groupCode='$groupCode' AND std2018_student.prefixID=prefix.prefix_id  order by std2018_student.studentCode ASC"; 
	}
	
}
	
$db_query=mysqli_query($connect,$sql);
$num_rows=mysqli_num_rows($db_query); /* นับ Reccord ที่พบ */
//echo "มีจำนวน <b>$num_rows</b> คน";	
if($num_rows==0)
{
	echo "<div class=\"alert alert-warning\"><strong>Warning!</strong> ไม่พบข้อมูลนักศึกษา.</div>";
}else{
//--- เช็คช่วงเวลา/วันการเช็ครายชื่อร่วมกิจกรรมหน้าเสาธง
//echo "checking_time=$checking_time<br>";    
if($checking_time=='1'){    
    if($time>=$checking_time_start AND $time<=$checking_time_end){
        //echo "เช็คได้<br>";
        //ถ้าอยู่ในช่วงเช็คชื่อให้เช็คว่ากลุ่มนี้เช็คชื่อวันไหน
        $w=date("w",strtotime($today));   
        $num_week_group=num_record("week_group","where academicYear='$academicYear' AND  semester='$semester' AND groupCode='$groupCode' AND w='$w'");
        if($num_week_group!=0){
            $button_status="";
        }else{
            $button_status="disabled";
        echo"<div class=\"alert alert-warning\"><center><strong></strong> นักศึกษากลุ่มนี้ไม่มีกิจกรรมเข้วแถววันนี้ </center></div>";
        }

    }else{
        //echo "หมดเวลาในเช็คชื่อ<br>";
        $button_status="disabled";
        echo"<div class=\"alert alert-warning\"><center><strong></strong> ไม่อยู่ในช่วงเวลาการเช็คชื่อ  ($checking_time_start - $checking_time_end) </center></div>";
    }
}else{
    $w=date("w",strtotime($today));   
    $num_week_group=num_record("week_group","where academicYear='$academicYear' AND  semester='$semester' AND groupCode='$groupCode' AND w='$w'");
    if($num_week_group!=0){
            $button_status="";
    }else{
            $button_status="disabled";
    echo"<div class=\"alert alert-warning\"><center><strong></strong> นักศึกษากลุ่มนี้ไม่มีกิจกรรมเข้วแถววันนี้ </center></div>";
    }
}
    
//--- สิ้นสุด เช็คช่วงเวลา/วันการเช็ครายชื่อร่วมกิจกรรมหน้าเสาธง


$num_student=num_record("std2018_student","where groupCode='$groupCode' AND studentStatusCode='03'");     
$num_student_coming=num_record("checking_logs","where groupCode='$groupCode' AND checking_date='$today'");   
$num_lost =$num_student-$num_student_coming;  
$num_percen=($num_student_coming/$num_student)*100;
$formatted_number = number_format($num_percen, 2);    
?>
<span style="font-size: 14px"> จำนวนนักศึกษาทั้งหมด <b><?=$num_student?></b> คน   มา <b><?=$num_student_coming?></b> คน   ขาด <b><?=$num_lost?></b> คน   คิดเป็น <?=$formatted_number?>%<br>
</span>
 <form method="post" id="form1">        
  <table border="1" class="table table-hover table-striped table-condensed table-sm">
    <thead>
      <tr>
        <th><center>#</center></th>
        <th><center>รหัสประจำตัวนักศึกษา</center></th>
        <th>ชื่อ</th>
		<th>นามสกุล</th> 
	    <th>นำเข้า</th>
	    <th><center>เลือก</center></th>  		  
      </tr>
    </thead>
    <tbody>
<?php
$a=0;
$no=1;
$coming=0;    
while($a < $num_rows)
{
	$result = mysqli_fetch_array($db_query);
	$prefix_name=$result['prefix_name'];
	$firstname=$result['firstname'];
	$surname=$result['surname'];
	$studentCode=$result['studentCode'];
	$studentStatusCode=$result['studentStatusCode'];
	$gpax=$result["gpax"];
	
	 if($studentStatusCode=='03'){ $studentStatus="<font color=\"#0A7E02\">กำลังศึกษา</<font>"; }else{ $studentStatus="<font color=\"#FB4447\">พ้นสภาพ</<font>"; }
	

    //เช็คว่าเช็คชื่อไปแล้วหรือยังกลุ่มเรียนนี้แล้วหรือยัง
	$num_checking_logs=num_record("checking_logs","where studentCode='$studentCode' AND checking_date='$today' ");
    if($num_checking_logs!=0){ 
        $result333=select("checking_logs","where studentCode='$studentCode' AND checking_date='$today' ");
        $checking_note=$result333['checking_note'];
        if($checking_note=="Scan"){
            $checking_note="<i class=\"fa fa-qrcode fa-1x\" ></i>";
            $fff="disabled";
        }else{
            $checking_note="<i class=\"fa fa-hand-o-up fa-1x\" ></i>";
             $fff="";
        }
        
        
        
    }else{
        $checking_note="";
        $fff="";
    }
?>	
      <tr>
        <td><center><?php echo $a+1;?>.<a name="S<?=$studentCode?>" id="S<?=$studentCode?>"></a>
        </center></td>
        <td><center><?=$studentCode?></center></td>
        <td><?php echo "$prefix_name$firstname";?></td>
        <td><?=$surname?></td>
	    <td><?=$checking_note?></td>
	    <td><center>
            
			<?php
            //echo "num_checking_logs=$num_checking_logs";
        
			if($num_checking_logs==0){ 
				echo "<input type=\"checkbox\" name=\"studentCode_array[]\" value=\"$studentCode\" data-toggle=\"toggle\" data-style=\"android\" data-onstyle=\"success\" data-offstyle=\"secondary\" data-on=\"มา\" data-off=\"ขาด\" data-width=\"50\" data-height=\"25\" data-size=\"mini\" $button_status>";  
                
                
			}else{
				echo "<input type=\"checkbox\" name=\"studentCode_array[]\" value=\"$studentCode\" checked data-toggle=\"toggle\" data-style=\"android\" data-onstyle=\"success\" data-offstyle=\"secondary\" data-on=\"มา\" data-off=\"ขาด\" data-width=\"50\" data-height=\"25\" data-size=\"mini\" $fff $button_status>";
                $coming++;
                
			}
			?>
			
			</center></td>		  
      </tr>
<?php
    $no++;
	$a++;
}
?>	
    <tr>
	<td colspan="6"><center><button type="submit "  class="btn btn-sm btn-success" id="btnConfirm" <?=$button_status?> ><i class="fa fa-save fa-fw"></i> บันทึกกลุ่มเรียนที่เลือก</button>
	  <input name="action" type="hidden" id="action" value="add">
	  
	</center></td>	
		</tr>
    </tbody>
  </table>
</form>	
<?php
	}
?>	
	

