<?php

include('session_handler.php'); // Sudah uruskan sesi

// Proses apabila borang dihantar
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $old_password = $_POST['old_password'];
    $new_password = $_POST['new_password'];
    $confirm_password = $_POST['confirm_password'];

    if ($new_password !== $confirm_password) {
        $_SESSION['message'] = "Kata laluan baru dan pengesahan kata laluan tidak sepadan.";
        $_SESSION['alert_type'] = "danger";
        header("Location: change_password.php");
        exit;
    }

    // Dapatkan kata laluan lama dari database
    $query = "SELECT password FROM users WHERE id = ?";
    $stmt = $conn->prepare($query);
    $stmt->bind_param("i", $user_id);
    $stmt->execute();
    $stmt->store_result();
    $stmt->bind_result($hashed_password);
    $stmt->fetch();

    if (!password_verify($old_password, $hashed_password)) {
        $_SESSION['message'] = "Kata laluan lama tidak betul.";
        $_SESSION['alert_type'] = "danger";
        header("Location: change_password.php");
        exit;
    }

    // Hash kata laluan baru dan kemaskini
    $hashed_new_password = password_hash($new_password, PASSWORD_DEFAULT);
    $update_query = "UPDATE users SET password = ?, MdfDate = NOW() WHERE id = ?";
    $update_stmt = $conn->prepare($update_query);
    $update_stmt->bind_param("si", $hashed_new_password, $user_id);
    $update_stmt->execute();

    if ($update_stmt->affected_rows > 0) {
        $_SESSION['message'] = "Kata laluan telah berjaya dikemaskini.";
        $_SESSION['alert_type'] = "success";
    } else {
        $_SESSION['message'] = "Terdapat masalah semasa mengemaskini kata laluan.";
        $_SESSION['alert_type'] = "danger";
    }

    header("Location: change_password.php");
    exit;
}

// Dapatkan maklumat pengguna untuk paparan
$query = "SELECT u.NamaStaff, u.NomborStaff, u.email, u.MdfDate, j.NamaJbtn, b.Keterangan 
          FROM users u
          LEFT JOIN jabatan j ON u.jabatan = j.ID
          LEFT JOIN bahagian b ON u.bahagian = b.ID
          WHERE u.id = ?";

$stmt = $conn->prepare($query);
$stmt->bind_param("i", $user_id);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($NamaStaff, $NomborStaff, $email, $MdfDate, $NamaJbtn, $NamaBhgn);
$stmt->fetch();
$stmt->close();
$conn->close();
?>

<!DOCTYPE html>
<html lang="en">
<!-- header part -->
<?php
include('header.php');
?>
<!-- end header -->
<body class="skin-blue fixed-layout">
    <!-- ============================================================== -->
    <!-- Preloader - style you can find in spinners.css -->
    <!-- ============================================================== -->
    <div class="preloader">
        <div class="loader">
            <div class="loader__figure"></div>
            <p class="loader__label">SPNR Surat/Memo</p>
        </div>
    </div>
    <!-- ============================================================== -->
    <!-- Main wrapper - style you can find in pages.scss -->
    <!-- ============================================================== -->
    <div id="main-wrapper">
        <!-- ============================================================== -->
        <!-- Topbar header - style you can find in pages.scss -->
        <!-- ============================================================== -->
        <?php
        include('topbar.php');
        ?>
        <!-- ============================================================== -->
        <!-- End Topbar header -->
        <!-- ============================================================== -->
        <!-- ============================================================== -->
        <!-- Left Sidebar - style you can find in sidebar.scss  -->
        <!-- ============================================================== -->
         <?php
        include('leftsidebar.php');
        ?>
        <!-- ============================================================== -->
        <!-- End Left Sidebar - style you can find in sidebar.scss  -->
        <!-- ============================================================== -->
        <!-- ============================================================== -->
        <!-- Page wrapper  -->
        <!-- ============================================================== -->
        <div class="page-wrapper">
            <!-- ============================================================== -->
            <!-- Container fluid  -->
            <!-- ============================================================== -->
            <div class="container-fluid">
            <?php if (isset($_SESSION['message'])): ?>
        <div class="alert alert-<?php echo $_SESSION['alert_type']; ?> alert-dismissible fade show" role="alert">
                        <?php echo $_SESSION['message']; ?>
                        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                    </div>
        <?php unset($_SESSION['message'], $_SESSION['alert_type']); ?>
    <?php endif; ?>
                <!-- ============================================================== -->
                <!-- Bread crumb and right sidebar toggle -->
                <!-- ============================================================== -->
                <div class="row page-titles">
                    <div class="col-md-5 align-self-center">
                        <h4 class="text-themecolor">Ubah Kata Laluan</h4>
                    </div>
                    <div class="col-md-7 align-self-center text-end">
                        <div class="d-flex justify-content-end align-items-center">
                            <ol class="breadcrumb justify-content-end">
                                <li class="breadcrumb-item"><a href="javascript:void(0)">Home</a></li>
                                <li class="breadcrumb-item active">Reset KataLaluan</li>
                            </ol>
                        </div>
                    </div>
                </div>

                <div class="row">
                    <div class="col-6">
                        <div class="card">
                            <div class="card-body">
                                <?php
                                // Output maklumat pengguna yang telah diambil dari database
                                echo "<h4><b>Nama: $NamaStaff</b></h4>";
                                echo "<p>Nombor Staf: $NomborStaff</p>";
                                echo "<p>Email: $email</p>";
                                echo "<p>Jabatan: $NamaJbtn</p>";
                                echo "<p>Bahagian: $NamaBhgn</p>";
                                echo "<p>Tarikh Kemaskini: $MdfDate</p>";
                                ?>
                            </div>
                        </div>
                    </div>

                    <div class="col-6">
                        <div class="card">
                            <div class="card-body">
                                <!-- Borang untuk ubah kata laluan -->
                                <h5>Ubah Kata Laluan</h5>
                                <form method="POST" action="change_password.php">
                                    <div class="form-group">
                                        <label for="old_password">Kata Laluan Lama</label>
                                        <input type="password" class="form-control" id="old_password" name="old_password" required>
                                    </div>
                                    <div class="form-group">
                                        <label for="new_password">Kata Laluan Baru</label>
                                        <input type="password" class="form-control" id="new_password" name="new_password" required>
                                    </div>
                                    <div class="form-group">
                                        <label for="confirm_password">Sahkan Kata Laluan Baru</label>
                                        <input type="password" class="form-control" id="confirm_password" name="confirm_password" required>
                                    </div>
                                    <button type="submit" class="btn btn-primary">Kemaskini</button>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>

            </div>
        </div>
       
        <!-- footer -->
        <!-- ============================================================== -->
        <?php
        include('footer.php');
        ?>
        <!-- ============================================================== -->
        <!-- End footer -->
        <!-- ============================================================== -->
    </div>
    
    <!-- All Jquery -->
    <!-- ============================================================== -->
     <?php
        include('jquery.php');
    ?>
</body>
</html>