<?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 (isset($_GET['keyword']) && isset($_GET['type'])) {
    $keyword = $_GET['keyword']; // Cắt bỏ khoảng trắng
    //$keyword = mysqli_real_escape_string($conn, $keyword); // Lọc các ký tự đặc biệt	
    if ($_GET['type'] == 0) {
        $query = "select ID from account_web where Account like '%$keyword%' or Email like '%$keyword%' or Phone like '%$keyword%' and Server = '" . $_GET['Server'] . "' order by ID asc";
        $res   = mysqli_query($conn, $query);
    } else {
        // connect sv
        /* ------------------------------------------- CONNECT CHAR SV ------------------------------- */
        $get_Server = mysqli_query($conn, "Select * From config_server WHERE ServerID = '" . $_GET['Server'] . "'");
        if (!mysqli_num_rows($get_Server)) {
            die('Lấy thông tin Máy chủ thất bại.');
        }
        $get_Server = mysqli_fetch_array($get_Server, MYSQLI_ASSOC);
        $conn_char  = @mysqli_connect($get_Server['IPdata'], $get_Server['Userdata'], $get_Server['PassData'], $get_Server['Databases']);
        if ($conn_char->mysqli_connect_errno) {
            die("Connection failed: " . $conn_char->mysqli_connect_errno);
        }
        mysqli_query($conn_char, "SET NAMES 'utf8'");
        /* ------------------------------------------- CONNECT CHAR SV ------------------------------- */
        $query = "select ID from player where ID like '%$keyword%' or PlayerName like '%$keyword%' or Account like '%$keyword%' order by ID asc";
        $res   = mysqli_query($conn_char, $query);
    }
} else {
    $query = "select ID from account_web  WHERE Server = '" . $_GET['Server'] . "' order by ID asc";
    $res   = mysqli_query($conn, $query);
}
$count          = mysqli_num_rows($res);
$page           = (int) (!isset($_REQUEST['pageId']) ? 1 : $_REQUEST['pageId']);
$page           = ($page == 0 ? 1 : $page);
$recordsPerPage = $limit_page;
$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='changePagination(" . ($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='changePagination(" . ($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='changePagination(" . ($counter) . ");'>$counter</a>";
            }
            $pagination .= "...";
            $pagination .= "<a href=\"#Page=" . ($lpm1) . "\" onClick='changePagination(" . ($lpm1) . ");'>$lpm1</a>";
            $pagination .= "<a href=\"#Page=" . ($lastpage) . "\" onClick='changePagination(" . ($lastpage) . ");'>$lastpage</a>";
            
        } elseif ($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2)) {
            $pagination .= "<a href=\"#Page=\"1\"\" onClick='changePagination(1);'>1</a>";
            $pagination .= "<a href=\"#Page=\"2\"\" onClick='changePagination(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='changePagination(" . ($counter) . ");'>$counter</a>";
            }
            $pagination .= "..";
            $pagination .= "<a href=\"#Page=" . ($lpm1) . "\" onClick='changePagination(" . ($lpm1) . ");'>$lpm1</a>";
            $pagination .= "<a href=\"#Page=" . ($lastpage) . "\" onClick='changePagination(" . ($lastpage) . ");'>$lastpage</a>";
        } else {
            $pagination .= "<a href=\"#Page=\"1\"\" onClick='changePagination(1);'>1</a>";
            $pagination .= "<a href=\"#Page=\"2\"\" onClick='changePagination(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='changePagination(" . ($counter) . ");'>$counter</a>";
            }
        }
    }
    if ($page < $counter - 1)
        $pagination .= "<a href=\"#Page=" . ($next) . "\" onClick='changePagination(" . ($next) . ");'>Tiếp &raquo;</a>";
    else
        $pagination .= "<span class='disabled'>Tiếp &raquo;</span>";
    
    $pagination .= "</div>";
}

