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

        $pmsAppointmentStatusObj = new pmsAppointmentStatus();
        $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  = $pmsAppointmentStatusObj->pullAppointmentStatuses($arg);
        if($status){
            header("Location:pmsAppointmentStatuslist.php?successMsg=Successfully pulled all patients for ".$practiceDetails->practice_office_id);
            exit;
        }else{
             header("Location:pmsAppointmentStatuslist.php?successMsg=Successfully pulled all patients for ".$practiceDetails->practice_office_id);
            exit;
        }
    }else{
        header("Location:pmsAppointmentStatuslist.php?failedMsg=Error Occur.");
        exit;
    }
}

if(isset($_REQUEST['action']) && $_REQUEST['action']!=''){
    
    if($_REQUEST['action']=="delete_all"){

        $chk_del = implode(",",$_POST['check']);
        
        /*$patientdata = " <b>Delete Id</b>: "." ".$chk_del;
        $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$patientdata);*/
        
        $rs = $pmsAppointmentStatusObj->deleteManyRecords($_POST['check']);
        $successMsg = base64_encode(RECORD_ALL_DELETE);
    }
}

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['practice_name']) && $_REQUEST['practice_name']!=""){
        $where .= " AND  pr.practice_name = '".$_REQUEST['practice_name']."' ";
    }

    if (isset($_REQUEST['search']['value']) && $_REQUEST['search']['value']!=""){
        $where = " pmsAppointmentStatus_status LIKE '%".$_REQUEST['search']['value']."%' OR pmsAppointmentStatus_office_id LIKE '%".$_REQUEST['search']['value']."%' ";
    }    
    $orderFields = array('pmsAppointmentStatus_status','pr.practice_name','pmsAppointmentStatus_office_id');
    
    // 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(" pmsAppointmentStatus_id desc ");
    }

    $fields  = array('pr.*','p.*');
    $tables = array('tbl_practice pr');
    $jfields = array('pr.practice_office_id = p.pmsAppointmentStatus_office_id');
    $jtype   = array("INNER"); 
    $data    = array('fields' => $fields, 'where' => $where,  'joinTables' =>$tables, 'joinFields' =>$jfields, 'joinTypes' => $jtype, 'limit' => $limit,'orderBy'=>$orderby);

    $pmsAppointmentStatusDetails = $pmsAppointmentStatusObj->getAllRecords($data);
    $recordsTotal   =   $pmsAppointmentStatusObj->getAllCount($data);

        
    if ($pmsAppointmentStatusDetails && count($pmsAppointmentStatusDetails)>0){
        foreach ($pmsAppointmentStatusDetails as $key => $pmsAppointmentStatus){
            $response[] = array(
                    '<input type="checkbox" name="check[]" id="check[]" value="'.$pmsAppointmentStatus->pmsAppointmentStatus_id.'" class="check_class" />',
                    $pmsAppointmentStatus->pmsAppointmentStatus_status,
                    $pmsAppointmentStatus->practice_name,
                    $pmsAppointmentStatus->pmsAppointmentStatus_office_id
             );
        }
        $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')
?>