<?php
/*
 * pages/guilds.php
 * PHP logic: INTACT - $paginate creat de include/functions/pages/guilds.php
 * HTML: design Nostalgia2
 *
 * Din dataview() original al guilds: returneaza name, master, level, ladder_point
 * Folosim getPlayerName($master) pentru a obtine numele liderului (ca in original)
 */

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

$records_per_page = 10;

if(isset($search)) {
    $query = "SELECT id, name, master, level, ladder_point FROM guild WHERE name NOT LIKE '[%]%' AND name LIKE :search ORDER BY level DESC, ladder_point DESC, exp DESC, name ASC";
    $newquery = $paginate->paging($query, $records_per_page);
    $paginate_search = $search;
} else {
    $query = "SELECT id, name, master, level, ladder_point FROM guild WHERE name NOT LIKE '[%]%' ORDER BY level DESC, ladder_point DESC, exp DESC, name ASC";
    $newquery = $paginate->paging($query, $records_per_page);
    $paginate_search = NULL;
}

// Fetch - folosim runQueryPlayer (guilds e tot in player DB)
$stmt = $database->runQueryPlayer($newquery);
if(isset($search))
    $stmt->bindValue(':search', '%'.$search.'%');
$stmt->execute();
$guilds = $stmt->fetchAll(PDO::FETCH_ASSOC);

$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['guilds']); ?>
        </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 guilds!
                    </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['guild']; ?>"
                    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 small text-bluevioletmutted" href="<?php echo $site_url; ?>ranking/players">
            <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 active small text-white" href="#">
            <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['guild']; ?></th>
                <th scope="col" class="text-gray pb-3 fw-normal"><?php echo $lang['leader']; ?></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>
                <th scope="col" class="text-gray pb-3 fw-normal"><?php echo $lang['points']; ?></th>
            </tr>
        </thead>
        <tbody>
            <?php
            $i = $offset + 1;
            if(empty($guilds)) {
                echo '<tr><td colspan="6" class="text-muted">Nothing here...</td></tr>';
            }
            foreach($guilds as $row) {
                // getPlayerName si getAccountID sunt functii din basic.php - ca in original
                $leader_name = getPlayerName($row['master']);
                $empire = get_player_empire(getAccountID($row['master']));
            ?>
            <tr>
                <th scope="row" class="fw-normal"><?php echo $i++; ?></th>
                <td><?php echo htmlspecialchars($row['name']); ?></td>
                <td class="text-green"><?php echo htmlspecialchars($leader_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>
                <td class="text-gold"><?php echo number_format($row['ladder_point'], 0, '', '.'); ?></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>
