<?php

include('includes/session.php');

error_reporting(0);

include('includes/dbconnection.php');
require_once 'includes/session_term_helper.php';
$stResult = resolveCurrentSessionTerm($con);
$currentSessionName = $stResult['session_name'];
$currentTermName    = $stResult['term_name'];

include('includes/flash.php');



if (strlen($_SESSION['student_id']) == 0) {

  header('location:logoutstd.php');

}



if (isset($_POST['submit'])) {

  // Get the student ID from the session

  $id = $_SESSION['student_id'];



  // Get form input values

  $old_password = $_POST['password'];

  $new_password = $_POST['password1'];

  $confirm_password = $_POST['password2'];



  // Validate the new password and confirm password match

  if ($new_password != $confirm_password) {

    flash_alert('New password and confirm password do not match.');

  } else {

    // Fetch the current password from the database

    $query = "SELECT password FROM students WHERE id = :id";

    $stmt = $dbh->prepare($query);

    $stmt->bindParam(':id', $id, PDO::PARAM_INT);

    $stmt->execute();

    $row = $stmt->fetch(PDO::FETCH_ASSOC);



    // Check if the old password matches the stored password

    if ($row && password_verify($old_password, $row['password'])) {

      // Hash the new password

      $password = password_hash($new_password, PASSWORD_BCRYPT);



      // Update the password in the database

      $update_query = "UPDATE students SET password = :password WHERE id = :id";

      $update_stmt = $dbh->prepare($update_query);

      $update_stmt->bindParam(':password', $password, PDO::PARAM_STR);

      $update_stmt->bindParam(':id', $id, PDO::PARAM_INT);  // Use $id instead of $student_id

      $update_stmt->execute();



      flash_alert('Password changed successfully');

    } else {

      flash_alert('Incorrect old password.');

    }

  }

}

?>



<?php @include("includes/head.php"); ?>



<body class="hold-transition sidebar-mini layout-fixed">

  <div class="wrapper">

    <!-- Navbar -->

    <?php @include("includes/headerstd.php"); ?>

    <!-- /.navbar -->

    <!-- Side bar and Menu -->

    <?php @include("includes/sidebaroldstdboard.php"); ?>

    <!-- /.sidebar and menu -->



    <!-- Content Wrapper. Contains page content -->

    <div class="content-wrapper">

      <section class="content-header">

<section class="content-header premium-header">

    <div class="container-fluid">

        <div class="header-glass-card">

            <div class="row align-items-center">

                <div class="col-md-8">

                    <div class="title-section">

                        <div class="icon-box bg-primary-gradient shadow-sm">

                            <i class="fas fa-user-graduate"></i>

                        </div>

                        <div>

                            <h1 class="premium-title text-primary">

                                Student Information Management

                                <span class="premium-subtitle text-secondary">Manage Password</span>

                            </h1>
                            <div class="mt-1">
                              <?php if ($currentSessionName): ?>
                                <span class="badge badge-primary mr-1">
                                  <i class="fas fa-calendar-alt"></i>
                                  <?= htmlspecialchars($currentSessionName) ?>
                                </span>
                              <?php endif; ?>
                              <?php if ($currentTermName): ?>
                                <span class="badge badge-success">
                                  <i class="fas fa-bookmark"></i>
                                  <?= htmlspecialchars($currentTermName) ?>
                                </span>
                              <?php else: ?>
                                <span class="badge badge-warning">
                                  <i class="fas fa-exclamation-triangle"></i> No active term
                                </span>
                              <?php endif; ?>
                            </div>

                        </div>

                    </div>

                </div>

                <div class="col-md-4">

                    <nav aria-label="breadcrumb">

                        <ol class="breadcrumb premium-breadcrumb">

                            <li class="breadcrumb-item"><a href="oldstdboard.php" class="text-primary">Home</a></li>

                            <li class="breadcrumb-item active" aria-current="page">Change Password</li>

                        </ol>

                    </nav>

                </div>

            </div>

        </div>

    </div>

</section>

      <div class="card">

        <div class="col-md-12">

          <div class="card card-primary">

            <div class="card-header">

              <h3 class="card-title">Change Password</h3>

            </div>

            <div class="card-body">

              <!-- Date -->



              <form role="form" method="post" enctype="multipart/form-data" class="form-horizontal">



                <div class="card-body">

                  <div class="form-group">

                    <label for="oldPassword">Old Password</label>

                    <div class="input-group">

                      <input type="password" name="password" class="form-control" id="oldPassword" required>

                      <div class="input-group-append">

                        <span class="input-group-text toggle-password" data-target="oldPassword" style="cursor:pointer;">

                          <i class="fas fa-eye"></i>

                        </span>

                      </div>

                    </div>

                  </div>



                  <div class="form-group">

                    <label for="newPassword">New Password</label>

                    <div class="input-group">

                      <input type="password" name="password1" class="form-control" id="newPassword" required>

                      <div class="input-group-append">

                        <span class="input-group-text toggle-password" data-target="newPassword" style="cursor:pointer;">

                          <i class="fas fa-eye"></i>

                        </span>

                      </div>

                    </div>

                  </div>



                  <div class="form-group">

                    <label for="confirmPassword">Confirm Password</label>

                    <div class="input-group">

                      <input type="password" name="password2" class="form-control" id="confirmPassword" required>

                      <div class="input-group-append">

                        <span class="input-group-text toggle-password" data-target="confirmPassword" style="cursor:pointer;">

                          <i class="fas fa-eye"></i>

                        </span>

                      </div>

                    </div>

                  </div>



                </div>



                <div class="form-group text-center">

                  <button type="submit" name="submit" class="btn btn-primary"> <i class="fas fa-sync"></i> Change Password</button>

                </div>



              </form>



            </div>

            <!-- /.card-body -->

          </div>

          <!-- /.card -->

        </div><!-- /.container-fluid -->

      </div>

      <!-- /.content-header -->

    </div>

    <!-- /.content-wrapper -->

    <?php @include("includes/footer.php"); ?>

    <?php @include("includes/foot.php"); ?>

    <script>

      document.querySelectorAll('.toggle-password').forEach(function(toggle) {

        toggle.addEventListener('click', function() {

          const targetId = this.getAttribute('data-target');

          const input = document.getElementById(targetId);

          const icon = this.querySelector('i');



          if (input.type === 'password') {

            input.type = 'text';

            icon.classList.remove('fa-eye');

            icon.classList.add('fa-eye-slash');

          } else {

            input.type = 'password';

            icon.classList.remove('fa-eye-slash');

            icon.classList.add('fa-eye');

          }

        });

      });

    </script>



</body>



</html>