<?php include('inc/header.php'); ?>
<h1>Suche - Items</h1>
<div class="container">
    <div class="backend_button">
        <a href="javascript:history.back();">Gehe zurück</a>
    </div>
    Dieses Skript braucht eine gewisse Zeit, bis es alle Tabellen durchsucht hat. Gedulde dich einen Moment.
    <form name="search" method="get">
        <input type="text" name="itemID" placeholder="Eindeutige Item-ID" required="required" style="width: 253px; margin: 4px 0;" value="<?php echo $_GET['itemID'];?>"/>
        <input type="submit" name="startSearch" value="Suchen" />
    </form>
<?php
if(isset($_GET['itemID'])) {
    if(is_numeric($_GET['itemID'])) {

        $counter = 0;
        $resInventories = @odbc_exec($odbc_connect, '
            SELECT	[m_idPlayer]
            FROM	[' . $_CONFIG['db_databases']['chr'] . '].[dbo].[INVENTORY_TBL]
            WHERE   [m_Inventory] LIKE \'%,' . $_GET['itemID'] . ',%\'
        ');
        $resBags = @odbc_exec($odbc_connect, '
            SELECT	[idPlayer]
            FROM	[' . $_CONFIG['db_databases']['chr'] . '].[dbo].[tblPocket]
            WHERE   [szItem] LIKE \'%,' . $_GET['itemID'] . ',%\'
        ');
        $resBanks = @odbc_exec($odbc_connect, '
            SELECT	[m_idPlayer]
            FROM	[' . $_CONFIG['db_databases']['chr'] . '].[dbo].[BANK_TBL]
            WHERE   [m_Bank] LIKE \'%,' . $_GET['itemID'] . '%,\'
        ');
        $resGuildBanks = @odbc_exec($odbc_connect, '
            SELECT	g.[m_szGuild]
            FROM	[' . $_CONFIG['db_databases']['chr'] . '].[dbo].[GUILD_BANK_TBL] AS gb
                LEFT JOIN [' . $_CONFIG['db_databases']['chr'] . '].dbo.[GUILD_TBL] AS g ON gb.[m_idGuild] = g.[m_idGuild]
            WHERE   gb.[m_GuildBank] LIKE \'%,' . $_GET['itemID'] . ',%\'
        ');
        $resMails = @odbc_exec($odbc_connect, '
            SELECT	[idReceiver]
            FROM	[' . $_CONFIG['db_databases']['chr'] . '].[dbo].[MAIL_TBL]
            WHERE	[ReadDt] IS NULL
            WHERE   [dwSerialNumber] = ' . $_GET['itemID'] . '
        ');

        if(@odbc_num_rows($resInventories) > 0) {
            echo '
            <div>
                <p><b></u>Im Inventar von</u>:</b></p>
                <ul>';
            while($strArray = @odbc_fetch_array($resInventories)) {
                echo '<li><a href="search.php?search=' . getUserName($strArray['m_idPlayer']) . '&character">' . getUserName($strArray['m_idPlayer']) . '</a></li>';
                $counter++;
            }
            echo '
                </ul>
            </div>';
        }
        if(@odbc_num_rows($resBags) > 0){
            echo '
            <div>
                <p><b></u>In einer der Itembags von</u>:</b></p>
                <ul>';
            while($strArray = @odbc_fetch_array($resBags)) {
                echo '<li><a href="search.php?search=' . getUserName($strArray['idPlayer']) . '&character">' . getUserName($strArray['idPlayer']) . '</a></li>';
                $counter++;
            }
            echo '
                </ul>
            </div>';
        }
        if(@odbc_num_rows($resBanks) > 0){
            echo '
            <div>
                <p><b></u>In einem Bankfach von</u>:</b></p>
                <ul>';
            while($strArray = @odbc_fetch_array($resBanks)) {
                echo '<li><a href="search.php?search=' . getUserName($strArray['m_idPlayer']) . '&character">' . getUserName($strArray['m_idPlayer']) . '</a></li>';
                $counter++;
            }
            echo '
                </ul>
            </div>';
        }
        if(@odbc_num_rows($resGuildBanks) > 0){
            echo '
            <div>
                <p><b></u>Im Gildenlager der Gilde</u>:</b></p>
                <ul>';
            while($strArray = @odbc_fetch_array($resGuildBanks)) {
                echo '<li>' . $strArray['m_szGuild'] . '</li>';
                $counter++;
            }
            echo '
                </ul>
            </div>';
        }
        if(@odbc_num_rows($resMails) > 0){
            echo '
            <div>
                <p><b></u>In den Ingamenachricht von</u>:</b></p>
                <ul>';
            while($strArray = @odbc_fetch_array($resMails)) {
                echo '<li><a href="search.php?search=' . getUserName($strArray['idReceiver']) . '&character">' . getUserName($strArray['idReceiver']) . '</a></li>';
                $counter++;
            }
            echo '
                </ul>
            </div>';
        }
        if($counter == 0) {
            echo '<div class="fail">Das Item wurde nicht gefunden. Durchsucht wurden: Inventare, Itembags, (Gilden-)Bänke und Ingamenachrichten.</div>';
        }
    }
    else {
        echo '<div class="fail">Keine gültige Item-ID</div>';
    }
}
?>
</div>
<?php include('inc/footer.php'); ?>