<?php 
if (!defined('IN_SITE')) {
    die('The Request Not Found');
}
$title = 'Quản Lý | ' . $DVR->site('title');
$body = [
    'title' => 'Danh sách Đơn Tố Cáo'
];
$body['header'] = '';
$body['footer'] = '';
require_once(__DIR__.'/Header.php');
require_once(__DIR__.'/Sidebar.php');
require_once(__DIR__ . '/../../../core/is_user.php');
CheckLogin();
CheckAdmin();


if(!empty($_GET['page']))
{
$page = xss(intval($_GET['page']));}
else
{
    $page = 1;
}
if(!empty($_GET['limit']))
{
$limit = xss(intval($_GET['limit']));}
else
{
    $limit = 10;
}
$sourceCodes = $DVR->get_list("SELECT * FROM `news` ORDER BY id DESC");
$totalItems = count($sourceCodes);
$totalPages = ceil($totalItems / $limit);

$from = ($page - 1) * $limit;
$order_by = ' ORDER BY id DESC ';
$where = '1=1';
$id_don = '';
$tieude = '';
$link = '';
$ngay = '';
if(!empty($_GET['id_don']))
{
    $id_don = xss($_GET['id_don']);
   $where .= " AND `id` = '$id_don'";
}

if(!empty($_GET['tieude']))
{
    $tieude = xss($_GET['tieude']);
  
        $where .= " AND `tieude` = '$tieude'";
}
if(!empty($_GET['link']))
{
    $link = xss($_GET['link']);
  
        $where .= " AND `link` = '$link'";
}
if(!empty($_GET['ngay']))
{
    $ngay = xss($_GET['ngay']);
  
        $where .= " AND `ngaydang` = '$ngay'";
}
?>
<div class="main-content app-content">
    <div class="container-fluid">
        <div class="d-md-flex d-block align-items-center justify-content-between my-4 page-header-breadcrumb">
            <h1 class="page-title fw-semibold fs-18 mb-0">Danh Sách Bài Viết</h1>
            <div class="ms-md-1 ms-0">
                <nav>
                    <ol class="breadcrumb mb-0">
                        <li class="breadcrumb-item active" aria-current="page">Danh Sách Bài Viết</li>
                    </ol>
                </nav>
            </div>
        </div>
        <div class="row">
            <div class="col-xl-12">
                <div class="card custom-card">
                    <div class="card-header justify-content-between">
                        <div class="card-title">
                            Danh Sách Bài Viết
                        </div>
                    </div>
                    <div class="card-body">
                        <form action="" class="align-items-center mb-3" name="formSearch" method="GET">
                            <input type="hidden" value="<?=$getUser['token']?>" id="token">
                            <div class="row row-cols-lg-auto g-3 mb-3">
                                <div class="col-lg col-md-4 col-6">
                                    <input class="form-control form-control-sm" value="" name="id_don" placeholder="ID Đơn">
                                </div>
                                <div class="col-lg col-md-4 col-6">
                                    <input class="form-control form-control-sm" value="" name="tieude" placeholder="Tiêu Đề">
                                </div>
                                <div class="col-lg col-md-4 col-6">
                                    <input class="form-control form-control-sm" value="" name="link" placeholder="Link Seo">
                                </div>
                              
                                <div class="col-lg col-md-4 col-6">
                                    <input type="text" name="ngay" class="form-control form-control-sm" id="daterange" value="" placeholder="Chọn thời gian">
                                </div>
                                <div class="col-12">
                                    <button class="btn btn-hero btn-sm btn-primary"><i class="fa fa-search"></i>
                                        Search </button>
                                    <a class="btn btn-hero btn-sm btn-danger" href="/Cpanel/news"><i class="fa fa-trash"></i>
                                        Clear filter </a>
                                </div>
                            </div>
                            <div class="top-filter">
                                <div class="filter-show">
                                    <label class="filter-label">Show :</label>
                                    <select name="limit" onchange="this.form.submit()" class="form-select filter-select">
                                        <option <?= ($limit == '5') ? 'selected' : ''; ?> value="5">5</option>
                                        <option <?= ($limit == '10') ? 'selected' : ''; ?> value="10">10</option>
                                        <option <?= ($limit == '20') ? 'selected' : ''; ?> value="20">20</option>
                                        <option <?= ($limit == '50') ? 'selected' : ''; ?> value="50">50</option>
                                        <option <?= ($limit == '100') ? 'selected' : ''; ?> value="100">100</option>
                                        <option <?= ($limit == '500') ? 'selected' : ''; ?> value="500">500</option>
                                        <option <?= ($limit == '1000') ? 'selected' : ''; ?> value="1000">1.000</option>
                                        <option <?= ($limit == '5000') ? 'selected' : ''; ?> value="5000">5.000</option>
                                        <option <?= ($limit == '10000') ? 'selected' : ''; ?> value="10000">10.000</option>
                                    </select>
                                </div>
                                <div class="filter-short">
                                    <label class="filter-label">Short by Date:</label>
                                    <select name="shortByDate" onchange="this.form.submit()" class="form-select filter-select">
                                        <option value="">Tất cả</option>
                                        <option value="1">Hôm nay </option>
                                        <option value="2">Tuần này </option>
                                        <option value="3">
                                            Tháng này </option>
                                    </select>
                                </div>
                            </div>
                        </form>
                        <div class="table-responsive table-wrapper mb-3">
                            <table class="table text-nowrap table-striped table-hover table-bordered">
                                <thead>
                                    <tr>
                                        <th>ID</th>
                                        <th>Tiêu Đề</th>
                                        <th>Ảnh Bìa</th>
                                        <th>Link Seo</th>
                                        <th>Ngay Đăng</th>
                                        <th>Thao Tác</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php foreach ($DVR->get_list("SELECT * FROM `news` WHERE $where $order_by LIMIT $from,$limit") as $row) {?>
                                                                            <tr>
                                            <td>
                                                <a class="text-primary" href="/news/<?= $row['code']; ?>"> [ID <?= $row['id']; ?>]</a>
                                            </td>
                                            <td class="text-center">
                                                <span class="badge bg-success-gradient"><?= $row['tieude']; ?></span>
                                            </td>
                                            <td><img src="<?= $row['image_news']; ?>" style="border-radius: 10px;" alt="pic" height="100px"></td>
                                            <td><?= $row['link']; ?>
                                            </td>
                                   
   
                                            <td><?= $row['ngaydang']; ?></td>
                        <td class="text-right">
                                                <button style="color:white;" onclick="RemoveRow(<?=$row['id']?>)"
                                                    class="btn btn-danger btn-sm btn-icon-left m-b-10" type="button">
                                                   <span class="">Delete</span>
                                                </button>
                                            </td>
                                        </tr>
                                        
                                                          <?php } ?>                  
                                                                    </tbody>
                            </table>
                        </div>
                        <div class="row">

                            <div class="col-sm-12 col-md-12 mb-3">
                                <div class="pagination-style-1">
                                    <div class="d-flex justify-content-center">
                                        <center> 
                                        <div class="paging_simple_numbers">
                                            <ul class="pagination">
                                                <?php for ($i = 1; $i <= $totalPages; $i++) { ?>
            <?php if ($i === $page) { ?>
                <li class="page-item active">
                    <a class="page-link link" href="#"><?php echo $i; ?></a>
                </li>
            <?php } else if ($i <= 4 || $i > $totalPages - 4 || abs($i - $page) <= 1) { ?>
                <li class="page-item <?php if($i == $page) { echo'active';}?>">
                    <a class="page-link link" href="/Cpanel/news?id_don=<?=$id_don;?>&tieude=<?=$tieude;?>&link=<?=$link;?>&ngay=<?=$ngay;?>&limit=<?=$limit;?>&shortByDate=&page=<?php echo $i; ?>"><?php echo $i; ?></a>
                </li>
            <?php } else if (abs($i - $page) === 2) { ?>
                <li class="page-item">
                    <a class="page-link link" href="">...</a>
                </li>
            <?php } ?>
        <?php } ?>
        <li class="paginate_button page-item previous "><a class="page-link" href="/Cpanel/news?id_don=<?=$id_don;?>&tieude=<?=$tieude;?>&link=<?=$link;?>&ngay=<?=$ngay;?>&limit=<?=$limit;?>&shortByDate=&page=<?=$page + 1;?>">Next</a></li>
                                                </ul></div></center>                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
function RemoveRow(id) {
    cuteAlert({
        type: "question",
        title: "Xác Nhận Xóa",
        message: "Bạn có chắc chắn muốn xóa ID " + id + " không ?",
        confirmText: "Đồng Ý",
        cancelText: "Hủy"
    }).then((e) => {
        if (e) {
            $.ajax({
                url: "<?=BASE_URL('')?>ajaxs/admin/remove_news.php",
                method: "POST",
                dataType: "JSON",
                data: {
                    id: id,
                    token: $("#token").val()
                },
               success: function(respone) {
                  if (respone.status == 'success') {
                     showMessage(respone.msg, "success");
                     setTimeout(function() {
                     location.reload(); }, 2000);
                  } else {
                     showMessage(respone.msg, respone.status);
                  }
                },
                error: function() {
                    alert(html(respone));
                    location.reload();
                }
            });
        }
    })
}
</script>
<?php
require_once(__DIR__.'/Footer.php');
?>