<?php
session_start();
include_once('../config/#Config.php');
if(!$_SESSION['UserName']){
die('<script>window.location.href=\''.$config['site']['url'].'index.php\';</script>');
}
$get_AD = mysqli_query($conn, "Select * From config_admin WHERE Account = '".$_SESSION['UserName']."'");
if(!mysqli_num_rows($get_AD)){
	die('Không đủ quyền.');
}
if(!is_numeric($_POST['id'])){
		die('IDEvent Không hợp lệ.');
} else {
$getinfo = mysqli_query($conn, "Select * From box_mocnap WHERE BoxID = '".$_POST['id']."'");
$getinfo = mysqli_fetch_array($getinfo, MYSQLI_ASSOC);
$query="select ID from box_mocnap_item WHERE BoxID = '".$getinfo['BoxID']."' order by ID asc";	
$res    = mysqli_query($conn,$query);
$count  = mysqli_num_rows($res);
$page = 1;
$recordsPerPage = 10;
$start = ($page-1) * $recordsPerPage;
$adjacents = "2";
 
$prev = $page - 1;
$next = $page + 1;
$lastpage = ceil($count/$recordsPerPage);
$lpm1 = $lastpage - 1;   
$pagination = "";
if($lastpage > 1)
    {  
        $pagination .= "<div class='pagination' style='float:right'>";
        if ($page > 1)
            $pagination.= "<a href=\"#Page=".($prev)."\" onClick='PaginationItem(".($prev).");'>&laquo; Lui&nbsp;&nbsp;</a>";
        else
            $pagination.= "<span class='disabled'>&laquo; Lui&nbsp;&nbsp;</span>";   
        if ($lastpage < 7 + ($adjacents * 2))
        {   
            for ($counter = 1; $counter <= $lastpage; $counter++)
            {
                if ($counter == $page)
                    $pagination.= "<span class='current'>$counter</span>";
                else
                    $pagination.= "<a href=\"#Page=".($counter)."\" onClick='PaginationItem(".($counter).");'>$counter</a>";     
 
            }
        }   
 
        elseif($lastpage > 5 + ($adjacents * 2))
        {
            if($page < 1 + ($adjacents * 2))
            {
                for($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
                {
                    if($counter == $page)
                        $pagination.= "<span class='current'>$counter</span>";
                    else
                        $pagination.= "<a href=\"#Page=".($counter)."\" onClick='PaginationItem(".($counter).");'>$counter</a>";     
                }
                $pagination.= "...";
                $pagination.= "<a href=\"#Page=".($lpm1)."\" onClick='PaginationItem(".($lpm1).");'>$lpm1</a>";
                $pagination.= "<a href=\"#Page=".($lastpage)."\" onClick='PaginationItem(".($lastpage).");'>$lastpage</a>";   
 
           }
           elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
           {
               $pagination.= "<a href=\"#Page=\"1\"\" onClick='PaginationItem(1);'>1</a>";
               $pagination.= "<a href=\"#Page=\"2\"\" onClick='PaginationItem(2);'>2</a>";
               $pagination.= "...";
               for($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
               {
                   if($counter == $page)
                       $pagination.= "<span class='current'>$counter</span>";
                   else
                       $pagination.= "<a href=\"#Page=".($counter)."\" onClick='PaginationItem(".($counter).");'>$counter</a>";     
               }
               $pagination.= "..";
               $pagination.= "<a href=\"#Page=".($lpm1)."\" onClick='PaginationItem(".($lpm1).");'>$lpm1</a>";
               $pagination.= "<a href=\"#Page=".($lastpage)."\" onClick='PaginationItem(".($lastpage).");'>$lastpage</a>";   
           }
           else
           {
               $pagination.= "<a href=\"#Page=\"1\"\" onClick='PaginationItem(1);'>1</a>";
               $pagination.= "<a href=\"#Page=\"2\"\" onClick='PaginationItem(2);'>2</a>";
               $pagination.= "..";
               for($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
               {
                   if($counter == $page)
                        $pagination.= "<span class='current'>$counter</span>";
                   else
                        $pagination.= "<a href=\"#Page=".($counter)."\" onClick='PaginationItem(".($counter).");'>$counter</a>";     
               }
           }
        }
        if($page < $counter - 1)
            $pagination.= "<a href=\"#Page=".($next)."\" onClick='PaginationItem(".($next).");'>Tiếp &raquo;</a>";
        else
            $pagination.= "<span class='disabled'>Tiếp &raquo;</span>";
 
        $pagination.= "</div>";       
    }
 
$id='0';
$query="select * from box_mocnap_item WHERE BoxID = '".$getinfo['BoxID']."' order by ID asc
limit ".mysqli_real_escape_string($conn,$start).",$recordsPerPage";
$res    =   mysqli_query($conn,$query);
$i=0;
            if (mysqli_num_rows($res)) {
                // Hiển thị dữ liệu
                while ($row = mysqli_fetch_array($res, MYSQLI_ASSOC)) {
                    $i++;
					$name = getnameText($row['Item']);
					if(is_numeric($row['Item'])){
					$name = getnameItem($row['Item']);	
					}
 		$HTML .= '<tr>
                          <td class="font-weight-medium">
                           '.$i.'
                          </td>
                          <td>
                            '.mysubstr($name,10).' <img src="https://img.icons8.com/metro/1600/search.png" style="width:25px;height:25px" title="'.$name.'"/>
                          </td>
                          <td>
                            '.$row['Count'].'
                          </td>						 							  
                        <td>
                           <input type="submit" class="btn btn-success btn-block" value="Xóa" onclick="xoaitemNapThang(\''.$row['ID'].'\');" style="float:right;margin-top:-37px;margin-top: 2px;">
                          </td>						 							  
                        </tr>';
                }
            }
}
echo $HTML;
echo $pagination;
?>