<?php 

// Include main file
include_once('../libs/main.php');

// Check admin Login
include_once('checkLogin.php');

$successMsg =  isset($_REQUEST['successMsg']) ? $_REQUEST['successMsg'] : '';
$failedMsg = isset($_REQUEST['failedMsg']) ? $_REQUEST['failedMsg'] : '';

$adminlogObj = new admin_log();
$modulepagesObj = new module_pages();
$filename=basename($_SERVER['SCRIPT_FILENAME']);
if($filename == 'changepassword.php')
{
    $module_id = $adminlogObj->getModuleId($filename);
    $page_id = $adminlogObj->getPageId($filename);
    $allpages = $modulepagesObj->fromId($page_id);
    $pagename = $allpages->page;
}

// Logical section of page
if($_REQUEST['user_type']=="" || $_REQUEST['user_id']=="" || !isset($_REQUEST['user_type']) || !isset($_REQUEST['user_id'])){
	header('Location:userlist.php');
	exit;
}

if(isset($_POST['submit'])){
	
	if($_POST['user_password']==$_POST['user_repassword']){
		
			if($_POST['user_type']=='user'){
				$userObj =  new  user();
				$userData['user_password'] = w::password($_POST['user_password']);
					
				$rs = $userObj->updateRecords($userData,$_POST['user_id']);
				if($rs){
					$changepassword_detail = "<b>Change Password User</b> ".$_POST['user_password'];

					$admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$changepassword_detail);

					$successMsg =base64_encode(PASSWORD_CHANGE);			
					header('Location:userlist.php?successMsg='.$successMsg);
					exit;
				}
					
			}else if($_POST['user_type']=='admin'){
				$adminObj = new admin();
				$adminData['admin_password'] = md5($_POST['user_password']);

				$rs = $adminObj->updateRecords($adminData,$_POST['user_id']);
				if($rs){

					$changepassword_detail = "<b>Change Password Admin</b> ".$_POST['user_password'];

					$admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$changepassword_detail);

                    $successMsg =base64_encode(PASSWORD_CHANGE);			
                    header('Location:adminlist.php?successMsg='.$successMsg);
                    exit;
				}
			}
			else if($_POST['user_type']=='superuser'){
				$superuserObj = new superuser();
				$superuserData['superuser_password'] = w::password($_POST['user_password']);
					
				$rs = $superuserObj->updateRecords($superuserData,$_POST['user_id']);
				if($rs){

					$changepassword_detail = "<b>Change Password SuperUser</b> ".$_POST['user_password'];

					$admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$changepassword_detail);

                    $successMsg =base64_encode(PASSWORD_CHANGE);			
                    header('Location:superuserlist.php');
                    exit;
				}
			}
		
			
	}else{
		$failedMsg = PASSWORD_NO_MATCH;;
	}
}

// Include Template 
include_once('../libs/routs.php')

?>