<?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 == 'adminlist.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']=='change_status' && isset($_REQUEST['admin_id'])){
    $adminDetailLog = $adminObj->fromId($_REQUEST['admin_id']);
    if($adminDetailLog->admin_status == '1')
    {
        $admin_staus = 'Active';
    }else{
        $admin_staus = 'Inactive';
    }
    $admin_detail = " <b>Id</b>:".$_REQUEST['admin_id']." <br/><b>Name</b>:".$adminDetailLog->admin_fname." ".$adminDetailLog->admin_lname." <br/><b>Change Status Old data</b> ".$admin_staus;
	if(isset($_REQUEST['status'])){
            
            if($_REQUEST['status'] == '1')
            {
                $adminstatusnew = 'Inactive';
            }else{
                $adminstatusnew = 'Active';
            }
            $admin_detail .= " <b>To</b> ".$adminstatusnew;
            
            $adminObj->changeStatus($_REQUEST['admin_id']);
            echo RECORD_STATUS;

            $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$admin_detail);
	}
        return true;
}

if(isset($_REQUEST['act']) && $_REQUEST['act']=='delete' && isset($_REQUEST['admin_id'])){

    $adminData = $adminObj->fromId($_REQUEST['admin_id']);

    $admin_detail = " <b>Delete record id</b>:  ".$_REQUEST['admin_id']." and <b>Name</b>: ".$adminData->admin_fname." ".$adminData->admin_lname." and <b>Email</b>: ".$adminData->admin_email_address;
    
    //$adminObj->deleteFromId($_REQUEST['admin_id']);
    $data['admin_isdeleted'] = '1';
    $rs=$adminObj->updateRecords($data,$_REQUEST['admin_id']);
	$successMsg = base64_encode(ADMIN_DELETE);
    
    $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$admin_detail);
    header('Location:adminlist.php');
    exit;
}

if(isset($_REQUEST['action']) && $_REQUEST['action']!=''){

        if($_REQUEST['action']=="activate_all"){
            $data=array("admin_status" => '1');
            $chk_data = $_POST['check'];
            foreach($chk_data as $akey => $val_actchk)
            {
                $old_data = $adminObj->fromId($val_actchk);
                if($old_data->admin_type == '1')
                {
                    $adminiddata = $old_data->admin_id;
                }
                if($old_data->admin_type != '1')
                {
                    if($old_data->admin_status == '1')
                    {
                        $old_status = 'Active';
                    }else{
                        $old_status = 'Inactive';
                    }

                    if($old_data->admin_status != '1')
                    {
                        $new_status = 'Active';
                        $admindata = "<b>Id</b>:".$val_actchk." <br/><b> change Status</b>: "." ".$old_status." <b>To</b> ".$new_status." ";
                        $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$admindata);
                    }
                }
            }
            if (($key = array_search($adminiddata, $_POST['check'])) !== false) {
                    unset($_POST['check'][$key]);
            }
            $rs = $adminObj->updateManyRecords($data, $_POST['check']);
            $successMsg = base64_encode(ADMIN_ALL_ACTIVE);
        }else if($_REQUEST['action']=="inactivate_all"){
            $data=array("admin_status" => '0');
            $chk_data = $_POST['check'];
            foreach($chk_data as $akey => $val_actchk)
            {
                $old_data = $adminObj->fromId($val_actchk);
                if($old_data->admin_type == '1')
                {
                    $adminiddata = $old_data->admin_id;
                }
                if($old_data->admin_type != '1')
                {
                    if($old_data->admin_status == '1')
                    {
                        $old_status = 'Active';
                    }else{
                        $old_status = 'Inactive';
                    }

                    if($old_data->admin_status != '0')
                    {
                        $new_status = 'Inactive';
                        //$admindata = " <b>Status</b>: "." ".$old_status." <b>To</b> ".$new_status." ";
                        $admindata = "<b>Id</b>:".$val_actchk." <br/><b> change Status</b>: "." ".$old_status." <b>To</b> ".$new_status." ";
                        $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$admindata);
                    }
                }
            }
            if (($key = array_search($adminiddata, $_POST['check'])) !== false) {
                    unset($_POST['check'][$key]);
            }
            
            $rs = $adminObj->updateManyRecords($data, $_POST['check']);
            $successMsg = base64_encode(ADMIN_ALL_INACTIVE);
        }else if($_REQUEST['action']=="delete_all"){
            
            $chk_del = implode(",",$_POST['check']);
        
            $admindata = " <b>Delete Id</b>: "." ".$chk_del;
            $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$admindata);

            $rs = $adminObj->deleteManyRecords($_POST['check']);
            $successMsg = base64_encode(ADMIN_ALL_DELETE);
        }
}


