<?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($_POST['btnsave']))
	{
		$UserId = $_POST['UserId'];
		
		$Teacher_Name = $_POST['Teacher_Name'];
		$CourseName = $_POST['CourseName'];
		$Gender = $_POST['Gender'];
		$Address = $_POST['Address'];
		$Phone = $_POST['Phone'];
		$Nid = $_POST['Nid'];
		$Date = $_POST['Date'];
		
		$imgFile = $_FILES['user_image']['name'];
		$tmp_dir = $_FILES['user_image']['tmp_name'];
		$imgSize = $_FILES['user_image']['size'];
		
		
		if(empty($Teacher_Name)){
			$errMSG = "Please Enter Teacher Name !";
		}
		else if(empty($CourseName)){
			$errMSG = "Please SELECT Course Name !";
		}
		
		else
		{
			$upload_dir = 'user_images/'; // upload directory
	
			$imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION)); // get image extension
		
			// valid image extensions
			$valid_extensions = array('jpeg', 'jpg', 'png', 'gif'); // valid extensions
		
			// rename uploading image
			$upic = rand(1000,1000000).".".$imgExt;
				
			// allow valid image file formats
			if(in_array($imgExt, $valid_extensions)){			
				// Check file size '5MB'
				if($imgSize < 5000000)				{
					move_uploaded_file($tmp_dir,$upload_dir.$upic);
				}
				else{
					$errMSG = "Sorry, your file is too large.";
				}
			}
			//else{
			//	$errMSG = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";		
			//}
		}
		
		
		// if no error occured, continue ....
		if(!isset($errMSG))
		{
			$stmt = $DB_con->prepare('INSERT INTO teacher (user_id,teacher_name,subject,gender,address,contact_info,national_id,join_date,status,userPic) 
			                                         VALUES(:UserId,:Teacher_Name,:CourseName, :Gender,:Address,:Phone,:Nid,:Date,1,:upic)');
			
			
			$stmt->bindParam(':UserId',$UserId);
			$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(':upic',$upic);
			
			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 = "error while inserting....";
			}
		}
	}
?>

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

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

    <div class="pagetitle bg-light rounded p-2"> 
	  <h1>New Teacher Add  | <span> <a href="teacher-list">   <i class="bi bi-table"></i> </a> </span> </h1>
	  
    </div> 
	
    <section class="section">
      <div class="row">
        <div class="col-lg-6">

          <div class="card">
            <div class="card-body">
              <h5 class="card-title">
			  <?php
	if(isset($errMSG)){
			?>
            <div class="alert alert-danger">
            	<span class="glyphicon glyphicon-info-sign"></span> <strong><?php echo $errMSG; ?></strong>
            </div>
            <?php
	}
	else if(isset($successMSG)){
		?>
        <div class="alert alert-success">
              <strong><span class="glyphicon glyphicon-info-sign"></span> <?php echo $successMSG; ?></strong>
        </div>
        <?php
	}
	?>  
			  
</h5>


<form method="post" enctype="multipart/form-data" class="form-horizontal" >
	    
	<table class="table table-hover table-responsive">
	
   <tr>
    	
		<?php 
				   $pq=mysqli_query($con,"select * from stuff left join `user` on user.userid=stuff.userid where stuff.userid='".$_SESSION['id']."'");
				   while($pqrow=mysqli_fetch_array($pq)){
				?>
        
        <input class="form-control" type="hidden" name="UserId"  value="<?php echo $pqrow['userid']; ?>" />
		<?php }?>
    </tr>
		
	<tr>
    	<td><label class="control-label">Teacher Name </label></td>
        <td><input class="form-control" type="text" name="Teacher_Name" placeholder="Teacher Name" required></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" required>
							<option value="" required>Select Course</option>

							<?php
							$sql = "SELECT product_id,product_name FROM product where user_id='".$_SESSION['id']."'";
							$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"> <option>Male </option> <option>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" required></td>
    </tr>
	
	<tr>
    	<td><label class="control-label">Phone No </label></td>
        <td><input class="form-control" type="text" name="Phone" placeholder="Phone No" required></td>
    </tr>
	
	<tr>
    	<td><label class="control-label">National Id</label></td>
        <td><input class="form-control" type="text" name="Nid" placeholder="National Id" required></td>
    </tr>
	
		<tr>
    	<td><label class="control-label">Join Date </label></td>
		<td><input class="form-control" type="date" name="Date" id="Date" placeholder="" value="<?php date_default_timezone_set("Asia/Dhaka"); echo date("Y-m-d") ;?>" /></td>
    </tr>
	
    <tr>
    	<td><label class="control-label">Photo </label></td>
        <td><input class="input-group" type="file" name="user_image" accept="image/*" /></td>
    </tr>
	
	
    
    </table>
	<tr>
        <td colspan="2"><button type="submit" name="btnsave" class="btn btn-primary">
       <span class="bi bi-save"></span>  &nbsp; Save
        </button>
        </td>
    </tr>
    
</form>

            </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>