<?php 
include("header.php");

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

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


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


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

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


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

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



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;

$count=get_latest_items('');
$page_cnt=50;
$sno=(($page_index-1)*$page_cnt);  
$pages=ceil($count/$page_cnt); 
$lmt=$sno.",".$page_cnt;

 $get_data= get_lesson('','','','');
$course_master = get_course_master('','','','');
  $gradeings = get_grade('','','','');
  $modules = get_module('','','','');
  
 ?>


 
  <div class="container-fluid w-100 mt-5">
    <div class="row g-0 register-card w-100 bg-light">
		
        <a href="lessons.php" class="btn btn-primary"> + New Lesson</a>
        

		<div class="mx-5 mt-5 mr-5 ">
       <form method="post" action="<?php $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data"  id="" class="">
        <div class="row">
        
        <div class="col">
        <select id="course_id" name="course_id"  class="form-select">
        	<option value="">Select Course:</option>
           <?php  foreach($course_master as $course){?>
                    <option value="<?php echo $course['id']; ?>"><?php echo $course['title'] ?? "";?></option>
                    <?php } ?>
        </select>
        </div>
        
            <div class="col">
        <select id="module_id" name="module_id" class="form-select"  class="form-select">
        	<option value="">Select Module:</option>
            <?php foreach($modules as $module){?>
                   <option value="<?php echo $module['id'];?>"><?php echo $module['module_title'] ?? "";?></option>
                   <?php } ?>
        </select>
        </div>
        
        <div class="col">
        	<input type="text" name="lesson_title" id= "lesson_title" class="form-control" placeholder="Enter Lesson Title." value="<?php echo $lesson_title ?? ""?>" />
        </div>
        
        <div class="col">
         <select  name="lesson_type" class="form-select" id="lesson_type" />
                   <option value="">Select Type:</option>
                   <option value="Video">Video</option>
                   <option value="Text Materials">Text Materials</option>
                   <option value="Assignments">Assignments</option>
                
                   </select>
        </div>
        
        <div class="col">
        <button type="submit" name="search" class="btn btn-success">Search</button>
        </div>
        
        
        
        </div>
        
        </form>
        
        </div>
  
      
        
    
 <div class="container  mr-5 p-4">
 <table class="table table-bordered">
 	<thead >
    	<th>#</th>
      <!--  <th>Category</th>-->
        <th>Module Title</th>
        <th>Lesson Title</th>
        <th>Lession Type</th>
        <th>More Details</th>
        <th>Status</th>
        <th colspan="3">Action</th>
    
    </thead>
    <tbody>
      <?php
				  $sno=1;
				  for($i=0;$i<count($get_data);$i++)

				  {
				  		if($get_data[$i]['status']==0)
						$clr="red";
						else
						$clr="blue";
				  ?>
                    <tr>
                      <td><?php echo $sno; ?></td>
           
               <?php /*?>       <td><?php  echo $get_data[$i]['course_title'] ?? "";?></td><?php */?>
                        
                
                     <td><?php  echo $get_data[$i]['module_title'] ?? "";?></td>
                        <td><?php  echo $get_data[$i]['lesson_title'] ?? "";?></td>
                      <td><?php echo $get_data[$i]['lesson_type']; ?></td>
                       <td><a href="lesson_card.php?view_id=<?php echo $get_data[$i]['id']?>" class="btn btn-info"><i class="bi bi-eye"></i> View Details</a></td>
                     
                   
                     
                    
                      <td style="color:<?php echo $clr; ?>; font-weight:600"><?php echo $get_data[$i]['st']; ?></td>
                      
                     <td>
                <div class="d-flex gap-3">
                <button class="btn btn-sm <?php echo ($get_data[$i]['st'] == 'Active') ? 'btn-success' : 'btn-secondary'; ?>" title="Change Status" onclick="validate_status('<?php echo $get_data[$i]['id']; ?>','<?php echo $get_data[$i]['st1']; ?>','course_report.php');">  <i class="bi <?php echo ($get_data[$i]['st1'] == 'Active') ? 'bi-toggle-on' : 'bi-toggle-off'; ?>"></i> <?php echo $get_data[$i]['st1']; ?> </button>
                <button class="btn btn-sm btn-outline-primary" title="Edit Course" onclick="validate_update('<?php echo $get_data[$i]['id']; ?>','my_courses.php');"><i class="bi bi-pencil-square"></i> </button>
                <button class="btn btn-sm btn-outline-danger" title="Delete Course" onclick="validate_delete('<?php echo $get_data[$i]['id']; ?>','course_report.php');"> <i class="bi bi-trash"></i></button>
                
                </div>
                </td>
                                      
                    </tr>
                     <?php
				   		$sno++;
				   }
				   ?>
                    <tr>
                      <td colspan="8">
                      <?php
					  if($pages>1)
					  {
					  	for($i=1;$i<=$pages;$i++)
						{
						?>
							| <a style="cursor:pointer; color:#000033; font-size:16px;" onClick="pageindex('<?php echo $i; ?>','course_report.php')"><?php echo $i; ?></a>
                        <?php
						}
					  }
					  ?>
                    
                      </td>
                    </tr>
    </tbody>
 </table>
 </div>
 
 
 

 </div>
 </div>
 
 
 
 <script>
 $("#course_id").val('<?php echo $course_id ?? "";?>');
  $("#module_id").val('<?php echo $module_id ?? "";?>');
   $("#lesson_type").val('<?php echo $lesson_type ?? "";?>');
   
 
 </script>
 
 
 <?php include("footer.php");?>