<?php 
include("header.php");
if(!empty($_GET['view_id']))
$view_id = $_GET['view_id'];
elseif(!empty($_POST['view_id']))
$view_id = $_POST['view_id'];
else
$view_id = '';

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



if(!empty($_GET['did']) && !empty($_GET['view_id']))
$upd=delete_module($_GET['did'],$_GET['view_id']);

if(!empty($_GET['ldid']) && !empty($_GET['view_id']))
$upd=delete_lesson_data($_GET['ldid'],$_GET['view_id']);

if(isset($_POST['save_module'])){
modules($view_id);
}

 $get_data= get_inst_course($view_id,'','','');
 
 $modules = get_module('',$view_id,'','','');

?>
<div class="card shadow border-0 mb-4 w-100">


    <div class="card-header bg-dark text-white d-flex justify-content-between align-items-center">
        <h5 class="mb-0"><i class="bi bi-list"></i>  <?php echo  $get_data[0]['course_title'] ?? ""; ?>  </h5>

	<div>
    

</div>
    </div>


    <div class="list-group list-group-flush">

        <?php foreach($modules as $m){ ?>

 
<div class="list-group-item">

    <div class="d-flex justify-content-between align-items-center">

      
        <div>
            <i class="bi bi-folder text-primary"></i>
            <strong ><?php echo $m['module_title']; ?></strong>
        </div>

     
        <button class="btn btn-sm btn-light toggle-btn"
            onclick="toggleLessons(<?php echo $m['id']; ?>, this)">
            <i class="bi bi-plus-lg"></i>
        </button>

    </div>

  
    <div id="lesson_<?php echo $m['id']; ?>" class="mt-2 ms-4 d-none">

      
       

        <?php $lessons = get_lesson('', $view_id, $m['id'], '', '', ''); 
        if(!empty($lessons)){ foreach($lessons as $l){ ?>

        <div class="d-flex justify-content-between align-items-center py-1 border-bottom">

           
            <div>
            <a href="view_lesson.php?view_id=<?php echo $view_id; ?>&lesson_id=<?php echo $l['id']; ?>"  class="text-decoration-none text-bark">
                <i class="bi bi-play-circle text-secondary"></i>
                <?php echo $l['lesson_title']; ?>

                <span class="badge bg-light text-dark ms-2">
                    <?php echo ucfirst($l['lesson_type']); ?>
                </span>
              </a>
                
            </div>

         
            <div class="d-flex gap-2">

                <a href="view_lesson.php?view_id=<?php echo $view_id; ?>&lesson_id=<?php echo $l['id']; ?>" 
                   class="btn btn-sm btn-outline-success" title="View">
                    <i class="bi bi-eye"></i>
                </a>

            </div>

        </div>

        <?php }} else { ?>

        <p class="text-muted small">No lessons added</p>

        <?php } ?>

    </div>

</div>
        <?php } ?>

    </div>

</div>






<div class="modal fade" id="addModuleModal" tabindex="-1">
  <div class="modal-dialog modal-dialog-centered">
    <div class="modal-content shadow">

  
      <div class="modal-header bg-dark text-white">
        <h5 class="modal-title">
            <i class="bi bi-folder-plus"></i> Add New Module
        </h5>
        <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
      </div>

     
      <div class="modal-body">

        <form action="<?php $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data">

       
            <input type="hidden" name="course_id" value="<?php echo $view_id; ?>">

            <div class="mb-3">
                <label class="form-label">Module Title</label>
                <input type="text" name="module_title" id = "module_title" class="form-control" value="" placeholder="Enter module name"  required>
            </div>
				<input type="hidden" name="course_id" id="course_id" value="<?php echo $view_id;?>" />
                <input type="hidden" name="course_title" id="course_title" value="<?php echo  $get_data[0]['course_title'] ?? ""; ?>" />
                
               <input type="hidden" name="mid" id="mid" />
                
           
            <div class="text-end">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">
                    Cancel
                </button>

                <button type="submit" name="save_module" class="btn btn-success">
                    <i class="bi bi-check-circle"></i> Save Module
                </button>
            </div>

        </form>

      </div>

    </div>
  </div>
</div>
<script>



function edit_mod_btn(modid) {

    if (!confirm("Are You Sure To Edit Module Name?")) {
        return false; 
    }

    $.ajax({
        url: "include/ajax.php",
        method: "POST",
        data: { modid: modid },
        success: function (data) {

            var parts = data.trim().split("~");

            if (parts[0] == "get_modid_title") {
                $("#module_title").val(parts[1]);
                $("#mid").val(modid);

           
                var myModal = new bootstrap.Modal(document.getElementById('addModuleModal'));
                myModal.show();
            }
        },
        error: function () {
            alert("Something went wrong!");
        }
    });

    return false; 
}


function delete_mod_btn(delete_id,view_id){
	if(confirm("Are You Sure To Delete Module?")){
		location.href="all_cv.php?did="+delete_id+"&view_id="+view_id;
	}else{
		return false;
	}

}


function delete_lesson(delete_id,view_id){
	if(confirm("Are You Sure To Delete Lesson?")){
		location.href="all_cv.php?ldid="+delete_id+"&view_id="+view_id;
	}else{
		return false;
	}

}




function toggleLessons(id, btn){

    let el = document.getElementById("lesson_" + id);

    if(el.classList.contains("d-none")){
        el.classList.remove("d-none");

        
        btn.innerHTML = '<i class="bi bi-dash-lg"></i>';

    } else {
        el.classList.add("d-none");

    
        btn.innerHTML = '<i class="bi bi-plus-lg"></i>';
    }
}
</script>

<?php include("footer.php"); ?>