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

        $treatment_plan_statusesObj = new treatment_plan_statuses();
        $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  = $treatment_plan_statusesObj->pullTreatmentPlanStatuses($arg);
        if($status){
            header("Location:treatmentStatuslist.php?successMsg=Successfully pulled all statuses for ".$practiceDetails->practice_office_id);
            exit;
        }else{
             header("Location:treatmentStatuslist.php?successMsg=Successfully pulled all statuses for ".$practiceDetails->practice_office_id);
            exit;
        }
    }else{
        header("Location:treatmentStatuslist.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('clinic_id','office_id','status');
    
    // 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_plan_statuses_id desc ");
    }

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

    $treatment_plan_statusesDetails = $treatment_plan_statusesObj->getAllRecords($data);
    $recordsTotal   =   $treatment_plan_statusesObj->getAllCount($data);

        
    if ($treatment_plan_statusesDetails && count($treatment_plan_statusesDetails)>0){
        foreach ($treatment_plan_statusesDetails as $key => $treatment_plan_statuses){
            

            $response[] = array($treatment_plan_statuses->clinic_id,
                    $treatment_plan_statuses->office_id,
                    $treatment_plan_statuses->status
             );
        }
        $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');
?>