<?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=add_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";

$sel=executework("select * from lms_child_details where id='".$child_id."'");
$cnt=@mysqli_num_rows($sel);
$row=@mysqli_fetch_array($sel);
$grade=$row['grade'];

$latest_news=get_course_details('',$grade,'',$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>
    
    <div class="row g-4">
     <?php
		for($i=0;$i<count($latest_news);$i++)
		{
		
			$sela=executework("SELECT * FROM  lms_cart_list 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="col-md-4 d-flex">
            <div class="course-box w-100">
                        
           <!-- <div class="course-title"><?php  //$course=get_course_type($latest_news[$i]['course_id']); echo strtoupper(str_replace(['_', '-'], ' ', $course)); ?></div>-->
            
            <div class="d-flex justify-content-between align-items-center mb-2">
                <div class="course-type"><?php  $course=get_course_type($latest_news[$i]['course_id']); echo strtoupper(str_replace(['_', '-'], ' ', $course)); ?></div>
                <div class="course-price">$<?php echo $latest_news[$i]['course_price']; ?></div>
            </div>
            
            <div class="course-name"><?php echo get_course_name($latest_news[$i]['course_id']); ?></div>
            
            <div class="course-desc"> 
				<?php 
					 $content = urldecode($latest_news[$i]['details']); 
					$text = strip_tags($content);
					$lines = preg_split('/\r\n|\r|\n/', $text);
					// Take first 3 lines
					$first3 = array_slice($lines, 0, 3);
					
					$details = implode("<br>", $first3);
					
					if (count($lines) > 3) {
						$details .= "...";
					}
					
					echo $details;
				 ?>
            </div>
            
            <div class="course-meta">
                <div class="meta-item">
                ⏱ Duration : <?php echo $latest_news[$i]['duration']; ?> Months
                </div>
                
                <div class="meta-item mb-2">
                📚 Classes : <?php echo $latest_news[$i]['no_of_class']; ?>
                </div>
            </div>
            
            <div class="course-buttons">
            
                <a href="@" class="btn btn-course btn-view">
                View Course
                </a>
            
                <?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_courses.php','add');">  Enroll </button>
                <?php } else{ ?>
                	<button class="btn btn-course btn-enroll" onClick="purchage_delete('<?php echo $rowa['id']; ?>','<?php echo $child_id; ?>','view_courses.php','remove');">  Remove </button>
                <?php } ?>
            
            </div>
            
            </div>
            </div>

    <?php } if(count($latest_news)==0){ ?>
    <center class="mt-4 text-danger"><strong> No Courses Found</strong></center>
    <?php } ?>
    
    </div>

</div>




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