<?php
	error_reporting(1);
	@session_start();
	if($_SESSION["logstatus"] === "Active")
	{
	require_once("../db_connect/config.php");
	require_once("../db_connect/conect.php");

	$db = new database();
	global $msg;
	global $table;
	global $chek;
	
	$fetch[2]='';
	$fetch[1]="";
	$prefix=date("y"."m"."d");
	$fetch[0]=$db->withoutPrefix('add_group','id',"32".$prefix,'12');
	

//add dat......................................
if(isset($_POST['add']))
	{
		$id=$db->escape($_POST['id']);
		$groupname=$db->escape($_POST['group']);
		$class_name=$db->escape(isset($_POST['classname'])?$_POST['classname']:"");
		@$exploide=explode("and",$class_name);
		if(!empty($id) && !empty($groupname) && $class_name !="Select One")
		{
			$query="INSERT INTO `add_group` (`id`,`class_id`,`group_name`) VALUES ('".$fetch[0]."','$exploide[0]','$groupname')";
			$resultisnsert=$db->insert_query($query);
			//print_r($query);
			$fetch[0]=$db->withoutPrefix('add_group','id',"32".$prefix,'12');

		}
		else
		{
			$msg="<span class='text-center text-danger glyphicon glyphicon-remove'><strong>&nbsp;Please Fill Up TextField</strong></span>";
		}
	}
	//end add data.........................
//post delete data......................................
if(isset($_POST['Delete']))
	{
		$id=$db->escape($_POST['id']);
		$query="DELETE FROM `add_group` WHERE `id`='$id'";
		$delete=$db->delete_query($query);
		$fetch[0]=$db->withoutPrefix('add_group','id',"32".$prefix,'12');
		print "<script>location='Add_group.php'</script>";
		
	}
//post end delete data.........................................
	//post update data..........................................
if(isset($_POST['Update']))
	{

		$id=$db->escape($_POST['id']);
		$groupname=$db->escape($_POST['group']);
		$class_name=$db->escape(isset($_POST['classname'])?$_POST['classname']:"");
		@$exploide=explode("and",$class_name);
		if(!empty($id) && !empty($groupname) && $class_name !="Select One")
		{
			$query="REPLACE INTO add_group (`id`,`class_id`,`group_name`) VALUES ('$id','$exploide[0]','$groupname')";
			$resultisnsert=$db->update_query($query);
			//print_r($query);
			
		}
		else
		{
			$msg="<span class='text-center text-danger glyphicon glyphicon-remove'><strong>&nbsp;Please Fill Up TextField</strong></span>";
		}
	}
//end post update data...........................
//view data...............................
	if(isset($_POST['View']))
	{
		$query="select * from add_group";
			$result=$db->select_query($query);
		if($result)
		{
				$select_class="SELECT `add_class`.`class_name`,`add_group`.* FROM `add_group` INNER JOIN `add_class`
ON `add_group`.`class_id`=`add_class`.`id` GROUP BY `add_group`.`class_id`";
				$result_class=$db->select_query($select_class);
				if($result_class)
				{
						$table="<div class='col-md-10 col-lg-10 col-md-offset-1' style='margin-top:30px'>"."<table class='table table-responsive table-hover table-bordered' align='center' style='margin-top:-20px;'>";
        $table.="<tr class='warning' >"."<td align='left' colspan='2'>"."<a href='Add_group.php' class='btn btn-primary'>"."<span class='link text-center'>Back<span>"."</a>"."<span class='text-success' style='font-size:18px; padding-left:380px; font-weight:blod;'>View All Group</span>"."</td>"."</tr>";
						while($fetch_class=$result_class->fetch_array())
						{
							$table.="<tr>"."<td colspan='2' align='center'>"."<span class='text-Warning' style='font-size:15px; font-weight:blod;'>".$fetch_class[0]."</span>"."</td>"."</tr>";
							$selec_group="SELECT * FROM `add_group` WHERE `class_id`='".$fetch_class[2]."' ORDER BY `group_name` ASC";
							$result_group=$db->select_query($selec_group);
							if($result_group)
							{
								$table.="<tr class='text-center info'>"."<td class='text-center'>group Name</td>"."<td>Edit</td>"."</tr>";
								while($fetch_group=$result_group->fetch_array())
								{
									$table.="<tr>"."<td class='text-center text-danger'>".$fetch_group[2]."</td>"."<td class='text-center'>";
									$table.="<a href='?edit=$fetch_group[0]'class='btn btn-primary'onclick='return confirm_click()' style='width:80px'>Edit</a>";
									$table.="</td>"."</tr>";
								
								}
								
							}
						}
				
				$table.="</table>"."<div>";
				
				}
				
				

				
		}
		
		
	}

//end view data.....................................

//link edit data...................................	

	if(isset($_GET['edit']))
	{
		$src_text=$db->escape($_GET['edit']);
		$query="SELECT `add_group`.*,`add_class`.`class_name`  FROM `add_group` INNER JOIN `add_class` ON `add_group`.`class_id`=`add_class`.`id` 
WHERE `add_group`.`id`='$src_text'";
		$chek=$db->select_query($query);
		if($chek)
			{
				$fetch=$chek->fetch_array();
			}

	}
//end link edit data..........................
	//link dlt data.....................................
	if(isset($_GET['dlt']))
	{
		$linid=$db->escape($_GET['dlt']);
		$query="DELETE FROM `add_group` WHERE `id`='$linid'";
		$delete=$db->delete_query($query);
		$fetch[0]=$db->withoutPrefix('add_group','id',"32".$prefix,'12');
		print "<script>location='Add_group.php'</script>";

	}
//end link delete data........................

	if(isset($_POST['Exit']))
	{
		print exit();
	}

	if(isset($_POST['Clear']))
	{
		print "<script>location='Add_group.php'</script>";
	}
?>


<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
   
    <title></title>

    <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <script type="text/javascript">
    	function confirm_click()
    	{
    		$confirm_click=confirm('Are You Confirm Update');
    		if($confirm_click===true)
    		{
    			return true;
    		}
    		else
    		{
    			return false;
    		}
    	}

    	function confirm_delete()
    	{
    		$confirm_click=confirm('Are You Confirm Delete');
    		if($confirm_click===true)
    		{
    			return true;
    		}
    		else
    		{
    			return false;
    		}
    	}
    </script>
  </head>
	
  <body>
  	<form name="" action="" method="post"  enctype="multipart/form-data" class="form-horizontal" >
	<?php 
		if(isset($_POST["View"]))
		{
			if($result)
			{
				echo $table;
			}
			else
			{
				 echo "<span class='text-danger' style='font-size:22px;'>"."<strong>"."No Record  Found"."<a href='Add_group.php'>"."Go Back"."</a>"."<strong>"."</span>";
			}
		}
		else
		{
?>
  	<div class="has-feedback col-xs-12 col-sm-8 col-lg-8 col-md-8 col-sm-offset-2 col-md-offset-2">
  	<table align="center" class="table table-responsive box" style="border:1px #CCCCCC solid; margin-top:30px; color: #000;">
  			<tr>
  				<td bgcolor="#f4f4f4" class="warning" colspan="4" bgcolor="#dddddd" align="center"><span style="font-size:22px; color:#333; display:block;">Add Group</span> </td>
  			</tr>
			
		
						
						
					
			<tr>
				<td class="info"> Class Name  </td>
				<td class="info">:</td>
				<td class="info">
					<div class="col-lg-12 has-warning">
					<input type="hidden" readonly="" name="id" value="<?php echo $fetch[0]; ?>" />
						<select name="classname" class="form-control">
							
							<?php 
									if($chek){
							?>
							<option value="<?php echo "$fetch[1]and$fetch[3]"?>"><?php echo $fetch[3];?></option>
							<?php } ?>
							
							<?php 
								$select_section = "SELECT * FROM `add_class`";
								$cheked_query=$db->select_query($select_section);
								if($cheked_query)
								{
									while($fetchsection=$cheked_query->fetch_array())
								{
							?>
							<option value="<?php echo "$fetchsection[0]and$fetchsection[2]"?>"><?php echo $fetchsection[2];?></option>
							<?php } } ?>
						</select>
					</div>
				</td>
				<td class='info'><span class="text-danger text-justify"><strong></strong></span></td>
			</tr>
			<tr>
				<td class="info"> Group Name  </td>
				<td class="info">:</td>
				<td class="info">
					<div class="col-lg-12 has-warning">
						<input type="text" name="group" placeholder="Group Name" value="<?php echo $fetch[2]?>" class="form-control" />
						<span class="glyphicon glyphicon-warning-sign form-control-feedback"></span>
					</div>
				</td>
				<td class='info'><span class="text-danger text-justify"><strong>*&nbsp;Try It English</strong></span></td>
			</tr>
			
  				<td class="danger" colspan="4" bgcolor="#dddddd" align="center"><span>
  					<?php 
  						if(isset($msg))
  						{
  							echo "<strong>".$msg."</strong>";
  						}
  						else 
  						{
  							 echo "<strong>".$db->sms."<strong>";
  						}



  					?>

  				</span> </td>
  			</tr>
			<tr>
  				<td bgcolor="#f4f4f4" class="warning" colspan="4"align="center" >
				<?php 
						if(!$chek)
						{
				?>
					<input type="submit" value="Add" name="add" class="btn btn-primary btn-sm" style="width:80px;" />
					<?php } else {?>
					<input type="submit" value="Update" name="Update" onClick="return confirm_click()" class="btn btn-primary btn-sm" style="width:80px;"/>
					
					<?php } ?>
					<input type="submit" value="View" name="View" class="btn btn-primary btn-sm" style="width:80px;"/>
							
					<input type="submit" value="Clear" name="Clear" class="btn btn-primary btn-sm" style="width:80px;"/>
					<input type="submit" value="Exit" name="Exit" class="btn btn-primary btn-sm" style="width:80px;"/>
				</td>
  			</tr>
  	</table>
		</div>
  		<?php } ?>
	</form>
  
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="bootstrap/js/bootstrap.min.js"></script>
  </body>
</html>
<?php } else { print "<script>location='../adminloginpanel/index.php'</script>";}?>
