<?php
$day_show=$_POST['day_show'];
if($day_show==""){
    $day_show=$today;
    $w = date("w",strtotime($day_show)); 
    $result_week_day=select("week_day","where w='$w'");
    $day_name=$result_week_day['day_name'];
}else{
    $w = date("w",strtotime($day_show)); 
    $result_week_day=select("week_day","where w='$w'");
    $day_name=$result_week_day['day_name'];
}


?>

<div class="card">
    <div class="card-body">
<h2>ภาคเรียนที่ <?=$semester?> ปีการศึกษา <?=$academicYear?> </h2>
<!-- <div align="right"><a href="?op=summarize&module=groups-list&show=all" class="btn btn-secondary" role="button"> <i class="fa fa-share" aria-hidden="true"></i> แสดงกลุ่มเรียนทั้งหมด</a> </div> -->
<p>ข้อมูลจำนวนกลุ่มเรียน และจำนวนนักเรียน นักศึกษาที่ร่วมกิจกรรมหน้าเสาธง ภาคเรียนที่ <?=$semester?> ปีการศึกษา <?=$academicYear?> </p>
<form action="" method="post" class="form-inline">
<h3></h3>  
<label for="date">เลือก วัน-เดือน-ปี:</label>
<input type="date" class="form-control" name="day_show" id="date" value="<?=$day_show?>">&nbsp;&nbsp;
<input type="submit" class="btn btn-primary"  name="แสดง" id="แสดง" value="แสดง">
</form>
 <hr> 
        
<div id="containerpie" style="min-width:600px;  max-width: 900px; margin: 0 auto"></div>

  
 <?php
global $connect;	
    
    $num_checking_logs=num_record("checking_logs","where checking_date='$day_show' ");
    if($num_checking_logs==0){
    echo "<br><div class=\"alert alert-warning\"><center><strong>ไม่พบข้อมูล</strong></center> </div>";
    }else{
        
 ?>
 <hr>        
  <h3>กลุ่มเรียน/ห้อง ที่ร่วมกิจกรรมฯ วัน <u><?=$day_name?></u> ภาคเรียนที่ <?=$semester?> ปีการศึกษา <?=$academicYear?> </h3>              
<?php
	$sql="Select * From week_group,std2018_studentgroup where week_group.w='$w' AND week_group.academicYear='$academicYear' AND week_group.semester='$semester'  AND week_group.groupCode=std2018_studentgroup.groupCode order by week_group.level_no,week_group.groupCode 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\"><center>ไม่พบข้อมูลกลุ่มเรียน/ห้องเรียน.</center></div>";
}else{    
?>
       
  <table class="table table-hover table-striped table-condensed table-sm table-bordered">
    <thead>
		
      <tr>
        <th width="39"><center>#</center></th>
        <th width="35">ระดับ</th>
        <th width="56">รหัสกลุ่ม</th>
		<th width="300">ชื่อกลุ่ม</th>  
		<th width="40"><center>จำนวน</center></th>
		<th width="40"><center>มา</center></th>
		<th width="40"><center>ขาด</center></th>
		<th width="40"><center>%</center></th>     
      </tr>
    </thead>
    <tbody>
<?php
$sum_incom=0;
$sum_lost=0;    
$sum_student=0;		
$a=0;
$no=1;		
    
while($a < $num_rows)
{
	$result = mysqli_fetch_array($db_query);
	$week_group_id=$result['week_group_id'];
	$regis_group_id=$result['id'];
	$grade=$result['grade'];
	$groupCode=$result['groupCode'];
	$groupName=$result['groupName'];
	$groupAbbr=$result['groupAbbr'];
	$teacherIdcard=$result['teacherIdcard'];
	$teacherFirstname=$result['teacherFirstname'];
	$teacherLastname=$result['teacherLastname'];

	//นับจำนวนนักศึกษาในกลุ่มเรียน
	$num_student=num_record("std2018_student","where groupCode='$groupCode' AND studentStatusCode='03'");
	//echo $num_student;
	$sum_student=$sum_student+$num_student;
	
	//นับจำนวนการเช็คชื่อ
	$num_incom=num_record("checking_logs","where academicYear='$academicYear' AND semester='$semester' AND groupCode='$groupCode' AND checking_date='$day_show' ");
    $num_lost=$num_student-$num_incom;
    
    //รวมนักศึกษาที่มา
    $sum_incom=$sum_incom+$num_incom;
    //รวมนักศึกษาที่ขาด
    $sum_lost=$sum_lost+$num_lost;
    
    //คิดเป็น % ต่อห้องเรียน
    $percen_group=($num_incom/$num_student)*100;
    $percen_group=number_format($percen_group, 2, '.', '');
	
	?>		
      <tr class="success">
        <td><center><?=$no?>.<a name="t<?=$no?>" id="t<?=$no?>"></a>
        </center></td>
        <td><?=$grade?></td>
        <td><?=$groupCode?></td>
		<td><?=$groupName?>  (<?=$groupAbbr?>)<br>
		  ครูที่ปรึกษา (หลัก)  <br><?php echo " ครู$teacherFirstname  $teacherLastname"; ?>
		<?php
		//ดึงข้อมูลที่ปรึกษาร่วม
		  $sql5="Select * From regis_studentgroup where groupCode='$groupCode'";
		  $db_query5=mysqli_query($connect,$sql5);
		  $num_rows5=mysqli_num_rows($db_query5);
		  if($num_rows5!=0){
			  echo "<br>ครูที่ปรึกษา (ร่วม)";
		  }	
		  $a5=0;
		  while($a5 < $num_rows5)
		  {
			$result5 = mysqli_fetch_array($db_query5);
			$del_id=$result5['id'];
			$teacherIdcard2=$result5['teacherIdcard'];
			$teacherFirstname2=$result5['teacherFirstname'];
			$teacherLastname2=$result5['teacherLastname'];
			echo "<br>- ครู$teacherFirstname2 $teacherLastname2";  
			?>
			
			<?php
			$a5++;
		  }

	
		?>
		  </td> 

        <td><center><?=$num_student?></center></td>
	    <td style="color: #050505"><center><?=$num_incom?></center></td>
	    <td><center><?=$num_lost?></center></td>
	    <td><center><?=$percen_group?></center></td>
      </tr>
	
        
	
 <?php
	$no++;			
	$a++;
}

   $percen_incom=($sum_incom/$sum_student)*100;
    $percen_incom=number_format($percen_incom, 2, '.', '');
?>	
	<tr>
		<td colspan="4"><center>
		  <strong>รวม 
		  <?=$level_name?>
		  </strong>
		</center></td>
		<td width="95"><center>
		  <strong><?php echo number_format($sum_student);?></strong>
		</center></td>
        <td style="color: #050505"><center><?=$sum_incom?></center></td>
         <td><center><?=$sum_lost?></center></td>
         <td><center><?=$percen_incom?></center></td>
	</tr>	
    </tbody>
  </table>
<?php } ?>
<!--- รูป pie ---->
<script src="Highcharts-6.0.3/highcharts.js"></script>
<script src="Highcharts-6.0.3/modules/exporting.js"></script>
<?php


