<?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 = 'Practice' ";

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


    /*if (isset($_REQUEST['search']['value']) && $_REQUEST['search']['value']!=""){
        $where .= " AND m.messagecentereventlog_id LIKE '%".$_REQUEST['search']['value']."%' OR  m.appointment_id LIKE '%".$_REQUEST['search']['value']."%' OR  m.email LIKE '%".$_REQUEST['search']['value']."%' OR m.phone LIKE '%".$_REQUEST['search']['value']."%' OR fcmtoken LIKE '%".$_REQUEST['search']['value']."%' OR c.clinic_id LIKE '%".$_REQUEST['search']['value']."%' OR c.clinic_name LIKE '%".$_REQUEST['search']['value']."%' OR c.clinic_phone LIKE '%".$_REQUEST['search']['value']."%' OR c.clinic_email LIKE '%".$_REQUEST['search']['value']."%' OR m.eventname LIKE '%".$_REQUEST['search']['value']."%' OR m.notificationtype LIKE '%".$_REQUEST['search']['value']."%' ";
    }*/


    $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);

    $practicelogDetails = $clinic_logObj->getAllRecords($data);
    //print_r($practicelogDetails);exit;
    $recordsTotal   =   $clinic_logObj->getAllCount($data);
     
    if ($practicelogDetails && count($practicelogDetails)>0){
        
        foreach ($practicelogDetails as $key => $practicelogdata)
        {
            if($practicelogdata->clinic_log_parameter == 'Social assistance specific program')
            {       
                    if($practicelogdata->clinic_log_old_value != '')
                    {
                        $old_socialassist = getsocialassistname($practicelogdata->clinic_log_old_value);
                         
                        $old_value = "<b>Old Value:</b> ".$old_socialassist;         
                    }
                    else{
                        $old_value = "<b>Old Value:</b> ";
                    }

                    if($practicelogdata->clinic_log_new_value != '')
                    {
                        $new_socialassist = getsocialassistname($practicelogdata->clinic_log_new_value);
                         
                        $new_value = "<b>New Value:</b> ".$new_socialassist;         
                    }
                    else{
                        $new_value = "<b>New Value:</b> ";
                    }

                    //print_r($oldsocial_assistDetail);exit;
            }
            else if($practicelogdata->clinic_log_parameter == 'Practice speciality')
            {
                    
                    if($practicelogdata->clinic_log_old_value != '')
                    {
                        $old_specality = getspecalityname($practicelogdata->clinic_log_old_value);
                         
                        $old_value = "<b>Old Value:</b> ".$old_specality;         
                    }else{
                        $old_value = "<b>Old Value:</b> ";
                    }

                    if($practicelogdata->clinic_log_new_value != '')
                    {
                        $new_specality = getspecalityname($practicelogdata->clinic_log_new_value);
                         
                        $new_value = "<b>New Value:</b> ".$new_specality;         
                    }else{
                        $new_value = "<b>New Value:</b> ";
                    }
                    
            }else if($practicelogdata->clinic_log_parameter == 'Practice hygiene features')
            {
                    
                    if($practicelogdata->clinic_log_old_value != '')
                    {
                        $old_hygiene = gethygienefeaturesname($practicelogdata->clinic_log_old_value);
                         
                        $old_value = "<b>Old Value:</b> ".$old_hygiene;         
                    }else{
                        $old_value = "<b>Old Value:</b> ";
                    }

                    if($practicelogdata->clinic_log_new_value != '')
                    {
                        $new_hygiene = gethygienefeaturesname($practicelogdata->clinic_log_new_value);
                         
                        $new_value = "<b>New Value:</b> ".$new_hygiene;         
                    }else{
                        $new_value = "<b>New Value:</b> ";
                    }
                    
            }else if($practicelogdata->clinic_log_parameter == 'Practice features')
            {
                    
                    if($practicelogdata->clinic_log_old_value != '')
                    {
                        $old_features = getpracticefeaturesname($practicelogdata->clinic_log_old_value);
                         
                        $old_value = "<b>Old Value:</b> ".$old_features;         
                    }else{
                        $old_value = "<b>Old Value:</b> ";
                    }

                    if($practicelogdata->clinic_log_new_value != '')
                    {
                        $new_features = getpracticefeaturesname($practicelogdata->clinic_log_new_value);
                         
                        $new_value = "<b>New Value:</b> ".$new_features;         
                    }else{
                        $new_value = "<b>New Value:</b> ";
                    }
                    
            }else if($practicelogdata->clinic_log_parameter == 'Practice insurance')
            {
                    
                    if($practicelogdata->clinic_log_old_value != '')
                    {
                        $old_insurance = getpracticeinsurancename($practicelogdata->clinic_log_old_value);
                         
                        $old_value = "<b>Old Value:</b> ".$old_insurance;         
                    }else{
                        $old_value = "<b>Old Value:</b> ";
                    }

                    if($practicelogdata->clinic_log_new_value != '')
                    {
                        $new_insurance = getpracticeinsurancename($practicelogdata->clinic_log_new_value);
                         
                        $new_value = "<b>New Value:</b> ".$new_insurance;         
                    }else{
                        $new_value = "<b>New Value:</b> ";
                    }
                    
            }
            else{

                $old_value = "<b>Old Value:</b> ".$practicelogdata->clinic_log_old_value;
                $new_value = "<b>New Value: </b>".$practicelogdata->clinic_log_new_value;
            }
            
            $response[] = array( 
                    
                    "<b>Xcare Id</b>: ".$practicelogdata->clinic_id."<br/><b>Name: </b>".$practicelogdata->clinic_name."<br/><b>Phone</b>: ".$practicelogdata->clinic_phone."<br/><b>Email</b>: ".$practicelogdata->clinic_email,
                    $practicelogdata->user_fname." ".$practicelogdata->user_lname,
                    $practicelogdata->clinic_log_action,
                    $practicelogdata->clinic_log_parameter,
                    $practicelogdata->clinic_log_type,
                    $old_value."<br/>".$new_value."<br/><b>Timestamp: </b>".$practicelogdata->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 gethygienefeaturesname($data)
{
    $hygin_val = explode(",",$data);
    $data_hygiene =  "'".implode ( "','", $hygin_val )."'";
    $hygiene_obj = new hygiene();
    $where = "hygiene_id IN (".$data_hygiene.")";
    $hygieneArg = array('where' => $where);
    $hygieneDetail = $hygiene_obj->getAllRecords($hygieneArg);
    $hygiene_name = '';    
    if(!empty($hygieneDetail))
    {
        foreach($hygieneDetail as $hkey => $hgval)
        {
              $hygiene_name .= $hgval->hygiene_title.",";  
        }    
    }

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

function getpracticefeaturesname($data)
{
    $features_val = explode(",",$data);
    $data_features =  "'".implode ( "','", $features_val )."'";
    $features_obj = new feature();
    $where = "feature_id IN (".$data_features.")";
    $featuresArg = array('where' => $where);
    $featuresDetail = $features_obj->getAllRecords($featuresArg);
    $features_name = '';    
    if(!empty($featuresDetail))
    {
        foreach($featuresDetail as $fkey => $feval)
        {
              $features_name .= $feval->feature_name.",";  
        }    
    }

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

function getpracticeinsurancename($data)
{
    $insurance_val = explode(",",$data);
    $data_insurance =  "'".implode ( "','", $insurance_val )."'";
    $insurance_obj = new insurance();
    $where = "insurance_id IN (".$data_insurance.")";
    $insuranceArg = array('where' => $where);
    $insuranceDetail = $insurance_obj->getAllRecords($insuranceArg);
    $insurance_name = '';    
    if(!empty($insuranceDetail))
    {
        foreach($insuranceDetail as $inkey => $inval)
        {
              $insurance_name .= $inval->insurance_name.",";  
        }    
    }

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

function getsocialassistname($data)
{

    $socialassist_val = explode(",",$data);
    $data_socialassist =  "'".implode ( "','", $socialassist_val )."'";
    $social_assist_obj = new social_assist();
    $where = "social_assist_id IN (".$data_socialassist.")";
    $socialassistArg = array('where' => $where);
    $socialassistDetail = $social_assist_obj->getAllRecords($socialassistArg);
    $socialassist_name = '';    
    if(!empty($socialassistDetail))
    {
        foreach($socialassistDetail as $sakey => $saval)
        {
              $socialassist_name .= $saval->social_assist_title.",";  
        }    
    }
    
    return rtrim($socialassist_name,",");
}
// Include Template 
include_once('../libs/routs.php');
?>