/*$page=1;
$records=999;
$start=0;
if(isset($_GET['page']) && $_GET['page']!=''){
    $page=$_GET['page'];
}
$start=($page-1)*$records;

$where = ' 1=1 ';
if(isset($_REQUEST['search_input']) && $_REQUEST['search_input']!=''){
    $where = " ( admin_fname LIKE '".$_REQUEST['search_input']."%' OR admin_fname LIKE '".$_REQUEST['search_input']."%' OR admin_email_address LIKE '".$_REQUEST['search_input']."%' OR admin_type LIKE '%".$_REQUEST['search_input']."%' OR admin_created_date LIKE '%".$_REQUEST['search_input']."%' OR admin_last_login_date LIKE '%".$_REQUEST['search_input']."%' )";
}

$orderFields = array('admin_fname','admin_email_address','admin_type','admin_created_date','admin_last_login_date');

$orderby=array();
if (isset($_REQUEST['order']['0']['column']) && $_REQUEST['order']['0']['column']!=""){
    $orderby = array(" ".$orderFields[$_REQUEST['order']['0']['column']]." ".$_REQUEST['order']['0']['dir']);
}else{
    $orderby = array(" admin_id asc ");
}
$limit = array('start' => $start, 'record' => $records);
$data = array('where' => $where,'orderBy'=>$orderby, 'limit' => $limit);
$adminDetails = $adminObj->getAllRecords($data);

$rows = $adminObj->getAllCount();*/