//echo" ยังไม่เข้าระบบ $num_nologin <br>";
//echo" รอการชำระเงิน $num_login <br>";
//echo" ชำระเงินเรียบร้อย $num_ok <br>";
?>
 
	  <script type="text/javascript">


// Radialize the colors
Highcharts.setOptions({
    colors: Highcharts.map(Highcharts.getOptions().colors, function (color) {
        return {
            radialGradient: {
                cx: 0.5,
                cy: 0.3,
                r: 0.7
            },
            stops: [
                [0, color],
                [1, Highcharts.Color(color).brighten(-0.3).get('rgb')] // darken
            ]
        };
    })
});

// Build the chart	
Highcharts.chart('containerpie', {
    chart: {
        plotBackgroundColor: null,
        plotBorderWidth: null,
        plotShadow: false,
        type: 'pie'
    },
    title: {
        text: 'สถิติการเข้าร่วมกิจกรรมหน้าเสาธง วัน <?=$day_name?> ที่ <?=thai4($day_show)?>'
       /* text: 'สถิติการเข้าร่วมกิจกรรมหน้าเสาธง วัน <?=$day_name?> ที่ <?=thai4($day_show)?> ภาคเรียนที่ <?=$semester?> ปีการศึกษา <?=$academicYear?> ' */
    },
    tooltip: {
        pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
    },
    plotOptions: {
        pie: {
            allowPointSelect: true,
            cursor: 'pointer',
            dataLabels: {
                enabled: true,
                format: '<b>{point.name}</b>: {point.percentage:.1f} %',
                style: {
                    color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
                },
                connectorColor: 'silver'
            }
        }
    },

    series: [{
        name: 'นักศึกษา',
        data: [
    
            { 
                name: 'ขาด', 
			 	y: <?=$sum_lost?> ,
			 	color: '#A3000B',
			},
            { name: 'มา', 
			 	y: <?=$sum_incom?> ,
			 	color: '#04860C',
			}

        ]
    }]
});
		</script>
<!-----สิ้นสุด รุูป pic ----->

</div>
<?php } ?>    
</div></div>