<?php 
  $link_status4 = "";
  $link_status5 = "active";
  $link_status6 = "";

  $dtt = date("Y-m-d h:i:s");
?>
<?php 
  include("includes/top-header.php"); 
  if(isset($_POST["change_password"])){
    $opw = $_POST['opw'];
    $npw = $_POST['npw'];
    $rpw = $_POST['rpw'];
    
    if($opw==$pw){
        if($npw==$rpw){
          $sql="update users set pass='".$rpw."', password_change='".$dtt."' where uID='".$uid."'";
          $rs=mysqli_query($conn, $sql);
            
          if($rs){
      			echo '<script>alert("Password changed successfully!")</script>';
      			echo '<script>window.location.href = "settings-security.php";</script>';
          }else{
             echo '<script>alert("An Error Occurred, try again later...")</script>';
          }
        }else{
      	  echo '<script>alert("Password mismatch! Re-enter new password correctly.")</script>';
          echo '<script>window.location.href = "settings-security.php";</script>';
        }
    }else{
  	    echo '<script>alert("Incorrect Password! Please input correct details and try again.")</script>';
  		  echo '<script>window.location.href = "settings-security.php";</script>';
    }
  }
  
  if($twofa == 0){
      $tfa_status = "<span class='bg-danger text-white rounded-pill d-inline-block px-2 mb-0'><i class='fas fa-times-circle'></i> Inactive</span>";
      $tfa_button = "<a href='settings-security.php?r=activate2fa' class='btn btn-success'>Activate 2FA Status</a>";
  }elseif($twofa == 1){
      $tfa_status = "<span class='bg-success text-white rounded-pill d-inline-block px-2 mb-0'><i class='fas fa-check-circle'></i> Active</span>";
      $tfa_button = "<a href='settings-security.php?r=deactivate2fa' class='btn btn-danger'>De-activate 2FA Status</a>";
  }
  
  if(isset($_GET['r'])){
      if($_GET['r'] == "activate2fa"){
          $sql="update users set 2fa='1' where uID='".$uid."'";
        $rs=mysqli_query($conn, $sql);
      }elseif($_GET['r'] == "deactivate2fa"){
          $sql="update users set 2fa='0' where uID='".$uid."'";
        $rs=mysqli_query($conn, $sql);
      }

      if($rs){
        echo '<script>alert("2FA status updated successfully!")</script>';
        echo '<script>window.location.href = "settings-security.php";</script>';
      }else{
        echo '<script>alert("An Error Occurred, try again later...")</script>';
      }
  }
?>  
        <!-- Secondary menu
        ============================================= -->
        <!--<div class="bg-primary">-->
        <!--  <div class="container d-flex justify-content-center">-->
        <!--    <ul class="nav nav-pills alternate nav-lg border-bottom-0">-->
        <!--      <li class="nav-item"> <a class="nav-link <?php echo $link_status4; ?>" href="settings-profile.php">Account</a></li>-->
        <!--      <li class="nav-item"> <a class="nav-link <?php echo $link_status5; ?>" href="settings-security.php">Security</a></li>-->
        <!--      <li class="nav-item"> <a class="nav-link <?php echo $link_status6; ?>" href="settings-payment-methods.php">Payment Methods</a></li>-->
        <!--    </ul>-->
        <!--  </div>-->
        <!--</div>-->
        <!-- Secondary menu end --> 
        
<?php include("includes/header.php"); ?>
        <!-- Left Panel End --> 
        
        <!-- Middle Panel
        ============================================= -->
        <div class="col-lg-9"> 
          
          <!-- Password
          ============================================= -->
          <div class="bg-white shadow-sm rounded p-4 mb-4">
            <h3 class="text-5 fw-400 d-flex align-items-center mb-4">Password<a href="#change-password" data-bs-toggle="modal" class="ms-auto text-2 text-uppercase btn-link"><span class="me-1"><i class="fas fa-edit"></i></span>Change</a></h3>
            <hr class="mx-n4 mb-4">
            <p class="text-3"><span class="text-muted">Last changed: <i><?php echo $last_p_change; ?></i></span></p>
          </div>
          <!-- Edit Details Modal
          ================================== -->
          <div id="change-password" class="modal fade " role="dialog" aria-hidden="true">
            <div class="modal-dialog modal-dialog-centered" role="document">
              <div class="modal-content">
                <div class="modal-header">
                  <h5 class="modal-title fw-400">Change Password</h5>
                  <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body p-4">
                  <form id="changePassword" method="post">
                    <div class="mb-3">
                      <label for="existingPassword" class="form-label">Confirm Current Password</label>
                      <input type="password" class="form-control" name="opw" id="existingPassword" required placeholder="Enter Current Password">
                    </div>
                    <div class="mb-3">
                      <label for="newPassword" class="form-label">New Password</label>
                      <input type="password" class="form-control" name="npw" id="newPassword" required placeholder="Enter New Password">
                    </div>
                    <div class="mb-3">
                      <label for="confirmPassword" class="form-label">Confirm New Password</label>
                      <input type="password" class="form-control" name="rpw" id="confirmPassword" required placeholder="Enter Confirm New Password">
                    </div>
                    <div class="d-grid mt-4"><button class="btn btn-primary" name="change_password" type="submit">Update Password</button></div>
                  </form>
                </div>
              </div>
            </div>
          </div>
          <!-- Password End --> 
          
          
          <!-- 2-Step Verification
          ============================================= -->
          <div class="bg-white shadow-sm rounded p-4 mb-4">
            <h3 class="text-5 fw-400 d-flex align-items-center mb-4">2-Step Verification<a href="#two-step-verification" data-bs-toggle="modal" class="ms-auto text-2 text-uppercase btn-link"><span class="me-1"><i class="fas fa-edit"></i></span>Change</a></h3>
            <hr class="mx-n4 mb-4">
            <div class="row gx-3 align-items-center">
              <p class="col-sm-9 text-3"><?php echo $tfa_status; ?></p>
            </div>
            <p class="text-3">Use a one-time security code in addition to your password each time you log in.</p>
          </div>
          <!-- Edit Details Modal
          ================================== -->
          <div id="two-step-verification" class="modal fade " role="dialog" aria-hidden="true">
            <div class="modal-dialog modal-dialog-centered" role="document">
              <div class="modal-content">
                <div class="modal-header">
                  <h5 class="modal-title fw-400">2-Step Verification</h5>
                  <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body p-4 text-center">
                  <p class="lead">Protect your account with 2-step verification</p>
                  <form id="changePassword" method="post">
                    <div class="d-grid mt-4"><?php echo $tfa_button; ?></div>
                  </form>
                </div>
              </div>
            </div>
          </div>
          <!-- 2-Step Verification End --> 
          
        </div>
        <!-- Middle Panel End --> 
      </div>
    </div>
  </div>
  <!-- Content end --> 
  
  <!-- Footer
  ============================================= -->
  <?php include("includes/footer.php"); ?>
  <!-- Footer end --> 
  
</div>
<!-- Document Wrapper end --> 

<!-- Back to Top
============================================= --> 
<a id="back-to-top" data-bs-toggle="tooltip" title="Back to Top" href="javascript:void(0)"><i class="fa fa-chevron-up"></i></a> 

<!-- Styles Switcher -->

<!-- Styles Switcher End --> 

<!-- Script --> 
<script src="vendor/jquery/jquery.min.js"></script> 
<script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script> 
<!-- Style Switcher --> 
<script src="js/switcher.min.js"></script> 
<script src="js/theme.js"></script>
</body>
</html>