<?php
// Include main file
include_once('../libs/main.php');
// Check admin Login
include_once('checkLogin.php');
// Logical section of page
$pmsentryanalysisObj =  new  pmsEntryAnalysis();
//$pmsentryanalysisObj =  new  pmsentryanalysis();

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  pmsEntryAnalysis_clinic_id = '".$_REQUEST['clinic_id']."' ";
    }
    if (isset($_REQUEST['analysis_type']) && $_REQUEST['analysis_type']!=""){
        $where .= " AND  pmsEntryAnalysis_type = '".$_REQUEST['analysis_type']."' ";
    }
    if(isset($_REQUEST['analysis_pmsid']) && $_REQUEST['analysis_pmsid'] != '')
    {
        $where .= " AND pmsEntryAnalysis_pmsid = '".$_REQUEST['analysis_pmsid']."'";
    }


    if (isset($_REQUEST['search']['value']) && $_REQUEST['search']['value']!=""){
        $where = " pmsEntryAnalysis_id LIKE '%".$_REQUEST['search']['value']."%' OR pmsEntryAnalysis_clinic_id LIKE '%".$_REQUEST['search']['value']."%'  OR pmsEntryAnalysis_type LIKE '%".$_REQUEST['search']['value']."%' OR pmsEntryAnalysis_pmsid LIKE '%".$_REQUEST['search']['value']."%'   OR pmsEntryAnalysis_loadeddate LIKE '%".$_REQUEST['search']['value']."%' OR clinic_id LIKE '%".$_REQUEST['search']['value']."%' OR clinic_name LIKE '%".$_REQUEST['search']['value']."%' OR clinic_email LIKE '%".$_REQUEST['search']['value']."%' OR clinic_phone LIKE '%".$_REQUEST['search']['value']."%' ";
    }    
    $orderFields = array('pmsEntryAnalysis_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(" pmsEntryAnalysis_id desc ");
    }

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

    $pmsentryanalysisDetails = $pmsentryanalysisObj->getAllRecords($data);
    $recordsTotal   =   $pmsentryanalysisObj->getAllCount($data);

        
    if ($pmsentryanalysisDetails && count($pmsentryanalysisDetails)>0)
    {
        
        foreach ($pmsentryanalysisDetails as $key => $entryanalysis)
        {
            $appdetail = '';
            if ($entryanalysis->pmsEntryAnalysis_type == 'appointment') 
            {

                /*$pmsappointmentObj = new pmsAppointment();
                //$pmsappointmentObj = new pmsappointment();
                $where =array('pmsAppointment_sr_no='=>$entryanalysis->pmsEntryAnalysis_pmsid,'pmsAppointment_clinic_id='=>$entryanalysis->pmsEntryAnalysis_clinic_id);
                $data =array('where'=>$where);
                $pmsappointment = $pmsappointmentObj->FromFields($data);
                
                if($pmsappointment)
                {

                    $appdetail = "<b>Id: </b>".$pmsappointment->pmsAppointment_id."<br/><b>Sr.No: </b>".$pmsappointment->pmsAppointment_sr_no."<br/><b>Patient Id: </b>".$pmsappointment->pmsAppointment_patient_id."<br/><b>Date: </b>".$pmsappointment->pmsAppointment_date."<br/><b>Time: </b>".$pmsappointment->pmsAppointment_time."<br/><b>Status: </b>".$pmsappointment->pmsAppointment_status;
                }*/
            }
            $response[] = array(
                    "<b>PmsId: </b>".$entryanalysis->pmsEntryAnalysis_pmsid,
                    "<b>Id: </b>".$entryanalysis->clinic_id.'<br/><b>Name: </b>'.$entryanalysis->clinic_name.'<br/><b>Email: </b>'.$entryanalysis->clinic_email.'<br/><b>Phone: </b>'.$entryanalysis->clinic_phone,
                    $entryanalysis->pmsEntryAnalysis_type,
                    //$appdetail,
                    $entryanalysis->pmsEntryAnalysis_created_at,
             );
        }
        $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');
?>