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

/*if (isset($_REQUEST['act']) && $_REQUEST['act']=='load1' && isset($_REQUEST['practice_xcare_id']) && $_REQUEST['practice_xcare_id']!='' && isset($_REQUEST['patient_id']) && $_REQUEST['patient_id']!='' && isset($_REQUEST['search_api']) && $_REQUEST['search_api']!=''){
        

        $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);


        
        $fields = array('p.*');
        $where = array("practice_xcare_id="=>$_REQUEST['practice_xcare_id']);
        $pracricArg    = array('where' => $where);
        $practiceDetails = $practiceObj->fromFields($pracricArg);

        
            $pmsAppointmentObj = new pmsAppointment();
            $arg =array(
              "request_key"=>$practiceDetails->practice_request_key,
              "patient_id"=>$_REQUEST['patient_id'],
              "search_api" => $_REQUEST['search_api'],
              "office_id"=>$practiceDetails->practice_office_id,
              "secret_key"=>$practiceDetails->practice_secret_key,

            );
            if($_REQUEST['search_api'] == '2')
            {
                $arg['loaded_startdate'] = $_REQUEST['start_date'];
                $arg['loaded_enddate'] = $_REQUEST['end_date'];
            }
            
            $status  = $pmsAppointmentObj->pullSearchDataAppointment($arg);
           

        if(!empty($status))
        {
            if(isset($status['error_code']) && ( $status['error_code']=='API1025' || $status['error_code']=='API1004' ) )
            {
                $parameter = array("office_id"=>$practiceDetails->practice_office_id,"secret_key"=>$practiceDetails->practice_secret_key);
                $results = sikka::requestKeyByPractice($parameter);
                if(!empty($results)){
                    $results = $results[0];
                    if(isset($results['request_key']) && $results['request_key']!=""){
                        //$practiceObj = new practice();
                        $practiceData = array('practice_request_key'=>$results['request_key']);
                        $where =array('practice_office_id='=>$practiceDetails->practice_office_id);
                        $practiceObj->updateFromFields($practiceData,$where);
                        $data['request_key'] = $results['request_key'];
                        $data['offset'] = sikka::OFFSET;
                        $data['patient_id'] = $_REQUEST['patient_id'];
                        $data["search_api"] = $_REQUEST['search_api'];
                        if($_REQUEST['search_api'] == '2')
                        {
                            $data['loaded_startdate'] = $_REQUEST['start_date'];
                            $data["loaded_enddate"] = $_REQUEST['end_date'];
                        }
                        
                        
                        if(!empty($practiceObj)){
                            $status = $pmsAppointmentObj->pullSearchDataAppointment($data);
                        }
                    }
                }
            }

            $pData = $status[0];
            
            if(!empty($pData))
            {
                $recordsTotal   =   count($pData['items']);
            }
            $appointments=$pData['items'];
           
            if ($appointments && count($appointments)>0)
            {
                foreach ($appointments as $key => $appointment)
                {

                    $i++;
                    $response[] = array(
                        $i,
                        //json_encode($appointment),
                        print_r($appointment),
                    );
                }
                $result=array('recordsFiltered'=>$recordsTotal,'recordsTotal'=>$recordsTotal,'data'=>$response);
            }else
            {
                $result=array('recordsFiltered'=>0,'recordsTotal'=>0,'data'=>array());
            }
        }else
        {
            $result=array('recordsFiltered'=>0,'recordsTotal'=>0,'data'=>array());
        }   
        
        echo json_encode($result);
        exit;

    
}*/
//&& isset($_REQUEST['patient_id']) && $_REQUEST['patient_id']!=''
if (isset($_REQUEST['act']) && $_REQUEST['act']=='load' && isset($_REQUEST['practice_xcare_id']) && $_REQUEST['practice_xcare_id']!='' && isset($_REQUEST['search_api']) && $_REQUEST['search_api']!=''){
        //$t = array();
        $fields = array('p.*');
        $where = array("practice_xcare_id="=>$_REQUEST['practice_xcare_id']);
        $pracricArg    = array('where' => $where);
        $practiceDetails = $practiceObj->fromFields($pracricArg);
            
            $search_event = $_REQUEST['search_event'];

            $pmsAppointmentObj = new pmsAppointment();
            $arg =array(
              "request_key"=>$practiceDetails->practice_request_key,
              "patient_id"=>$_REQUEST['patient_id'],
              "provider_id"=>$_REQUEST['provider_id'],
              "serial_no"=>$_REQUEST['serial_no'],
              "search_event"=>$search_event,
              "search_api" => $_REQUEST['search_api'],
              "interval"=> $_REQUEST['interval_search'],
              "office_id"=>$practiceDetails->practice_office_id,
              "secret_key"=>$practiceDetails->practice_secret_key,
              "endpoint"=>$practiceDetails->practice_endpoint
            );
            if($_REQUEST['search_api'] == '2' || $_REQUEST['search_api'] == '1')
            {
                $arg['loaded_startdate'] = $_REQUEST['start_date'];
                $arg['loaded_enddate'] = $_REQUEST['end_date'];
            } 

            if($_REQUEST['search_api'] == '4' || $_REQUEST['search_api'] == '5')
            {
                $arg['firstname'] = $_REQUEST['firstname_search'];
                $arg['lastname'] = $_REQUEST['lastname_search'];
                $arg['email'] = $_REQUEST['email_search'];
                $arg['cell'] = $_REQUEST['cell_search'];
                if($_REQUEST['search_api'] == '5')
                {
                    $arg['guarantor_id'] = $_REQUEST['guarantor_id'];
                }
            }

            
            $status  = $pmsAppointmentObj->pullSearchDataAppointment($arg);

            if(!empty($status))
            {
                if(isset($status['error_code']) && ( $status['error_code']=='API1025' || $status['error_code']=='API1004' ) )
                {
                    $parameter = array("office_id"=>$practiceDetails->practice_office_id,"secret_key"=>$practiceDetails->practice_secret_key);
                    $results = sikka::requestKeyByPractice($parameter,$practiceDetails->practice_endpoint);
                    
                    if(!empty($results)){
                        // $results = $results[0];
                        if(isset($results['request_key']) && $results['request_key']!=""){
                            //$practiceObj = new practice();
                            $practiceData = array('practice_request_key'=>$results['request_key']);
                            $where =array('practice_office_id='=>$practiceDetails->practice_office_id);
                            $practiceObj->updateFromFields($practiceData,$where);
                            $data['request_key'] = $results['request_key'];
                            $data['offset'] = sikka::OFFSET;
                            $data['patient_id'] = $_REQUEST['patient_id'];
                            $data['provider_id'] = $_REQUEST['provider_id'];
                            $data['serial_no'] = $_REQUEST['serial_no'];
                            $data['search_event'] = $search_event;
                            $data['interval'] = $_REQUEST['interval_search'];
                            $data["search_api"] = $_REQUEST['search_api'];
                            $data['endpoint'] = $practiceDetails->practice_endpoint;
                            if($_REQUEST['search_api'] == '2' || $_REQUEST['search_api'] == '1')
                            {
                                $data['loaded_startdate'] = $_REQUEST['start_date'];
                                $data["loaded_enddate"] = $_REQUEST['end_date'];
                            }
                             if($_REQUEST['search_api'] == '4' || $_REQUEST['search_api'] == '5')
                            {
                                $data['firstname'] = $_REQUEST['firstname_search'];
                                $data['lastname'] = $_REQUEST['lastname_search'];
                                $data['email'] = $_REQUEST['email_search'];
                                $data['cell_search'] = $_REQUEST['cell_search'];
                                if($_REQUEST['search_api'] == '5')
                                {
                                    $data['guarantor_id'] = $_REQUEST['guarantor_id'];
                                }
                            }   
                            
                            //print_r($data);exit;
                            if(!empty($practiceObj)){
                                $status = $pmsAppointmentObj->pullSearchDataAppointment($data);
                            }
                        }
                    }
                }
            }
            /*echo "<pre>";            
                print_r($status);
            echo "</pre>";
            exit;*/
           if(!empty($status))
           {
                 //echo $_REQUEST['search_api'];
                
                if($_REQUEST['search_api'] == '1')
                {
                    if($_REQUEST['serial_no'] != '')
                    {
                        $status_data = json_decode(json_encode($status),true); 
                    }else
                    {
                        $status_data = json_decode(json_encode($status),true); 
                    }
                       
                }else if($_REQUEST['search_api'] == '2')
                {
                    $status_data = json_decode(json_encode($status),true); 
                    // if($search_event == 'appointments')
                    // {
                    //     $status_data = json_decode(json_encode($status[0]['appointments'][0]),true);    
                    // }else if($search_event == 'patients')
                    // {
                    //     $status_data = json_decode(json_encode($status[0]['patients'][0]),true);
                    // }
                    // else if($search_event == 'transactions')
                    // {
                    //     $status_data = json_decode(json_encode($status[0]['transactions'][0]),true);
                    // }
                    // else if($search_event == 'guarantors')
                    // {
                    //     $status_data = json_decode(json_encode($status[0]['guarantors'][0]),true);
                    // }    
                }else
                {
                    $status_data = json_decode(json_encode($status),true); 
                }
                
                //$total_count = json_encode($status[0]['total_count']);
                //echo "Total Count : ".$status_data['total_count'];
                /*if($_REQUEST['serial_no'] == '')
                {
                    echo '<div style="float:right;"><b>Total Count</b> :'.$status_data['total_count'].'</div>';
                }
                if($_REQUEST['serial_no'] != '')
                {
                    echo "<pre>";
                    print_r($status_data);
                    echo "</pre>";
                    exit; 
                }else{
                    echo "<pre>";
                    print_r($status_data['items']);
                    echo "</pre>";
                    exit; 
                }*/

                
                if($_REQUEST['search_api'] == '2')
                {
                    echo '<div style="float:right;"><b>Total Count</b> :'.$status_data['total_count'].'</div>';
                    echo "<pre>";
                    print_r($status_data);
                    echo "</pre>";
                    exit;     
                }else{
                    echo '<div style="float:right;"><b>Total Count</b> :'.$status_data[0]['total_count'].'</div>';

                    echo "<pre>";
                    print_r($status_data);
                    echo "</pre>";
                    exit;     
                }
                

                
                
           }else{
                echo "No records found";
                exit;
           }
           
    }

    
$fields = array('p.*','c.clinic_name','c.clinic_id');
$tables = array('tbl_clinic c');
$jfields = array('c.clinic_id = p.practice_xcare_id');
$jtype   = array("INNER"); 
$where = array();
$practicArg    = array('fields'=>$fields,'where' => $where,  'joinTables' =>$tables, 'joinFields' =>$jfields, 'joinTypes' => $jtype);
$allClinic = $practiceObj->getAllRecords($practicArg);


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