<?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']=="refresh"){
    $param = array(
        "app_id"=>sikka::APPID,
        "app_key"=>sikka::APPKEY,
        "offset"=>sikka::OFFSET,
        "limit"=>50
    );

    $curlParam =array(
        "endpoint" =>sikka::ENDPOINT_AUTH,
        "event"=>"authorized_practices",
        "query_data"=>$param
    );

    $practiceObj = new practice();
    $status  = $practiceObj->pullPractices($curlParam);
    if($status){
        header("Location:practicelist.php");
        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['search']['value']) && $_REQUEST['search']['value']!=""){
        $where = " practice_office_id LIKE '%".$_REQUEST['search']['value']."%' OR practice_name LIKE '%".$_REQUEST['search']['value']."%'  OR practice_pms LIKE '%".$_REQUEST['search']['value']."%'";
    }    
    $orderFields = array('practice_office_id','practice_name','practice_name');
    
    // 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(" practice_id desc ");
    }

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

    $practiceDetails = $practiceObj->getAllRecords($data);
    $recordsTotal   =   $practiceObj->getAllCount($data);

        
    if ($practiceDetails && count($practiceDetails)>0){
        foreach ($practiceDetails as $key => $practice){
            $clinicname="";
            if(is_null($practice->practice_name)){
                if(!is_null($practice->practice_xcare_id)){
                    $clinicObj = new clinic();
                    $c = $clinicObj->fromId($practice->practice_xcare_id);
                    $clinicname = $c->clinic_name;
                    $updateData  = array(
                        "practice_name" =>$clinicname
                    );
                    $practiceObj->updateRecords($updateData,$practice->practice_id);
                }
            }else{
                $clinicname = $practice->practice_name;
            }
            $pmsPatientObj = new pmsPatient();
            $where =array('pmsPatient_practice_id='=>$practice->practice_office_id);
            $data =array('where'=>$where);
            $exist = $pmsPatientObj->FromFields($data);
            $status="";
            if(!$exist){
                $status = '<a href="pmsPatientlist.php?office_id='.$practice->practice_office_id.'" title="Fetch PMS patient"><span class="badge badge-success">Pull Patients</span></a>';
            }
            $pmsProviderObj = new pmsProvider();
            $where =array('pmsProvider_office_id='=>$practice->practice_office_id);
            $data =array('where'=>$where);
            $exist = $pmsProviderObj->FromFields($data);
            if(!$exist){
                $status .= '<a href="pmsProviderlist.php?office_id='.$practice->practice_office_id.'" title="Fetch PMS Providers"><span class="badge badge-success">Pull Providers</span></a>';
            }
            $guarantorObj = new guarantor();
            $where =array('guarantor_practice_id='=>$practice->practice_office_id);
            $data =array('where'=>$where);
            $exist = $guarantorObj->FromFields($data);
            if(!$exist){
                $status .= '<a href="guarantorlist.php?office_id='.$practice->practice_office_id.'" title="Fetch PMS patient"><span class="badge badge-success">Pull Guarantors</span></a>';
            }
            $pmsAppointmentObj = new pmsAppointment();
            $where =array('pmsAppointment_practice_id='=>$practice->practice_office_id);
            $data =array('where'=>$where);
            $exist = $pmsAppointmentObj->FromFields($data);
            if(!$exist){
                $status .= '<a href="pmsAppointmentlist.php?office_id='.$practice->practice_office_id.'" title="Fetch PMS Appointments"><span class="badge badge-success">Pull Appointments</span></a>';
            }
            $response[] = array(
                    "<b>Office Id : </b>".$practice->practice_office_id."<br/><b>Sikka Id :</b>".$practice->practice_sikka_id,
                    "<b>Xcare Id :</b>".$practice->practice_xcare_id,
                    $practice->practice_pms, 
                    $clinicname,
                    $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')
?>