if (isset($_REQUEST['act']) && $_REQUEST['act']=='load'){

    $records = $_REQUEST['length'];
    $start=0;
    if(isset($_GET['start']) && $_GET['start']!='0'){
        $page = $_GET['start'];
    }
    $start=($page-1)*$limit;
    $limit = array('start'=>$_GET['start'],'record'=>$records);
    
    $where = " 1=1 and admin_isdeleted = '0'";

    if(isset($_REQUEST['fullname']) && $_REQUEST['fullname'] != ''){
        $adminname = preg_replace('/\s\s+/', ' ', $_REQUEST['fullname']);
        $fullname = explode(" ", $adminname);
        if($fullname && count($fullname)>0){
            $where ="";
            foreach ($fullname as $key => $f){
                $where .=" admin_fname LIKE '%".$f."%' OR admin_fname LIKE '%".$f."%'  OR admin_lname LIKE '%".$f."%' OR admin_lname LIKE '%".$f."%'";
                if( ($key+1) != count($fullname) )
                {
                        $where .=" OR ";
                }
            }
        }
    }
    if(isset($_REQUEST['admin_email']) && $_REQUEST['admin_email'] != ''){
        $where .= " AND admin_email_address = '".$_REQUEST['admin_email']."'";
    }
    if(isset($_REQUEST['admin_role_type']) && $_REQUEST['admin_role_type'] != ''){
        $where .= " AND admin_type = '".$_REQUEST['admin_role_type']."'";
    }
    if(isset($_REQUEST['admin_status']) && $_REQUEST['admin_status'] != ''){
        $where .= " AND admin_status = '".$_REQUEST['admin_status']."'";
    }
    
    if(isset($_REQUEST['search_input']) && $_REQUEST['search_input']!=''){
    $where = " ( admin_fname LIKE '".$_REQUEST['search_input']."%' OR admin_fname LIKE '".$_REQUEST['search_input']."%' OR admin_email_address LIKE '".$_REQUEST['search_input']."%' OR admin_type LIKE '%".$_REQUEST['search_input']."%' OR admin_created_date LIKE '%".$_REQUEST['search_input']."%' OR admin_last_login_date LIKE '%".$_REQUEST['search_input']."%' )";
    }
    
    $orderFields = array('admin_fname','admin_email_address','admin_type','admin_created_date','admin_last_login_date');    
    
    // Array of order by 
    $orderby=array();
    if (isset($_REQUEST['order']['0']['column']) && $_REQUEST['order']['0']['column']!=""){
        $orderby = array(" ".$orderFields[$_REQUEST['order']['0']['column']]." ".$_REQUEST['order']['0']['dir']);
    }else{
        $orderby = array(" admin_id desc ");
    }
    
    $data    = array('where' => $where,'limit' => $limit, 'orderBy'=>$orderby);
    $adminDetails = $adminObj->getAllRecords($data);
    //print_r($adminDetails);exit;
    $recordsTotal   =   $adminObj->getAllCount($data);

    if ($adminDetails && count($adminDetails)>0){
        foreach ($adminDetails as $key => $admin){

            if(isset($admin->admin_type) && $admin->admin_type !='')
            {
                  $roleobj = new role;
                  $fields = array('role_id,role_name');
                  $where = "role_id='".$admin->admin_type."'";
                  $roleArg = array("fields"=>$fields,"where"=>$where);
                  $roledata = $roleobj->fromFields($roleArg);
                  if($roledata)
                  {
                    $role_name = $roledata->role_name;  
                  }else{
                    $role_name = '';
                  }
                  
            }else{ 
                $role_name = '';
            }
            
            if($admin->admin_type != '1')
            {
                if($admin->admin_status == '1')
                {
                    
                  $status = '<a href="javascript:void(0)" onclick="change_status('.$admin->admin_id.',1)" title="Inactivate"><span class="badge badge-success">Active</span></a>';
                }else{

                  $status = '<a href="javascript:void(0)" onclick="change_status('.$admin->admin_id.',0)" title="Activate"><span class="badge badge-danger">Inactive</span></a>';
                }

                $action = '<a href="adminadd.php?act=edit&admin_id='.$admin->admin_id.'" class="btn btn-success white-btn"><i class="nav-icon i-Pen-2 "></i></a>&nbsp;&nbsp;<a id="delete-confirm" onclick="delete_entry('.$admin->admin_id.');" class="btn btn-danger white-btn"><i class="nav-icon i-Close-Window "></i></a><br/><br/><a href="changepassword.php?user_type=admin&user_id='.$admin->admin_id.'" class="tooltips" title="Change Password"><i class="i-Lock nav-icon"></i></a>';
            }else{
                $status = '';
                $action = '<a href="adminadd.php?act=edit&id='.$admin->admin_id.'" class="btn btn-success white-btn"><i class="nav-icon i-Pen-2 "></i></a>';
            }
            $response[] = array('<input type="checkbox" name="check[]" id="check[]" value="'.$admin->admin_id.'" class="check_class" />',
                    $admin->admin_fname.' '.$admin->admin_lname,
                    $admin->admin_email_address,
                    $role_name,
                    date('d-m-Y',strtotime($admin->admin_created_date)),
                    date('d-m-Y',strtotime($admin->admin_last_login_date)),
                    $admin->admin_last_login_ip,
                    "<div id='td_status_".$admin->admin_id."'>".$status."</div>",
                    $action,
             );
        }
        $result=array('recordsFiltered'=>$recordsTotal,'recordsTotal'=>$recordsTotal,'data'=>$response);
    }else{
        $result=array('recordsFiltered'=>0,'recordsTotal'=>0,'data'=>array());
    }
    echo json_encode($result);
    exit;
}

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