<?php   
// Include main file
include_once('../libs/main.php');

include_once('checkLogin.php');

// Logical section of page
$clinic_logObj =  new  clinic_log();

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 and clinic_log_module_name = 'Provider' ";

    if(isset($_REQUEST['clinic_id']) && $_REQUEST['clinic_id'] != '')
    {
        $where .= " AND c.clinic_log_clinic_id = '".$_REQUEST['clinic_id']."'";
    
    }

    $orderFields = array('clinic_log_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(" clinic_log_id desc ");
    }

    
    $tables = array('tbl_clinic cl','tbl_user u');
    $jfields = array('cl.clinic_id=c.clinic_log_clinic_id','u.user_id=c.clinic_log_user_id');
    $jtype   = array("INNER","LEFT");
    $data   = array('where' => $where,  'joinTables' =>$tables, 'joinFields' =>$jfields, 'joinTypes' => $jtype,'orderBy'=>$orderby,'limit' => $limit);

    $providerlogDetails = $clinic_logObj->getAllRecords($data);
    //print_r($providerlogDetails);exit;
    $recordsTotal   =   $clinic_logObj->getAllCount($data);
     
    if ($providerlogDetails && count($providerlogDetails)>0){
        
        foreach ($providerlogDetails as $key => $providerlogdata)
        {
             if($providerlogdata->clinic_log_parameter == 'Provider speciality')
            {
                    
                    if($providerlogdata->clinic_log_old_value != '')
                    {
                        $old_specality = getspecalityname($providerlogdata->clinic_log_old_value);
                         
                        $old_value = "<b>Old Value:</b> ".$old_specality;         
                    }else{
                        $old_value = "<b>Old Value:</b> ";
                    }

                    if($providerlogdata->clinic_log_new_value != '')
                    {
                        $new_specality = getspecalityname($providerlogdata->clinic_log_new_value);
                         
                        $new_value = "<b>New Value:</b> ".$new_specality;         
                    }else{
                        $new_value = "<b>New Value:</b> ";
                    }
                    
            }else if($providerlogdata->clinic_log_parameter == 'Services offered by provider')
            {
                    
                    if($providerlogdata->clinic_log_old_value != '')
                    {
                        $old_services = getservicesname($providerlogdata->clinic_log_old_value);
                         
                        $old_value = "<b>Old Value:</b> ".$old_services;         
                    }else{
                        $old_value = "<b>Old Value:</b> ";
                    }

                    if($providerlogdata->clinic_log_new_value != '')
                    {
                        $new_services = getservicesname($providerlogdata->clinic_log_new_value);
                         
                        $new_value = "<b>New Value:</b> ".$new_services;         
                    }else{
                        $new_value = "<b>New Value:</b> ";
                    }
                    
            }else if($providerlogdata->clinic_log_parameter == 'Provider Languages')
            {
                    
                    if($providerlogdata->clinic_log_old_value != '')
                    {
                        $old_features = getproviderlanguagesname($providerlogdata->clinic_log_old_value);
                         
                        $old_value = "<b>Old Value:</b> ".$old_features;         
                    }else{
                        $old_value = "<b>Old Value:</b> ";
                    }

                    if($providerlogdata->clinic_log_new_value != '')
                    {
                        $new_features = getproviderlanguagesname($providerlogdata->clinic_log_new_value);
                         
                        $new_value = "<b>New Value:</b> ".$new_features;         
                    }else{
                        $new_value = "<b>New Value:</b> ";
                    }
                    
            }
            else{

                $old_value = "<b>Old Value:</b> ".$providerlogdata->clinic_log_old_value;
                $new_value = "<b>New Value: </b>".$providerlogdata->clinic_log_new_value;
            }
            
            $response[] = array( 
                    
                    "<b>Xcare Id</b>: ".$providerlogdata->clinic_id."<br/><b>Name: </b>".$providerlogdata->clinic_name."<br/><b>Phone</b>: ".$providerlogdata->clinic_phone."<br/><b>Email</b>: ".$providerlogdata->clinic_email,
                    $providerlogdata->user_fname." ".$providerlogdata->user_lname,
                    $providerlogdata->clinic_log_action,
                    $providerlogdata->clinic_log_parameter,
                    $providerlogdata->clinic_log_type,
                    $old_value."<br/>".$new_value."<br/><b>Timestamp: </b>".$providerlogdata->clinic_log_created_at,
            );
        }
        $result=array('recordsFiltered'=>$recordsTotal,'recordsTotal'=>$recordsTotal,'data'=>$response);
    }else{
        $result=array('recordsFiltered'=>0,'recordsTotal'=>0,'data'=>array());
    }
    echo json_encode($result);
    exit;
}

function getspecalityname($data)
{
    $spec_val = explode(",",$data);
    $data_specality =  "'".implode ( "','", $spec_val )."'";
    $speciality_obj = new speciality();
    $where = "speciality_id IN (".$data_specality.")";
    $specalityArg = array('where' => $where);
    $specalityDetail = $speciality_obj->getAllRecords($specalityArg);
    $specality_name = '';    
    if(!empty($specalityDetail))
    {
        foreach($specalityDetail as $skey => $spval)
        {
              $specality_name .= $spval->speciality_name.",";  
        }    
    }

    return rtrim($specality_name,",");
}

function getservicesname($data)
{
    $service_val = explode(",",$data);
    $data_service =  "'".implode ( "','", $service_val )."'";
    $service_obj = new service();
    $where = "service_id IN (".$data_service.")";
    $serviceArg = array('where' => $where);
    $serviceDetail = $service_obj->getAllRecords($serviceArg);
    $service_name = '';    
    if(!empty($serviceDetail))
    {
        foreach($serviceDetail as $skey => $srval)
        {
              $service_name .= $srval->service_name.",";  
        }    
    }

    return rtrim($service_name,",");
}

function getproviderlanguagesname($data)
{
    $languages_val = explode(",",$data);
    $data_languages =  "'".implode ( "','", $languages_val )."'";
    $languages_obj = new language();
    $where = "language_id IN (".$data_languages.")";
    $languagesArg = array('where' => $where);
    $languagesDetail = $languages_obj->getAllRecords($languagesArg);
    $languages_name = '';    
    if(!empty($languagesDetail))
    {
        foreach($languagesDetail as $lkey => $lval)
        {
              $languages_name .= $lval->language_name.",";  
        }    
    }

    return rtrim($languages_name,",");
}


// Include Template 
include_once('../libs/routs.php');
?>