<?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['logId'] && $_REQUEST['clinic_id']!="")
{
    if($_REQUEST['logId'] == '1')
    {
        $dir = DIR_ADMIN_LOG.'syncAppointment/'.$_REQUEST['clinic_id'];
        
        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  }
            }
        }
    }
    else if($_REQUEST['logId'] == '2')
    {
        $dir = DIR_ADMIN_LOG.'syncPatient/'.$_REQUEST['clinic_id'];
        
        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  }
            }
        }
    }
    else if($_REQUEST['logId'] == '3')
    {
        $dir = DIR_ADMIN_LOG.'syncGuarantor/'.$_REQUEST['clinic_id'];
        
        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['log_id'] != '' && $_REQUEST['clinic_id'] != ''){
   

    $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($_REQUEST['log_id']))
    {
        $alldttime = '';
        $searchData = $_REQUEST['searchvaluedata'];
        $log_search_data = $_REQUEST['log_search_data'];
        $searchdate = $_REQUEST['searchdatedata'];
        $searchtime = date("H:i A", strtotime($_REQUEST['searchtimedata']));
        if(isset($searchdate) && $searchdate != '' && isset($searchtime) && $searchtime != '')
        {
            $alldttime = $searchdate.' '.$searchtime;
        }
        
        //echo $alldttime;exit;
        $file_name = '';

        if($_REQUEST['log_id'] == '1')
        {
            $file_name = DIR_ADMIN_LOG."syncAppointment/".$_REQUEST['clinic_id']."/".$_REQUEST['logfilename'].'.log';

        }else if($_REQUEST['log_id'] == '2')
        {
            $file_name = DIR_ADMIN_LOG."syncPatient/".$_REQUEST['clinic_id']."/".$_REQUEST['logfilename'].'.log';
        }else if($_REQUEST['log_id'] == '3')
        {
            $file_name = DIR_ADMIN_LOG."syncGuarantor/".$_REQUEST['clinic_id']."/".$_REQUEST['logfilename'].'.log';
        }
        
        if(file_exists($file_name))
        {
            //$lines = array_slice(file($file_name), -50);
            $lines = file($file_name);
            //if(!empty($lines))
            if(!empty($lines))
            {
                
                $found = false;
                $i=1;
                $j=0;
                $line_number = '';
                $y = 1;
                $response = array();                
                
                foreach($lines as $key => $line)
                {

                  $j++;  
                  if(isset($searchData) && $searchData != '' && $alldttime == '')
                  {
                    if(strpos($line, $searchData) !== false)
                    {
                        if(!empty($line))
                        {
                            $response[] = array(
                                $i++,  
                                $line
                            );                        
                        }
                    }
                  }
                  if(isset($log_search_data) && $log_search_data != '' && $alldttime == '' && $log_search_data != 'other')
                  {
                        
                        if(strpos($line, $log_search_data) !== false)
                        {
                            if(!empty($line))
                            {
                                $response[] = array(
                                    $i++,  
                                    $line
                                );                
                            }
                        }


        
                  }
                  if(isset($alldttime) && $alldttime != '')
                  {
                      
                      if(strpos($line, $alldttime) !== false)
                      {
                         
                         $line_number .=  $j;
                         $spcificline = array_slice(file($file_name), $line_number);

                         if(!empty($spcificline))
                         {
                             foreach($spcificline as $allline)
                             {
                                 if(isset($searchData) && $searchData != '')
                                 {
                                     if(strpos($allline, $searchData) !== false)
                                     {
                                         if(!empty($allline))
                                         {
                                             $response[] = array(
                                                 $i++,  
                                                 $allline
                                             );
                                         }
                                     }
                                 }else
                                 {
                                     $response[] = array(
                                         $i++,  
                                         $allline
                                     );
                                 }
                             }
                         }
                      }
                  }
                }

                //print_r($response);exit;

                if($_REQUEST['length'] != '')
                {
                    $response1 = array_slice($response, $_REQUEST['start'], $_REQUEST['length']);
                }   
                $recordsTotal = count($response);    
                    
                $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());
        }
    }else{
        $result=array('recordsFiltered'=>0,'recordsTotal'=>0,'data'=>array());
    }

    echo json_encode($result);
    exit;
}
$practiceobj= new practice();
$tables = array('tbl_clinic c');
$jfields = array('c.clinic_id = p.practice_xcare_id');
$jtype   = array("INNER"); 
$allpractices= $practiceobj->getAllRecords(array('joinTables' =>$tables, 'joinFields' =>$jfields, 'joinTypes' => $jtype));
// Include Template 
include_once('../libs/routs.php')
?>