<?php
include('header.php');
$sql = "SELECT * FROM users WHERE id='$_SESSION[media_cms_user]' AND super_admin='1'";
$query = $mysqli->query($sql);
$row = $query->fetch_assoc();
if (isset($_POST['change_password'])) {
$current_password = make_safe(xss_clean($_POST['current_password']));
$new_password = make_safe(xss_clean($_POST['new_password']));
$confirm_password = make_safe(xss_clean($_POST['confirm_password']));
if (empty($current_password)) {
$message = notification('warning','Please Insert The Current Password.');	
} elseif (empty($new_password)) {
$message = notification('warning','Please Insert The New Password.');	
} elseif (empty($confirm_password)) {
$message = notification('warning','Please Confirm The New Password.');		
} elseif ($new_password != $confirm_password) {
$message = notification('warning','New Password Isn\'t Match the Confirmation.');		
} elseif ($row['password'] != hash('sha256', md5($current_password))) {
$message = notification('warning','The Current Password Is Wrong.');		
} else {
$encoded_password = hash('sha256', md5($new_password));
$sql = "UPDATE users SET password='$encoded_password' WHERE id='$_SESSION[media_cms_user]' AND super_admin='1'";
$query = $mysqli->query($sql);
if ($query) {
$message = notification('success','The Password Changed Successfully.');	
} else {
$message = notification('danger','Error Happened');	
}
}	
}

if (isset($_POST['change_info'])) {
$username = make_safe(xss_clean($_POST['username']));
$email = make_safe(xss_clean($_POST['email']));
if (empty($username)) {
$message = notification('warning','Please Insert Username.');	
} elseif (empty($email)) {
$message = notification('warning','Please Insert Email.');	
} else {
$sql = "UPDATE users SET username='$username',email='$email' WHERE id='$_SESSION[media_cms_user]' AND super_admin='1'";
$query = $mysqli->query($sql);
if ($query) {
$message = notification('success','The Admin Info Changed Successfully.');	
} else {
$message = notification('danger','Error Happened');	
}
}	
}
?>
			<div class="page-header page-heading">
				<h1><i class="fa fa-lock"></i> Change Username & Email</h1>
			</div>
			<?php if (isset($message)) {echo $message;} ?>
		<form role="form" method="POST" action="">
		  <div class="form-group">
			<label for="username">Username <span>*</span></label>
			<input type="text" class="form-control" name="username" id="username" value="<?php echo $row['username']; ?>" />
		  </div>
		  <div class="form-group">
			<label for="email">E-Mail <span>*</span></label>
			<input type="text" class="form-control" name="email" id="email" value="<?php echo $row['email']; ?>" />
		  </div>
		  <button type="submit" name="change_info" class="btn btn-primary">Save</button>
		</form>
		<hr />
		<div class="page-header page-heading" style="margin-top:40px;">
				<h1><i class="fa fa-lock"></i> Change Password</h1>
			</div>
			<?php if (isset($message)) {echo $message;} ?>
		<form role="form" method="POST" action="">
		  <div class="form-group">
			<label for="current_password">Current Password <span>*</span></label>
			<input type="password" class="form-control" name="current_password" id="current_password" />
		  </div>
		  <div class="form-group">
			<label for="new_password">New Password <span>*</span></label>
			<input type="password" class="form-control" name="new_password" id="new_password" />
		  </div>
		  <div class="form-group">
			<label for="confirm_password">Confirm New Password <span>*</span></label>
			<input type="password" class="form-control" name="confirm_password" id="confirm_password" />
		  </div>
		  <button type="submit" name="change_password" class="btn btn-primary">Save</button>
		</form>
<?php
include('footer.php');
?>