<?php
/*
 * index.php - Metin2 CMS cu design Nostalgia2
 * Faza 1: Layout principal complet
 * 
 * Variabile disponibile din header.php:
 *   $site_url, $site_title, $site_domain, $language_code
 *   $database (USER class), $offline
 *   $lang[] (array traduceri)
 *   $page (pagina curenta)
 *   $title (titlul paginii)
 *   $forum, $support, $item_shop
 *   $web_admin, $jsondataPrivileges
 *   $jsondataFunctions, $statistics
 *   $json_languages (limbi disponibile)
 *   $_SESSION['id'] (daca logat)
 *   $offline_players, $offline_guilds (daca offline)
 */
@ob_start();
include 'include/functions/header.php';
?>
<!DOCTYPE html>
<html lang="<?php echo $language_code; ?>">

<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta charset="UTF-8">
    <title><?php echo $site_title; ?> | <?php echo $title; ?></title>

    <link rel="stylesheet" href="<?php echo $site_url; ?>assets/css/main.css">
    <link rel="stylesheet" href="<?php echo $site_url; ?>assets/css/bootstrap.css">
    <link href="https://fonts.googleapis.com/css?family=Merriweather:400,500,600,700,800,900" rel="stylesheet">
    <link rel="stylesheet" href="https://pro.fontawesome.com/releases/v5.10.0/css/all.css" integrity="sha384-AYmEC3Yw5cVb3ZcuHtOA93w35dYTsvhLPVnYs9eStHfGJvOvKxVfELGroGkvsg+p" crossorigin="anonymous"/>
    <link rel="shortcut icon" href="<?php echo $site_url; ?>images/favicon.ico">

    <script src="<?php echo $site_url; ?>assets/js/jquery-3.js"></script>
    <script src="<?php echo $site_url; ?>assets/js/bootstrap.js"></script>
</head>

<body class="text-opensans bg-main">

