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

if(isset($_REQUEST['act']) && $_REQUEST['act']=='delete' && isset($_REQUEST['agency_id'])){
    $agencyObj->deleteFromId($_REQUEST['agency_id']);
    $successMsg = base64_encode(RECORD_DELETE);
}
if(isset($_REQUEST['act']) && $_REQUEST['act']=='change_status' && isset($_REQUEST['agnecy_id'])){
    if(isset($_REQUEST['status'])){
        $agencyObj->changeStatus($_REQUEST['agency_id']);
    }
        return true;
}
if(isset($_REQUEST['action']) && $_REQUEST['action']!=''){
    if($_REQUEST['action']=="activate_all"){
        $data=array("user_status" => '1');
        $rs = $agencyObj->updateManyRecords($data, $_POST['check']);
        $successMsg = base64_encode(RECORD_ALL_ACTIVE);
    }else if($_REQUEST['action']=="inactivate_all"){
        $data=array("user_status" => '0');
        $rs = $agencyObj->updateManyRecords($data, $_POST['check']);
        $successMsg = base64_encode(RECORD_ALL_INACTIVE);
    }else if($_REQUEST['action']=="delete_all"){
        $rs = $agencyObj->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['agency_name']) && $_REQUEST['agency_name'] != ''){
        $where .= " AND agency_name = '".$_REQUEST['agency_name']."'";
    }
    $data    = array('where' => $where,'limit' => $limit);
    $agencyDetails = $agencyObj->getAllRecords($data);
    $recordsTotal   =   $agencyObj->getAllCount($data);

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

                  $status = '<a href="javascript:void(0)" onclick="change_status('.$agency->agency_id.',0)" title="Activate"><span class="badge badge-danger">Inactive</span></a>';
                }
            
            $action = '
                 <a href="agencyadd.php?act=edit&agency_id='.$agency->agency_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="'.$agency->agency_id .'" class="check_class" />',
                    $agency->agency_name,
                    $admin->admin_fname." ".$admin->admin_lname,
                    $agency->agency_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;
}

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