<?php 

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

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

// Logical section of page
$pointmasterObj =  new  pointmaster();
/*$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['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 ";

    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(" pointmaster_id desc ");
    }
    //print_r($orderby);exit;
    $data    = array('where' => $where,'limit' => $limit, 'orderBy'=>$orderby);
    $pointmasterDetails = $pointmasterObj->getAllRecords($data);
    //print_r($adminDetails);exit;
    $recordsTotal   =   $pointmasterObj->getAllCount($data);

    if ($pointmasterDetails && count($pointmasterDetails)>0){
        foreach ($pointmasterDetails as $key => $pointdata){

            
            
            if($pointdata->pointmaster_status == '1')
            {
                $status = 'Active</span>';
            }else{
                $status = 'Inactive</span>';
            }

            if(!is_null($pointdata->pointmaster_created_at))
            {
                $create_at = $pointdata->pointmaster_created_at;
            }else{
                $create_at = '-';
            }

            if(!is_null($pointdata->pointmaster_inactive_at))
            {
                $inactive_at = $pointdata->pointmaster_inactive_at;
            }else{
                $inactive_at = '-';
            }

            $action = '<a href="pointadd.php?act=edit&point_id='.$pointdata->pointmaster_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="'.$pointdata->pointmaster_id.'" class="check_class" />',
                    $pointdata->pointmaster_title,
                    $pointdata->pointmaster_type,
                    $pointdata->pointmaster_code,
                    $pointdata->pointmaster_sender,
                    $pointdata->pointmaster_receiver,
                    "<div id='td_status_".$pointdata->admin_id."'>".$status."</div>",
                    $create_at,
                    $inactive_at,
                    //$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')
?>