<!-- ===================== NAVBAR ===================== -->
<nav class="navbar nav-bg navbar-expand-md navbar-light bg-transparent header-position">
    <button type="button" class="navbar-toggler"
        data-bs-toggle="collapse" data-bs-target="#navbarCollapse"
        aria-controls="navbarCollapse" data-target="#navbarCollapse"
        aria-expanded="false" aria-label="Toggle navigation">
        <i class="fas fa-bars"></i>
    </button>

    <div class="collapse navbar-collapse justify-content-md-center w-100" id="navbarCollapse">
        <ul class="navbar-nav text-center text-white small row w-100 mx-auto align-items-center main-max-w">

            <!-- Branding -->
            <li class="nav-item nav-item-hov col-3 d-none d-lg-block">
                Enter the Dark - <a href="<?php echo $site_url; ?>"><?php echo $site_domain; ?></a>
            </li>

            <?php if(!$offline) { ?>

            <!-- Noutati -->
            <li class="nav-item nav-item-hov col-1">
                <a class="nav-link text-white nav-linking" href="<?php echo $site_url; ?>"><?php echo $lang['news']; ?></a>
            </li>

            <!-- Descarca -->
            <li class="nav-item nav-item-hov col-1">
                <a class="nav-link text-white nav-linking" href="<?php echo $site_url; ?>download"><?php echo $lang['download']; ?></a>
            </li>

            <!-- Inregistrare (doar daca nu e logat) -->
            <?php if(!$database->is_loggedin()) { ?>
            <li class="nav-item nav-item-hov col-1">
                <a class="nav-link text-white nav-linking" href="<?php echo $site_url; ?>users/register"><?php echo $lang['register']; ?></a>
            </li>
            <?php } ?>

            <!-- Forum -->
            <?php if($forum) { ?>
            <li class="nav-item nav-item-hov col-1">
                <a class="nav-link text-white nav-linking" href="<?php echo $forum; ?>" target="_blank">Forum</a>
            </li>
            <?php } ?>

            <!-- Clasament dropdown -->
            <li class="nav-item col-1 dropdown">
                <a class="nav-link text-white dropdown-toggle" href="#" role="button"
                    data-bs-toggle="dropdown" aria-expanded="false">
                    <?php echo $lang['ranking']; ?>
                </a>
                <ul class="dropdown-menu">
                    <li><a class="dropdown-item" href="<?php echo $site_url; ?>ranking/players"><?php echo $lang['players']; ?></a></li>
                    <li><a class="dropdown-item" href="<?php echo $site_url; ?>ranking/guilds"><?php echo $lang['guilds']; ?></a></li>
                </ul>
            </li>

            <?php } else { ?>
            <!-- Offline: arata doar mesaj -->
            <li class="nav-item nav-item-hov col-1">
                <a class="nav-link text-white nav-linking" href="<?php echo $site_url; ?>">
                    <strong class="text-danger"><?php echo $lang['server-offline']; ?></strong>
                </a>
            </li>
            <?php } ?>

            <!-- Selector limba (daca sunt mai multe limbi) -->
            <?php if(isset($json_languages) && count($json_languages['languages']) > 1) { ?>
            <li class="nav-item col-1 dropdown">
                <a class="nav-link text-white dropdown-toggle" href="#" role="button"
                    data-bs-toggle="dropdown" aria-expanded="false">
                    <?php echo $json_languages['languages'][$language_code]; ?>
                </a>
                <ul class="dropdown-menu">
                    <?php foreach($json_languages['languages'] as $key => $value) { ?>
                    <li>
                        <a class="dropdown-item" href="<?php echo $site_url; ?>?lang=<?php echo $key; ?>">
                            <?php if(file_exists('assets/img/flags/'.$key.'.png')) { ?>
                            <img src="<?php echo $site_url; ?>assets/img/flags/<?php echo $key; ?>.png" width="20" alt="<?php echo $key; ?>">
                            <?php } ?>
                            <?php echo $value; ?>
                        </a>
                    </li>
                    <?php } ?>
                </ul>
            </li>
            <?php } ?>

            <!-- Buton INREGISTRARE (daca nu e logat) -->
            <?php if(!$database->is_loggedin()) { ?>
            <li class="nav-item col-2">
                <a class="nav-link text-white" href="<?php echo $site_url; ?>users/register">
                    <button class="btn bg-button-2"><?php echo strtoupper($lang['register']); ?></button>
                </a>
            </li>
            <?php } ?>

        </ul>
    </div>
</nav>


<!-- ===================== HEADER CU VIDEO ===================== -->
<header class="d-flex justify-content-start align-items-end position-relative text-white d-none d-md-block">

    <!-- Statistici online (daca sunt activate si serverul e online) -->
    <?php if(!$offline && $statistics) { ?>
    <div class="d-none d-lg-block">
        <div class="d-flex align-items-center sign col-6">
            <div class="d-flex align-items-center px-5">
                <img src="<?php echo $site_url; ?>assets/img/sign.png" alt="">
                <div>
                    <span class="stats-value"><?php echo number_format(getStatistics('players-online'), 0, '', '.'); ?></span>
                    <p class="stats-text-header"><?php echo $lang['players-online']; ?></p>
                </div>
            </div>
            <div class="d-flex align-items-center px-5">
                <img src="<?php echo $site_url; ?>assets/img/sign.png" alt="">
                <div>
                    <span class="stats-value"><?php echo number_format(getStatistics('players-online-last-24h'), 0, '', '.'); ?></span>
                    <p class="stats-text-header"><?php echo $lang['players-online-last-24h']; ?></p>
                </div>
            </div>
        </div>
    </div>
    <?php } ?>

    <!-- Video background -->
    <video playsinline autoplay muted loop>
        <source src="<?php echo $site_url; ?>assets/header.mp4" type="video/mp4">
    </video>

    <!-- Imagine de tranzitie (overlapping header->content) -->
    <img class="transition" src="<?php echo $site_url; ?>assets/img/img/test.png" alt="">

</header>


