<?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']))
	{
		$customer_id = $_GET['update'];
		$stmt_edit = $DB_con->prepare('SELECT * FROM customer 
		
		left join batch on batch.id = customer.batch_no
		left join product on product.product_id = customer.course_id
		LEFT JOIN cust_position ON cust_position.id = customer.position
		LEFT JOIN degree ON degree.deg_id  = customer.last_edu
		LEFT JOIN status ON status.status_id = customer.status
		
		WHERE customer.customer_id =:uid');
		$stmt_edit->execute(array(':uid'=>$customer_id));
		$edit_row = $stmt_edit->fetch(PDO::FETCH_ASSOC);
		extract($edit_row);
	}
	else
	{
		header("Location: index");
	}
	 
	if(isset($_POST['btn_save_updates']))
	{
		
		$customer_name = $_POST['customer_name'];
		$customer_no = $_POST['customer_no'];
		$BatchNo = $_POST['BatchNo'];
	    $CourseName = $_POST['CourseName'];
					
		$FatherName = $_POST['FatherName'];
	   $MotherName = $_POST['MotherName']; 
		$Position = $_POST['Position'];
		$gender = $_POST['gender'];
		
		$address = $_POST['address'];
		$contact_info = $_POST['contact_info'];
		$Email = $_POST['Email'];
		$national_id = $_POST['national_id'];
		$Status = $_POST['Status'];
		
 		
	$MrgInfo = $_POST['MrgInfo'];
	$Religion = $_POST['Religion'];
    $LastEdu = $_POST['LastEdu'];
	$Institute = $_POST['Institute'];
 
	$GuarPhn = $_POST['GuarPhn'];
	$Refer = $_POST['Refer'];
	$Date = $_POST['Date'];
	 		
		$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 customer 
									     SET customer_name=:customer_name, 
										     customer_no=:customer_no, 
											 batch_no=:BatchNo,
											 course_id=:CourseName,
											 father_name=:FatherName,
											 mother_name=:MotherName,
											 
										     position=:Position, 
										     gender=:gender, 
										     address=:address, 
										     contact_info=:contact_info, 
										     email=:Email, 
										     national_id=:national_id, 
											 
										     marriage_info=:MrgInfo, 
										     religion=:Religion, 
										     last_edu=:LastEdu, 
										     school=:Institute, 
										    
											 gar_phn=:GuarPhn, 
										     refers=:Refer, 
										     join_date=:Date,
										     status=:Status,
										     userPic=:upic 
											 
								       WHERE customer.customer_id=:uid ');
            
			
			$stmt->bindParam(':customer_name',$customer_name);
			$stmt->bindParam(':customer_no',$customer_no);
			$stmt->bindParam(':BatchNo', $BatchNo);
	        $stmt->bindParam(':CourseName', $CourseName);
			$stmt->bindParam(':FatherName', $FatherName);
		    $stmt->bindParam(':MotherName', $MotherName);
			
			$stmt->bindParam(':Position',$Position);
			$stmt->bindParam(':gender',$gender);
			
			$stmt->bindParam(':address',$address);
			$stmt->bindParam(':contact_info',$contact_info);
			$stmt->bindParam(':Email',$Email);
			$stmt->bindParam(':national_id',$national_id);
			$stmt->bindParam(':Status',$Status);
	 	
	    $stmt->bindParam(':MrgInfo', $MrgInfo);
		$stmt->bindParam(':Religion', $Religion);
		$stmt->bindParam(':LastEdu', $LastEdu);
		$stmt->bindParam(':Institute', $Institute);
	    $stmt->bindParam(':GuarPhn', $GuarPhn);
		$stmt->bindParam(':Refer', $Refer);
		$stmt->bindParam(':Date',$Date);
		 		
			$stmt->bindParam(':upic',$userpic);
			$stmt->bindParam(':uid',$customer_id);
				
			if($stmt->execute()){
				?>
                <script>
				
				</script>
                <?php
			}
			else{
				$errMSG = "Sorry Data Could Not Updated !";
			}
		
		}
		
						
	}
	
?>

