<?php
// Include main file
include_once('../libs/main.php');
// Check admin Login
// include_once('checkLogin.php');
// Logical section of page
$practiceObj =  new  practice();
$reminderObj = new reminder();
if(isset($_REQUEST['office_id']) && $_REQUEST['office_id']!=""){
    $practiceObj = new practice();
    $where =array('practice_office_id='=>$_REQUEST['office_id']);
    $arg = array('where'=>$where);
    $practiceDetails = $practiceObj->FromFields($arg);
    if($practiceDetails){
            /*$reminderObj = new reminder();
            $where =array('reminder_practice_id='=>$practiceDetails->practice_office_id);
            $data =array('where'=>$where);
            $exist = $reminderObj->FromFields($data);
            if($exist){
               header("Location:reminderlist.php?failedMsg=Patients already fetched from PMS for this practice.");
               exit;
            }*/

        $reminderObj = new reminder();
        $arg =array(
          "request_key"=>$practiceDetails->practice_request_key,
          "office_id"=>$practiceDetails->practice_office_id,
          "secret_key"=>$practiceDetails->practice_secret_key,
          "clinic_id"=>$practiceDetails->practice_xcare_id       
        );
        $status  = $reminderObj->pullReminders($arg);            
        if($status){
            header("Location:reminderlist.php?successMsg=Successfully pulled all patients for ".$practiceDetails->practice_office_id);
            exit;
        }else{
             header("Location:reminderlist.php?successMsg=Successfully pulled all patients for ".$practiceDetails->practice_office_id);
            exit;
        }
    }else{
        header("Location:reminderlist.php?failedMsg=Error Occur.");
        exit;
    }
    exit;
}



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['clinic_id']) && $_REQUEST['clinic_id']!=""){
        $where .= " AND  clinic_id = '".$_REQUEST['clinic_id']."' ";
    }
   
    $orderFields = array('reminder_id','status','plan_sr_no');
    
    // 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(" reminder_id desc ");
    }

    $data    = array('where' => $where,'limit' => $limit, 'orderBy'=>$orderby);

    $reminderDetails = $reminderObj->getAllRecords($data);
    $recordsTotal   =   $reminderObj->getAllCount($data);

        
    if ($reminderDetails && count($reminderDetails)>0){
        foreach ($reminderDetails as $key => $reminder){
            $response[] = array(
                    "<b>Appointment Sr No: </b>".$reminder->appointment_sr_no."<br/><b>OfficeId : </b>".$reminder->office_id."<br/><b>ClinicId : </b>".$reminder->clinic_id."<br/><b>PatientId : </b>".$reminder->patient_id."<br/><b>GuarantorId : </b>".$reminder->guarantor_id."<br/><b>Xcare Patient Id : </b>".$reminder->dependant_id,
                   "<b>Set Date: </b>".$reminder->set_date."<br/><b>Due Date : </b>".$reminder->due_date."<br/><b>Prior : </b>".$reminder->prior_date."<br/><b>Provider : </b>".$reminder->provider_id."<br/><b>Time Unit : </b>".$reminder->time_unit."<br/><b>Status : </b>".$reminder->status."<br/><b>Description : </b>".$reminder->description, 
                   "<b>interval_type: </b>".$reminder->interval_type."<br/><b>interval_length : </b>".$reminder->interval_length."<br/><b>procedure_code : </b>".$reminder->procedure_code."<br/><b>last_exam_date : </b>".$reminder->last_exam_date."<br/><b>scaling_interval : </b>".$reminder->scaling_interval."<br/><b>loadeddate : </b>".$reminder->loadeddate."<br/><b>created_at : </b>".$reminder->created_at, 
                    
             );
        }
        $result=array('recordsFiltered'=>$recordsTotal,'recordsTotal'=>$recordsTotal,'data'=>$response);
    }else{
        $result=array('recordsFiltered'=>0,'recordsTotal'=>0,'data'=>array());
    }
    echo json_encode($result);
    exit;
}

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