<?php
include_once('header.php');
if(!empty($_POST['mid']))
$mid=$_POST['mid'];
else if(!empty($_GET['mid']))
$mid=$_GET['mid'];
else
$mid='';

if(!empty($_GET['sid']))
$upd=child_states($_GET['sid'],$_GET['status']);

if(!empty($_GET['did']))
$upd=delete_child($_GET['did']);

if(!empty($_GET['page_index']))
$page_index=$_GET['page_index'];
else if(!empty($_POST['page_index']))
$page_index=$_POST['page_index'];
else
$page_index=1;

$latest=array();
if($mid!='')
$latest=get_child($mid,'','');

$n=get_child_count('','');
if(!empty($n[0]['cnt']))
$count=$n[0]['cnt'];
else
$count=0;
$page_cnt=50; 
$sno=(($page_index-1)*$page_cnt)+1;
$pages=ceil($count/$page_cnt); 
$limit=(($page_index-1)*$page_cnt).",50";

$latest_news=get_child('','',$limit);
?>



  <main class="main-content">
    <div class="page-head">
      <div>
        &nbsp; 
      </div>
      <a href="add_child.php" class="btn btn-primary rounded-pill px-3"><i class="bi bi-person-fill"></i> Add Child</a>
    </div>
    
     <?php
		if(!empty($_GET['fail']))
		{
	  ?>
		<div class="alert alert-danger" align="center"><strong style="color:black"> <centrer> Invalid Child Details </centrer> </strong></div>
	<?php
		}
		if(!empty($_GET['succ']))
		{
	?>
		<div class="alert alert-success" align="center"><strong style="color:black"> <centrer> Child Details Updated Successfully </centrer> </strong></div>
	<?php
		}
	?>

    <section class="content-card">
      <div class="content-card__head">
        <h2>Child Details</h2>
      </div>
      <div class="table-responsive">
      	<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data" id="change_password" name="form1" class="needs-validation" novalidate>
        	<input name="mid" type="hidden" id="mid" value="<?php echo $mid; ?>" />
                     <input name="page_index" type="hidden" id="page_index" value="<?php echo $page_index; ?>" />
        </form>
      
        <table class="table recent-table align-middle mb-0">
          <thead>
            <tr>
              <th>#</th>
              <th>Child Name</th>
              <th>Child Age</th>
              <th>Grade</th>
              <th>Status</th>
              <th colspan="2">&nbsp;</th>
            </tr>
          </thead>
          <tbody>
            <tr>
             <?php
				  for($i=0;$i<count($latest_news);$i++)
				  {
				  
				  	if($latest_news[$i]['status']==0)
					$clr="primary";
					else
					$clr="success";
			  ?>
              <td><?php echo $sno; ?></td>
              <td><?php echo $latest_news[$i]['child_name']; ?></td>
              <td><?php echo $latest_news[$i]['child_age']; ?></td>
              <td><?php echo $latest_news[$i]['grade']; ?></td>
              <td><span class="badge-soft badge-soft--<?php echo $clr; ?>"><?php echo $latest_news[$i]['st']; ?></span></td>
              
               <td><a href="view_courses.php?child_id=<?php echo $latest_news[$i]['id']; ?>"> <span class="badge-soft badge-soft--info"> Browse Courses </span></a></td>
               
               <td><a href="buy_courses.php?child_id=<?php echo $latest_news[$i]['id']; ?>"> <span class="badge-soft badge-soft--secondary"> View Courses </span></a></td>
              
              <?php /*?><td> <a href="add_child.php?mid=<?php echo $latest_news[$i]['id']; ?>" style="text-decoration:none" class="text-dark"> <i class="bi bi-pencil-square"></i> Edit </a></td><?php */?>
            </tr>
           <?php  $sno++; } ?>
            <?php if($pages>1){ ?>
                   <tr>
                   		<td colspan="8"> 
                          <ul class="pagination justify-content-center">
                            <li class="page-item <?php if($page_index<=1){ ?> disabled <?php } ?>">
                              <a class="page-link" onclick="pageindex('<?php echo $page_index-1; ?>','childs_details.php')" >Previous</a>
                            </li>
                             <?php 
								 if($pages>1)
								  {
									for($i=1;$i<=$pages;$i++)
									{
								?>                            
                                    <li class="page-item <?php if($i==$page_index){ ?> active <?php } ?>">
                                      <a class="page-link" onclick="pageindex('<?php echo $i; ?>','childs_details.php')"><?php echo $i; ?></a>
                                    </li>
                                <?php
									}
								  }
								 ?> 
                                 
                                <li class="page-item  <?php if($page_index==$pages){ ?> disabled <?php } ?>">
                                  <a class="page-link" onclick="pageindex('<?php echo $page_index+1; ?>','childs_details.php')">Next</a>
                                </li>
                          </ul>
                        </td>
                   </tr>
                  <?php } if(count($latest_news)==0) { ?>
                  <tr>
                  	<td colspan="8"><center> <strong class="text-danger"> No Data Found</strong> </center></td>
                  </tr>
                  <?php } ?>
          </tbody>
        </table>
      </div>
    </section>
  </main>

<?php include_once('footer.php'); ?>