<!-- ===================== MAIN CONTENT: 3 coloane ===================== -->
<main class="main-position position-relative px-4 pb-5 main-max-w">
    <div class="row">


        <!-- ===== COLOANA STANGA: Login/Userpanel + Players Rank ===== -->
        <section class="mt-4 mt-xl-0 col-11 col-sm-10 mx-auto col-md-6 col-xl-3 order-xl-1">

            <?php if(!$database->is_loggedin()) { ?>
            <!-- BOX LOGIN -->
            <div class="col-12 p-3 bg-part-title small px-5">
                <div class="row">
                    <h5 class="h5 col text-white w-100 small m-0 pt-1"><?php echo strtoupper($lang['login']); ?></h5>
                    <p class="col text-right w-100 m-0 small text-green">
                        <a href="<?php echo $site_url; ?>users/register" class="text-right text-gold"><?php echo $lang['register']; ?></a>
                    </p>
                </div>
                <div class="col-12"></div>
            </div>

            <div class="col-12 bg-part-content text-center p-4 small">
                <p class="col w-100 m-0 small text-muted">Enter your login details</p>
                <form role="form" method="post" action="<?php echo $site_url; ?>users/login">
                    <div class="left-inner-addon input-container mt-2">
                        <img src="<?php echo $site_url; ?>assets/img/icon-user.png" alt="">
                        <input name="username" type="text"
                            class="form-control-input form-control form-input text-white small"
                            placeholder="<?php echo $lang['user-name-or-email']; ?>"
                            pattern=".{5,64}" maxlength="64" autocomplete="off"
                            <?php if($offline) echo 'disabled'; else echo 'required'; ?>>
                    </div>
                    <div class="left-inner-addon input-container mt-2">
                        <img src="<?php echo $site_url; ?>assets/img/icon-pass.png" alt="">
                        <input name="password" type="password"
                            class="form-control-input form-control form-input text-white small"
                            placeholder="<?php echo $lang['password']; ?>"
                            pattern=".{5,16}" maxlength="16"
                            <?php if($offline) echo 'disabled'; else echo 'required'; ?>>
                    </div>
                    <div class="sider-btn">
                        <button class="my-2 mx-auto text-center sign-in-button btn bg-button small fw-bold"
                            type="submit" style="display:block;"
                            <?php if($offline) echo 'disabled'; ?>>
                            <?php echo $lang['login']; ?>
                        </button>
                    </div>
                    <div class="row mx-auto small text-white">
                        <div class="login-options p-0 text-start">
                            <ul>
                                <li><a href="<?php echo $site_url; ?>users/register" class="text-start">&#8226; <?php echo $lang['register']; ?></a></li>
                                <li><a href="<?php echo $site_url; ?>users/lost" class="text-start">&#8226; <?php echo $lang['forget-password']; ?></a></li>
                            </ul>
                        </div>
                    </div>
                </form>
            </div>

            <?php } else { ?>
            <!-- BOX USERPANEL -->
            <div class="col-12 p-3 bg-part-title small px-5">
                <div class="row">
                    <h5 class="h5 col text-white w-100 small m-0 pt-1"><?php echo strtoupper($lang['user-panel']); ?></h5>
                </div>
            </div>

            <div class="col-12 bg-part-content text-center p-4 small">
                <div class="text-center">
                    <h5 class="h5 w-100 small m-0 pt-1 text-white">
                        <?php echo $lang['my-account']; ?>: <strong><?php echo htmlspecialchars(getAccountName($_SESSION['id'])); ?></strong>
                    </h5>
                </div>
                <div class="user_panel_buttons text-center">
                    <div class="list-group p-3">
                        <?php if($web_admin >= $jsondataPrivileges['news']) { ?>
                        <a href="<?php echo $site_url; ?>admin" class="list-group-item list-group-item-action text-bluegray fw-normal">
                            <?php echo strtoupper($lang['administration']); ?>
                        </a>
                        <?php } ?>
                        <?php
                        $donate_list = json_decode(file_get_contents('include/db/donate.json'), true);
                        if(count($donate_list)) {
                        ?>
                        <a href="<?php echo $site_url; ?>user/donate" class="list-group-item list-group-item-action text-bluegray fw-normal">
                            <?php echo strtoupper($lang['donate']); ?>
                        </a>
                        <?php } ?>
                        <a href="<?php echo $site_url; ?>user/administration" class="list-group-item list-group-item-action text-bluegray fw-normal">
                            <?php echo strtoupper($lang['account-data']); ?>
                        </a>
                        <a href="<?php echo $site_url; ?>users/logout" class="list-group-item list-group-item-action text-bluegray fw-normal">
                            <?php echo strtoupper($lang['logout']); ?>
                        </a>
                    </div>
                </div>
            </div>
            <?php } ?>


            <!-- PLAYERS RANK -->
            <div class="col-12 p-3 mt-4 bg-part-title small px-5">
                <div class="row">
                    <h5 class="h5 col text-white w-100 small m-0 pt-1"><?php echo strtoupper($lang['players']); ?> RANK</h5>
                </div>
                <div class="col-12"></div>
            </div>
            <div class="col-12 bg-part-content text-center py-4 small">
                <table class="table table-sm table-borderless table-striped small text-left">
                    <thead>
                        <tr>
                            <th scope="col" class="pb-3 fw-normal px-3">#</th>
                            <th scope="col" class="pb-3 fw-normal"><?php echo $lang['name']; ?></th>
                            <th scope="col" class="pb-3 fw-normal"><?php echo $lang['empire']; ?></th>
                            <th scope="col" class="pb-3 fw-normal"><?php echo $lang['level']; ?></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        if(!$offline) {
                            // Query direct cu level - topPlayers() nu include level
                            $stmt_top = $database->runQueryPlayer(
                                "SELECT id, name, account_id, level FROM player 
                                 WHERE name NOT LIKE '[%]%' 
                                 ORDER BY level DESC, exp DESC, playtime DESC, name ASC 
                                 LIMIT 10"
                            );
                            $stmt_top->execute();
                            $top_players = $stmt_top->fetchAll(PDO::FETCH_ASSOC);
                            $i = 1;
                            foreach($top_players as $player) {
                                $emp = get_player_empire($player['account_id']);
                        ?>
                        <tr>
                            <th scope="row" class="text-bluegray fw-normal px-3"><?php echo $i++; ?></th>
                            <td><?php echo htmlspecialchars($player['name']); ?></td>
                            <td>
                                <img src="<?php echo $site_url; ?>images/empire/<?php echo $emp; ?>.jpg"
                                     alt="<?php echo empire_name($emp); ?>" width="20">
                            </td>
                            <td class="text-gold">Lv. <?php echo $player['level']; ?></td>
                        </tr>
                        <?php
                            }
                        } else {
                            // Offline: afisam top10 din backup JSON
                            echo $offline_players;
                        }
                        ?>
                    </tbody>
                </table>
            </div>

        </section>


        <!-- ===== COLOANA CENTRU: Carousel + Continut pagina ===== -->
        <section class="col-11 col-sm-10 mx-auto col-xl-6 order-xl-2" style="overflow:hidden;">

            <!-- Carousel ultimele 3 stiri (doar daca are stiri) -->
            <?php
            $carousel_news = [];
            if(!$offline) {
                // news e in SQLite - folosim runQuerySqlite
                $stmt_car = $database->runQuerySqlite(
                    "SELECT id, title, time FROM news ORDER BY id DESC LIMIT 3"
                );
                $stmt_car->execute();
                $carousel_news = $stmt_car->fetchAll(PDO::FETCH_ASSOC);
            }
            if(count($carousel_news) > 0) {
            ?>
            <div id="carouselExampleControls" class="carousel slide d-none d-md-block" data-bs-ride="carousel">
                <div class="carousel-inner">
                    <?php $first = true; foreach($carousel_news as $cn) { ?>
                    <div class="carousel-item <?php echo $first ? 'active' : ''; ?>">
                        <img src="<?php echo $site_url; ?>assets/img/news-placeholder.png" class="d-block w-100" alt="">
                        <div class="position-absolute img-news-desc">
                            <p class="text-green m-0 px-2 px-md-4 pb-0"><?php echo htmlspecialchars($cn['time']); ?></p>
                            <h5 class="text-white m-0 px-2 px-md-4 pb-5"><?php echo htmlspecialchars($cn['title']); ?></h5>
                        </div>
                    </div>
                    <?php $first = false; } ?>
                </div>
                <button class="carousel-control-prev" type="button"
                    data-bs-target="#carouselExampleControls" data-bs-slide="prev">
                    <img src="<?php echo $site_url; ?>assets/img/arrow-left.png" alt="prev">
                </button>
                <button class="carousel-control-next" type="button"
                    data-bs-target="#carouselExampleControls" data-bs-slide="next">
                    <img src="<?php echo $site_url; ?>assets/img/arrow-right.png" alt="next">
                </button>
            </div>
            <?php } ?>

            <!-- Continut pagina curent (news, register, ranking, etc.) -->
            <div class="col-12 bg-news mt-4 text-white position-relative" style="overflow:hidden; min-height:100px;">
                <?php include 'pages/'.$page.'.php'; ?>
            </div>

        </section>


        <!-- ===== COLOANA DREAPTA: Download + Statistici + Guilds Rank ===== -->
        <section class="mt-4 mt-xl-0 col-11 col-sm-10 mx-auto col-md-6 col-xl-3 order-xl-3">

            <!-- BOX REGISTER (afisat pe pagina de register) -->
            <?php if($page == 'register') { ?>
            <div class="col-12 p-3 bg-part-title small px-5 mb-0">
                <div class="row">
                    <h5 class="h5 col text-white w-100 small m-0 pt-1"><?php echo strtoupper($lang['register']); ?></h5>
                </div>
                <div class="col-12"></div>
            </div>
            <div class="col-12 bg-part-content text-center p-4 small">
                <p class="text-muted small mb-3"><?php echo isset($lang['register-info']) ? $lang['register-info'] : 'Create your account and join the adventure!'; ?></p>
                <ul class="list-unstyled text-start">
                    <li class="mb-2"><a href="<?php echo $site_url; ?>users/login" class="text-gold">&#8226; <?php echo $lang['login']; ?></a></li>
                    <li class="mb-2"><a href="<?php echo $site_url; ?>users/lost" class="text-gold">&#8226; <?php echo $lang['forget-password']; ?></a></li>
                </ul>
            </div>
            <?php } ?>

            <!-- Buton Download -->
            <a href="<?php echo $site_url; ?>download">
                <div class="col-12 side-btn side-btn-presentation d-flex flex-column justify-content-center align-items-center">
                    <span class="title"><?php echo strtoupper($lang['download']); ?></span>
                </div>
            </a>

            <!-- Statistici server (doar daca sunt activate) -->
            <?php if(!$offline && $statistics) { ?>
            <div class="col-12 p-3 bg-part-title small px-5">
                <div class="row">
                    <h5 class="h5 col text-nowrap text-white w-100 small m-0 pt-1">
                        <?php echo strtoupper($lang['statistics']); ?>
                    </h5>
                </div>
            </div>
            <div class="col-12 bg-part-content text-center py-4 px-3 small">
                <table class="table table-sm table-borderless text-white small text-left">
                    <tbody>
                        <?php
                        // Afisam doar statisticile activate (valoare != 0) din functions.json
                        foreach($jsondataFunctions as $key => $status) {
                            // Excludem flagurile interne
                            if($key == 'active-registrations' || $key == 'players-debug' || $key == 'active-referrals') continue;
                            if(!$status) continue;
                        ?>
                        <tr>
                            <td class="stats-text bg-table-list-name py-2">
                                <?php echo $lang[$key]; ?>:
                                <span class="text-gold"><?php echo number_format(getStatistics($key), 0, '', '.'); ?></span>
                            </td>
                        </tr>
                        <?php } ?>
                    </tbody>
                </table>
            </div>
            <?php } ?>


            <!-- GUILDS RANK -->
            <div class="col-12 p-3 mt-4 bg-part-title small px-5">
                <div class="row">
                    <h5 class="h5 col text-white w-100 small m-0 pt-1">GUILDS RANK</h5>
                </div>
                <div class="col-12"></div>
            </div>
            <div class="col-12 bg-part-content text-center py-4 small">
                <table class="table table-sm table-striped table-borderless small text-left">
                    <thead>
                        <tr>
                            <th scope="col" class="text-gray pb-3 fw-normal px-3">#</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['leader']; ?></th>
                            <th scope="col" class="text-gray pb-3 fw-normal"><?php echo $lang['points']; ?></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        if(!$offline) {
                            // Query direct cu ladder_point - topGuilds() nu include acest camp
                            $stmt_guilds = $database->runQueryPlayer(
                                "SELECT name, master, ladder_point FROM guild 
                                 WHERE name NOT LIKE '[%]%' 
                                 ORDER BY level DESC, ladder_point DESC, exp DESC, name ASC 
                                 LIMIT 10"
                            );
                            $stmt_guilds->execute();
                            $top_guilds = $stmt_guilds->fetchAll(PDO::FETCH_ASSOC);
                            $i = 1;
                            foreach($top_guilds as $guild) {
                        ?>
                        <tr>
                            <th scope="row" class="fw-normal px-3"><?php echo $i++; ?></th>
                            <td><?php echo htmlspecialchars($guild['name']); ?></td>
                            <td class="text-green"><?php echo htmlspecialchars($guild['master']); ?></td>
                            <td class="text-gold"><?php echo number_format($guild['ladder_point'], 0, '', '.'); ?></td>
                        </tr>
                        <?php
                            }
                        } else {
                            echo $offline_guilds;
                        }
                        ?>
                    </tbody>
                </table>
            </div>

        </section>

    </div>
