<?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.');
}
$get_Server = mysqli_query($conn, "Select * From config_server WHERE ServerID = '" . $_REQUEST['maychu'] . "'");
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'"); 
?>
 <div class="table-responsive">
                                        
                                        <table class="align-middle mb-0 table table-borderless table-striped table-hover">
                                            <thead>
                                            <tr>
                                                <th class="text-center">#</th>
                                                <th>Tài Khoản</th>
                                                <th class="text-center">ID Nhân Vật</th>
                                                <th class="text-center">Tên Nhân Vật</th>
                                                <th class="text-center">Cấp Độ</th>
                                                <th class="text-center">Bang Hội</th>
                                                <th class="text-center">Kim Nguyên Bảo</th>
                                                <th class="text-center">Bạc</th>
                                                <th class="text-center">Cống Hiến</th>
                                                <th class="text-center">Lực Chiến</th>
                                                <th class="text-center">Chức Năng</th>

                                            </tr>
                                            </thead>
                                            <tbody>
                                                <?php
                                             if($_REQUEST['query']){
                                                 $phantich = explode('&',$_REQUEST['query']);
                                                 for($i=0;$i<=count($phantich);$i++){
                                                         $phantichnew = explode('=',$phantich[$i]);
                                                         $_GET[''.$phantichnew[0].''] = $phantichnew[1];
                                                 }
                                                 if($_GET['keyword']){
                                                    $_REQUEST['keyword'] = $_GET['keyword'];
                                                 }
                                             }   
                                             $_GET['CauQuery'] = 'TimKiemQuery';

          include_once('paginator.class.jquery.php'); 
		$condition		=	"";
        $keyword		=	$_REQUEST['keyword'];
        if($keyword){
			if($_REQUEST['LoaiTim'] == 0){
        $qryStr		=	"select * from player_query_data WHERE Account Like '%$keyword%' or RoleId Like '%$keyword%'";  
			} else {
        $qryStr		=	"select * from Player WHERE PlayerName Like N'%$keyword%'";  				
			}
        } else {
			if($_REQUEST['LoaiTim'] == 0){
        $qryStr		=	"select * from player_query_data";  
			} else {
        $qryStr		=	"select * from Player";  				
			}
        }
        $country	=	mysqli_query($conn_char,$qryStr);
		$count		=	mysqli_num_rows($country);
        $pages = new Paginator($count,9);
		echo '<nav class="" aria-label="Page navigation example"> <ul class="pagination">';
		echo $pages->display_pages();
		echo '</ul></nav>';
		echo '</div>';
		echo '<div class="clearfix"></div>';
		$limit	= $pages->limit_start.','.$pages->limit_end;
        $qry 	=	mysqli_query($conn_char, $qryStr.' LIMIT '.$limit);
        if($count>0){
            $n	=	1;
            while($val	=	mysqli_fetch_array($qry, MYSQLI_ASSOC)){ 
			if($_REQUEST['LoaiTim'] == 1){
                $val = mysqli_fetch_assoc(mysqli_query($conn_char, "Select * From player_query_data WHERE RoleId = '".$val['ID']."'"));
				$PlayerName['PlayerName'] = $val['PlayerName'];
			}
                $PlayerName = mysqli_fetch_assoc(mysqli_query($conn_char, "Select PlayerName From Player WHERE ID = '".$val['RoleId']."'"));
                $KinName = mysqli_fetch_assoc(mysqli_query($conn_char, "Select KinName From kin_data WHERE KinID = '".$val['KinId']."'"));
                echo ' <tr>
                <td class="text-center text-muted">#'.$n.'</td>
                <td>
                    <div class="widget-content p-0">
                        <div class="widget-content-wrapper">
                            <div class="widget-content-left flex2">
                                <div class="widget-heading">'.$val['Account'].'</div>
                            </div>
                        </div>
                    </div>
                </td>
                <td class="text-center">'.$val['RoleId'].'</td>
                <td class="text-center">'.$PlayerName['PlayerName'].'</td>
                <td class="text-center"><div class="badge badge-warning">'.number_format($val['Level']).'</div></td>
                <td class="text-center">'.$KinName['KinName'].'</td>
                <td class="text-center">'.number_format($val['Gold']).'</td>
                <td class="text-center">'.number_format($val['Coin']).'</td>
                <td class="text-center">'.number_format($val['Contrib']).'</td>
                <td class="text-center"><div class="badge badge-warning">'.number_format($val['FightPower']).'</div></td>

                <td class="text-center">
                <div class="dropdown d-inline-block">
                <button type="button" aria-haspopup="true" aria-expanded="false" data-toggle="dropdown" class="mb-2 mr-2 dropdown-toggle btn btn-primary">Quản Lý</button>
                <div tabindex="-1" role="menu" aria-hidden="true" class="dropdown-menu">
                    <button type="button" tabindex="0" class="dropdown-item" onClick="showpupuweb(\'quanlytaikhoan\',\''.$val['Account'].'\');">Quản Lý Tài Khoản</button>
                    <button type="button" tabindex="0" class="dropdown-item" onClick="showpupuweb(\'quanlynhanvat\',\''.$val['Account'].'\');">Quản Lý Nhân Vật</button>
                    <button type="button" tabindex="0" class="dropdown-item" onClick="showpupuweb(\'chuyennap\',\''.$val['Account'].'\');">Chuyển Nạp</button>
                </div>
            </div>
                </td>
            </tr>';
            $n++;
            }
        }
    ?>
                                            </tbody>
                                        </table>
                                    </div>
                                   