<?php
include_once('header.php');

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


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;

if(!empty($_GET['sid']))
$upd=purchage_product($_GET['sid'],$_GET['child_id']);

if(!empty($_GET['did']))
$upd=delete_course_details($_GET['did'],$_GET['child_id']);

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

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


<div class="container py-5">
	<h3 class="text-center mb-5">Available Courses</h3>
    <form method="post" action="<?php $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data" name="form1">
        	<input name="child_id" type="hidden" id="child_id" value="<?php echo $child_id; ?>" />
             <input name="page_index" type="hidden" id="page_index" value="<?php echo $page_index; ?>" />
     </form>
     
     <?php
		if(!empty($_GET['succ']))
		{
	?>
		<div class="alert alert-success" align="center"><strong style="color:black"> <centrer> Course Added To Cart Successfully 🛒. </centrer> </strong></div>
	<?php
		}
	?>
     
    <?php
		for($i=0;$i<count($latest_news);$i++)
		{
		
			$sela=executework("SELECT * FROM  lms_course_purchage where child_id ='".$child_id."' and purchage_course_id='".$latest_news[$i]['id']."'");
			$cnta=@mysqli_num_rows($sela);
			$rowa=@mysqli_fetch_array($sela); 
	 ?>
    <div class="course-row">
        <div class="course-header">
            <div class="course-name"><?php echo get_course_name($latest_news[$i]['course_id']); ?></div>
            <div class="course-type"><?php  $course=get_course_type($latest_news[$i]['course_id']); echo strtoupper(str_replace(['_', '-'], ' ', $course)); ?></div>
        </div>
        
        <div class="course-title"> <?php echo $latest_news[$i]['title']; ?> </div>
        <div class="course-desc"><?php $content = $latest_news[$i]['short_discription']; $words = explode(" ",$content); $limited = implode(" ",array_slice($words,0,50)); echo $limited."..."; ?> </div>
       
        <div class="course-footer">
            <div class="meta-badge"> ⏱ Duration : <?php echo $latest_news[$i]['duration']; ?> </div>
            <div class="meta-badge"> 📚 <?php echo $latest_news[$i]['no_classes']; ?> Classes</div>
            
             <div class="course-buttons">
                <button class="btn btn-course btn-view">View Course</button>
                <?php if($cnta==0){ ?>
                	<button class="btn btn-course btn-enroll" onClick="purchage_status('<?php echo $latest_news[$i]['id']; ?>','<?php echo $child_id; ?>','view_course_details.php');">  Enroll </button>
                <?php } else{ ?>
                	<button class="btn btn-course btn-enroll" onClick="purchage_delete('<?php echo $rowa['id']; ?>','<?php echo $child_id; ?>','view_course_details.php');">  Remove </button>
                <?php } ?>
             </div>
        </div>    
    </div>
	<?php $sno++; } ?>
    <?php if($pages>1){ ?>
                   
          <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; ?>','view_course_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; ?>','view_course_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; ?>','view_course_details.php')">Next</a>
                      </li>
                 </ul>
                        
      <?php } if(count($latest_news)==0) { ?>
         <div>
              	<center> <strong class="text-danger"> No Available Courses </strong> </center>
         </div>
      <?php } ?>
</div>


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