<?php
$retrun_id=$_GET['id'];
$regis_group_id=$_GET['regis_group_id'];
$show=$_GET['show'];
?>
<h2>เพิ่มค่าธรรมเนียน และค่าหน่วยกิต </h2>
  <p>จัดการข้อมูลค่าธรรมเนียนการลงทะเบียน และค่าหน่วยกิตของกลุ่มเรียน <b><?php echo $_GET['groupCode']; ?></b> ภาคเรียนที่ <b><?=$semester?></b> ปีการศึกษา <b><?=$academicYear?></b></p>  
<div align="right"> 
<?php
if($_GET['show']=="all"){
?>
<a href="?op=groups&module=show-list&show=all#<?=$regis_group_id?>" class="btn btn-secondary"><i class="fa fa-reply" aria-hidden="true"></i> ย้อนกลับ</a>	
	
<?php	
}else{
	  
?>	
<a href="?op=groups&module=show&detail=list&id=<?=$retrun_id?>&level_id=<?=$level_id?>&retrun=1#data" class="btn btn-primary"><i class="fa fa-reply" aria-hidden="true"></i> ย้อนกลับ</a><?php } ?></div><br>
<?php
global $connect;		
$groupCode=$_GET['groupCode'];	  	
//ดึงข้อมูลกลุ่มเรียน	
$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'];	
	  
	 $num_student=num_record("std2018_student","where groupCode='$groupCode' AND studentStatusCode='03'"); 
?>
  <div class="card bg-light text-dark">
    <div class="card-body">
		
		  <div class="card">
    <div class="card-body"><p>ระดับ <b><?=$grade?></b>  รหัสกลุ่ม <b><?=$groupCode?></b>  <b><?=$groupName?></b>  <br>ครูที่ปรึกษา <b>ครู<?=$teacherFirstname?>   <?=$teacherLastname?> </b>
		  <br>จำนวนนักศึกษา <b><?=$num_student?></b> คน
		</p></div>
  </div>
	 
	
<br>
<?php
//ดึงข้อมูลแผนการเรียน
global $connect;	
$groupCode=$_GET['groupCode'];	  
$sql="Select * From std2018_curi_plan where academicYear='$academicYear' AND semester='$semester' AND GroupCode='$groupCode' order by subjectCode 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
{
?>	
 <div class="card">
    <div class="card-body">		
<center><h4>ภาคเรียนที่ <?=$semester?> ปีการศึกษา <?=$academicYear?> <br>รายวิชาที่ลงทะเบียน</h4></center>
        
    <table class="table table-hover table-striped table-condensed table-sm">
    <thead>
      <tr>
        <th><center>#</center></th>
        <th><center>รหัสวิชา</center></th>
		  <th>ชื่อรายวิชา</th>
		<th><center>หน่วยกิต</center></th>	
      </tr>
    </thead>
    <tbody>
<?php
$a=0;
 $total_credit=0;
while($a < $num_rows)
{
	$result = mysqli_fetch_array($db_query);
	$subjectCode=$result['subjectCode'];
	$subjectNameTh=$result['subjectNameTh'];
	$credit=$result['credit'];
		//นับจำนวนหน่วยกิต
	$total_credit=$total_credit+$credit;


	
	?>	
      <tr>
        <td><center><?php echo $a+1;?></center></td>
        <td><center><?=$subjectCode?></center></td>
        <td><?=$subjectNameTh?></td>
        <td><center><?=$credit?></center></td>
      </tr>
<?php
		$a++;
}
	
?>
	<tr>
		<td colspan="3"><center><b>รวมหน่วยกิต</b></center></td>
		<td><center><b><?php   
 //อัพเดจ รวมค่าธรรมเนียม
	update("regis_group","credit='$total_credit' ","where academicYear='$academicYear' AND semester='$semester' AND groupCode='$groupCode' ");
 echo $total_credit?></b></center></td>
		
	</tr>	
    </tbody>
  </table>
		
<div class="card bg-light text-dark">
	<form method="post" class="form-inline" id="form1">
    <div class="card-body" align="right">
 <?php
if($_POST['action']=="sum"){
	//echo "sum";
	$famount=$_POST['famount'];
	//อัพเดจ รวมค่าหน่วยกิจ
	update("regis_group","cost_credit='$famount' ","where academicYear='$academicYear' AND semester='$semester' AND groupCode='$groupCode' ");
	echo "<meta http-equiv=\"refresh\" content=\"1;url=?op=groups&module=itemadd&id=$retrun_id&level_id=$level_id&groupCode=$groupCode&show=$show&regis_group_id=$regis_group_id\">";
	echo "<center><img src=\"images/reload.gif\" width=\"48\" height=\"48\" alt=\"\"/><br>บันทึกข้อมูลค่าหน่วยกิต</center>";
	exit();

}			
			
?>	     
      จำนวนหน่วยกิต <input name="textfield" type="text" id="textfield" value="<?=$total_credit?>" size="2" readonly="readonly">
      หน่วยกิต 
       X  
		<?php
	 $ccc= substr($groupCode, 2, 1);
 	if($ccc==2){  
		$varCreditsper=0; 
	}else{
		$varCreditsper=$Creditsper;
	}

	//กำหนดค่าหน่วยกิตอัตโนมัต
	//ตรวจสอบว่าในกลุ่มมีค่าหน่วยกิตหรือยัง
	$result=select("regis_group","where academicYear='$academicYear' AND semester='$semester' AND groupCode='$groupCode' ");
	$amount=$result["cost_credit"];
	//echo " $amount";
	if($amount=="0" AND $varCreditsper!=0){
		$amount=$total_credit*$varCreditsper;
	}else{
		$amount=$result["cost_credit"];	
	}


	 $result_regis_group=select("regis_group","where  academicYear='$academicYear' AND semester='$semester' AND groupCode='$groupCode' ");
 	 $cost_credit=$result_regis_group["cost_credit"];
	 
	 	?>
         <input name="textfield" type="text" id="textfield" value="<?=$varCreditsper?>" size="2" readonly="readonly">
         บาท =  <input name="famount" type="text" id="famount" value="<?=$amount?>" size="4"> บาท
		<input name="action" type="hidden" id="action" value="sum">
		
		
       <?php if($cost_credit!=$amount){ ?>  
		<button type="submit" class="btn btn-danger" id="btnAdd" ><i class="fa fa-save fa-fw"></i> บันทึกค่าหน่วยกิต</button>
		<?php }else{ ?>
		<button type="submit" class="btn btn-success" id="btnAdd" ><i class="fa fa-save fa-fw"></i> บันทึกค่าหน่วยกิต</button> 
		<?php }?>
		
		
    </div>
	</form>		
  </div>

		
</div>
  </div>
		<br>
<?php
}
	  ?>

 <div class="card">
    <div class="card-body">
