<script type="text/javascript" >

function validate(){
	var pwrd = form.pword.value();
	var pwrd2 = form.pasword.value();
	
	if(document.form.pwrd.value !=""  && document.form.pwrd2.value != ""){
		if( pwrd != pwrd2){
			alert("Passwords didn't match! please try again");
			document.form.pword.focus();
			return false;
		}
		return true;
		
	}else{
		alert("Fill all required fields!");
		return false;
	}
}
</script>

<div class="modal fade" id="resetpassword<?php echo $id ?>" tabindex="-1" role="form" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true"> Cancel &times;</button>
                <h4 class="modal-title" id="myModalLabel"> Reset your Password &nbsp;&nbsp;
				<?php $user = $dat['username']; $mail = $dat['email'];  /* echo $user." : ".$mail;*/ ?> </h4> 
            </div>
				<div class="modal-body">
			   
					<form name="form" data-parsley-validate class="form-horizontal form-label-left" method="post" action="" onsubmit="return validate()">	
					<table border="0" style="margin-left:80px">
					    
				    <tr>
					  <div class="form-group">
                       <td> <label  for="first-name"> Old Password <span class="required"> </span>  </label> </td>
                       
                       <td> <div class="col-md-4 col-sm-4 col-xs-5">
                          <input type="password" id="pword" name="pwordold"   required="required" size="25px" >
                        </div> 
						</td>
                      </div> 
					 </tr>
				
					 <tr style="height:10px;">
					     <td></td> <td></td>
					 </tr>
					 
					<tr>
					  <div class="form-group">
                       <td> <label  for="first-name"> New Password <span class="required"> </span>  </label> </td>
                       
                       <td> <div class="col-md-4 col-sm-4 col-xs-5">
                          <input type="password" id="pword" name="pword"   required="required" size="25px" >
                        </div> 
						</td>
                      </div> 
					 </tr>
				
					 <tr style="height:10px;">
					     <td></td> <td></td>
					 </tr>
					 <tr> <div class="form-group">
                       <td> <label  for="first-name">Re-Enter Password <span class="required"> </span>  </label> </td>
                       
                       <td> <div class="col-md-4 col-sm-4 col-xs-5">
                          <input type="password" id="pasword" name="pasword" required="required" size="25px" >
                        </div> </td>
                      </div> 
					  </tr>
					  
					  </table>
					  
						<br/>
							<div class="form-group" style="margin-left:80px;">
								<div class="col-md-4col-sm-4 col-xs-5 col-md-offset-3">
									<input type="submit"  class="btn btn-info" name="update" value="Save Changes"/>
								</div>
							</div>	
					  
							
					</form>
					<?php
					if(isset($_POST['update'])){
						
						$oldpw = $_POST['pwordold'];
						$pw = $_POST['pword'];
						$pw2 = $_POST['pasword'];
						
						$host = 'localhost';
        $db   = 'ki223469_temp';
        $user = 'ki223469_temp';
        $pass = 'm0bi.2018$';
        $charset = 'utf8mb4';
        
        $dsn = "mysql:host=$host;dbname=$db;charset=$charset";
        $options = [
            PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
            PDO::ATTR_EMULATE_PREPARES   => false,
        ];
        $pdo = new PDO($dsn, $user, $pass, $options);
        
        
        
        //create a select query
		$stmt = $pdo->prepare('SELECT * FROM airtime_customer WHERE customer_id = :cust');
		try{
		    
		    $stmt->execute(['cust' => $session_id]);
		    //get the selected array
		    $user=$stmt->fetch();
		    if(count($user)>0){
		        
		        //verify the password
		        	if(password_verify($oldpw, $user['password'])){
		        	   
		        	   	if($pw == $pw2){
		        	   	    $phashed = password_hash($pw2, PASSWORD_BCRYPT);
								$sql = mysql_query("update airtime_customer set password = '$phashed' where customer_id = '$session_id' ") or die (mysql_error());	
								
								if($sql){
								    
								   $qry = mysql_query("select * from airtime_logs where id = '$session_lid' ");
							        $sdata = mysql_fetch_array($qry);
							        $actions = $sdata['user_actions'];
						        	mysql_query("update airtime_logs set user_actions = '$actions Updated Account Password.' where id = '$session_lid'  "); 
								
								    	?>
									        <script>
									        	alert("Success. Your Password updated successfully.");
									        	window.location="admin/adminprofile.php";
									        </script>
								        <?php
								}
							
							}else{
								?>
									<script>
										alert("Passwords didn't match! Please try again");
										window.location="admin/adminprofile.php";
									</script>
								<?php
								//echo "Passwords didn't match! Please try again!";
							}
		        	   
		        	}
		        	else{
		        	    	?>
								<script>
									alert("Wrong Password! Please try again");
									window.location="admin/adminprofile.php";
								</script>
							<?php
		        	}
		    }
		    else
		    {
		       ?>
								<script>
									alert("Account not found");
									window.location="admin/adminprofile.php";
								</script>
							<?php 
		    }
		    
		}catch(PDOException $ex){
		    	?>
		   <script>
									alert("Error! Kindly contact the Admin");
									window.location="admin/adminprofile.php";
								</script> 
								<?php
		}
        
        
        
                        /*
						$checkpwrd = mysql_query("select password from airtime_customer where password = '$oldpw' and customer_id = '$session_id' ");
						
						if(mysql_num_rows($checkpwrd) > 0){
							
							if($pw == $pw2){
								$sql = mysql_query("update airtime_customer set password = '$pw2' where customer_id = '$session_id' ") or die (mysql_error());	
								?>
									<script>
										alert("Success");
										window.location="homepage.php";
									</script>
								<?php
							}else{
							/*	?>
									<script>
										alert("Passwords didn't match! Please try again");
										window.location="profile.php";
									</script>
								<?php 								echo "Passwords didn't match! Please try again!";
							}
							
						}else{
							?>
								<script>
									alert("Wrong Password! Please try again");
								//	window.location="profile.php";
								</script>
							<?php 
						//	echo "Wrong Password! Please try again.";
						}*/
						
					}
					
					?>
					
				</div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>