<?php 
// Include main file
include_once('../libs/main.php');
// Check admin Login
include_once('checkLogin.php');
// Logical section of page

if(isset($_REQUEST['act']) && $_REQUEST['act']=='logfileget' && $_REQUEST['cliniclogId'])
{
    if($_REQUEST['cliniclogId'] != '')
    {
        $dir = DIR_CLINIC_LOG.$_REQUEST['cliniclogId'];
        
        if (is_dir($dir))
        {
            $allfile = scandir($dir, SCANDIR_SORT_DESCENDING); ?>
            <option value="">Select File</option>
            <?php foreach ($allfile as $file) {
                if($file != '.' && $file != '..')
                {
                    //echo $file .'<br>';
                    $allFileName = pathinfo($file, PATHINFO_FILENAME); ?>
                    <option value="<?php echo $allFileName; ?>" ><?php echo $allFileName ; ?></option>                
        <?php  }
            }
        }
    }
    
    exit;
}

if (isset($_REQUEST['act']) && $_REQUEST['act']=='load' && $_REQUEST['cliniclogId'] != ''){
   
    $cliniclogId = $_REQUEST['cliniclogId'];
   
    $records = $_REQUEST['length'];

    $start=0;
    if(isset($_REQUEST['start']) && $_REQUEST['start']!='0'){
        $page = $_GET['start'];
    }
    $start=($page-1)*$limit;
    $limit = array('start'=>$_REQUEST['start'],'record'=>$records);
    
    $POSTS_PER_PAGE = 10;

    if(isset($cliniclogId))
    {
        $file_name = '';
        if($cliniclogId != '')
        {
            $file_name = DIR_CLINIC_LOG.$_REQUEST['cliniclogId']."/".$_REQUEST['logfilename'].'.txt';

        }
        
        if(file_exists($file_name))
        {
            $allresponse = file_get_contents($file_name);
            
            $alldata =  explode("====", $allresponse);

            $i=1;
            $get_value = '';
            foreach($alldata as $key => $val)
            {
                $response[] = array(
                                         $i++,  
                                         $val,
                                     );    
            }

            if($_REQUEST['length'] != '')
            {
                $response1 = array_slice($response, $_REQUEST['start'], $_REQUEST['length']);
            } 
            $recordsTotal = count($alldata);
            $result=array('recordsFiltered'=>$recordsTotal,'recordsTotal'=>$recordsTotal,'data'=>$response1);
            
        }else{
            $result=array('recordsFiltered'=>0,'recordsTotal'=>0,'data'=>array());
        }
    }else{
        $result=array('recordsFiltered'=>0,'recordsTotal'=>0,'data'=>array());
    }

    echo json_encode($result);
    exit;
}

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