<center><h4>รายการค่าธรรมเนียม</h4>  </center>
<?php
//เพิ่มกลุ่มค่าธรรมเนียม
if($_GET['add']=="group"){
		//echo "<center><img src=\"images/reload.gif\" width=\"48\" height=\"48\" alt=\"\"/><br>เพิ่มกลุ่มค่าธรรมเนียม</center>";
		$cost_id=$_GET['cost_id'];
		//ดึงค่าธรรมเนียมแบบกลุ่ม
	$sql_cost_item="Select * From cost_item_list,cost_item where cost_item_list.cost_id='$cost_id' and cost_item_list.cost_item_id=cost_item.cost_item_id order by cost_item_list.id ASC";
	$db_query_cost_item=mysqli_query($connect,$sql_cost_item);
	$num_rows_cost_item=mysqli_num_rows($db_query_cost_item); /* นับ Reccord ที่พบ */
	//echo "num_rows_cost_item=$num_rows_cost_item";
	$a_cost_item=0;
	$total_cost=0;		
	while($a_cost_item < $num_rows_cost_item)
	{
		$result_cost_item= mysqli_fetch_array($db_query_cost_item);
		$item_id=$result_cost_item['id'];
		$cost_item_id=$result_cost_item['cost_item_id'];
		$cost_subject=$result_cost_item['cost_subject'];
		$cost_var=$result_cost_item['cost_var'];
		$option=$result_cost_item['option'];
		$total_cost=$total_cost+$cost_var;
		//echo "$cost_item_id <br> ";
		//ตรวจสอบว่ามีรายการนี้อยู่หรือไม่
		$num_cost_groups_item_list=num_record("cost_groups_item_list","where cost_item_id='$cost_item_id' AND academicYear='$academicYear' AND semester='$semester' AND groupCode='$groupCode'");
		//echo "num_cost_groups_item_list=$num_cost_groups_item_list<br>";
		if($num_cost_groups_item_list==0){
			//เพิ่มรายการค่าธรรมเนียมในกลุ่ม
			insert("NULL,'$academicYear','$semester','$groupCode','$cost_item_id' ","cost_groups_item_list");
		}
		
		$a_cost_item++;	
	}

}
		
