<?php
session_start();

if (isset($_SESSION['username']) == true) {
	include 'master.php';
	$username = $_SESSION['username'];
}else{
	header("location:login");
}
?>
		<!--page-wrapper-->
		<div class="page-wrapper">
			<!--page-content-wrapper-->
			<div class="page-content-wrapper">
				<div class="page-content">
					<!--breadcrumb-->
					<div class="page-breadcrumb d-none d-md-flex align-items-center mb-3">
						<div class="breadcrumb-title pr-3">Change Password</div>
						<div class="pl-3">
							<nav aria-label="breadcrumb">
								<ol class="breadcrumb mb-0 p-0">
									<li class="breadcrumb-item"><a href="javascript:;"><i class='bx bx-home-alt'></i></a>
									</li>
									<li class="breadcrumb-item active" aria-current="page">Settings</li>
								</ol>
							</nav>
						</div>
						<div class="ml-auto">
							<div class="btn-group">
								<button type="button" class="btn btn-primary">Active</button>
							</div>
						</div>
					</div><br><br>
					<!--end breadcrumb-->
						<div class="row">
						<div class="col-12 col-lg-9 mx-auto">
							<div class="card radius-15">
								<div class="card-body">
									<form action="" method="post">
										<div class="form-group">
											<label>Current Password</label>
											<input type="password" class="form-control border-left" name="old_password">
										</div>
										<div class="form-group">
											<label>New Password</label>
											<input type="password" class="form-control border-left" name="new_password">
										</div>
										<div class="form-group">
											<label>Confirm Password</label>
											<input type="password" class="form-control border-left" name="confirm_password">
										</div>
										<div class="form-group">
											<button type="submit" class="btn btn-primary px-4" name="change">Change</button>
										</div>
									</form>
								</div>
							</div>
						</div>
					</div>
					<!--end row-->
				</div>
			</div>
			<!--end page-content-wrapper-->
		</div>
		<!--end page-wrapper-->
		<!--start overlay-->
		<div class="overlay toggle-btn-mobile"></div>
		<!--end overlay-->
		<!--Start Back To Top Button--> <a href="javaScript:;" class="back-to-top"><i class='bx bxs-up-arrow-alt'></i></a>
		<!--End Back To Top Button-->
<?php
include 'footer.php';

if (isset($_POST['change']) == true) {
	$old_password = sha1($_POST['old_password']);
	$new_password = sha1($_POST['new_password']);
	$confirm_password = sha1($_POST['confirm_password']);

	if (empty($old_password) == true || empty($new_password) == true || empty($confirm_password) == true) {
		echo "<script>swal('{text:'All fields are required', icon:'error'}')</script>";
	}else{
		if ($new_password != $confirm_password) {
			echo "<script>swal({text:'Password does not match', icon:'error'})</script>";
		}else{
			$sel_pass = $conn->query("SELECT password FROM administrator WHERE username = '$username' ");
			$val = mysqli_fetch_array($sel_pass);
			if ($old_password != $val['password']) {
				echo "<script>swal({text:'Incorrect old password', icon:'error'})</script>";
			}else{
				$conn->query("UPDATE administrator SET password = '$new_password' WHERE username = '$username' ");
				echo "<script>swal({text:'Password changed successfully', icon:'success'})</script>";
			}
		}
	}
}