<?php
include('includes/headerSec.php');
if (isset($_GET['cat_id']) && (int) $_GET['cat_id'] != "") {
    $type = 'cat';
    $cat = (int) $_GET['cat_id'];
    $getTitle = $obj->getDataByField(NEWS_CAT, 'cat01id', $cat);
    $sub_cat=$getTitle['cat01id'];
    $categoryName = $getTitle['cat01category'];
    $thePage = $getTitle['eng_title'];
}
elseif (isset($_GET['subcatId']) && (int) $_GET['subcatId'] != "") {
    $type = 'subcat';
    $cat = (int) $_GET['subcatId'];
    $getTitle = $obj->getDataByField('tbl_sub_cat_news', 'cat01id', $cat);
    $getCategoryname = $obj->getDataByField(NEWS_CAT, 'cat01id', $getTitle['type']);
    $sub_cat = $getCategoryname['cat01id'];
    $categoryMainName = $getCategoryname['cat01category'];
    $categoryName = $getTitle['cat01category'];
    $thePage = $getTitle['cat01category'];
}elseif (isset($_GET['keyword']) && $_GET['keyword'] != "") {
    $type = 'search';
    $cat = "'" . $_GET['keyword'] . "'";
    $categoryName = "Search Result for " . $_GET['keyword'];
}
else {
    $cat = '0';
    $type = ' latest';
    $categoryName = 'Latest';
}
include('includes/header.php');
?>
<article class="innr">
	<div class="container">
		<div class="row">
			<div class="col-md-8 col-lg-9">
				<div class="row">
					<div class="col-md-12">
						<div class="headings">
							<h2><a href=""><?php echo $categoryName;?></a></h2>
						</div>
					</div>
                </div>
                <div class="row" id="result-news">

                </div>
                <div class="row">
					<div class="col-md-12">
						<div class="pgnatn">
							<nav aria-label="Page navigation example">
                                <ul class="pagination">
                                    <li class="page-item" class="active"><a class="page-link" href="#" id="1">1 <span class="sr-only">(current)</span></a></li>
                                </ul>
							</nav>
						</div>
					</div>
				</div>
			</div>
			<div class="col-md-4 col-lg-3">
				<div class="side-bar">
					<div class="headings">
						<h2><a href="">ताजा समाचार</a></h2>
					</div>
					<ul class="artha-list">
                            <?php $obj->popularNews(popularNews);?>
					</ul>
				</div>
			</div>
		</div>
	</div>
</article>
<?php include('includes/footer.php');?>
<script>
    $('document').ready(function () {
        $(".pagination a").trigger('click'); // When page is loaded we trigger a click
    });

    $('.pagination').on('click', 'a', function (e) {
        var type = <?php echo $cat; ?>;
        var page = this.id;
        var perPage = '21';
        var pagination = '';
        var data = {'perPage': perPage, 'page': page, '<?php echo $type ?>': type};
        $.ajax({
            url: '<?php echo SITE_URL ?>ajax/pagination.php',
            type: 'POST',
            data: data,
            dataType: 'json',
            success: function (response) {
                $('#result-news').html(response.newsList);
                $('html, body').animate({ scrollTop: 0 } , "slow");
                if (page == 1)
                    pagination += '<li class="page-item disabled"><a class="page-link" href="#" aria-label="First"><span aria-hidden="true">First</span></a></li><li class="page-item disabled"><a class="page-link" href="#" aria-label="Previous"><span aria-hidden="true">Previous</span></a></li>';
                else
                    pagination += '<li class="page-item"><a class="page-link" href="#" id="1" aria-label="First"><span aria-hidden="true">First</span></a></li><li class="page-item"><a class="page-link" href="#" id="' + (page - 1) + '" aria-label="Previous"><span aria-hidden="true">Previous</span></a></li>';

                for (var i = parseInt(page) - 3; i <= parseInt(page) + 3; i++) {
                    //	alert(i);
                    if (i >= 1 && i <= response.numPage) {

                        if (i == page)
                            pagination += '<li class="active page-item"><a class="page-link" href="#" id="' + i + '">' + i + '</a></li>';
                        else
                            pagination += '<li class="page-item"><a class="page-link" href="#" id="' + i + '">' + i + '</a></li>';

                    } //end of if
                } // end for loop

                if (page == response.numPage)
                    pagination += '<li class="disabled page-item"><a class="page-link" href="#"  aria-label="Next"><span aria-hidden="true">Next</span></a></li><li class="page-item disabled"><a class="page-link" href="#" aria-label="Last"><span aria-hidden="true">Last</span></a></li>';
                else
                    pagination += '<li class="page-item"><a class="page-link" href="#" id="' + (parseInt(page) + 1) + '" aria-label="Next"><span aria-hidden="true">Next</span></a></li><li class="page-item"><a class="page-link" href="#" id="' + response.numPage + '" aria-label="Last"><span aria-hidden="true">Last</span></a></li>';

                $('.pagination').html(pagination); // We update the pagination DIV


            }, // end of success

            error: function () { }

        });

        return false;

    });
</script>
