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

// Logical section of page
$overdueObj =  new  overdue();


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_clinic_id='".$_REQUEST['clinic_id']."'";
    
    if(isset($_REQUEST['firstname']) && $_REQUEST['firstname'] != '' && $_REQUEST['lastname'] != '')
    {
        $firstname = trim($_REQUEST['firstname']);
        $lastname = trim($_REQUEST['lastname']);

        $where .= " AND ((d.dependant_fname='".w::e($firstname)."' and   d.dependant_lname='".w::e($lastname)."') OR (d.dependant_fname='".w::e(ucfirst($firstname))."' and   d.dependant_lname='".w::e(ucfirst($lastname))."'))";
    }else if(isset($_REQUEST['firstname']) && $_REQUEST['firstname'] != '' && $_REQUEST['lastname'] == ''){

        $firstname = trim($_REQUEST['firstname']);

        $where .= " AND ((d.dependant_fname LIKE '%".w::e($firstname)."%') OR (d.dependant_fname LIKE '%".w::e(ucfirst($firstname))."%' ))";
    }else if(isset($_REQUEST['lastname']) && $_REQUEST['lastname'] != '' && $_REQUEST['firstname'] == ''){

        $lastname = trim($_REQUEST['lastname']);

        $where .= " AND ((d.dependant_lname LIKE '%".w::e($lastname)."%') OR ( d.dependant_lname LIKE '%".w::e(ucfirst($lastname))."%'))";
    }
    
    /*if(isset($_REQUEST['patient_fullname']) && $_REQUEST['patient_fullname'] != '')
    {
        $patientname = preg_replace('/\s\s+/', ' ', $_REQUEST['patient_fullname']);
        $fullname = explode(" ", $patientname);

        if(count($fullname)==2){
            $where .=" AND ((d.dependant_fname='".w::e($fullname['0'])."' AND  d.dependant_lname='".w::e($fullname['1'])."' ) OR (d.dependant_lname='".w::e($fullname['0'])."' AND  d.dependant_fname='".w::e($fullname['1'])."' ) OR (d.dependant_fname='".w::e(ucfirst($fullname['0']))."' AND  d.dependant_lname='".w::e(ucfirst($fullname['1']))."' ) OR (d.dependant_lname='".w::e(ucfirst($fullname['0']))."' AND  d.dependant_fname='".w::e(ucfirst($fullname['1']))."' ) )";
        }else{
            foreach ($fullname as $key => $f) {
                if($key==0){
                    $where .=" AND ( ";
                }
                $where .=" d.dependant_fname LIKE '%".w::e($f)."%' OR d.dependant_fname LIKE '%".w::e(ucfirst($f))."%'  OR d.dependant_lname LIKE '%".w::e($f)."%' OR dependant_lname LIKE '%".w::e(ucfirst($f))."%' OR d.dependant_email_address ='".w::e($f)."' OR d.dependant_phone = '".w::e($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_appointment_id = '".$_REQUEST['overdue_appointment_id']."'";   
    }

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

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

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

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

    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_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,  'joinTables' =>$tables, 'joinFields' =>$jfields, 'joinTypes' => $jtype,'orderBy'=>$orderby,'limit' => $limit);

    $overdueDetails = $overdueObj->getAllRecords($data);
    //print_r($overdueDetails);exit;
    $recordsTotal   =   $overdueObj->getAllCount($data);
     
    if ($overdueDetails && count($overdueDetails)>0){
        foreach ($overdueDetails as $key => $overduedata)
        {
            $metadata =json_decode($overduedata->appointment_metadata,1);
            $srNo =$metadata['pms']['srNo'];
            $patientObj = new patient();
            $signInEmail ="";
            $signInPhone ="";
            if(!is_null($overduedata->dependant_overduein_id)){
                $patient=$patientObj->fromId($overduedata->dependant_overduein_id);
                $signInEmail=w::d($patient->patient_email_address);
                $signInPhone =w::d($patient->patient_phone);
            }

            if(is_null($overduedata->overdue_appointment_date) && is_null($overduedata->overdue_appointment_time)){
                
                $appointment_date = $overduedata->appointment_date." ".$overduedata->appointment_time;
            }
            else{
                $appointment_date = $overduedata->overdue_appointment_date." ".$overduedata->overdue_appointment_time;
            }
            $guarantor = $patientObj->fromId($overduedata->dependant_guarantor_id);
            $ginfo ="<b>Xcare ID: </b>".$guarantor->patient_id."<br/><b>External Id: </b>".$guarantor->patient_unique_id."<br/><b>First Name: </b>".w::d($guarantor->patient_fname)."<br/><b>Last Name: </b>".w::d($guarantor->patient_lname)."<br/><b>Email: </b>".w::d($guarantor->patient_email_address)."<br/><b>Phone: </b>".w::d($guarantor->patient_phone);

            $response[] = array( 
                    "<b>Origin</b>: ".$overduedata->overdue_id."<br/><b>Overdue Appoinmnet Id: </b>".$overduedata->appointment_id,
                    "<b>Type: </b>".$overduedata->overdue_type."<br/><b>Overdue Date: </b>".$overduedata->overdue_date."<br/><b>Appointment Date: </b>".$overduedata->overdue_appointment_date."<br/><b>Procedure Code: </b>".$overduedata->overdue_procedure_code."<br><b>Procedure Category: </b>".$overduedata->overdue_procedure_category,
                    "<b>Patient Id</b>: ".$overduedata->dependant_id."<br/><b>External Id: </b>".$overduedata->dependant_unique_id."<br/><b>First Name</b>: ".w::d($overduedata->dependant_fname)."<br/><b>Last Name</b>: ".w::d($overduedata->dependant_lname)."<br/><b>Email Id</b>: ".w::d($overduedata->dependant_email_address)."<br/><b>Phone</b>: ".w::d($overduedata->dependant_phone)."<br/><b>Preference</b>: ".$overduedata->dependant_contact_method,
                    $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');
?>