if (isset($_GET['pageId']) && !empty($_GET['pageId'])) {
    $id = $_GET['pageId'];
} else {
    $id = '0';
}
if (isset($_GET['keyword'])) {
    if ($_GET['type'] == 0) {
        $query = "select *from account_web where Account like '%$keyword%' or Email like '%$keyword%' or Phone like '%$keyword%' and Server = '" . $_GET['Server'] . "' order by ID asc limit " . mysqli_real_escape_string($conn, $start) . ",$recordsPerPage";
        $res   = mysqli_query($conn, $query);
        
    } else {
        $query = "select ID,PlayerName from Player where ID like '%$keyword%' or PlayerName like '%$keyword%' or Account like '%$keyword%' order by ID asc";
        $res   = mysqli_query($conn_char, $query);
    }
} else {
    $query = "select *from account_web  WHERE Server = '" . $_GET['Server'] . "' 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++;
        $fromblockacc = '<div class="dropdown-divider"></div>
                            <a class="dropdown-item" href="javascript:blockACC(\'' . $row['Account'] . '\',\'1\')">
                              <i class="fa fa-check text-success fa-fw" ></i><font color="blue">Khóa Tài khoản</font></a>';
        if ($row['Block'] == 1) {
            $fromblockacc = '<div class="dropdown-divider"></div>
                            <a class="dropdown-item" href="javascript:blockACC(\'' . $row['Account'] . '\',\'0\')">
                              <i class="fa fa-check text-success fa-fw" ></i><font color="red">Mỡ khóa tài khoản</font></a>';
        }
        if ($_GET['type'] <= 0) {
            $HTML .= '<tr>
                          <td class="font-weight-medium">
                           ' . $i . '
                          </td>					  
                          <td>
                            ' . $row['Account'] . '
                          </td>
                          <td>
                           ' . $row['Phone'] . '
                          </td>
                          <td>
                           ' . $row['Email'] . '
                          </td>						  
                          <td>
                          ' . number_format($row['Money']) . '
                          </td>
                          <td>
                          ' . number_format($row['TienThat']) . '
                          </td>							  	
                         <td>
                      <div class="ticket-actions col-md-2">
                        <div class="btn-group dropdown">
                          <button type="button" class="btn btn-success dropdown-toggle btn-sm" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                            Xem Tùy chọn
                          </button>
                          <div class="dropdown-menu">
                            <a class="dropdown-item" href="javascript:loadinfo(\'' . $row['Account'] . '\');">
                              <i class="fa fa-reply fa-fw"></i>Quản lý Đổi thông tin</a>
							   <div class="dropdown-divider"></div>
                            <a class="dropdown-item" href="javascript:loadkichuser(\'' . $row['Account'] . '\')">
                              <i class="fa fa-history fa-fw"></i>Kích Người Chơi</a>
                            <div class="dropdown-divider"></div>
							<a class="dropdown-item" href="javascript:sendmail(\'' . $row['Account'] . '\')">
                              <i class="fa fa-history fa-fw"></i>Gửi thư nhanh</a>
                            <div class="dropdown-divider"></div>
                            <a class="dropdown-item" href="javascript:covernap(\'' . $row['Account'] . '\')">
                              <i class="fa fa-history fa-fw"></i>Chuyển Nạp</a>
                            ' . $fromblockacc . '						  
                          </div>
                        </div>
                      </div>						 
                       </td>						 
                        </tr>';
        } else {
			$phantich = mysqli_query($conn_char, "Select RoleID,Account,Level,VipLevel,Coin,Gold From player_query_data WHERE RoleID = '".$row['ID']."'");
			$phantich=mysqli_fetch_array($phantich, MYSQLI_ASSOC);
            $HTML .= '<tr>
                          <td class="font-weight-medium">
                           ' . $i . '
                          </td>					  
                          <td>
                            ' . $phantich['RoleID'] . '
                          </td>
                          <td>
                           ' . $phantich['Account'] . '
                          </td>
                          <td>
                           '.$row['PlayerName'].'
                          </td>						  
                          <td>
                          ' . number_format($phantich['Level']) . '
                          </td>
                          <td>
                          ' . number_format($phantich['VipLevel']) . '
                          </td>
						  <td>
                          ' . number_format($phantich['Gold']) . '
                          </td>
						  <td>
                          ' . number_format($phantich['Coin']) . '
                          </td>							  	
                         <td>
                      <div class="ticket-actions col-md-2">
                        <div class="btn-group dropdown">
                          <button type="button" class="btn btn-success dropdown-toggle btn-sm" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                            Xem Tùy chọn
                          </button>
                          <div class="dropdown-menu">
                            <a class="dropdown-item" href="javascript:loadinfo(\'' . $phantich['Account'] . '\');">
                              <i class="fa fa-reply fa-fw"></i>Quản lý Đổi thông tin</a>
							   <div class="dropdown-divider"></div>
                            <a class="dropdown-item" href="javascript:loadkichuser(\'' . $phantich['Account'] . '\')">
                              <i class="fa fa-history fa-fw"></i>Kích Người Chơi</a>
                            <div class="dropdown-divider"></div>
							<a class="dropdown-item" href="javascript:sendmail(\'' . $phantich['Account'] . '\')">
                              <i class="fa fa-history fa-fw"></i>Gửi thư nhanh</a>
                            <div class="dropdown-divider"></div>
                            <a class="dropdown-item" href="javascript:covernap(\'' . $phantich['Account'] . '\')">
                              <i class="fa fa-history fa-fw"></i>Chuyển Nạp</a>
                            ' . $fromblockacc . '						  
                          </div>
                        </div>
                      </div>						 
                       </td>						 
                        </tr>';
        }
    }
} else {
    $HTML = 'Không có kết quả nào cho từ khóa :"' . $_GET['keyword'] . '"';
}
$aaa['text']  = $HTML;
$aaa['trang'] = $pagination;
echo json_encode($aaa);
?>