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


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


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




 $get_data= get_inst_course('','','','');
$course_master = get_course_master('','','','');
  $gradeings = get_grade('','','','');
  $get_catg = get_category('','','','');
  
  
$sel = executework("select * from  lms_instructor_courses where id = '".$mid."'");
$row = mysqli_fetch_assoc($sel);
 ?>


 
  <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 Course
                    </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_mycourse();">
		
            
           	 <div class="row g-3 m-3 p-2">
             
             	 
              <div class="col-12 col-md-6">
                  <label class="form-label col-md-3"> Category </label>
                   <select  class="form-select" name="category_id" id = "category_id" required>
                  	<option value="">Select :</option>
      					<?php foreach($get_catg as $catgs) { ?>
                    <option value="<?php echo $catgs['id'] ?? "";?>"><?php echo $catgs['title'] ?? ""; ?></option>
                    <?php } ?>
                 
                  </select>
                  
                     <input type="hidden" name="category_title" value="<?php echo $row['category_title'] ?? "";?>" id="category_title"  />
                    <div class="err"></div>
                  </div>
             
             
             
             
             
              <div class="col-12 col-md-6">
                  <label class="form-label col-md-3">Course Title</label>
                   <select name="course_id" class="form-select" id="course_id" required >
                   	<option value="">Select:</option>
                    <?php  foreach($course_master as $course){?>
                    <option value="<?php echo $course['id']; ?>"><?php echo $course['title'] ?? "";?></option>
                    <?php } ?>
                   </select>
                   
                   <input type="hidden" name="course_title" value="<?php echo $row['course_title'] ?? "";?>" id="course_title"  />
                    <div class="err"></div>
                  </div>
            
            	
             
               <div class="col-12 col-md-6">
                  <label class="form-label col-md-3"> Course Type </label>
                   <input type="text" name="course_type" class="form-control" id="course_type" value="<?php echo $row['course_type'] ?? ""; ?>"  readonly="readonly" required />
                    <div class="err"></div>
                </div>
                
        
                 <div class="col-12 col-md-6">
                  <label class="form-label col-md-3"> Course Image </label>
                  <?php if($mid !="") { ?>
                  <img src="courses_upload/<?php echo $row['course_img'] ?? ""; ?>" style="width:100px;height:100px;" alt="course_image" />
                  <?php }?>
               <input type="file" name="course_img" id="course_img" class="form-control" value="<?php echo $row['course_img'] ?? ""; ?>"   />
                    <div class="err"></div>
                  </div>
     
                
                 <div class="col-12 col-md-6">
                  <label class="form-label col-md-3"> Course Video URL </label>
                
               <input type="url" name="course_video" id="course_video" class="form-control" value="<?php echo $row['course_video'] ?? ""; ?>" />
                    <div class="err"></div>
                  </div>
                  
                   <div class="col-12 col-md-6">
                  <label class="form-label col-md-3"> No.of Classes </label>
               <input type="number" name="no_of_class" id="no_of_class" class="form-control" value="<?php echo $row['no_of_class'] ?? ""; ?>" required />
                    <div class="err"></div>
                </div>
                
                 <div class="col-12 col-md-6">
                  <label class="form-label col-md-3"> Course Duration </label>
               <input type="number" name="duration" id="duration" class="form-control" value="<?php echo $row['duration'] ?? ""; ?>" required />
                    <div class="err"></div>
                </div>
                  
                  
                 
              
                
                   <div class="col-12 col-md-6">
                  <label class="form-label col-md-3"> Course Price </label>
               <input type="number" name="course_price" id="course_price" class="form-control" value="<?php echo $row['course_price'] ?? ""; ?>" required />
                    <div class="err"></div>
                </div>
                
                    <div class="col-12 col-md-6">
                  <label class="form-label col-md-3"> Course Discount(%) </label>
               <input type="number" name="course_discount" id="course_discount" class="form-control" value="<?php echo $row['course_discount'] ?? ""; ?>" />
                    <div class="err"></div>
                </div>
                
               <div class="col-12 col-md-6">
                  <label class="form-label col-md-3"> Course Level </label>
                   <select  name="course_level" class="form-select" id="course_level"required >
                   	<option value="">Select Level:</option>
                    <option value="Beginner">Beginner</option>
                    <option value="Intermediate">Intermediate</option>
                   </select>
                    <div class="err"></div>
                </div>
                
                <div class="col-12 col-md-6">
                  <label class="form-label col-md-3"> Course Start </label>
                   <input type="date" name="start_date" class="form-control" id="start_date" value="<?php echo $row['start_date'] ?? ""; ?>" required />
                    <div class="err"></div>
                </div>
                
                
               <div class="col-12 col-md-6">
                  <label class="form-label col-md-3"> Language </label>
                   <input type="text" name="language" class="form-control" id="language" value="<?php echo $row['language'] ?? ""; ?>" required />
                    <div class="err"></div>
                </div>
                
            <div class="col-12 col-md-6">
                  <label class="form-label col-md-3"> Grade </label>      
                  <select name="grade" id="grade" class="form-control" required>
                  <option value="">Select Grade:</option>
                  <?php foreach($gradeings as $grade){?>
                  <option value="<?php echo $grade['id']?>" ><?php echo $grade['age']." ".$grade['grade'] ?? "";?></option>
                  <?php }?>
                  </select>          
                 
                    <div class="err"></div>
                </div>
         </div>
           <div class="row g-3 m-3 p-2">   
              <div class="col-12 col-md-6">
                  <label class="form-label col-md-3"> Course Overview </label>             
                    <textarea   name="course_overview"  cols="3" rows="4"  class="form-control texteditor" id="course_overview" required> <?php echo $row['course_overview'] ?? null; ?> </textarea>
                    <div class="err"></div>         
                </div>
                
            <div class="col-12 col-md-6">
                  <label class="form-label col-md-3">Details</label>             
                    <textarea  name="details" cols="5" rows="5"  class="form-control texteditor" id="details" required> <?php echo $row['details'] ?? ""; ?> </textarea>
                    <div class="err"></div>
                </div>
                
                
             
              
              
              <div class="col-12 col-md-6">
                  <label class="form-label col-md-3"> Additional Info </label>              
                    <textarea   name="additional_info"  cols="3" rows="4" class="form-control" id="additional_info" required> <?php echo $row['additional_info'] ?? null; ?> </textarea>
                    <div class="err"></div>             
                </div>

              </div>

              <div class="col-1 d-grid mt-2 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>
 $("#category_id").val('<?php echo $row['category_id'] ?? "";?>');
  $("#course_id").val('<?php echo $row['course_id'] ?? "";?>');
   $("#course_level").val('<?php echo $row['course_level'] ?? "";?>');
    $("#grade").val('<?php echo $row['grade'] ?? "";?>');
 
 
 
 

$("#category_id").on("change",function(){
 	let catg_id = $("#category_id").val();
	
	$.ajax({
		url: "include/ajax.php",
		method:"POST",
		data: {catg_id : catg_id},
		success:function(data){
			var parts = data.trim().split("~");
			//alert(parts);
			if(parts[0] == "get_catg_title" ){
				$("#category_title").val(parts[1]);
			}else{
				alert("Data Not Found!");
			}
		},
		error:function(error){
			alert(error);
		}

	}); 
 });
 
 
 
 
  $("#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);
		}
	}); 
 });
 
 
 </script>
 
 
 
 
 
 <?php include("footer.php");?>