<?php
session_start();
include('../include/connection.php');
//pagination
$pagination = "";
$sql="select * from staff";

$text = "";

$conditions = array();
$fields=array();

if(isset($_GET['searchkey']) && $_GET['searchkey'] !="") {
    $conditions[] = "firstname like :searchkey OR lastname like :searchkey OR othernames like :searchkey OR position like :searchkey OR dept like :searchkey";
    $keyword = '%'.$_GET['searchkey'].'%';
    $fields[':searchkey'] = $keyword;
}

if (count($conditions) > 0) {
    $sql .= " WHERE " . implode(' AND ', $conditions);
}

$r=$con->select_query($sql,$fields);
$sn=1;

$number_of_results = count($r);

$number_of_pages = ceil($number_of_results/$_GET['no_records']);
$page = $_GET['page'];
$start_from = ($page - 1) * $_GET['no_records'];

$sql .= " order by id DESC LIMIT ".$start_from.','.$_GET['no_records'];
$r=$con->select_query($sql,$fields);
$sn=$_GET['lastrecord'];
if($page == 1)
    $sn = 1;
    $today = date('d-m');
    $birthday_count = 0;
    foreach($r as $value)
    {
      
        $star_mark = "";
        
        if($today == date('d-m', strtotime($value['dob'])))
        {
            $star_mark = ' <i class="ft-user-check text-danger"></i>';
            $birthday_count += 1;
        }
            $text .= '<tr>
                                                                <td style="font-size:12px;">'.$sn.'</td>
                                                                <td>'.$value['firstname'].' '.$value['othernames'].' '.$value['lastname'].'</td>
                                                                <td>'.$value['dept'].' - '.$value['position'].'</td>
                                                                <td>'.date('d F, Y', strtotime($value['dob'])).$star_mark.'</td>
                                                                <td>'.date('d-m-Y', strtotime($value['date_created'])).'</td>
                                                                <td><a href="staff_birthday_setup?id='.$value['id'].'" class="btn btn-warning btn-sm">Edit</a></td>
                                                                <td><button onclick="Delete('.$value['id'].')" class="btn btn-danger btn-sm">Delete</button></td>
                                                            </tr>';
            $sn++;
    }
    
    $prev = $page > 1 ? $page - 1 : $page;
    $next = $page + 1;
    $pagination = '<ul class="pagination pull-right">
                   <li class="footable-page-arrow"><a data-page="first" href="javascript:;" onclick="loadArticles(0, 1)"><span class="fa fa-angle-double-left"></span></a></li>
                   <li class="footable-page-arrow"><a data-page="prev" href="javascript:;" onclick="loadArticles(0, '.$prev.')"><span class="fa fa-angle-left"></span></a></li>';
    for($i=1; $i<=$number_of_pages; $i++)
    {
        $active = "";
        if($i == $page)
        {
            $active = "active";
        }
        $pagination .= '<li class="footable-page '.$active.'" onclick="loadArticles(0, '.$i.')"><a data-page="0" href="javascript:;">'.$i.'</a></li>';
    }
    
    
    $pagination .= '<li class="footable-page-arrow"><a data-page="next" href="javascript:;" onclick="loadArticles(0, '.$next.')"><span class="fa fa-angle-right"></span></a></li>
      <li class="footable-page-arrow"><a data-page="last" href="javascript:;" onclick="loadArticles(0, '.$number_of_pages.')"><span class="fa fa-angle-double-right"></span></a></li>
  </ul>';
    echo json_encode(array('text'=>$text, "pagination"=>$pagination, 'lastrecord'=>$sn, 'birthday_count'=>$birthday_count));
    ?>