<?php  
// Include main file
include_once('../libs/main.php');
// Check dependant Login
include_once('checkLogin.php');

// Logical section of page
$overdue_detailObj =  new  overdue_detail();


if (isset($_REQUEST['act']) && $_REQUEST['act']=='load' && $_REQUEST['clinic_id'] != ''){
    
    $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 AND overdue_detail_clinic_id='".$_REQUEST['clinic_id']."'";
    
    
    
    if(isset($_REQUEST['patient_fullname']) && $_REQUEST['patient_fullname'] != '')
    {
        $patientname = preg_replace('/\s\s+/', ' ', $_REQUEST['patient_fullname']);
        $fullname = explode(" ", $patientname);


        $where .=" AND (overdue_detail_pfullname LIKE '%".$patientname."%' OR overdue_detail_pfullname LIKE  '%".ucfirst($patientname)."%'  OR overdue_detail_pfullname LIKE  '%".$patientname."%' OR overdue_detail_pfullname LIKE  '%".ucfirst($patientname)."%' )";

        /*if($fullname && count($fullname)>0)
        {
            $where ="";
            foreach ($fullname as $key => $f) 
            {
                $where .=" (d.dependant_fname LIKE '%".w::e($f)."%' OR d.dependant_fname LIKE '%".w::e(ucfirst($f))."%'  OR overdue_detail_pfullname LIKE '%".w::e($f)."%' OR d.dependant_lname LIKE '%".w::e(ucfirst($f))."%')";
                /*$where .=" (d.dependant_fname LIKE '%".($f)."%' OR d.dependant_fname LIKE '%".(ucfirst($f))."%'  OR d.dependant_lname LIKE '%".($f)."%' OR d.dependant_lname LIKE '%".(ucfirst($f))."%')";*/
                /*if( ($key+1) != count($fullname) )
                {
                        $where .=" OR ";
                }
            }
        }*/

        /*if(count($fullname)==2){
            $where .=" AND ((overdue_detail_pfullname='".$fullname['0']."' AND  overdue_detail_pfullname='".$fullname['1']."' ) OR (overdue_detail_pfullname='".$fullname['0']."' AND  overdue_detail_pfullname='".$fullname['1']."' ) OR (overdue_detail_pfullname='".ucfirst($fullname['0'])."' AND  overdue_detail_pfullname='".ucfirst($fullname['1'])."' ) OR (overdue_detail_pfullname='".ucfirst($fullname['0'])."' AND  overdue_detail_pfullname='".ucfirst($fullname['1'])."' ) )";
        }else{*/
            /*foreach ($fullname as $key => $f) {
                if($key==0){
                    $where .=" AND ( ";
                }
                $where .=" overdue_detail_pfullname LIKE '%".$f."%' OR overdue_detail_pfullname LIKE '%".ucfirst($f)."%'  OR overdue_detail_pfullname LIKE '%".$f."%' OR overdue_detail_pfullname LIKE '%".ucfirst($f)."%'";
                if( ($key+1) != count($fullname) ){
                        $where .=" OR ";
                }
                if( ($key+1) == count($fullname) ){
                    $where .=" )";
                }
            }*/
        //}
    }
    
    if(isset($_REQUEST['overdue_appointment_id']) && $_REQUEST['overdue_appointment_id'] != '')
    {
        $where .= " AND overdue_detail_appointment_id = '".$_REQUEST['overdue_appointment_id']."'";   
    }

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

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

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

    if (isset($_REQUEST['search']['value']) && $_REQUEST['search']['value']!=""){
        $where = " overdue_id LIKE '%".$_REQUEST['search']['value']."%' OR  overdue_appointment_id LIKE '%".$_REQUEST['search']['value']."%' ";
    }

    $orderFields = array('overdue_id');
    
    // 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(" overdue_detail_id desc ");
    }

    /*$tables = array('tbl_appointment ap','tbl_dependant d','tbl_clinic c');
    $jfields = array('ap.appointment_id = o.overdue_appointment_id','o.overdue_patient_id = d.dependant_id','c.clinic_id=o.overdue_clinic_id');
    $jtype   = array("INNER","INNER","INNER");*/
    $data   = array('where' => $where,  'orderBy'=>$orderby,'limit' => $limit);

    $overdueDetails = $overdue_detailObj->getAllRecords($data);
    //print_r($overdueDetails);exit;
    $recordsTotal   =   $overdue_detailObj->getAllCount($data);
     
    if ($overdueDetails && count($overdueDetails)>0){
        foreach ($overdueDetails as $key => $overduedata)
        {
            
            
            $ginfo ="<b>Full Name: </b>".$overduedata->overdue_detail_gfullname."<br/><b>Communication Email: </b>".$overduedata->overdue_detail_gemail."<br/><b>Communication Phone: </b>".$overduedata->overdue_detail_gphone;

            $response[] = array( 
                    "<b>Origin</b>: ".$overduedata->overdue_detail_id."<br/><b>Overdue Appoinmnet Id: </b>".$overduedata->overdue_detail_appointment_id,
                    "<b>Type: </b>".$overduedata->overdue_detail_type."<br/><b>Overdue Report Date: </b>".$overduedata->overdue_detail_date."<br/><b>Last Appointment Date: </b>".$overduedata->overdue_detail_appointment_date."<br><b>Procedure Category: </b>".$overduedata->overdue_detail_procedure_category."<br/><b>Procedure Code: </b>".$overduedata->overdue_detail_procedure_code,
                    "<b>Full Name</b>: ".$overduedata->overdue_detail_pfullname."<br/><b>Communication Email </b>: ".$overduedata->overdue_detail_pemail."<br/><b>Communication Phone</b>: ".$overduedata->overdue_detail_pphone."<br/><b>Preference</b>: ".$overduedata->overdue_detail_pcontactmethod,
                    $ginfo,
                    /*"Sign In Email: ".$signInEmail."<br/>Sign In Mobile: ".$signInPhone."<br/>Guarantor Info: ".$ginfo,*/
                    
                    
            );
        }
        $result=array('recordsFiltered'=>$recordsTotal,'recordsTotal'=>$recordsTotal,'data'=>$response);
    }else{
        $result=array('recordsFiltered'=>0,'recordsTotal'=>0,'data'=>array());
    }
    echo json_encode($result);
    exit;
}

/*$clinicObj = new clinic();
$where = "clinic_status = '1'";
$data = array('where'=>$where);
$allclinic = $clinicObj->getAllRecords($data);*/

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