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

include_once('checkLogin.php');

// Logical section of page
$messagecenterapilogObj =  new  messagecenterapilog();

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 m.clinicid = '".$_REQUEST['clinic_id']."'";
    
    }

    if(isset($_REQUEST['chg_parameter']) && $_REQUEST['chg_parameter'] != '')
    {
        $where .= " AND m.section = '".$_REQUEST['chg_parameter']."'";
    
    }


    /*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('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(" id desc ");
    }

    $fields = array('m.*','cl.clinic_id','cl.clinic_name','cl.clinic_phone','cl.clinic_email','u.user_fname','u.user_lname');
    $tables = array('tbl_clinic cl','tbl_user u');
    $jfields = array('cl.clinic_id=m.clinicid','u.user_id=m.userid');
    $jtype   = array("INNER","LEFT");
    $data   = array('fields'=>$fields,'where' => $where,  'joinTables' =>$tables, 'joinFields' =>$jfields, 'joinTypes' => $jtype,'orderBy'=>$orderby,'limit' => $limit);

    $messagecenterapilogDetails = $messagecenterapilogObj->getAllRecords($data);
    //print_r($messagecenterapilogDetails);exit;
    $recordsTotal   =   $messagecenterapilogObj->getAllCount($data);
     
    if ($messagecenterapilogDetails && count($messagecenterapilogDetails)>0){
        
        foreach ($messagecenterapilogDetails as $key => $messagecenterapilogdata)
        {

            $old_value = "<b>Old Value:</b> ".utf8_encode($messagecenterapilogdata->olddata);
            
            $new_value = "<b>New Value: </b>".utf8_encode($messagecenterapilogdata->newdata);
            
            $response[] = array( 
                    
                    "<b>Xcare Id</b>: ".$messagecenterapilogdata->clinic_id."<br/><b>Name: </b>".$messagecenterapilogdata->clinic_name."<br/><b>Phone</b>: ".$messagecenterapilogdata->clinic_phone."<br/><b>Email</b>: ".$messagecenterapilogdata->clinic_email,
                    $messagecenterapilogdata->user_fname." ".$messagecenterapilogdata->user_lname,
                    $messagecenterapilogdata->type,
                    $messagecenterapilogdata->section,
                    $old_value."<br/>".$new_value."<br/><b>Timestamp: </b>".$messagecenterapilogdata->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');
?>