<?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']=='checkdata')
{
    $clinicId = $_REQUEST['clinic_id'];
    $serial_no = $_REQUEST['serial_no'];

    $pmsAppointmentObj = new pmsAppointment();
    $fields = array('pmsAppointment_id');
    $where = array('pmsAppointment_clinic_id='=>$clinicId,'pmsAppointment_sr_no='=>$serial_no);
    $data = array('fields'=>$fields,'where'=>$where);
    $existpmsAppointment = $pmsAppointmentObj->fromFields($data);
    if($existpmsAppointment)
    {
        echo 'record exist';
        exit;
    }else{
        echo 'not record exist';
        exit;
    }
}
 
if(isset($_REQUEST['act']) && $_REQUEST['act'] == 'saveappointment')
{
    $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);
                            }
                        }
                    }
                }
            }
            //print_r($status);exit;
            if(!empty($status))
            {
                $pmsappointmentObj = new pmsappointment();
                foreach($status as $ekey => $sdvalue)
                { 
                    
                    if($ekey == 'appointment_sr_no')
                    {
                        $appoimnet_srno = $sdvalue;    
                    }
                    if($ekey == 'patient_id')
                    {
                        $patientId = $sdvalue;
                    }
                    if($ekey == 'operatory')
                    {
                        $app_operatory = $sdvalue;
                    }
                    if($ekey == 'date')
                    {
                        $app_date = $sdvalue;
                    }
                    if($ekey == 'time')
                    {
                        $app_time = $sdvalue;
                    }
                    if($ekey == 'length')
                    {
                        $app_length = $sdvalue;
                    }
                    if($ekey == 'amount')
                    {
                        $app_amount = $sdvalue;
                    }
                    if($ekey == 'status')
                    {
                        $app_status = $sdvalue;
                    }
                    if($ekey == 'last_changed_date')
                    {
                        $app_lastChanged = $sdvalue;
                    }
                    if($ekey == 'provider_id')
                    {
                        $app_provider_id = $sdvalue;
                    }
                    if($ekey == 'schedule')
                    {
                        $app_schedule = $sdvalue;
                    }
                    if($ekey == 'loadeddate')
                    {
                        $app_loadeddate = $sdvalue;   
                    }
                    if($ekey == 'cancelled')
                    {
                        $app_cancelled = $sdvalue;   
                    }
                    if($ekey == 'procedure_code1')
                    {
                        $app_procedure_code1 = $sdvalue;
                    }
                    if($ekey == 'procedure_code1_amount')
                    {
                        $app_procedure_amount1 = $sdvalue;
                    }
                    if($ekey == 'procedure_code2')
                    {
                        $app_procedure_code2 = $sdvalue;
                    }
                    if($ekey == 'procedure_code2_amount')
                    {
                        $app_procedure_amount2 = $sdvalue;
                    }
                    if($ekey == 'procedure_code3')
                    {
                        $app_procedure_code3 = $sdvalue;
                    }
                    if($ekey == 'procedure_code3_amount')
                    {
                        $app_procedure_amount3 = $sdvalue;
                    }

                    if($ekey == 'procedure_code4')
                    {
                        $app_procedure_code4 = $sdvalue;
                    }
                    if($ekey == 'procedure_code4_amount')
                    {
                        $app_procedure_amount4 = $sdvalue;
                    }

                    if($ekey == 'procedure_code5')
                    {
                        $app_procedure_code5 = $sdvalue;
                    }
                    if($ekey == 'procedure_code5_amount')
                    {
                        $app_procedure_amount5 = $sdvalue;
                    }

                    if($ekey == 'procedure_code6')
                    {
                        $app_procedure_code6 = $sdvalue;
                    }
                    if($ekey == 'procedure_code6_amount')
                    {
                        $app_procedure_amount6 = $sdvalue;
                    }

                    if($ekey == 'procedure_code7')
                    {
                        $app_procedure_code7 = $sdvalue;
                    }
                    if($ekey == 'procedure_code7_amount')
                    {
                        $app_procedure_amount7 = $sdvalue;
                    }
                    
                    
                    $pmsAppData =array(
                        "pmsAppointment_sr_no" =>trim($appoimnet_srno),
                        "pmsAppointment_clinic_id" =>$_REQUEST['practice_xcare_id'],
                        "pmsAppointment_patient_id" =>trim($patientId),
                        "pmsAppointment_provider_id" =>trim($app_provider_id),
                        "pmsAppointment_date" =>date("Y-m-d",strtotime(trim($app_date))),
                        "pmsAppointment_time" =>trim($app_time),
                        "pmsAppointment_operatory" =>trim($app_operatory),
                        "pmsAppointment_length"=>$app_length,
                        "pmsAppointment_amount" =>trim($app_amount),
                        "pmsAppointment_status" =>trim($app_status),
                        "pmsAppointment_procedure_code1" =>trim($app_procedure_code1),
                        "pmsAppointment_procedure_code2" =>trim($app_procedure_code2),
                        "pmsAppointment_procedure_code3" =>trim($app_procedure_code3),
                        "pmsAppointment_procedure_code4" =>trim($app_procedure_code4),
                        "pmsAppointment_procedure_code5" =>trim($app_procedure_code5),
                        "pmsAppointment_procedure_code6" =>trim($app_procedure_code6),
                        "pmsAppointment_procedure_code7" =>trim($app_procedure_code7),
                        "pmsAppointment_procedure_amount1" =>trim($app_procedure_amount1),
                        "pmsAppointment_procedure_amount2" =>trim($app_procedure_amount2),
                        "pmsAppointment_procedure_amount3" =>trim($app_procedure_amount3),
                        "pmsAppointment_procedure_amount4" =>trim($app_procedure_amount4),
                        "pmsAppointment_procedure_amount5" =>trim($app_procedure_amount5),
                        "pmsAppointment_procedure_amount6" =>trim($app_procedure_amount6),
                        "pmsAppointment_procedure_amount7" =>trim($app_procedure_amount7),
                        "pmsAppointment_loadeddate" =>$app_loadeddate,
                        "pmsAppointment_last_changed_date" =>$app_lastChanged,
                        "pmsAppointment_practice_id" =>trim($practiceDetails->practice_office_id),
                        "pmsAppointment_schedule" =>trim($app_schedule),
                        "pmsAppointment_cancelled" =>trim($app_cancelled)
                    );    
                }
                $allpmsappointment = $pmsappointmentObj->addRecords($pmsAppData);
                if($allpmsappointment)
                {
                    echo 'success'; 
                }
            }
            exit;
}

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))
           {
                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
                {
                    $status_data = json_decode(json_encode($status),true);
                }
                

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

                //echo  '<script> $("#list_data_chk").html("'.$status_data.'") </script>';

                //echo '<script>console.log("'.$status_data.'"); </script>';
                /*$phone = $status_data;
                $script = 'alert("' . json_encode($status_data) .'")';
                $script .= '</script>';
                echo $script;*/

                /*$script =  "<script>";
                $script .= "var jsArray = [];";
                $script .= 'var jsArray = ' .json_encode($status_data).';';
                $script .= "console.log(jsArray);";
                //$script .= "console.log($('#list_data_chk').html(jsArray));";
                $script .= "$('#list_data_chk').html(jsArray)";
                $script .= "</script>";
                echo $script;*/
                /*$response = array(
                    'data'=>$status_data
                );
                echo json_encode($response);*/
                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');
?>