
<!DOCTYPE html>
<html lang="en">

<head>
<?php ob_start(); ?>
<?php   require_once 'head_link.php'; ?> 

</head>

<body>
 
<?php   require_once 'header1.php'; ?> 

<?php 
	if(isset($_POST['btnsave']))
	{
		$UserId = $_POST['UserId'];
        
		$ProductName = $_POST['ProductName']; 
		$Details = $_POST['Details']; 
		$Quantity = $_POST['Quantity'];  
		$WholSaleRate = $_POST['WholSaleRate']; 
		$ReatilSaleRate = $_POST['ReatilSaleRate']; 
		$BuyRate = $_POST['BuyRate']; 
		$Date = $_POST['Date'];
		
		$imgFile = $_FILES['user_image']['name'];
		$tmp_dir = $_FILES['user_image']['tmp_name'];
		$imgSize = $_FILES['user_image']['size'];
		
		if(empty($ProductName)){
			$errMSG = "Please Enter Product Id And Click Ok.";
		}
		
		else if(empty($Details)){
			$errMSG = "Please Enter Details .";
		}
		
		else
		{
			$upload_dir = 'assests/images/stock/'; // 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
			$userpic = 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.$userpic);
				}
				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 product  (user_id,product_name,product_details,product_image, quantity,rate,retail_rate,buy_rate,active,status, entry_date,last_update) 
			        VALUES(:UserId,:ProductName,:Details,:upic, :Quantity, :WholSaleRate,:ReatilSaleRate,:BuyRate,1,1,:Date,:Date )');
			 
			$stmt->bindParam(':UserId',$UserId); 
			$stmt->bindParam(':ProductName',$ProductName); 
			$stmt->bindParam(':Details',$Details);  
			$stmt->bindParam(':Quantity',$Quantity);  
			$stmt->bindParam(':WholSaleRate',$WholSaleRate); 
			$stmt->bindParam(':ReatilSaleRate',$ReatilSaleRate); 
			$stmt->bindParam(':BuyRate',$BuyRate); 
			$stmt->bindParam(':Date',$Date);
			
			$stmt->bindParam(':upic',$userpic);
			
			if($stmt->execute())
			{
			
			//$successMSG = "New Product Add Successful...";
			
			}
			else
			{
				$errMSG = "error while inserting....";
			}
		}
	}
?>

<?php 

	if(isset($_POST['btnsave']))
	{
		$UserId = $_POST['UserId'];
		 
		$Product_Id = $_POST['Product_Id'];
		$Quantity = $_POST['Quantity']; 
		$WholSaleRate = $_POST['WholSaleRate'];
		$ReatilSaleRate = $_POST['ReatilSaleRate'];
		$BuyRate = $_POST['BuyRate'];
		$Date = $_POST['Date'];
		
				
		if(empty($Product_Id)){
			$errMSG = "Please Enter Product Id.";
		}
		
		else if(empty($Details)){
			$errMSG = "Please Enter Details .";
		}
		
		// if no error occured, continue ....
		if(!isset($errMSG))
		{
			$stmt = $DB_con->prepare('INSERT INTO product_details (user_id,invoice_no,product_id,quantity,add_qty,rec_qty,rate,retail_rate,buy_rate,entry_date,cuses,type) 
			                                               VALUES(:UserId,"0",:Product_Id,"0",:Quantity,:Quantity,:WholSaleRate,:ReatilSaleRate,:BuyRate,:Date,"New Add",1)');
			
			$stmt->bindParam(':UserId',$UserId);
			$stmt->bindParam(':Product_Id',$Product_Id);
			$stmt->bindParam(':Quantity',$Quantity); 
			$stmt->bindParam(':WholSaleRate',$WholSaleRate);
			$stmt->bindParam(':ReatilSaleRate',$ReatilSaleRate);
			$stmt->bindParam(':BuyRate',$BuyRate);
			$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 = 'courses-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 Course |  <span> <a href="courses-list">    <i class="bi bi-table"></i> </a> </span> </h1> 
    </div> 
	
    <section class="section">
      <div class="row">
        <div class="col-lg-8">

          <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 style="display:none;">
    	<td><label class="control-label">Product Code</label> </td>
        <td> 
			<?php 
				   $pq=mysqli_query($con,"select MAX(product_id)+1 as product_id from  `product` where user_id='".$_SESSION['id']."' ");
				   while($pqrow=mysqli_fetch_array($pq)){
				?>
			  
		<input  type="text" class="form-control" id="Product_Id11" name="Product_Id11" placeholder="Product Id" value="<?php echo $pqrow['product_id'];?>" disabled="true" >  
		<input  type="hidden" class="form-control" id="Product_Id" name="Product_Id" placeholder="Product Id" value="<?php echo $pqrow['product_id'];?>" >  
		 <?php }?>
		</td>
    </tr>
 
   
  <tr>
     <td><label class="control-label">Course Name  </label></td>
     <td><input class="form-control" type="text" id="ProductName" name="ProductName" placeholder="Course Name"  required></td>
  </tr>
  
    <tr>
    	<td><label class="control-label">Duration</label></td>
        <td>
       <input class="form-control" type="text" id="Details"  name="Details"   placeholder="Duration"  required></td>
    </tr>
 
	 
    <tr style="display:none;">
    	<td><label class="control-label"> Quantity </label></td>
        <td><input class="form-control" type="text" id="Quantity" name="Quantity" placeholder="Quantity Number" value="0"   /></td>
    </tr>
	
	<tr>
    	<td><label class="control-label"> Cost Price</label></td>
        <td><input class="form-control" type="text" id="BuyRate" name="BuyRate" placeholder=" Cost Price" required></td>
    </tr>
	
	<tr>
    	<td><label class="control-label">Sale  Price </label></td>
        <td> <input class="form-control" type="text" id="ReatilSaleRate"  name="ReatilSaleRate" placeholder="Sale Price"  oninput="myFunction()" required></td>
    </tr>
	
  
		
	<tr style="display:none;">
    	<td><label class="control-label">Whole Sale Rate</label></td>
        <td><input class="form-control" type="text" id="WholSaleRate"  name="WholSaleRate" value="0"   oninput="myFunctionTP()" required></td>
    </tr>
	 

  <tr style="display:none;">
    	<td><label class="control-label">Photo </label></td>
        <td><input class="input-group" type="file" name="user_image" accept="image/*" /></td>
    </tr>
    
	<tr style="display:none;">
    	<td><label class="control-label">Date</label></td>
        <td><input class="form-control" type="text" name="Date" placeholder="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>
 