</main>


<!-- ===================== FOOTER NAV ===================== -->
<section class="side-footer pt-4 pb-5">
    <div class="justify-content-md-center w-100" id="navbarNav">
        <ul class="navbar-nav text-center small row w-100 mx-auto main-max-w list-group-horizontal-md">
            <li class="nav-item col">
                <a class="nav-link text-uppercase" href="<?php echo $site_url; ?>"><?php echo $lang['news']; ?></a>
            </li>
            <?php if(!$database->is_loggedin()) { ?>
            <li class="nav-item col">
                <a class="nav-link text-uppercase" href="<?php echo $site_url; ?>users/register"><?php echo $lang['register']; ?></a>
            </li>
            <?php } ?>
            <li class="nav-item col">
                <a class="nav-link text-uppercase" href="<?php echo $site_url; ?>download"><?php echo $lang['download']; ?></a>
            </li>
            <li class="nav-item col dropdown">
                <a class="nav-link text-uppercase dropdown-toggle" href="#" role="button"
                    data-bs-toggle="dropdown" aria-expanded="false">
                    <?php echo $lang['ranking']; ?>
                </a>
                <ul class="dropdown-menu">
                    <li><a class="dropdown-item text-uppercase" href="<?php echo $site_url; ?>ranking/players"><?php echo $lang['players']; ?></a></li>
                    <li><a class="dropdown-item text-uppercase" href="<?php echo $site_url; ?>ranking/guilds"><?php echo $lang['guilds']; ?></a></li>
                </ul>
            </li>
            <?php if($forum) { ?>
            <li class="nav-item col">
                <a class="nav-link text-uppercase" href="<?php echo $forum; ?>" target="_blank">Forum</a>
            </li>
            <?php } ?>
        </ul>
    </div>
</section>


<!-- ===================== FOOTER ===================== -->
<footer class="d-flex justify-content-between px-3 bg-footer text-white small">

    <p>
        &copy; <?php
        $year = date('Y');
        echo ($year > 2017) ? '2017 - '.$year : $year;
        echo ' '.$site_title;
        ?> - All rights reserved.
    </p>

    <p style="margin-right:110px; display:flex; align-items:center;">
         by 
        <a href="https://discord.gg/pGZR8b37f4">
            <img src="<?php print $site_url;?>images/make.png" style="height:20px;" alt=".make">
        </a>
    </p>

</footer>