<?php 
// Include main file
include_once('../libs/main.php');
// Check admin Login
include_once('checkLogin.php');
// Logical section of page
$claimObj =  new  claim();
if(isset($_REQUEST['act']) && $_REQUEST['act']=='change_status' && isset($_REQUEST['claim_id'])){
    if(isset($_REQUEST['status'])){
            $claimObj->changeStatus($_REQUEST['claim_id']);
            echo RECORD_STATUS;
    }
        return true;
}

if(isset($_REQUEST['act']) && $_REQUEST['act']=='delete' && isset($_REQUEST['claim_id'])){
    $claimObj->deleteFromId($_REQUEST['claim_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['search']['value']) && $_REQUEST['search']['value']!=""){
        $where = " patient_fname LIKE '%".$_REQUEST['search']['value']."%' OR patient_lname LIKE '%".$_REQUEST['search']['value']."%' OR patient_phone LIKE '%".$_REQUEST['search']['value']."%' OR patient_email_address LIKE '%".$_REQUEST['search']['value']."%' ";
    }   
    $orderFields = array('claim_id','claim_appointment_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(" claim_id desc ");
    }
    $tables = array('tbl_patient p','tbl_appointment p','tbl_clinic cl','tbl_doctor d');
    $jfields = array('c.claim_patient_id = p.patient_id','c.claim_appointment_id = a.appointment_id','a.appointment_clinic_id=cl.clinic_id','a.appointment_doctor_id = d.doctor_id');
    $jtype   = array("INNER","INNER","INNER","INNER"); 

    $data    = array('where' => $where,  'joinTables' =>$tables, 'joinFields' =>$jfields, 'joinTypes' => $jtype, 'limit' => $limit,'orderBy'=>$orderby);

    $claimDetails = $claimObj->getAllRecords($data);
    $recordsTotal   =   $claimObj->getAllCount($data);

        
    if ($claimDetails && count($claimDetails)>0){
        foreach ($claimDetails as $key => $claim){

            $action = '<a href="claimadd.php?act=edit&claim_id='.$claim->claim_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="'.$claim->claim_id.'" class="check_class" />', 
                    $claim->patient_fname." ".$claim->patient_lname ,
                 $claim->claim_appointment_date,
                 $claim->claim_desc,
                 "<div id='td_status_".$claim->claim_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;
}
$specialityObj = new speciality();
$allspecialities = $specialityObj->getAllRecords();
// Include Template 
include_once('../libs/routs.php')
?>