<?php

use Core\Config;
use Core\Db;
use Model\EkrExchangeRates;

function getAlchemistsAsJsString(): string
{
    $result = '';
    $sql = 'select id, login, align, level, online, (select name from room where id = room) as room from users where align = 50 order by online desc, align';
    $rows = Db::getRows($sql);
    foreach ($rows as $row) {
        if ($row['online'] <= time() - 120) {
            $row['id'] = '';
            $row['level'] = '';
            $row['online'] = '';
        }
        // generate: w(login,id,level,online);
        $result .= "w({$row['login']},{$row['id']},{$row['level']},{$row['online']});";
    }
    return $result;
}

$exchangeToday = new EkrExchangeRates();
$alchemists = getAlchemistsAsJsString();

?>
<script src="/js/jquery.js"></script>
<script>
    let nlevel = 0;

    function w(login, id, level, online) {
        let s = '';
        const imagehost = '<?= Config::img() ?>';
        const alchemistAlign = 50;

        if (online) {
            s += '<a href="javascript:top.chat.addto(\'' + login + '\',\'private\');"><img src="' + imagehost + '/i/lock.gif" alt="Приватно"></a>';
            s += ' <img src="' + imagehost + '/i/align/align' + alchemistAlign + '.gif" alt="">';
            s += '<a href="javascript:top.chat.addto(\'' + login + '\',\'to\');">' + login + '</a>[' + level + ']' +
                '<a href="/info/' + id + '" target="_blank"><img src="' + imagehost + '/i/inf_capitalcity.gif" alt="Информация о персонаже"></a>';
            s += ' - ' + online;
        } else {
            s += '<img src="' + imagehost + '/i/offline.gif" alt="Нет в клубе">';
            s += ' <img src="' + imagehost + '/i/align/align' + alchemistAlign + '.gif" alt="">';

            if (nlevel === 0) {
                nlevel = 1;
                s = "<br>" + s;
            }
            s += '<span style="color:gray;">' +
                '<b>' + login + '</b>[' + level + ']<a href="/info/' + id + '" target="_blank"><img src="' + imagehost + '/i/inf.gif" alt="Информация о персонаже"></a> - Нет в клубе' +
                '</span>';

        }
        document.write(s + '<br>');
    }
</script>
<div id=hint4 class=ahint></div>
<table style="width: 100%;" role="none">
    <tr>
        <td style="width: 40%; vertical-align: top; "><br>
            <table style="width: 100%;" role="none">
                <TR>
                    <td style="text-align: center;"><h4>Алхимики</h4></td>
                </TR>
                <TR>
                    <td style="background-color: lavender; white-space: nowrap;">
                        <?php if (!empty($alchemists)): ?>
                            <script><?= getAlchemistsAsJsString() ?></script>
                        <?php else: ?>
                            <div style="text-align: center;">Нет ни одного назначенного Алхимика</div>
                        <?php endif; ?>
                    </td>
                <tr>
                    <td>
                        <div style="font-size: xx-small;">
                            Курс покупки ЕвроКредитов: <b>1</b> eкр. = <b><?= $exchangeToday->oneEkrInRUB() ?> </b>руб.<br>
                            Курс обмена ЕвроКредитов: <b>1</b> екр = <b><?= Config::EKR_TO_KR_EXCHANGE ?></b> кр.<br>
                            <?php if (Config::KR_TO_EKR_EXCHANGE > 0): ?>
                                Курс обмена Кредитов: <?= Config::KR_TO_EKR_EXCHANGE ?> кр = 1 екр.<br>
                            <?php endif; ?>
                            Продают еврокредиты и прочие платные услуги сервиса<BR>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                </tr>
            </table>

            <br>

            <table style="width: 100%; border: 0;">
                <caption><b>Курс еврокредита к мировой валюте</b></caption>
                <tr>
                    <th><small>Данные на <?= $exchangeToday->date() ?> без учета комиссий</small></th>
                </tr>
                <tr>
                    <td>
                        <span>1 екр. = </span>
                        <span style="display:inline-block;width:100px;font-weight: bold;"><?= $exchangeToday->oneEkrInUSD() ?></span>
                        <span>долларов США</span>
                    </td>
                </tr>
                <tr>
                    <td>
                        <span>1 екр. = </span>
                        <span style="display:inline-block;width:100px;font-weight: bold;"><?= $exchangeToday->oneEkrInRUB() ?></span>
                        <span>российских рублей</span>
                    </td>
                </tr>
            </table>
        </td>
        <td style="width: 5%; vertical-align: top; ">&nbsp;</td>
        <td style="width: 25%; vertical-align: top; text-align: right;">
            <button class="btn" onclick="location.href='/main.php?alh&rnd=<?= mt_rand() ?>'">Обновить</button>
            <button class="btn" onclick="location.href='/main.php'">Вернуться</button>
        </td>
    </tr>
</table>