<?php
// Include main file
include_once('../libs/main.php');
// Check admin Login
// include_once('checkLogin.php');
// Logical section of page
$practiceObj =  new  practice();
$treatment_plansObj = new treatment_plans();
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){
            /*$treatment_plansObj = new treatment_plans();
            $where =array('treatment_plans_practice_id='=>$practiceDetails->practice_office_id);
            $data =array('where'=>$where);
            $exist = $treatment_plansObj->FromFields($data);
            if($exist){
               header("Location:treatment_planslist.php?failedMsg=Patients already fetched from PMS for this practice.");
               exit;
            }*/

        $treatment_plansObj = new treatment_plans();
        $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       
        );
        // $treatment_plan_statusesObj = new treatment_plan_statuses();
        // $treatment_plan_statusesObj->resultAs ="array";
        // $param =array('where'=>array('clinic_id='=>$practiceDetails->practice_xcare_id));
        // $statusDetails =$treatment_plan_statusesObj->getAllRecords($param);
        // if(!empty($statusDetails) && count($statusDetails)>0){
        //     foreach ($statusDetails as $key => $value) {
        //         if($value['status']!="Completed"){
        //             $arg['status'] =$value['status']; 
                    
                    $status  = $treatment_plansObj->pullTreatmentPlans($arg);            
        //         }
        //     }
        // }
        if($status){
            header("Location:treatmentPlanlist.php?successMsg=Successfully pulled all patients for ".$practiceDetails->practice_office_id);
            exit;
        }else{
             header("Location:treatmentPlanlist.php?successMsg=Successfully pulled all patients for ".$practiceDetails->practice_office_id);
            exit;
        }
    }else{
        header("Location:treatmentPlanlist.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('treatment_plans_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(" treatment_plans_id desc ");
    }

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

    $treatment_plansDetails = $treatment_plansObj->getAllRecords($data);
    $recordsTotal   =   $treatment_plansObj->getAllCount($data);

        
    if ($treatment_plansDetails && count($treatment_plansDetails)>0){
        foreach ($treatment_plansDetails as $key => $treatment_plans){
            $response[] = array(
                    "<b>Plan Sr No: </b>".$treatment_plans->plan_sr_no."<br/><b>OfficeId : </b>".$treatment_plans->office_id."<br/><b>ClinicId : </b>".$treatment_plans->clinic_id."<br/><b>PatientId : </b>".$treatment_plans->patient_id."<br/><b>GuarantorId : </b>".$treatment_plans->guarantor_id."<br/><b>Xcare Patient Id : </b>".$treatment_plans->dependant_id,
                   "<b>Procedure Code: </b>".$treatment_plans->procedure_code."<br/><b>Category : </b>".$treatment_plans->category."<br/><b>Subcategory : </b>".$treatment_plans->subcategory."<br/><b>Subcategory Desc : </b>".$treatment_plans->subcategory_desc."<br/><b>Description : </b>".$treatment_plans->description, 
                   "<b>Amount: </b>".$treatment_plans->amount."<br/><b>Entry Date : </b>".$treatment_plans->entry_date."<br/><b>Status : </b>".$treatment_plans->treatment_plan_status."<br/><b>Procedure Date : </b>".$treatment_plans->procedure_date, 
                    
             );
        }
        $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');
?>