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


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

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['course_id']))
$course_id=$_POST['course_id'];
else if(!empty($_GET['course_id']))
$course_id=$_GET['course_id'];
else
$course_id='';


if(isset($_POST['save'])){
lessons_upload($mid);
}


$sel = executework("SELECT * FROM  lms_instructor_lessons WHERE id = '".$mid."'");
$row = mysqli_fetch_assoc($sel);

$modules = get_module($module_id,$course_id,'','','');

 ?>


 
  <div class="register-shell container-fluid " >
    <div class="row g-0 register-card w-100 bg-light">
   
        

        <div class="container mx-3 ">
        
        			 <div class=" bg-white p-4 mt-3 mx-2 shadow-sm rounded">
                    <h4 class="fw-bold mb-0">
                        <i class="bi bi-plus-circle me-2 text-primary"></i>
                        New Lesson
                    </h4>
                </div>
               
        
        <div class="mx-5">
        			      <?php 
                                if (!empty($_GET['upd'])) {
                                ?>
                                  <div class="alert alert-success text-center" style="font-size:13px; font-weight:bold;">
                                    Module Details updated successfully.
                                  </div>
                                <?php 
                                } elseif (!empty($_GET['succ'])) {
                                ?>
                                  <div class="alert alert-success text-center" style="font-size:13px; font-weight:bold;">
                                   Record Stored Successfully .
                                  </div>
                                <?php 
                                } elseif (!empty($_GET['fail'])) {
                                ?>
                                  <div class="alert alert-warning text-center" style="font-size:13px; font-weight:bold;">
                                  Failed to update Module details.
                                  </div>
                                <?php 
                                } 
                                ?>
                          </div>
          <form method="POST" action="<?php $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data"  id="instructorRegisterForm" class="needs-validation" novalidate onsubmit="validate_lessons();">
		
                   <div class="card border-0 shadow-sm py-0 mt-0 mx-4 mt-4">
            <div class="card-body d-flex justify-content-start align-items-center mr-5 pr-4 "
                 style="background: linear-gradient(135deg, #1e293b, #0f172a); border-radius:10px;">
        
                <div class="text-white">
                    <i class="bi bi-mortarboard me-2 text-primary"></i>
                    <span class="text-secondary fw-bold">Course:</span>
                    <span class="fw-semibold ms-1">
                        <?php echo $modules[0]['course_title']; ?>
                    </span>
                </div>
                &nbsp;&nbsp;<span class="text-warning"> -></span>	&nbsp;	&nbsp;
                <div class="text-white">
                    <i class="bi bi-collection me-2 text-primary"></i>
                    <span class="text-secondary fw-bold">Module:</span>
                    <span class="fw-semibold ms-1">
                        <?php echo $modules[0]['module_title']; ?>
                    </span>
                </div>
        
            </div>
        </div>
        
        <div class="row g-3 mx-3  p-2">
                        
                            
        
                
                  
               <input type="hidden" name="course_id" id="course_id" value="<?php echo $course_id;?>" />
                  <input type="hidden" name="course_title" id="course_title" value="<?php echo $modules[0]['course_title'] ?? "";?>" />
             <input type="hidden" name="module_id" id="module_id" value="<?php  echo ($module_id);?>" />
              <input type="hidden" name="module_title" id = "module_title" value="<?php echo $modules[0]['module_title'] ?? "";?>"  />
             
             
             
               <div class="col-12 col-md-6">
                  <label class="form-label col-md-3">  Lesson Title </label>
                   <input type="text" name="lesson_title" class="form-control" id="lesson_title"  value="<?php echo $row['lesson_title'] ?? "";?>"  required />
                    <div class="err"></div>
                  </div>
                
                  <div class="col-12 col-md-6">
                  <label class="form-label col-md-3"> Lesson Type </label>
                   <select  name="lesson_type" class="form-select" id="lesson_type"  required />
                   <option value="">Select Type:</option>
                   <option value="Video Lessons">Video Lessons</option>
                    <option value="Text Lessons">Text Lessons</option>
                   <option value="Material Lessons">Material Lessons</option>
                   <option value="Assignments">Assignments</option>
                
                   </select>
                    <div class="err"></div>
                  </div>
                  
                  
                     <div class="col-12 col-md-6"  id="m_video_url"  style="display:none;">
                  <label class="form-label col-md-3">  Video Url </label>
                   <input type="Url" name="video_url" class="form-control" id="video_url"  value="<?php echo $row['video_url'] ?? "";?>"  />
                    <div class="err"></div>
                  </div>
                  
                     <div class="col-12 col-md-6" id = "m_video_upload" style="display:none;">
                          <?php if($mid !=""){?>
                  	<img src="lessons_uploads/<?php echo $row['video_upload'] ?? ""; ?>" style="width:100px;height:100px;" alt="image" />
                  <?php }?>
                  <label class="form-label col-md-3">  Video Upload </label>
                   <input type="file" name="video_upload" class="form-control" id="video_upload"  value="<?php echo $row['video_upload'] ?? "";?>"  />
                    <div class="err"></div>
                  </div>
                  
                  
                       <div class="col-12 col-md-6" id="m_material_url" style="display:none;">
                  <label class="form-label col-md-3">  Material URL </label>
                   <input type="url" name="material_url" class="form-control" id="material_url"  value="<?php echo htmlspecialchars_decode($row['material_url'] ?? "");?>"  />
                    <div class="err"></div>
                  </div>
               
                  
                    <div class="col-12 col-md-6" id="m_material_upload" style="display:none;">
                  <label class="form-label col-md-3">  Material Upload </label>
                  <?php if($mid !=""){?>
                  	<img src="lessons_uploads/<?php echo $row['material_upload'] ?? ""; ?>" style="width:100px;height:100px;" alt="image" />
                  <?php }?>
                   <input type="file" name="material_upload" class="form-control" id="material_upload"  value="<?php echo $row['material_upload'] ?? "";?>"  />
                    <div class="err"></div>
                  </div>
                  
                      <div class="col-12 col-md-6" id="m_assignment_upload" style="display:none;">
                  <label class="form-label col-md-3">  Assignment Upload </label>
                        <?php if($mid !=""){?>
                  	<img src="lessons_uploads/<?php echo $row['assignment_upload'] ?? ""; ?>" style="width:100px;height:100px;" alt="image" />
                  <?php }?>
                   <input type="file" name="assignment_upload" class="form-control" id="assignment_upload"  value="<?php echo $row['assignment_upload'] ?? "";?>"  />
                    <div class="err"></div>
                  </div>
                  
                  
                <div class="col-12 col-md-6" id="m_lesson_url" style="display:none;">
                  <label class="form-label col-md-3">  Lesson URL </label>
                   <input type="url" name="lesson_url" class="form-control" id="lesson_url"  value="<?php echo $row['lesson_url'] ?? "";?>"  />
                    <div class="err"></div>
                  </div>
                  
                  
                  
                     <div class="col-12 col-md-6" id="m_text_upload" style="display:none;">
                  <label class="form-label col-md-3">  Lesson Upload </label>
                        <?php if($mid !=""){?>
                  	<img src="lessons_uploads/<?php echo $row['text_upload'] ?? ""; ?>" style="width:100px;height:100px;" alt="image" />
                  <?php }?>
                   <input type="file" name="text_upload" class="form-control" id="text_upload"  value="<?php echo $row['text_upload'] ?? "";?>"  />
                    <div class="err"></div>
                  </div>
                  
            </div>
            
             
             
                  
             <div class="row g-3 m-3 p-2">
                 <div class="col-12 col-md-6" id="m_lesson_content"  >
                  <label class="form-label col-md-3">  Content Box </label>
                   <textarea rows="2" cols="2" name="lesson_content" class="form-control texteditor" id="lesson_content"><?php echo $row['lesson_content'] ?? "";?></textarea>
                    <div class="err"></div>
                  </div>
                  <div class="col-12 col-md-6" id="m_assign_instruction" style="display:none;">
                  <label class="form-label col-md-3"> Assignment Instructions</label>
                   <textarea   rows="4" cols="4" name="assign_instruction" class="form-control texteditor" id="assign_instruction"  ><?php echo $row['assign_instruction'] ?? "";?>  </textarea>
                    <div class="err"></div>
                  </div>
                
               

              </div>

  

              <div class="col-1 d-grid mt-2 ml-5 mb-4">
              <input type="hidden" name="mid" value="<?php echo $mid; ?>">
          
              
                <button type="submit" name="save" class="btn btn-primary btn-register">Submit</button>
              </div>
            </div>
          </form>

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


