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

        $procedure_codeObj = new procedure_code();
        $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  = $procedure_codeObj->pullProcedureCodes($arg);
        if($status){
            header("Location:pmsProcedureCodelist.php?successMsg=Successfully pulled all procedure codes for ".$practiceDetails->practice_office_id);
            exit;
        }else{
             header("Location:pmsProcedureCodelist.php?successMsg=Successfully pulled all procedure codes for ".$practiceDetails->practice_office_id);
            exit;
        }
    }else{
        header("Location:pmsProcedureCodelist.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 = $procedure_codeObj->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['sikka_id']) && $_REQUEST['sikka_id'] != ""){
        $where .= " AND  procedure_code_sikkaId = '".$_REQUEST['sikka_id']."' ";
    }
    if (isset($_REQUEST['procedure_officeid']) && $_REQUEST['procedure_officeid']!=""){
        $where .= " AND  procedure_code_office_id = '".$_REQUEST['procedure_officeid']."' ";
    }
    if (isset($_REQUEST['practice_name']) && $_REQUEST['practice_name']!=""){
        $where .= " AND  practice_name = '".$_REQUEST['practice_name']."' ";
    }
    if (isset($_REQUEST['procedure_abbr']) && $_REQUEST['procedure_abbr']!=""){
        $where .= " AND  procedure_code_abbr = '".$_REQUEST['procedure_abbr']."' ";
    }
    if (isset($_REQUEST['clinic_id']) && $_REQUEST['clinic_id']!=""){
        $where .= " AND  procedure_code_clinic_id = '".$_REQUEST['clinic_id']."' ";
    }
    

    if (isset($_REQUEST['search']['value']) && $_REQUEST['search']['value']!=""){
        $where = " procedure_code_sikkaId LIKE '%".$_REQUEST['search']['value']."%' OR procedure_code_office_id LIKE '%".$_REQUEST['search']['value']."%'  OR procedure_code_abbr LIKE '%".$_REQUEST['search']['value']."%' OR procedure_code_clinic_id LIKE '%".$_REQUEST['search']['value']."%' ";
    }    
    $orderFields = array('procedure_code_sikkaId','pr.practice_name','','procedure_code_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(" procedure_code_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.procedure_code_office_id','p.procedure_code_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);

    $procedure_codeDetails = $procedure_codeObj->getAllRecords($data);
    $recordsTotal   =   $procedure_codeObj->getAllCount($data);

        
    if ($procedure_codeDetails && count($procedure_codeDetails)>0){
        foreach ($procedure_codeDetails as $key => $procedure_code){
            $response[] = array(
                    '<input type="checkbox" name="check[]" id="check[]" value="'.$procedure_code->procedure_code_id.'" class="check_class" />',
                    "<b>Code : </b>".$procedure_code->procedure_code_sikkaId."<br/><b>Abbreaviation : </b>".$procedure_code->procedure_code_abbr."<br/><b>Category : </b>".$procedure_code->procedure_code_category."<br/><b>CategoryId : </b>".$procedure_code->procedure_code_category_id,
                    $procedure_code->practice_name,
                    $procedure_code->procedure_code_description,
                    $procedure_code->procedure_code_office_id,
                    "<b>Id: </b>".$procedure_code->clinic_id."<br/><b>Name: </b>".$procedure_code->clinic_name."<br/><b>Phone: </b>".$procedure_code->clinic_phone."<br/><b>Email: </b>".$procedure_code->clinic_email
             );
        }
        $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')
?>