<?php
    include('session.php');
?>

<?php
     if($_SERVER["REQUEST_METHOD"] == "POST")
    {
          if (isset($_POST['savePassword']))
          {
              
              $tmpid = $_SESSION['login_id'];
              $struname = $_POST['username'];
              $strcpword = $_POST['cpassword'];
              $strnewpword = $_POST['newpassword'];
              $strcnewpword= $_POST['cnewpassword'];
              
              $sql=mysqli_query($conn,"SELECT * FROM $tblaccounts WHERE AutoIndex LIKE '$tmpid'");

              $num_rows = mysqli_num_rows($sql);
              
              if ($num_rows > 0)
              {
                  $row = mysqli_fetch_assoc($sql);
                  $dbpassword = $row["Password"];
                  
                  if ($strcpword === $dbpassword)
                      {
                          if (strlen($strcpword) === 0)
                              {
                                  $msg = "RALAT : Katalaluan Semasa Diperlukan!";
                                  $_SESSION['status_message'] = $msg;
                                  echo "<script>alert('" . $msg . "');</script>";
                              }
                          else if (strlen($strnewpword) === 0)
                              {
                                  $msg = "RALAT : Katalaluan Baru Diperlukan!";
                                  $_SESSION['status_message'] = $msg;
                                  echo "<script>alert('" . $msg . "');</script>";
                              }
                          else if (strlen($strcnewpword) === 0)
                              {
                                  $msg = "RALAT : Katalaluan Sah Diperlukan!";
                                  $_SESSION['status_message'] = $msg;
                                  echo "<script>alert('" . $msg . "');</script>";
                              }
                          else
                              {
                              if ($strnewpword === $strcnewpword)
                                  {
                                      $sql = "UPDATE $tblaccounts SET Password = '$strcnewpword' WHERE AutoIndex LIKE '$tmpid'";
                                      
                                      if ($conn->query($sql) === TRUE) {
                                          $msg = "Katalaluan Dikemaskini!";
                                          $_SESSION['status_message'] = $msg;
                                          echo "<script>alert('" . $msg . "');</script>";
                                          echo "<script>window.location.href = 'index.php';</script>";
                                      } else {
                                          $msg = "RALAT : Kemaskini Data!";
                                          $_SESSION['status_message'] = $msg;
                                          echo "<script>alert('" . $msg . "');</script>";
                                      }
                                    }
                              else
                                  {
                                      $msg = "RALAT : Katalaluan dan Katalaluan Sah Tidak Sama!";
                                      $_SESSION['status_message'] = $msg;
                                      echo "<script>alert('" . $msg . "');</script>";
                                  }
                            }
                    }
                else
                    {
                        $msg = "RALAT : Katalaluan Semasa Tidak Sah!";
                        $_SESSION['status_message'] = $msg;
                        echo "<script>alert('" . $msg . "');</script>";
                    }
               
              }
              else
              {
                  
                  $msg = "RALAT : Data Tidak WUJUD!";
                  $_SESSION['status_message'] = $msg;
                  echo "<script>alert('" . $msg . "');</script>";
                  
              }
              
            }
        $tmpid = $_SESSION['login_id'];
        
        $sql="SELECT * FROM $tblaccounts WHERE AutoIndex LIKE '$tmpid' ORDER BY AutoIndex ASC";
        
        if($result=mysqli_query($conn,$sql))
            {
                 while ($row=mysqli_fetch_array($result))
                 {
                     
                     $strnric = $row['NRIC'];
                     $strfullname = $row['FullName'];
                     $strusername = $row['Username'];
                     $strpassword = $row['Password'];
                     $struertype = $row['UserType'];
                     $stremail = $row['Email'];
                     $strmobile = $row['Mobile'];
                     $strcenter = $row['ACenter'];
                     $strstatus = $row['AStatus'];
                     
                 }
            }
     }