<!--Update orders Information -->
<?php 
	
	if(isset($_GET['update']) && !empty($_GET['update']))
	{
		$customer_id = $_GET['update'];
		$stmt_edit = $DB_con->prepare('SELECT * FROM customer WHERE customer_id =:uid');
		$stmt_edit->execute(array(':uid'=>$customer_id));
		$edit_row = $stmt_edit->fetch(PDO::FETCH_ASSOC);
		extract($edit_row);
	}
	else
	{
		header("Location: index");
	}
	
	if(isset($_POST['btn_save_updates']))
	{
		
		
		$customer_name = $_POST['customer_name'];
		$contact_info = $_POST['contact_info'];
		 
		// if no error occured, continue ....
		if(!isset($errMSG))
		{
			$stmt = $DB_con->prepare('UPDATE orders
									     SET  client_name=:customer_name,
										     client_contact=:contact_info 
											 
								       WHERE customer_id=:uid');
			
			$stmt->bindParam(':customer_name',$customer_name);
			$stmt->bindParam(':contact_info',$contact_info);
			$stmt->bindParam(':uid',$customer_id);
				
			if($stmt->execute()){
				?>
                <script>
				
				</script>
                <?php
			}
			else{
				$errMSG = "Sorry Data Could Not Updated !";
			}
		
		}
		
						
	}
	
?>

<!--Update orders_dues Information-->
<?php 

	if(isset($_GET['update']) && !empty($_GET['update']))
	{
		$customer_id = $_GET['update'];
		$stmt_edit = $DB_con->prepare('SELECT * FROM customer WHERE customer_id =:uid');
		$stmt_edit->execute(array(':uid'=>$customer_id));
		$edit_row = $stmt_edit->fetch(PDO::FETCH_ASSOC);
		extract($edit_row);
	}
	else
	{
		header("Location: index");
	}
	
	if(isset($_POST['btn_save_updates']))
	{
		
		
		$customer_name = $_POST['customer_name'];
		$contact_info = $_POST['contact_info'];
		
		
						
		
		// if no error occured, continue ....
		if(!isset($errMSG))
		{
			
			
			$stmt = $DB_con->prepare('UPDATE orders_dues
									     SET  client_name=:customer_name,
										     client_contact=:contact_info 
											 
								       WHERE customer_id=:uid');
									   
			
			$stmt->bindParam(':customer_name',$customer_name);
			$stmt->bindParam(':contact_info',$contact_info);
			$stmt->bindParam(':uid',$customer_id);
				
			if($stmt->execute()){
				?>
                <script>
				
				</script>
                <?php
			}
			else{
				$errMSG = "Sorry Data Could Not Updated !";
			}
		
		}
		
						
	}
	
?>


<!--Update orders_details Information-->
<?php 
	
	if(isset($_GET['update']) && !empty($_GET['update']))
	{
		$customer_id = $_GET['update'];
		$stmt_edit = $DB_con->prepare('SELECT * FROM customer WHERE customer_id =:uid');
		$stmt_edit->execute(array(':uid'=>$customer_id));
		$edit_row = $stmt_edit->fetch(PDO::FETCH_ASSOC);
		extract($edit_row);
	}
	else
	{
		header("Location: index");
	}
	
	if(isset($_POST['btn_save_updates']))
	{
		
		
		$customer_name = $_POST['customer_name'];
		$contact_info = $_POST['contact_info'];
		
		
						
		
		// if no error occured, continue ....
		if(!isset($errMSG))
		{
			
									   
			$stmt = $DB_con->prepare('UPDATE orders_details
									     SET  client_name=:customer_name,
										     client_contact=:contact_info 
											 
								       WHERE customer_id=:uid');						   
			
			$stmt->bindParam(':customer_name',$customer_name);
			$stmt->bindParam(':contact_info',$contact_info);
			$stmt->bindParam(':uid',$customer_id);
				
			if($stmt->execute()){
				?>
                <script>
				alert('Update Successfully Done...');
				window.location.href='student-list';
				</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>Student Information Edit</h1> 
    </div> 
	
    <section class="section">
      
          <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" >
			  
		<div class="row">
        <div class="col-lg-6">	  
		
		 
	  
	  <div class="d-none"> 
	  <label class="control-label">Customer No </label> 
	         <?php	// Auto Customer No
				  
				   $pq=mysqli_query($con,"select MAX(customer_no)+1 as max_id from customer  where member_id='".$_SESSION['id']."'");
				   while($pqrow=mysqli_fetch_array($pq)){
				?> 
      <input class="form-control" type="text" name="customer_no"  value="<?php echo $customer_no; ?>"  />  
      <?php }?>
	  </div>
			  
                <div class="row mb-3">
                  <label for="BatchNo" class="col-sm-4 col-form-label">Batch Name</label>
                  <div class="col-sm-8">
                    <select style="width:100%;" class="form-control chosen-select" Id="BatchNo" name="BatchNo" required="">
							<option value="<?php echo $batch_no; ?>"><?php echo $batch_name; ?></option>

							<?php
							$sql = "SELECT id,batch_name FROM batch ";
							$result = $con->query($sql);

							while ($row = $result->fetch_array()) {
								echo "<option value='" . $row[0] . "'>" . $row[1] . "</option>";
							}
							?>
						</select>
                  </div>
                </div>
				
                <div class="row mb-3">
                  <label for="CourseName" class="col-sm-4 col-form-label">Course Name</label>
                  <div class="col-sm-8">
                    <select style="width:100%;" class="form-control chosen-select" Id="CourseName" name="CourseName" required="">
							<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>
                  </div>
                </div>
				 
				
                <div class="row mb-3">
                  <label for="customer_name" class="col-sm-4 col-form-label">Student Name</label>
                  <div class="col-sm-8">
                    <input class="form-control" type="text" name="customer_name" id="customer_name"  value="<?php echo $customer_name; ?>">
                  </div>
                </div>
				
				<div class="row mb-3">
                  <label for="gender" class="col-sm-4 col-form-label"> Gender</label>
                  <div class="col-sm-8">
                   <select style="width:100%;" class="form-select" name="gender" id="gender" ><option><?php echo $gender; ?></option> <option>Male </option> <option>Female </option> <option>Other </option> </select> 
                  </div>
                </div>
								
                <div class="row mb-3">
                  <label for="Position" class="col-sm-4 col-form-label"> Profession</label>
                  <div class="col-sm-8">
                  <select style="width:100%;" class="form-control chosen-select" style="width:100%;" name="Position" id="Position">
		         <option value="<?php echo $position; ?>"><?php echo $position_name; ?></option>
				      	<?php 
				      	$sql = "SELECT distinct id, position_name FROM cust_position order by id asc  ";
								$result = $con->query($sql);

								while($row = $result->fetch_array()) {
									echo "<option value='".$row[0]."'>".$row[1]."</option>";
								} // while
				      	?>
		           </select> 
                  </div>
                </div>
				
                <div class="row mb-3">
                  <label for="FatherName" class="col-sm-4 col-form-label"> Father Name </label>
                  <div class="col-sm-8">
                   <input class="form-control" type="text" name="FatherName" id="FatherName" value="<?php echo $father_name; ?>" >
                  </div>
                </div>
				
                <div class="row mb-3">
                  <label for="MotherName" class="col-sm-4 col-form-label">Mother Name</label>
                  <div class="col-sm-8">
                   <input class="form-control" type="text" name="MotherName" id="MotherName" value="<?php echo $mother_name; ?>">
                  </div>
                </div>
				 				
                <div class="row mb-3">
                  <label for="address" class="col-sm-4 col-form-label">Address Name</label>
                  <div class="col-sm-8">
                    <input class="form-control" type="text" id="address" name="address" value="<?php echo $address; ?>" >
                  </div>
                </div>
				
                <div class="row mb-3">
                  <label for="contact_info" class="col-sm-4 col-form-label"> Phone Number</label>
                  <div class="col-sm-8">
                    <input class="form-control" type="text" name="contact_info" id="contact_info" value="<?php echo $contact_info; ?>" >
                  </div>
                </div>
				 
				
                <div class="row mb-3">
                  <label for="Email" class="col-sm-4 col-form-label"> Email Address</label>
                  <div class="col-sm-8">
                   <input class="form-control" type="text" name="Email" id="Email" value="<?php echo $email; ?>" >
                  </div>
                </div>
				
				<div class="row mb-3">
                  <label for="national_id" class="col-sm-4 col-form-label"> National Id</label>
                  <div class="col-sm-8">
                    <input class="form-control" type="text" name="national_id" id="national_id" value="<?php echo $national_id; ?>" >
                  </div>
                </div>				
			 
    </div>
		
	<div class="col-lg-6">
	
	            <div class="row mb-3">
                  <label for="MrgInfo" class="col-sm-4 col-form-label"> Marriage Info</label>
                  <div class="col-sm-8">
                    <select style="width:100%;" class="form-select" name="MrgInfo" id="MrgInfo">
					<option value="<?php echo $marriage_info; ?>"><?php echo $marriage_info; ?></option>
							<option value="Yes">Yes</option>
							<option value="No">No </option>
						</select>
                  </div>
                </div>
				 
				<div class="row mb-3">
                  <label for="Religion" class="col-sm-4 col-form-label"> Religion</label>
                  <div class="col-sm-8">
                   <select style="width:100%;" class="form-select" name="Religion" id="Religion">
				   <option value="<?php echo $religion; ?>"><?php echo $religion; ?></option>
							<option value="Muslim">Muslim</option>
							<option value="Hindu">Hindu </option>
							<option value="Christian">Christian </option>
							<option value="Buddha">Buddha </option>
							<option value="Other">Other </option>
						</select>
                  </div>
                </div>
				 
				<div class="row mb-3">
                  <label for="LastEdu" class="col-sm-4 col-form-label"> Last Education Qualification</label>
                  <div class="col-sm-8">
                  <select style="width:100%;" class="form-control chosen-select" Id="LastEdu" name="LastEdu">
							<option value="<?php echo $last_edu; ?>"><?php echo $deg_name; ?></option>

							<?php
							$sql = "SELECT deg_id,deg_name FROM degree ";
							$result = $con->query($sql);

							while ($row = $result->fetch_array()) {
								echo "<option value='" . $row[0] . "'>" . $row[1] . "</option>";
							}
							?>
						</select>
                  </div>
                </div>
				 
				<div class="row mb-3">
                  <label for="Institute" class="col-sm-4 col-form-label"> School/College/University</label>
                  <div class="col-sm-8">
                  <input class="form-control" type="text" name="Institute" id="Institute" value="<?php echo $school; ?>" />
				  </div>
                </div>
				 
				 <div class="row mb-3">
                  <label for="GuarPhn" class="col-sm-4 col-form-label"> Guardian / Phone </label>
                  <div class="col-sm-8">
                  <input class="form-control" type="text" name="GuarPhn" id="GuarPhn" value="<?php echo $gar_phn; ?>" />
				  </div>
                </div>
				 
				 <div class="row mb-3">
                  <label for="Refer" class="col-sm-4 col-form-label"> Reference </label>
                  <div class="col-sm-8">
                  <input class="form-control" type="text" name="Refer" id="Refer" value="<?php echo $refers; ?>" />
				  </div>
                </div>
				
				 <div class="row mb-3">
                  <label for="Date" class="col-sm-4 col-form-label"> Date</label>
                  <div class="col-sm-8">
                   <input class="form-control" type="date" name="Date" id="Date" value="<?php date_default_timezone_set("Asia/Dhaka"); echo date($join_date) ;?>" />
                  </div>
                </div>
				
				 <div class="row mb-3">
                  <label for="user_image" class="col-sm-4 col-form-label"> Active/In-Active</label>
                  <div class="col-sm-8">
                   <select style="width:100%;" class="form-select" name="Status">
		<option value="<?php echo $status; ?>"><?php echo $type; ?></option> 
		<option value="1">Active</option> 
		<option value="0">In-Active</option>
		</select> 
                  </div>
                </div>
				
				<div class="row mb-3">
                  <label for="user_image" class="col-sm-4 col-form-label"> Photo</label>
                  <div class="col-sm-8"> 
				   <p><img src="user_images/<?php echo $userPic; ?>" height="150" width="150" /></p>
        	<input class="input-group" type="file" name="user_image" id="user_image" accept="image/*" />
                  </div>
                </div>
	</div>
	
	 
				
    <div class="text-center"><hr>				
				<button type="submit" name="btn_save_updates" class="btn btn-primary"> <span class="glyphicon glyphicon-save"></span> Update </button>
    <a class="btn btn-danger" href="student-list"> <span class="glyphicon glyphicon-backward"></span> Cancel</a>
    </div>
				
             
			 </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>
	