<?php 

// Include main file
include_once('../libs/main.php');
// Check Login
include_once('checkLogin.php');
// Logical section of page
$coverageInsuranceCarrierObj =  new  coverageInsuranceCarrier();

if(isset($_REQUEST['act']) && $_REQUEST['act']=='change_status' && isset($_REQUEST['id'])){
    if(isset($_REQUEST['status'])){
        $coverageInsuranceCarrierObj->changeStatus($_REQUEST['id']);
        echo RECORD_STATUS;
    }
    return true;
}

if(isset($_REQUEST['act']) && $_REQUEST['act']=='delete' && isset($_REQUEST['id'])){
    $coverageInsuranceCarrierObj->deleteFromId($_REQUEST['id']);
    $successMsg = base64_encode(RECORD_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['title']) && $_REQUEST['title'] != '')
    {
        $where .= " AND title LIKE '%".$_REQUEST['title']."%'";
    }

    if(isset($_REQUEST['type']) && $_REQUEST['type'] != '')
    {
        $where .= " AND type = '".$_REQUEST['type']."'";
    }

    if (isset($_REQUEST['search']['value']) && $_REQUEST['search']['value']!=""){
        $where = " title LIKE '%".$_REQUEST['search']['value']."%' OR type LIKE '%".$_REQUEST['search']['value']."%'";
    }    
    $orderFields = array('','title','type','description','');
    // 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(" id desc ");
    }
    $data    = array('where' => $where,'limit' => $limit,'orderBy' => $orderby);
    $insuranceCarrierDetails = $coverageInsuranceCarrierObj->getAllRecords($data);
    $recordsTotal   =   $coverageInsuranceCarrierObj->getAllCount($data);

    if ($insuranceCarrierDetails && count($insuranceCarrierDetails)>0){
        foreach ($insuranceCarrierDetails as $key => $carrier){
            $typeLabel = ucfirst($carrier->type);
            $description = substr($carrier->description, 0, 100);
            if(strlen($carrier->description) > 100) {
                $description .= '...';
            }
            $action = '<a href="insuranceCarrieradd.php?act=edit&id='.$carrier->id.'" class="btn btn-success white-btn"><i class="nav-icon i-Pen-2 "></i></a>';
            $action .= ' <a href="javascript:void(0);" onclick="delete_entry('.$carrier->id.');" class="btn btn-danger white-btn"><i class="nav-icon i-Close-Window"></i></a>';
            $response[] = array('<input type="checkbox" name="check[]" id="check[]" value="'.$carrier->id.'" class="check_class" />', 
                    $carrier->title,
                    $typeLabel,
                    $description,
                    $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');
?>

