<?php 
// Include main file
include_once('../libs/main.php');
// Check admin Login
include_once('checkLogin.php');
// Logical section of page
$agencyuserObj =  new  agencyuser();

if(isset($_REQUEST['act']) && $_REQUEST['act']=='delete' && isset($_REQUEST['agencyuser_id'])){
    $agencyuserObj->deleteFromId($_REQUEST['agencyuser_id']);
    $successMsg = base64_encode(RECORD_DELETE);
}
if(isset($_REQUEST['act']) && $_REQUEST['act']=='change_status' && isset($_REQUEST['agencyuser_id'])){
    if(isset($_REQUEST['status'])){
        $agencyuserObj->changeStatus($_REQUEST['agencyuser_id']);
    }
        return true;
}
if(isset($_REQUEST['action']) && $_REQUEST['action']!=''){
    if($_REQUEST['action']=="activate_all"){
        $data=array("user_status" => '1');
        $rs = $agencyuserObj->updateManyRecords($data, $_POST['check']);
        $successMsg = base64_encode(RECORD_ALL_ACTIVE);
    }else if($_REQUEST['action']=="inactivate_all"){
        $data=array("user_status" => '0');
        $rs = $agencyuserObj->updateManyRecords($data, $_POST['check']);
        $successMsg = base64_encode(RECORD_ALL_INACTIVE);
    }else if($_REQUEST['action']=="delete_all"){
        $rs = $agencyuserObj->deleteManyRecords($_POST['check']);
        $successMsg = base64_encode(RECORD_ALL_DELETE);
    }
}

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['agencyuser_fname']) && $_REQUEST['agencyuser_fname'] != ''){
        $where .= " AND agencyuser_fname = '".$_REQUEST['agencyuser_fname']."'";
    }
    if(isset($_REQUEST['agencyuser_lname']) && $_REQUEST['agencyuser_lname'] != ''){
        $where .= " AND agencyuser_lname = '".$_REQUEST['agencyuser_lname']."'";
    }
    if(isset($_REQUEST['agencyuser_agency_id']) && $_REQUEST['agencyuser_agency_id'] != ''){
        $where .= " AND agencyuser_agency_id = '".$_REQUEST['agencyuser_agency_id']."'";
    }
    $data    = array('where' => $where,'limit' => $limit);
    $agencyuserDetails = $agencyuserObj->getAllRecords($data);
    $recordsTotal   =   $agencyuserObj->getAllCount($data);

        
    if ($agencyuserDetails && count($agencyuserDetails)>0){
        foreach ($agencyuserDetails as $key => $agencyuser){
            $agencyObj = new agency();
            $agency =$agencyObj->fromId($agencyuser->agencyuser_agency_id);
            $adminObj = new admin();
            $admin = $adminObj->fromId($agencyuser->agencyuser_created_by);
            if($agencyuser->agencyuser_status == '1'){
                    
                  $status = '<a href="javascript:void(0)" onclick="change_status('.$agencyuser->agencyuser_id.',1)" title="Inactivate"><span class="badge badge-success">Active</span></a>';
                }else{

                  $status = '<a href="javascript:void(0)" onclick="change_status('.$agencyuser->agencyuser_id.',0)" title="Activate"><span class="badge badge-danger">Inactive</span></a>';
                }
            
            $action = '
                 <a href="agencyuseradd.php?act=edit&agencyuser_id='.$agencyuser->agencyuser_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="'.$agencyuser->agencyuser_id .'" class="check_class" />',
                    $agency->agency_name,
                    $agencyuser->agencyuser_fname." ".$agencyuser->agencyuser_lname."(".$agencyuser->agencyuser_role.")",
                    $agencyuser->agencyuser_email."<br/>".$agencyuser->agencyuser_phone,
                    $admin->admin_fname." ".$admin->admin_lname,
                    $agencyuser->agencyuser_created_at,
                    $status,
                    $action,
             );
        }
        $result=array('recordsFiltered'=>$recordsTotal,'recordsTotal'=>$recordsTotal,'data'=>$response);
    }else{
        $result=array('recordsFiltered'=>0,'recordsTotal'=>0,'data'=>array());
    }
    echo json_encode($result);
    exit;
}
$agencyObj= new agency();
$allagency =$agencyObj->getAllRecords(array());
// Include Template 
include_once('../libs/routs.php');
?>