<?php ob_start(); ?>
<!DOCTYPE html>
<html lang="en">

<head>
<?php   require_once 'head_link.php'; ?>

</head>

<body>
 
<?php   require_once 'header1.php'; ?> 
<?php 
	if(isset($_GET['update']) && !empty($_GET['update']))
	{
		$teacher_id = $_GET['update'];
		$stmt_edit = $DB_con->prepare('SELECT * FROM teacher 
		left join product on product.product_id = teacher.subject
		WHERE teacher.teacher_id =:uid');
		$stmt_edit->execute(array(':uid'=>$teacher_id));
		$edit_row = $stmt_edit->fetch(PDO::FETCH_ASSOC);
		extract($edit_row);
	}
	else
	{
		header("Location: index.php");
	}
	
	
	
	if(isset($_POST['btn_save_updates']))
	{
		
		$Teacher_Name = $_POST['Teacher_Name'];
		$CourseName = $_POST['CourseName'];
		$Gender = $_POST['Gender'];
		
		$Address = $_POST['Address'];
		$Phone = $_POST['Phone'];
		$Nid = $_POST['Nid'];
		$Date = $_POST['Date'];
		$status = $_POST['status'];
			
		$imgFile = $_FILES['user_image']['name'];
		$tmp_dir = $_FILES['user_image']['tmp_name'];
		$imgSize = $_FILES['user_image']['size'];
					
		if($imgFile)
		{
			$upload_dir = 'user_images/'; // upload directory	
			$imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION)); // get image extension
			$valid_extensions = array('jpeg', 'jpg', 'png', 'gif'); // valid extensions
			$userpic = rand(1000,1000000).".".$imgExt;
			if(in_array($imgExt, $valid_extensions))
			{			
				if($imgSize < 5000000)
				{
					unlink($upload_dir.$edit_row['userPic']);
					move_uploaded_file($tmp_dir,$upload_dir.$userpic);
				}
				else
				{
					$errMSG = "Sorry, your file is too large it should be less then 5MB";
				}
			}
			else
			{
				$errMSG = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";		
			}	
		}
		else
		{
			// if no image selected the old image remain as it is.
			$userpic = $edit_row['userPic']; // old image from database
		}	
						
		
		// if no error occured, continue ....
		if(!isset($errMSG))
		{
			$stmt = $DB_con->prepare('UPDATE teacher 
									     SET 
										     teacher_name=:Teacher_Name, 
										     subject=:CourseName, 
										     gender=:Gender, 
										     address=:Address, 
										     contact_info=:Phone, 
										     national_id=:Nid, 
										     join_date=:Date, 
										     status=:status, 
											 
										     userPic=:upic
											 
								       WHERE teacher_id=:uid');

			
			$stmt->bindParam(':Teacher_Name',$Teacher_Name);
			$stmt->bindParam(':CourseName',$CourseName);
			$stmt->bindParam(':Gender',$Gender);
			
			$stmt->bindParam(':Address',$Address);
			$stmt->bindParam(':Phone',$Phone);
			$stmt->bindParam(':Nid',$Nid);
			$stmt->bindParam(':Date',$Date);
			$stmt->bindParam(':status',$status);
			
			$stmt->bindParam(':upic',$userpic);
			$stmt->bindParam(':uid',$teacher_id);
				
			if($stmt->execute()){
				?>
                 <script>
				  document.addEventListener('DOMContentLoaded', function () {
					const modalEl = document.getElementById('successModal');
					const progressBar = document.getElementById('progressBar');

					const modal = new bootstrap.Modal(modalEl, {
					  backdrop: 'static',
					  keyboard: false
					});

					modal.show();

					// Ensure animation runs after modal is fully visible
					modalEl.addEventListener('shown.bs.modal', function () {
					  progressBar.style.transition = 'width 3s ease-in-out';
					  progressBar.style.width = '100%';
					});

					// Redirect after 3 seconds
					setTimeout(() => {
					  window.location.href = 'teacher-list';
					}, <?= $MsgDuration; ?>);  // This seeting are in session.php 
				  });
				</script>
                <?php
			}
			else{
				$errMSG = "Sorry Data Could Not Updated !";
			}
		
		}
		
						
	}
	
