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

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

    $offerslogDetails = $clinic_logObj->getAllRecords($data);
    //print_r($offerslogDetails);exit;
    $recordsTotal   =   $clinic_logObj->getAllCount($data);
     
    if ($offerslogDetails && count($offerslogDetails)>0){
        
        foreach ($offerslogDetails as $key => $offerslogdata)
        {
             

            $old_value = "<b>Old Value:</b> ".$offerslogdata->clinic_log_old_value;
            $new_value = "<b>New Value: </b>".$offerslogdata->clinic_log_new_value;
            
            
            $response[] = array( 
                    
                    "<b>Xcare Id</b>: ".$offerslogdata->clinic_id."<br/><b>Name: </b>".$offerslogdata->clinic_name."<br/><b>Phone</b>: ".$offerslogdata->clinic_phone."<br/><b>Email</b>: ".$offerslogdata->clinic_email,
                    $offerslogdata->user_fname." ".$offerslogdata->user_lname,
                    $offerslogdata->clinic_log_action,
                    $offerslogdata->clinic_log_parameter,
                    $offerslogdata->clinic_log_type,
                    $old_value."<br/>".$new_value."<br/><b>Timestamp: </b>".$offerslogdata->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;
}

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