else
{
    $tmpid = $_SESSION['login_id'];
    
    $sql="SELECT * FROM $tblaccounts WHERE AutoIndex LIKE '$tmpid' ORDER BY AutoIndex ASC";
    
    if($result=mysqli_query($conn,$sql))
        {
             while ($row=mysqli_fetch_array($result))
             {
                 
                 $strnric = $row['NRIC'];
                 $strfullname = $row['FullName'];
                 $strusername = $row['Username'];
                 $strpassword = $row['Password'];
                 $struertype = $row['UserType'];
                 $stremail = $row['Email'];
                 $strmobile = $row['Mobile'];
                 $strcenter = $row['ACenter'];
                 $strstatus = $row['AStatus'];
                 
             }
        }
}
?>

<!DOCTYPE html>
<html lang="en" class="h-100">
	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, shrink-to-fit=9">
		<meta name="description" content="Gambolthemes">
		<meta name="author" content="Gambolthemes">		
        <title><?php echo $title ?></title>
		
		<!-- Favicon Icon -->
        <link rel="icon" type="image/png" href="images/psn/logo.png">
		
        <!-- Stylesheets -->
        <?php include 'plugincss.php' ?>


	</head>

    <body class="d-flex flex-column h-100">

	<!-- Header Start-->
	<header class="header">
        <?php include 'topmenu.php' ?>
	</header>
	<!-- Header End-->
	<!-- Left Sidebar Start -->
	<nav class="vertical_nav">
        <?php include 'sidemenu.php' ?>
	</nav>

	<!-- Left Sidebar End -->
	<!-- Body Start -->
	<div class="wrapper wrapper-body">
		<div class="dashboard-body">
			<div class="container-fluid">
				<div class="row">
					<div class="col-md-12">
						<div class="d-main-title">
							<h3><i class="fa-regular fa-credit-card me-3"></i>Profil Saya</h3>
						</div>
					</div>
                    <div class="col-md-12">
                            
                        <div class="col-xl-12 col-lg-7 col-md-12">
                            <div class="right-profile">
                                <div class="profile-tabs">
                                    <ul class="nav nav-pills nav-fill p-2 garren-line-tab" id="myTab" role="tablist">
                                        <li class="nav-item">
                                            <a class="nav-link active" id="about-tab" data-bs-toggle="tab" href="#about" role="tab" aria-controls="about" aria-selected="false"><i class="fa-solid fa-circle-info"></i>Maklumat</a>
                                        </li>
                                        <li class="nav-item">
                                            <a class="nav-link" id="setting-tab" data-bs-toggle="tab" href="#setting" role="tab" aria-controls="setting" aria-selected="false"><i class="fa-solid fa-gear"></i>Katalaluan</a>
                                        </li>
                                    </ul>
                                    <div class="tab-content" id="myTabContent">
                                        <div class="tab-pane fade active show" id="about" role="tabpanel" aria-labelledby="about-tab">
                                            <div class="main-card mt-4">
                                                <div class="bp-title position-relative">
                                                    <h4>Maklumat Saya</h4>
                                                </div>
                                                <div class="about-details">
                                                    <div class="col-lg-6 col-md-12">
                                                        <label class="form-label">Nama</label>
                                                        <span><?php echo $strfullname ?></span>
                                                    </div>
                                                    <br>
                                                    <div class="col-lg-6 col-md-12">
                                                        <label class="form-label">Emel</label>
                                                        <span><?php echo $stremail ?></span>
                                                    </div>
                                                    <br>
                                                    <div class="col-lg-6 col-md-12">
                                                        <label class="form-label">No Telefon</label>
                                                        <span><?php echo $strmobile ?></span>
                                                    </div>
                                                    <br>
                                                    <div class="col-lg-6 col-md-12">
                                                        <label class="form-label">Pusat</label>
                                                        <span><?php echo $strcenter ?></span>
                                                    </div>
                                                    <br>
                                                    <div class="col-lg-6 col-md-12">
                                                        <label class="form-label">Status</label>
                                                        <span><?php echo $strstatus ?></span>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="tab-pane fade" id="setting" role="tabpanel" aria-labelledby="setting-tab">
                                            <div class="row">
                                                <div class="col-lg-12">
                                                    <div class="tab-pane fade active show" id="tab-02" role="tabpanel">
                                                        <div class="bp-title">
                                                            <h4>Tetapan Katalaluan</h4>
                                                        </div>
                                                        <div class="password-setting p-4">
                                                            <div class="password-des">
                                                                <h4>Tukar Katalaluan</h4>
                                                                <p>Anda boleh mengemas kini katalaluan anda dari sini. Jika anda tidak dapat mengingati kata laluan semasa anda, hanya perlu log keluar dan klik pada Lupa kata laluan.</p>
                                                            </div>
                                                            <form class="change-password-form" method="post" action="">
                                                                <div class="form-group mt-4">
                                                                    <label class="form-label">Name Pengguna</label>
                                                                    <div class="loc-group position-relative">
                                                                        <input class="form-control h_50" id="username" name="username" type="text" value="<?php echo $strusername ?>" readonly>

                                                                    </div>
                                                                </div>
                                                                <div class="form-group mt-4">
                                                                    <label class="form-label">Katalaluan Semasa *</label>
                                                                    <div class="loc-group position-relative">
                                                                        <input class="form-control h_50" id="cpassword" name="cpassword" type="password">
                                                                        <span class="pass-show-eye" onclick="toggleCPassword()"><i class="fas fa-eye-slash"></i></span>
                                                                    </div>
                                                                </div>
                                                                <div class="form-group mt-4">
                                                                    <label class="form-label">Katalaluan Baru *</label>
                                                                    <div class="loc-group position-relative">
                                                                        <input class="form-control h_50" id="newpassword" name="newpassword" type="password" >
                                                                        <span class="pass-show-eye" onclick="toggleNPassword()"><i class="fas fa-eye-slash"></i></span>
                                                                    </div>
                                                                </div>
                                                                <div class="form-group mt-4">
                                                                    <label class="form-label">Katalaluan Sah *</label>
                                                                    <div class="loc-group position-relative">
                                                                        <input class="form-control h_50" id="cnewpassword" name="cnewpassword" type="password">
                                                                        <span class="pass-show-eye" onclick="toggleCNPassword()"><i class="fas fa-eye-slash"></i></span>
                                                                    </div>
                                                                </div>
                                                                <button class="main-btn btn-hover w-100 mt-5" type="submit" id="savePassword" name="savePassword">Kemaskini Katalaluan</button>
                                                            </form>
                                                        </div>
                                                    </div>
                                                </div>

                                        
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        </div>
                        </div>
                        </div>
                        </div>

                    </div>
				</div>
			</div>
		</div>
	</div>
	<!-- Body End -->
    <?php include 'pluginjs.php' ?>

    <script>
    function toggleCPassword() {
        var passwordInput = document.getElementById("cpassword");
        var eyeIcon = document.getElementById("eye-icon");

        if (passwordInput.type === "password") {
            passwordInput.type = "text";
            eyeIcon.classList.remove("fa-eye-slash");
            eyeIcon.classList.add("fa-eye");
        } else {
            passwordInput.type = "password";
            eyeIcon.classList.remove("fa-eye");
            eyeIcon.classList.add("fa-eye-slash");
        }
    }
    </script>

    <script>
    function toggleNPassword() {
        var passwordInput = document.getElementById("newpassword");
        var eyeIcon = document.getElementById("eye-icon");

        if (passwordInput.type === "password") {
            passwordInput.type = "text";
            eyeIcon.classList.remove("fa-eye-slash");
            eyeIcon.classList.add("fa-eye");
        } else {
            passwordInput.type = "password";
            eyeIcon.classList.remove("fa-eye");
            eyeIcon.classList.add("fa-eye-slash");
        }
    }
    </script>

    <script>
    function toggleCNPassword() {
        var passwordInput = document.getElementById("cnewpassword");
        var eyeIcon = document.getElementById("eye-icon");

        if (passwordInput.type === "password") {
            passwordInput.type = "text";
            eyeIcon.classList.remove("fa-eye-slash");
            eyeIcon.classList.add("fa-eye");
        } else {
            passwordInput.type = "password";
            eyeIcon.classList.remove("fa-eye");
            eyeIcon.classList.add("fa-eye-slash");
        }
    }
    </script>

</body>
</html>
