<!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'];
	$BatchName = $_POST['BatchName'];
	$BatchNo = $_POST['BatchNo'];
	$Trainer = $_POST['Trainer'];
	$Date = $_POST['Date'];


	if (empty($BatchName)) {
		$errMSG = "Please Enter Batch Name.";
	} else if (empty($BatchNo)) {
		$errMSG = "Please Enter Batch No.";
	}



	// if no error occured, continue ....
	if (!isset($errMSG)) {
		$stmt = $DB_con->prepare('INSERT INTO batch (user_id,batch_name,batch_no,trainer,entry_date) VALUES(:UserId,:BatchName,:BatchNo,:Trainer,:Date)');


		$stmt->bindParam(':UserId', $UserId);
		$stmt->bindParam(':BatchName', $BatchName);
		$stmt->bindParam(':BatchNo', $BatchNo);
		$stmt->bindParam(':Trainer', $Trainer);
		$stmt->bindParam(':Date', $Date);

		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 = 'batch-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>Add New Batch |  <span> <a href="batch-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 user where 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">Batch Name</label></td>
					<td><input class="form-control" type="text" name="BatchName" placeholder="Batch Name"></td>
				</tr>


				<tr class="d-none">
					<td><label class="control-label">Batch No </label></td>
					<td><input class="form-control" type="text" name="BatchNo" value="Na"></td>
				</tr>

	<tr>
    	<td><label class="control-label">Instructor Name </label></td>
		<td>
		
		<select style="width:100%;" class="form-select chosen-select1" Id="Trainer" name="Trainer" required>
							<option value="" required>Select Instructor</option>

							<?php
							$sql = "SELECT teacher_id,teacher_name FROM teacher 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">Date</label></td>
        <td><input class="form-control" type="date" name="Date" id="Date" value="<?php echo date("Y-m-d") ;?>" /></td>
    </tr>
				 

				

			</table>

			<button type="submit" name="btnsave" class="btn btn-primary"> <span class="bi bi-save"></span> &nbsp; Save </button>

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