<?php
/*
 * pages/players.php
 * PHP logic: INTACT - $paginate creat de include/functions/pages/players.php
 * HTML: design Nostalgia2
 *
 * Variabile disponibile dupa include:
 *   $paginate - instanta clasei din include/classes/players.php (conectata la player DB)
 *   $search - daca s-a trimis cautare
 */

include 'include/functions/pages/players.php';

$records_per_page = 10;

// Construim query in functie de search (LOGIC INTACT din CMS original)
if(isset($search)) {
    $query = "SELECT id, name, account_id, level, exp, job FROM player WHERE name NOT LIKE '[%]%' AND name LIKE :search ORDER BY level DESC, exp DESC, playtime DESC, name ASC";
    $newquery = $paginate->paging($query, $records_per_page);
    $paginate_search = $search; // pentru paginglink
} else {
    $query = "SELECT id, name, account_id, level, exp, job FROM player WHERE name NOT LIKE '[%]%' ORDER BY level DESC, exp DESC, playtime DESC, name ASC";
    $newquery = $paginate->paging($query, $records_per_page);
    $paginate_search = NULL;
}

// Fetch date - folosim $database->runQueryPlayer (players paginate are tot db privat)
$stmt = $database->runQueryPlayer($newquery);
if(isset($search))
    $stmt->bindValue(':search', '%'.$search.'%');
$stmt->execute();
$players = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Offset pentru numerotare corecta
$current_page = (isset($_GET['page_no']) && is_numeric($_GET['page_no'])) ? intval($_GET['page_no']) : 1;
$offset = ($current_page - 1) * $records_per_page;
?>

<!-- Header -->
<nav class="navbar navbar-dark bg-news-header">
    <div class="container-fluid">
        <h4 class="h4 small m-0 text-uppercase">
            <?php echo strtoupper($lang['ranking']); ?> - <?php echo strtoupper($lang['players']); ?>
        </h4>
        <div class="d-flex d-none d-md-block">
            <ul class="navbar-nav list-group-horizontal small">
                <li class="nav-item px-2">
                    <a class="nav-link small text-bluevioletmutted py-0" href="#">
                        The top ranking of best players!
                    </a>
                </li>
            </ul>
        </div>
    </div>
</nav>

<!-- Search form -->
<div class="p-3 bg-part-content">
    <form action="" method="POST">
        <div class="row g-2">
            <div class="col-9">
                <input type="text" name="search"
                    class="form-control-input form-control form-input text-white small"
                    placeholder="<?php echo $lang['name']; ?>"
                    value="<?php if(isset($search)) echo htmlspecialchars($search); ?>">
            </div>
            <div class="col-3">
                <button type="submit" class="btn bg-button small fw-bold w-100">
                    <i class="fa fa-search"></i> <?php echo $lang['search']; ?>
                </button>
            </div>
        </div>
    </form>
</div>

<!-- Tabs Players / Guilds -->
<ul class="nav nav-tabs mt-3 px-3">
    <li class="nav-item">
        <a class="nav-link active small text-white" href="#">
            <img src="<?php echo $site_url; ?>images/players.png" alt="" width="20">
            <?php echo $lang['players']; ?>
        </a>
    </li>
    <li class="nav-item">
        <a class="nav-link small text-bluevioletmutted" href="<?php echo $site_url; ?>ranking/guilds">
            <img src="<?php echo $site_url; ?>images/guilds.png" alt="" width="20">
            <?php echo $lang['guilds']; ?>
        </a>
    </li>
</ul>

<!-- Tabel ranking -->
<div class="col-12 text-center p-4 small">
    <table class="table table-sm table-borderless small text-center">
        <thead>
            <tr>
                <th scope="col" class="text-gray pb-3 fw-normal">#</th>
                <th scope="col" class="text-gray pb-3 fw-normal"><?php echo $lang['class']; ?></th>
                <th scope="col" class="text-gray pb-3 fw-normal"><?php echo $lang['name']; ?></th>
                <th scope="col" class="text-gray pb-3 fw-normal"><?php echo $lang['empire']; ?></th>
                <th scope="col" class="text-gray pb-3 fw-normal"><?php echo $lang['level']; ?></th>
            </tr>
        </thead>
        <tbody>
            <?php
            $i = $offset + 1;
            if(empty($players)) {
                echo '<tr><td colspan="5" class="text-muted">Nothing here...</td></tr>';
            }
            foreach($players as $row) {
                $empire = get_player_empire($row['account_id']);
            ?>
            <tr class="align-baseline">
                <th scope="row" class="text-bluegray fw-normal"><?php echo $i++; ?></th>
                <td>
                    <img width="45"
                        src="<?php echo $site_url; ?>assets/img/job/<?php echo intval($row['job']); ?>.png"
                        alt="">
                </td>
                <td><?php echo htmlspecialchars($row['name']); ?></td>
                <td>
                    <img src="<?php echo $site_url; ?>images/empire/<?php echo $empire; ?>.jpg"
                         alt="<?php echo empire_name($empire); ?>" width="20">
                </td>
                <td><span class="text-gold">Lv. <?php echo $row['level']; ?></span></td>
            </tr>
            <?php } ?>
        </tbody>
    </table>
</div>

<!-- Paginare -->
<div class="w-100 p-2 text-center mx-auto">
    <?php $paginate->paginglink($query, $records_per_page, $lang['first-page'], $lang['last-page'], $site_url, $paginate_search); ?>
</div>