if($_GET['add']=="item"){
		//echo "<center><img src=\"images/reload.gif\" width=\"48\" height=\"48\" alt=\"\"/><br>เพิ่มกลุ่มค่าธรรมเนียม</center>";
		$cost_id=$_GET['cost_id'];	
		$num_cost_groups_item_list=num_record("cost_groups_item_list","where cost_item_id='$cost_id' AND academicYear='$academicYear' AND semester='$semester' AND groupCode='$groupCode'");
		//echo "num_cost_groups_item_list=$num_cost_groups_item_list<br>";
		if($num_cost_groups_item_list==0){
			//เพิ่มรายการค่าธรรมเนียมในกลุ่ม
			insert("NULL,'$academicYear','$semester','$groupCode','$cost_id'","cost_groups_item_list");
		}
}
//ลบออก
if($_GET['del']=="item"){
		$cost_id=$_GET['cost_id'];	
		delete("cost_groups_item_list","where id='$cost_id'");
	
}

?>


<?php
//ดึงข้อมูลค่าธรรมเนียม
global $connect;	
$sql2="Select * From cost_groups_item_list,cost_item where cost_groups_item_list.academicYear='$academicYear' AND cost_groups_item_list.semester='$semester' AND cost_groups_item_list.groupCode='$groupCode'  AND cost_groups_item_list.cost_item_id=cost_item.cost_item_id order by cost_groups_item_list.id ASC";
$db_query2=mysqli_query($connect,$sql2);
$num_rows2=mysqli_num_rows($db_query2); /* นับ Reccord ที่พบ */
if($num_rows2==0){
	echo "<div class=\"alert alert-warning\"><strong>Warning!</strong> ยังไม่ได้เพิ่มค่าธรรมเนียม.</div>";
	
}
else
{


?>
<a name="data" id="data"></a>
    <table class="table table-hover table-striped table-condensed table-sm">
    <thead>
      <tr>
        <th><center>#</center></th>
        <th>รายละเอียดค่าธรรมเนียม</th>
        <th style="text-align: right">ค่าธรรมเนียม</th>
		<th>สถานะ</th>
		<th><center>จัดการ</center></th>  
      </tr>
    </thead>
    <tbody>
	<?php
$a2=0;
$cost_total2=0;	
while($a2 < $num_rows2)
{
	$result2 = mysqli_fetch_array($db_query2);
	$item_id2=$result2['id'];
	$cost_item_id2=$result2['cost_item_id'];
	$cost_subject2=$result2['cost_subject'];
	$cost_var2=$result2['cost_var'];

	$result33=select("cost_item_list","where cost_item_id='$cost_item_id2'");
	//echo $result["color_name"];
	$option=$result33['option'];
	if($option==0){
		$cost_total2=$cost_total2+$cost_var2;
	}	
	//นับจำนวนนักศึกษาในกลุ่มเรียน
	$num_student=num_record("std2018_student","where groupCode='$groupCode' AND studentStatusCode='03'");
	//echo $num_student;
	
	//เช็คว่าเลือกกลุ่มเรียนนี้แล้วหรือยัง
	$num_regis_group=num_record("regis_group","where academicYear='$academicYear' AND semester='$semester' AND groupCode='$groupCode' ");
	
	?>	
		
      <tr>
        <td><center><?php echo $a2+1;?></center></td>
        <td><?php //echo $cost_item_id2; ?><?=$cost_subject2?></td>
        <td style="text-align: right"><?php echo number_format($cost_var2);?> บาท</td>
		<td align="center">
			<?php
			if($option==0){
				echo "บังคับ";
			}else{
				echo "เลือก";
			}
			?></td>
        <td><center><a href="?op=groups&module=itemadd&id=<?=$retrun_id?>&level_id=<?=$level_id?>&groupCode=<?=$groupCode?>&del=item&cost_id=<?=$item_id2?>&show=<?=$show?>&regis_group_id=<?=$regis_group_id?>#data" class="btn btn-sm btn-info" role="button"><i class="fa fa-share" aria-hidden="true"></i> ยกเลิก</a></center></td>
      </tr>
<?php	
	$a2++;
}
	//อัพเดจ รวมค่าธรรมเนียม
	update("regis_group","cost_value='$cost_total2' ","where academicYear='$academicYear' AND semester='$semester' AND groupCode='$groupCode' ");
	?>
	<tr>
 		<td colspan="2"><center><b>รวมค่าธรรมเนียม</b></center></td>
		<td style="text-align: right"><b><?php echo number_format($cost_total2); ?> บาท</b></td>
		<td></td>
	</tr>	
    </tbody>
</table>
		
	 </div></div>
<?php 
}
?>
		<br>

  </div>
  </div>
<br>




<!---- เลือกค่าธรรมเนียม -->
  <!-- Nav tabs -->
  <ul class="nav nav-tabs">
    <li class="nav-item">
      <a class="nav-link active" data-toggle="tab" href="#home">นำเข้าค่าธรรมเนียม เลือกจากกลุ่ม</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" data-toggle="tab" href="#menu1">นำเข้าค่าธรรมเนียม เลือกจากรายการ</a>
    </li>

  </ul>

  <!-- Tab panes -->
  <div class="tab-content">
    <div id="home" class="container tab-pane active"><br>
      <h3>เลือกจากกลุ่ม</h3>
      <p>คลิก ปุ่ม นำเข้า </p>
 <?php