?>

<?php  require_once 'sidebar.php'; ?>

  <main id="main" class="main">

    <div class="pagetitle bg-light rounded p-2">
      <h1>Teacher Update</h1>  
    </div> 
	
    <section class="section">
	
      <div class="row">
        <div class="col-lg-6">

          <div class="card">
            <div class="card-body">
              
<form method="post" enctype="multipart/form-data" class="form-horizontal">	
     
    
	<table class="table table-hover table-responsive mt-3">
	
    <tr>
    	<td><label class="control-label">Teacher Name </label></td>
        <td><input class="form-control" type="text" name="Teacher_Name" value="<?php echo $teacher_name; ?>" /></td>
    </tr>
	
	<tr>
    	<td><label class="control-label">Course Name </label></td>
		<td>
			 
		<select style="width:100%;" class="form-select chosen-select1" Id="CourseName" name="CourseName">
							<option value="<?php echo $course_id; ?>"><?php echo $product_name; ?></option>

							<?php
							$sql = "SELECT product_id,product_name FROM product ";
							$result = $con->query($sql);

							while ($row = $result->fetch_array()) {
								echo "<option value='" . $row[0] . "'>" . $row[1] . "</option>";
							}
							?>
						</select>

		</td>
    </tr>
	
	<tr>
    <td><label class="control-label">Gender</label></td>
		<td><select style="width:100%;" class="form-select" name="Gender" value="<?php echo $gender; ?>" > 
		
		<option value="<?php echo $gender; ?>"><?php echo $gender; ?></option> 
		<option value="Male">Male</option> 
		<option value="Female">Female </option> </select> </td>
    </tr>
	
	
	<tr>
    	<td><label class="control-label">Address</label></td>
        <td><input class="form-control" type="text" name="Address" placeholder="address" value="<?php echo $address; ?>" /></td>
    </tr>
	
	<tr>
    	<td><label class="control-label">Phone No</label></td>
        <td><input class="form-control" type="text" name="Phone" placeholder="contact info" value="<?php echo $contact_info; ?>" /></td>
    </tr>
	
	<tr>
    	<td><label class="control-label">National Id</label></td>
        <td><input class="form-control" type="text" name="Nid" placeholder="National id" value="<?php echo $national_id; ?>" /></td>
    </tr>
	
	<tr>
    	<td><label class="control-label">Date</label></td>
        <td><input class="form-control" type="date" name="Date" id="Date" value="<?php date_default_timezone_set("Asia/Dhaka"); echo date($join_date) ;?>" /></td>
    </tr>
	
	<tr>
    	<td><label class="control-label">Active/In Active</label></td>
       	<td><select style="width:100%;" class="form-select" name="status"  value="<?php echo $status; ?>" />
		<option value="1">Active</option> 
		<option value="0">In Active</option>
		</select> </td>       
	</tr>
	
		
	
    <tr>
    	<td><label class="control-label">Photo </label></td>
        <td>
        	<p><img src="user_images/<?php echo $userPic; ?>" height="150" width="150" /></p>
        	<input class="input-group" type="file" name="user_image" accept="image/*" />
        </td>
    </tr>
    
    <tr> <td></td> <td></td></tr>
	
    </table>
	
	<tr>
        <td colspan="2"><button type="submit" name="btn_save_updates" class="btn btn-primary">
       <span class="bi bi-check-circle"></span>  Update
        </button>
        
        <a class="btn btn-danger" href="teacher-list"> <span class="bi bi-arrow-left-circle"></span> Cancel </a>
        
        </td>
    </tr>
    
</form>
			  
			  
            </div>
          </div>

          
        </div>
        </div> 

       
    </section>

  </main> 

    <?php  require_once 'footer1.php'; ?>
	
<script src = "js/chosen.js"></script>
<script type = "text/javascript">
	$('.chosen-select').chosen({width: "100%"});
</script>