
<h3>
<?php
	$id=$_GET['id'];
	 $result_cost_name=select("cost_name","where cost_id ='$id' ");
	 $show_cost_name=$result_cost_name["cost_name"];
	 echo "<i class=\"fa fa-shopping-cart fa-3x\" aria-hidden=\"true\"></i>  ชื่อกลุ่ม :  <b>$show_cost_name</b> ";
	 //echo $result_cost_name["cost_name"];
?>
</h3>

<?php

//$action=$_GET['action'];
//echo "action=$action<br>";
//นำเอาค่าธรรมเนียมออกจากลุ่ม 
if($_GET['action']=="remove"){
	echo "<center><img src=\"images/reload.gif\" width=\"48\" height=\"48\" alt=\"\"/><br>นำเอาค่าธรรมเนียมออกจากกลุ่ม $show_cost_name</center>";
	$remove_id=$_GET['remove_id'];
	//echo $remove_id;
	//insert(" '','$fcost_subject','$fcost_var' ","cost_item");
	delete("cost_item_list","where id='$remove_id'");
	insert("NULL,'ลบรายการค่าธรรมเนียม ออกจากกลุ่ม : $show_cost_name','$today $time','$_SESSION[name]' ","regis_system_log");
	
	echo "<meta http-equiv=\"refresh\" content=\"0;url=?op=dues&module=itemgroups&detail=show&id=$id#data\">";
	exit();
}
if($_GET['action']=="import"){
	echo "<center><img src=\"images/reload.gif\" width=\"48\" height=\"48\" alt=\"\"/><br>นำเข้าค่าธรรมเนียม $show_cost_name</center>";
	$import_id=$_GET['import_id'];
	$option=$_GET['option'];
	//echo $remove_id;
	insert("NULL,'$id','$import_id','$option'","cost_item_list");
	insert("NULL,'นำเข้ารายการค่าธรรมเนียม : $show_cost_name','$today $time','$_SESSION[name]'","regis_system_log");
	
	echo "<meta http-equiv=\"refresh\" content=\"0;url=?op=dues&module=itemgroups&detail=show&id=$id#data\">";
	exit();
}



?>


 <?php