global $connect;	
$sql="Select * From cost_name order by cost_id ASC";
$db_query=mysqli_query($connect,$sql);
$num_rows=mysqli_num_rows($db_query); /* นับ Reccord ที่พบ */
//echo "มีจำนวน <b>$num_rows</b> รายการ <font color=\"#9E0002\">คลิก ปุ่มรายละเอียด เพิ่มรายการค่าธรรมเนียมต่าง ๆ </font>";

?>
   <table class="table table-hover table-striped table-condensed table-sm">
    <thead>
      <tr>
        <th><center>#</center></th>
        <th>ชื่อกลุ่ม</th>
		  <th style="text-align: right">จำนวนรายการ</th>
        <th style="text-align: right">รวมค่าค่าธรรมเนียม</th>
		 <th style="text-align: center">นำเข้า</th> 
      </tr>
    </thead>
    <tbody>
	<?php
$a=0;
while($a < $num_rows)
{
	$result = mysqli_fetch_array($db_query);
	$cost_id=$result['cost_id'];
	$cost_name=$result['cost_name'];
	$cost_value=$result['cost_value'];
	$num_item=num_record("cost_item_list","where cost_id='$cost_id'");
	//echo num_item;
	$level_id=$_GET['level_id'];
	?>	
	   <tr>
        <td><center><?php echo $a+1;?></center></td>
        <td><?=$cost_name?></td>
		<td style="text-align: right"><?=$num_item?> รายการ</td>
		<td style="text-align: right"><?php echo number_format($cost_value);?> บาท</td>
		<td><center><a href="?op=groups&module=itemadd&id=<?=$retrun_id?>&level_id=<?=$level_id?>&groupCode=<?=$groupCode?>&add=group&cost_id=<?=$cost_id?>&show=<?=$show?>&regis_group_id=<?=$regis_group_id?>" class="btn btn-sm btn-success" role="button"><i class="fa fa-level-up" aria-hidden="true"></i> นำเข้า</a></center></td>   
      </tr>	
   <?php
		$a++;
}
	
	?>
    </tbody>
  </table>		
		
		
    </div>
    <div id="menu1" class="container tab-pane fade"><br>
      <h3>เลือกจากรายการ</h3>
       <p>คลิก ปุ่ม นำเข้า </p>
<?php
global $connect;	
$sql="Select * From cost_item order by cost_item_id ASC";
$db_query=mysqli_query($connect,$sql);
$num_rows=mysqli_num_rows($db_query); /* นับ Reccord ที่พบ */
//echo "มีจำนวน <b>$num_rows</b> รายการ";

?>
   <table class="table table-hover table-striped table-condensed table-sm">
    <thead>
      <tr>
        <th><center>#</center></th>
        <th>รายการ</th>
		<th style="text-align: right">ค่าธรรมเนียม</th>  
 		 <th style="text-align: center">จัดการข้อมูล</th> 
      </tr>
    </thead>
    <tbody>
	<?php
$a=0;
$n1=1;
while($a < $num_rows)
{
	$result = mysqli_fetch_array($db_query);
	$cost_item_id=$result['cost_item_id'];
	$cost_subject=$result['cost_subject'];
	$cost_var=$result['cost_var'];
	
	//ตรวจสอบว่ามีรายการนี้อยู่หรือไม่
		$num_cost_groups_item_list=num_record("cost_groups_item_list","where cost_item_id='$cost_item_id' AND academicYear='$academicYear' AND semester='$semester' AND groupCode='$groupCode'");
		//echo "num_cost_groups_item_list=$num_cost_groups_item_list<br>";
		if($num_cost_groups_item_list==0){
	?>	
 	   <tr>
        <td><center><?php echo $n1;?></center></td>
        <td><?=$cost_subject?></td>
		
		<td style="text-align: right"><?php echo number_format($cost_var);?> บาท</td>
		<td><center><a href="?op=groups&module=itemadd&id=<?=$retrun_id?>&level_id=<?=$level_id?>&groupCode=<?=$groupCode?>&add=item&cost_id=<?=$cost_item_id?>&show=<?=$show?>&regis_group_id=<?=$regis_group_id?>" class="btn btn-sm btn-success" role="button"><i class="fa fa-level-up" aria-hidden="true"></i> นำเข้า</a></center></td>   
      </tr>	
   <?php
		  $n1++;
		}
		$a++;
}
	
	?>
    </tbody>
  </table>	   			
		
    </div>

  </div>

