<?php
session_start();
include('../include/connection.php');
//pagination
$pagination = "";
$sql="select n.*,nc.name as categoryname from news n left outer join news_category nc on n.category_id=nc.id";

$text = "";

$conditions = array();
$fields=array();
if(isset($_GET['category']) && $_GET['category'] !="") {
    $conditions[] = "n.category_id=:category";
    $fields[':category']=$_GET['category'];
}

if(isset($_GET['searchkey']) && $_GET['searchkey'] !="") {
    $conditions[] = "n.title 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 n.id DESC LIMIT ".$start_from.','.$_GET['no_records'];
$r=$con->select_query($sql,$fields);
$sn=$_GET['lastrecord'];
if($page == 1)
    $sn = 1;

foreach($r as $value)
{
    $row_exist = true;
    $date_created = date('d-m-Y', strtotime($value['date_created']));
    if(!empty($_GET['startdate']))
    {
        if(strtotime($date_created) < strtotime($_GET['startdate']))
        {
            $row_exist = false;
        }
    }
    if(!empty($_GET['enddate']))
    {
        if(strtotime($date_created) > strtotime($_GET['enddate']))
        {
            $row_exist = false;
        }
    }
    
    if($row_exist)
    {
        if($value['is_published']==1)
        {
            $status="<span class='label label-primary'>Publish</span>";
        }
        else
        {
            $status="<span class='label label-default'>Not Published</span>";
        }
         
        $text .= '<tr>
                                                                <td style="font-size:12px;">'.$sn.'</td>
                                                                <td>'.$value['title'].'</td>
                                                                <td>'.$value['categoryname'].'</td>
                                                                <td>'.date('d-m-Y', strtotime($value['date_created'])).'</td>
                                                                <td>'.$status.'</td>
                                                                <td>
                                                                    <form method="post" action="../admin/article_setup">
                                                                        <input type="hidden" name="articleid" value="'.$value['id'].'"/>
                                                                        <input type="submit" name="article" value="Edit" class="btn btn-warning btn-sm"/>
                                                                    </form>
                                                                </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));
?>