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

        $pmsProviderObj = new pmsProvider();
        $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,
          "endpoint"=>$practiceDetails->practice_endpoint
        );
        $status  = $pmsProviderObj->pullProviders($arg);
        if($status){
            header("Location:pmsProviderlist.php?successMsg=Successfully pulled all provider for ".$practiceDetails->practice_office_id);
            exit;
        }else{
             header("Location:pmsProviderlist.php?successMsg=Successfully pulled all provider for ".$practiceDetails->practice_office_id);
            exit;
        }
    }else{
        header("Location:pmsProviderlist.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 = $pmsProviderObj->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['fullname']) && $_REQUEST['fullname']!="")
    {
        $fullname = str_replace(' ', '', $_REQUEST['fullname']);

        //$where .=" AND CONCAT(pmsProvider_fname,'',pmsProvider_lname) LIKE '%".$fullname."%' ";
        //$where .= " AND ('%".$fullname."%' LIKE Concat(Concat('%',pmsProvider_fname),'%') 
          //          OR '%".$fullname."%' LIKE  Concat(Concat('%',pmsProvider_lname),'%'))";

        $where .=" AND ( pmsProvider_fname LIKE '%".$_REQUEST['fullname']."%' OR CONCAT(pmsProvider_fname,'',pmsProvider_lname) LIKE '%".$fullname."%' OR pmsProvider_lname LIKE '%".$_REQUEST['fullname']."%') ";
    }*/

    if(isset($_REQUEST['firstname']) && $_REQUEST['firstname'] != '' && $_REQUEST['lastname'] != '')
    {
        $firstname = trim($_REQUEST['firstname']);
        $lastname = trim($_REQUEST['lastname']);

        $where .= " AND ((pmsProvider_fname='".($firstname)."' and   pmsProvider_lname='".($lastname)."') OR (pmsProvider_fname='".(ucfirst($firstname))."' and   pmsProvider_lname='".(ucfirst($lastname))."'))";
    }else if(isset($_REQUEST['firstname']) && $_REQUEST['firstname'] != '' && $_REQUEST['lastname'] == ''){

        $firstname = trim($_REQUEST['firstname']);

        $where .= " AND ((pmsProvider_fname LIKE '%".($firstname)."%') OR (pmsProvider_fname LIKE '%".(ucfirst($firstname))."%' ))";
    }else if(isset($_REQUEST['lastname']) && $_REQUEST['lastname'] != '' && $_REQUEST['firstname'] == ''){

        $lastname = trim($_REQUEST['lastname']);

        $where .= " AND ((pmsProvider_lname LIKE '%".($lastname)."%') OR ( pmsProvider_lname LIKE '%".(ucfirst($lastname))."%'))";
    }
    if(isset($_REQUEST['sikka_id']) && $_REQUEST['sikka_id'] != "")
    {
        $where .= " AND  pmsProvider_sikka_id = '".$_REQUEST['sikka_id']."' ";
    }
    if (isset($_REQUEST['provider_officeid']) && $_REQUEST['provider_officeid']!=""){
        $where .= " AND  pmsProvider_office_id = '".$_REQUEST['provider_officeid']."' ";
    }
    if (isset($_REQUEST['practice_name']) && $_REQUEST['practice_name']!=""){
        $where .= " AND  practice_name = '".$_REQUEST['practice_name']."' ";
    }
    if (isset($_REQUEST['provider_type']) && $_REQUEST['provider_type']!=""){
        $where .= " AND  pmsProvider_type = '".$_REQUEST['provider_type']."' ";
    }
    if (isset($_REQUEST['clinic_id']) && $_REQUEST['clinic_id']!=""){
        $where .= " AND  pmsProvider_clinic_id = '".$_REQUEST['clinic_id']."' ";
    }
    if(isset($_REQUEST['pmsprovider_id']) && $_REQUEST['pmsprovider_id'] != '')
    {
        $where .= " AND pmsProvider_id = '".$_REQUEST['pmsprovider_id']."'";
    }

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

    
    $fields  = array('pr.*','p.*','clinic_id','clinic_name','clinic_phone','clinic_email');
    $tables = array('tbl_practice pr','tbl_clinic c');
    $jfields = array('pr.practice_office_id = p.pmsProvider_office_id','p.pmsProvider_clinic_id = c.clinic_id');
    $jtype   = array("INNER","LEFT"); 
    $data    = array('fields' => $fields, 'where' => $where,  'joinTables' =>$tables, 'joinFields' =>$jfields, 'joinTypes' => $jtype, 'limit' => $limit,'orderBy'=>$orderby);

    $pmsProviderDetails = $pmsProviderObj->getAllRecords($data);
    $recordsTotal   =   $pmsProviderObj->getAllCount($data);

        
    if ($pmsProviderDetails && count($pmsProviderDetails)>0){
        foreach ($pmsProviderDetails as $key => $pmsProvider){
            $response[] = array(
                    '<input type="checkbox" name="check[]" id="check[]" value="'.$pmsProvider->pmsProvider_id.'" class="check_class" />',
                    "<b>Id : </b>".$pmsProvider->pmsProvider_id."<br/><b>SikkaId : </b>".$pmsProvider->pmsProvider_sikka_id,
                    $pmsProvider->practice_name,
                    "<b>First Name:</b>".$pmsProvider->pmsProvider_fname."<br/><b>Last Name: </b>".$pmsProvider->pmsProvider_lname,
                    $pmsProvider->pmsProvider_type,
                    $pmsProvider->pmsProvider_office_id,
                    "<b>Id: </b>".$pmsProvider->clinic_id."<br/><b>Name: </b>".$pmsProvider->clinic_name."<br/><b>Phone: </b>".$pmsProvider->clinic_phone."<br/><b>Email: </b>".$pmsProvider->clinic_email,
                    $pmsProvider->pmsProvider_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')
?>