<?php 

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

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

// Logical section of page
$adminObj =  new  admin();
$adminlogObj = new admin_log();
$modulepagesObj = new module_pages();
$filename=basename($_SERVER['SCRIPT_FILENAME']);

if($filename == 'adminadd.php')
{
    $module_id = $adminlogObj->getModuleId($filename);
    $page_id = $adminlogObj->getPageId($filename);
    $allpages = $modulepagesObj->fromId($page_id);
    $pagename = $allpages->page;
}

if(isset($_REQUEST['act']) && $_REQUEST['act']=='edit' && isset($_REQUEST['admin_id'])){
	$adminDetail = $adminObj->fromId($_REQUEST['admin_id']);
}

if(isset($_REQUEST['act']) && $_REQUEST['act']=='validate_email'){
	if($_REQUEST['email']==''){
            echo ENTER_EMAIL;
            exit;
	}else if(validateEmail($_REQUEST['email'])){
		
            $select=array('admin_email_address','admin_id');
            $where =array('admin_email_address='=>$_REQUEST['email']);

            if(isset($_REQUEST['admin_id']) && $_REQUEST['admin_id']!=""){
                $where['admin_id!='] =$_REQUEST['admin_id'];
            }
            $data =array('fields'=>$select,'where'=>$where);        
            
            if(isset($_REQUEST['admin_id']) && $adminObj->fromFields($data)){
                echo EMAIL_EXIST;
                exit;
            }else if(!isset($_REQUEST['admin_id']) && $adminObj->fromFields($data)){
                echo EMAIL_EXIST;
                exit;
            }
	}else{
            echo VALID_EMAIL;
            exit;
	}
	return true;
}

if(isset($_POST['submit'])){
	   
    	$data=array(
		  'admin_fname'=>trim($_POST['admin_fname']),
		  'admin_lname'=>trim($_POST['admin_lname']),
		  'admin_type'=>trim($_POST['admin_type']),
          'admin_phone'=>trim($_POST['admin_phone']),
          'admin_email_address'=>trim($_POST['admin_email_address']),
          'admin_type'=>trim($_POST['admin_role'])
	    );
        
        if(isset($_POST['admin_type']) && $_POST['admin_type']=='super administrator'){
            $data['admin_access'] 	=  'Full';	
            $data['admin_status']       = '1';	
    	}else{
                $data['admin_access'] 		=  (isset($_POST['admin_access']) && count($_POST['admin_access']) > 0) ? implode(',',$_POST['admin_access']) : '';	
                if(isset($_POST['admin_status'])){
                    $data['admin_status'] 			= '1';
                }else{
                    $data['admin_status'] 			= '0';
                }                
    	}
    $chages_data = false;
	if (isset($_POST['admin_id'])){
         $admin_detail = '';
         $admin_detail = 'Edit Admin Old';
         $adminDetailLog = $adminObj->fromId($_POST['admin_id']);

         if($adminDetailLog->admin_fname != $_POST['admin_fname'])
         {
            $admin_detail .= " <b>First name</b>: "." ".$adminDetailLog->admin_fname." <b>To</b> ".$_POST['admin_fname']." and ";
            
            $chages_data = true;
         }
         if($adminDetailLog->admin_lname != $_POST['admin_lname'])
         {
            $admin_detail.= " <b>Last Name</b>: "." ".$adminDetailLog->admin_lname." <b>To</b> ".$_POST['admin_lname']." and ";
         
            $chages_data = true;
         }
         if($adminDetailLog->admin_email_address != $_POST['admin_email_address'])
         {
            $admin_detail.= " <b>Email</b>: "." ".$adminDetailLog->admin_email_address." <b>To</b> ".$_POST['admin_email_address']." and ";
         
            $chages_data = true;
         }
         if($adminDetailLog->admin_phone != $_POST['admin_phone'])
         {
            $admin_detail.= " <b>Phone</b>: "." ".$adminDetailLog->admin_phone." <b>To</b> ".$_POST['admin_phone'] ." and ";
         
            $chages_data = true;
         }
         if($adminDetailLog->admin_type != $_POST['admin_role'])
         {
            $roleobj = new role();
            $oldroleDetails = '';
            $newroleDetails = '';
            if($adminDetailLog->admin_type != '')
            {
                $oldroleDetails = $roleobj->fromId($adminDetailLog->admin_type);
            }
            
            if(isset($_POST['admin_role']) && $_POST['admin_role'] != '')
            {
                $newroleDetails = $roleobj->fromId($_POST['admin_role']);    
            }
            
            $admin_detail.= " <b>Role</b>: "." ".$oldroleDetails->role_name." <b>To</b> ".$newroleDetails->role_name ." and ";
         
            $chages_data = true;
         }
         if($adminDetailLog->admin_status != $data['admin_status'])
         {
            if($data['admin_status'] == '1')
            {
                $admin_status = 'Active';
            }else{
                $admin_status = 'Inactive';
            }

            if($adminDetailLog->admin_status == '1')
            {
                $oldadmin_status = 'Active';
            }else{
                $oldadmin_status = 'Inactive';
            }

            $admin_detail.= " <b>Status</b> "." ".$oldadmin_status." <b>To</b> ".$admin_status." and ";
         
            $chages_data = true;
         }
         
         
		$id=$_POST['admin_id'];
		$rs=$adminObj->updateRecords($data,$id);
		if ($rs){
            if($chages_data == true)
            {
                $radmin_detail = substr($admin_detail, 0, -4);
                $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$radmin_detail);
            }

            $successMsg = base64_encode(ADMIN_UPDATE);			
            header('Location:adminlist.php');
           // header('Location:adminlist.php?successMsg='.$successMsg);
			exit;
		}

	}else{

        $data['admin_created_date']=date('Y-m-d H:i:s');
        $data['admin_password'] 	= md5($_POST['admin_password']);	
        $rs = $adminObj->addRecords($data);
        if($rs){

            if($data['admin_status'] == '1')
            {
                $admin_status = 'Active';
            }else{
                $admin_status = 'Inactive';
            }

            if(isset($_POST['admin_role']) && $_POST['admin_role'] != '')
            {
                $roleobj = new role();
                $roleDetails = $roleobj->fromId($_POST['admin_role']);    
            }

            $admin_detail = "Add Admin data "."<b>First name</b>: ".trim($_POST['admin_fname'])." and <b>Last Name<b/>: ".trim($_POST['admin_lname'])." and <b>Email address</b>: ".trim($_POST['admin_email_address'])." and <b>Phone</b>: ".trim($_POST['admin_phone'])." and <b>password<b/>: ".trim($_POST['admin_password'])." and <b>Role</b>: ".$roleDetails->role_name." and <b>Status</b>: ".$admin_status;

            $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$admin_detail);
            
            
    		$successMsg = base64_encode(ADMIN_ADD);			
            header('Location:adminlist.php');
    		//header('Location:adminlist.php?successMsg='.$successMsg);
    		exit;
        }
	}
}

$modulesObj = new modules();
$moduleData= array('where' => array('name!=' => 'settings'));
$adminModules = $modulesObj->getAllRecords($moduleData);

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

?>