$("#lesson_type").val('<?php echo $row['lesson_type'] ?? "";?>');



  $("#course_id").on("change",function(){
 	let course_id = $("#course_id").val();
	
	$.ajax({
		url: "include/ajax.php",
		method:"POST",
		data: {course_id : course_id},
		success:function(data){
			var parts = data.trim().split("~");
			//alert(parts);
			
			if(parts[0] == "get_course_master"){
				$("#course_title").val(parts[1]);
				$("#course_type").val(parts[2]);
				$("#course_image").val(parts[3]);
			}else{
				alert("Data Not Found!");
			}
		},
		error:function(error){
			alert(error);
		}
	}); 
 });
 
 
 
 
   $("#module_id").on("change",function(){
 	let module_id = $("#module_id").val();
	
	$.ajax({
		url: "include/ajax.php",
		method:"POST",
		data: {module_id : module_id},
		success:function(data){
			var parts = data.trim().split("~");
			//alert(parts);
			
			if(parts[0] == "get_module_title"){
				$("#module_title").val(parts[1]);
				
			}else{
				alert("Data Not Found!");
			}
		},
		error:function(error){
			alert(error);
		}
	}); 
 });
 
 

 
 
 function course_typeon(){
 	//$("#video_url, #video_upload, #material_url, #material_upload, #assignment_upload,#assign_instruction").val("")
	
 
 	
	let lesson_type = $("#lesson_type").val(); 
 	
	if(lesson_type === "Video Lessons"){
	$("#m_video_url, #m_video_upload,#m_lesson_content ").show();
	$(" #m_material_url, #m_material_upload, #m_assignment_upload,#m_assign_instruction,#m_lesson_url,#m_text_upload").hide();
	$(" #material_url, #material_upload, #assignment_upload,#assign_instruction,#lesson_url,#text_upload").val("")
	
	}else if(lesson_type == "Material Lessons"){
	$(" #m_material_url, #m_material_upload,#m_lesson_content").show();
	$("#m_video_url, #m_video_upload,  #m_assignment_upload,#m_assign_instruction, #m_lesson_url,#m_text_upload").hide();
	$("#video_url, #video_upload, #assignment_upload,#assign_instruction,#lesson_url,#text_upload").val("")
	
	}else if(lesson_type == "Assignments"){
	$(" #m_assignment_upload,#m_assign_instruction ").show();
	$("#m_video_url, #m_video_upload, #m_material_url, #m_material_upload,#m_lesson_url,#m_lesson_content,#m_text_upload ").hide();
	$("#video_url, #video_upload, #material_url, #material_upload, #lesson_url,#lesson_content,$text_upload").val("")
	
	}else if(lesson_type == "Text Lessons"){
	$("#m_text_upload,#m_lesson_content").show();
	$("#m_video_url, #m_video_upload, #m_material_url,#m_material_upload,#m_assignment_upload,#m_assign_instruction,#m_lesson_url ").hide();
	$("#video_url, #video_upload, #material_url, #material_upload, #assignment_upload,#assign_instruction,#lesson_url").val("")
	
	}
 }

 course_typeon();
$("#lesson_type").on("change", course_typeon);
 
 </script>

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