<?php

include('includes/session1.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 (!isset($_SESSION['sid'])) {

  header('location:logoutteacher.php');

  exit();

} else {

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

    $adminid = $_SESSION['sid']; // make sure you use the correct session variable

    $oldPassword = $_POST['password'];

    $newPassword = $_POST['password1'];

    $confirmPassword = $_POST['password2'];



    if ($newPassword !== $confirmPassword) {

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

    } else {

        // Fetch stored password

        $sql = "SELECT Password FROM tblusers WHERE id=:adminid";

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

        $query->bindParam(':adminid', $adminid, PDO::PARAM_STR);

        $query->execute();

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



        if ($row) {

            $storedPassword = $row['Password'];

            $validOldPassword = false;



            // First check if stored password is using password_hash

            if (password_verify($oldPassword, $storedPassword)) {

                $validOldPassword = true;

            }

            // Fallback: check MD5

            elseif ($storedPassword === md5($oldPassword)) {

                $validOldPassword = true;

            }



            if ($validOldPassword) {

                // Always update with password_hash (secure)

                $hashedNewPassword = password_hash($newPassword, PASSWORD_DEFAULT);



                $update = "UPDATE tblusers SET Password=:newpassword WHERE id=:adminid";

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

                $stmt->bindParam(':newpassword', $hashedNewPassword, PDO::PARAM_STR);

                $stmt->bindParam(':adminid', $adminid, PDO::PARAM_STR);

                $stmt->execute();



                flash_alert('Your password has been successfully changed');

            } else {

                flash_alert('Your current password is wrong');

            }

        }

    }

}



?>



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



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

    <div class="wrapper">

      <!-- Navbar -->

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

      <!-- /.navbar -->

      <!-- Side bar and Menu -->

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

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



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

      <div class="content-wrapper">

<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-tie"></i>

                        </div>

                        <div>

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

                                Teacher 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="teacher_dashboard.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="col-lg-12">

          <div class="card card-primary mb-4">

            <div class="card-header py-3 d-flex flex-row align-items-center justify-content-between">

              <h5 class="m-0 font-weight-bold"> <i class="fas fa-lock"></i> Update Password</h5>

            </div>

            <div class="card-body">

              <!-- Date -->



              <form role="form" id="" 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>

            <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>

<?php

} ?>