global $connect;	
$sql="Select * From cost_item_list,cost_item where cost_item_list.cost_id=$_GET[id] and cost_item_list.cost_item_id=cost_item.cost_item_id order by cost_item_list.id ASC"; 
$db_query=mysqli_query($connect,$sql);
$num_rows=mysqli_num_rows($db_query); /* นับ Reccord ที่พบ */
if($num_rows==0){
	echo "<div class=\"alert alert-warning\">  <strong>Warning!</strong> Indicates a warning that might need attention.</div>";
	
}
else
{
?>
   <table class="table table-hover table-striped table-condensed table-sm">
    <thead>
      <tr>
        <th><center>#</center></th>
        <th>รายการ  <?php echo "(มีจำนวน $num_rows รายการ)"; ?></th>
        <th style="text-align: right">ค่าธรรมเนียม</th>
		<th style="text-align: center">สถานะ</th>
		 <th style="text-align: center" width="25%">จัดการข้อมูล</th> 
      </tr>
    </thead>
    <tbody>
	<?php
		
		
$a=0;
$total_cost=0;		
while($a < $num_rows)
{
	$result = mysqli_fetch_array($db_query);
	$remoce_id=$result['id'];
	$cost_item_id=$result['cost_item_id'];
	$cost_subject=$result['cost_subject'];
	$cost_var=$result['cost_var'];
	$option=$result['option'];
	if($option==0){
		$total_cost=$total_cost+$cost_var;
	}
	?>	
    	
		
      <tr>
        <td align="center"><?php echo $a+1;?></td>
        <td> <?php
		 
		 echo "$cost_subject";

		 ?></td>
        <td align="right">
		  <?php
	
		 echo number_format($cost_var);
	
		 ?>
		  
	    </td>
		<td align="center">
			<?php
			if($option==0){
				echo "บังคับ";
			}else{
				echo "เลือก";
			}
			?></td>
		<td><center>

			<a href="?op=dues&module=itemgroups&detail=show&id=<?=$id?>&action=remove&remove_id=<?=$remoce_id?>#data" class="btn btn-sm btn-warning" role="button"><i class="fa fa-share" aria-hidden="true"></i> นำออกกลุ่ม</a></center>
			
		</td>
      </tr>



   <?php
		$a++;
}
	
	?>
	<tr>
	<td colspan="2" align="right"><b>รวมค่าธรรมเนียม</b></td>
	<td align="right"><?php 
	//	ตรวจสอบค่าธรรมเนียมว่าตรงกันหรือเปล่า
	$result_cost_name=select("cost_name","where cost_id='$id'");
	$cost_value=$result_cost_name["cost_value"];
	//echo $cost_value;
	$total_cost2=number_format($total_cost); 
 	//ปรับปรุงข้อมูล	
 update("cost_name","cost_value='$total_cost' ","where cost_id ='$id' ");
	if($cost_value!=$total_cost){
				echo"<font color=\"#A00003\"><b>$total_cost2</b></font>";
	}else{   echo"<font color=\"#166A00\"><b>$total_cost2</b></font>";  }
		?></td>
	<td></td>	
	<td><center><form id="form1" name="form1" method="post">
	  <input name="total_cost" type="hidden" id="total_cost" value="<?=$total_cost?>">
	  <input name="action" type="hidden" id="action" value="update_totalcost">
	 <input name="update_id" type="hidden" id="update_id" value="<?=$id?>">	
<?php
//$action=$_POST['action'];
//echo $action;
	if($_POST['action']=='update_totalcost'){
				echo "&nbsp;&nbsp;&nbsp;<img src=\"images/reload.gif\" width=\"32\"  alt=\"\"/>";
				$update_id=$_POST['update_id'];
				$total_cost=$_POST['total_cost'];
				update("cost_name","cost_value='$total_cost' ","where cost_id ='$update_id' ");
				echo "<meta http-equiv=\"refresh\" content=\"1;url=?op=dues&module=itemgroups&detail=show&id=$update_id#data\">";	
		//echo "$update_id $total_cost";
	}else{ if($cost_value!=$total_cost){
?>	
		<button type="submit" class="btn btn-success" id="btnAdd" ><i class="fa fa-save fa-fw"></i> บันทึกค่าธรรมเนียม (<?=$total_cost2?> บาท)</button>
<?php }} ?>		
	</form></center></td>	
	</tr>

    </tbody>
  </table>
<?php } ?>


<!-- รายการค่าธรรมเนียมที่จะนำเข้า -->
<center></center>
 <h4><img src="images/1200px-Eo_circle_green_arrow-up.svg.png" width="50"  alt=""/>  รายการค่าธรรมเนียมที่เหลือทั้งหมด  --> คลิก นำเข้ากลุ่ม</h4>
<?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" width="25%">จัดการข้อมูล</th> 
      </tr>
    </thead>
    <tbody>
	<?php
$a=0;
$no=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_item_list=num_record("cost_item_list","where cost_id='$id' and cost_item_id	='$cost_item_id' ");
	//echo $num_cost_item_list;
	if($num_cost_item_list==0){
	?>	
    	
		
      <tr>
        <td align="center"><?php echo $no;?><a name="<?=$cost_item_id?>" id="<?=$cost_item_id?>"></a></td>
        <td> <?php	 echo "$cost_subject"; ?></td>
		
        <td align="right"><?php echo number_format($cost_var); ?> </td>
		<td><center><a href="?op=dues&module=itemgroups&detail=show&id=<?=$id?>&action=import&import_id=<?=$cost_item_id?>&option=0#data" class="btn btn-sm btn-success" role="button"><i class="fa fa-level-up fa-1x" aria-hidden="true"></i> นำเข้ากลุ่ม (บังคับ)</a>  
		
		<a href="?op=dues&module=itemgroups&detail=show&id=<?=$id?>&action=import&import_id=<?=$cost_item_id?>&option=1#data" class="btn btn-sm btn-info" role="button"><i class="fa fa-level-up fa-1x" aria-hidden="true"></i> นำเข้ากลุ่ม (เลือก)</a>
	</center></td>  
      </tr>

        
        

   <?php
				$no++;
			}
		$a++;
}
	
	?>
    </tbody>
  </table>



<!--- หมดรายการ -->
</div>

