<?php include('inc/header.php'); ?>
<h1>Suche - Handelslogs</h1>
<div class="container">
    <div class="backend_button">
        <a href="javascript:history.back();">Gehe zurück</a>
    </div>
<?php

$resTradeGetTradeDatesAndIDs = odbc_exec($odbc_connect, '
    SELECT	a.[TradeID],
            date.[TradeDt]
    FROM	[' . $_CONFIG['db_databases']['log'] . '].[dbo].[tblTradeDetailLog] AS a
        LEFT JOIN [' . $_CONFIG['db_databases']['log'] . '].[dbo].[tblTradeLog] AS date ON date.[TradeID] = a.[TradeID]
    WHERE   [idPlayer] = \'' . mssql_escape_string($_GET['characterID']) . '\'
    GROUP BY a.[TradeID], date.[TradeDt]
    ORDER BY a.[TradeID] DESC
');
if(@odbc_num_rows($resTradeGetTradeDatesAndIDs) > 0) {
    while($arrayTradeDateAndID = @odbc_fetch_array($resTradeGetTradeDatesAndIDs)) {

        $strNameOfFirstTrader = odbc_result(odbc_exec($odbc_connect, '
            SELECT [m_szName]
            FROM [' . $_CONFIG['db_databases']['log'] . '].[dbo].[tblTradeDetailLog]
                LEFT JOIN [' . $_CONFIG['db_databases']['chr'] . '].dbo.[CHARACTER_TBL] on [idPlayer] = [m_idPlayer]
            WHERE [TradeID] = ' . $arrayTradeDateAndID['TradeID'] . '
            ORDER BY [m_szName] DESC
        '), 'm_szName');
        $strNameOfSecondTrader = odbc_result(odbc_exec($odbc_connect, '
            SELECT [m_szName]
            FROM [' . $_CONFIG['db_databases']['log'] . '].[dbo].[tblTradeDetailLog]
                LEFT JOIN [' . $_CONFIG['db_databases']['chr'] . '].dbo.[CHARACTER_TBL] on [idPlayer] = [m_idPlayer]
            WHERE [TradeID] = ' . $arrayTradeDateAndID['TradeID'] . '
            ORDER BY [m_szName] ASC
        '), 'm_szName');

        $resGetItemInformation = odbc_exec($odbc_connect, '
            SELECT	[m_szName],
                    [ItemIndex],
                    [ItemSerialNum],
                    [ItemCnt]

            FROM [' . $_CONFIG['db_databases']['log'] . '].[dbo].[tblTradeItemLog]
                LEFT JOIN [' . $_CONFIG['db_databases']['chr'] . '].dbo.[CHARACTER_TBL] on [idPlayer] = [m_idPlayer]

            WHERE [TradeID] = ' . $arrayTradeDateAndID['TradeID'] . '
            ORDER BY [m_szName] DESC
        ');

        echo '
        <br />
        <div class="tradeLog" id="' . $arrayTradeDateAndID['TradeID'] . '">
            <p>
                <b>Handel #' . $arrayTradeDateAndID['TradeID'] . '
                zwischen <a href="search.php?search=' . $strNameOfFirstTrader . '&character=Charaktersuche">' . $strNameOfFirstTrader . '</a> und <a href="search.php?search=' . $strNameOfSecondTrader . '&character=Charaktersuche">' . $strNameOfSecondTrader . '</a>
                vom ' . date('d.m.Y, H:i', strtotime($arrayTradeDateAndID['TradeDt'])) . ' Uhr:</b>
            </p>';

        while($arrayItemInformation = odbc_fetch_array($resGetItemInformation)) {
            $strNameOfItem = odbc_result(odbc_exec($odbc_connect, 'SELECT [name] FROM [' . $_CONFIG['db_databases']['web'] . '].dbo.[LOG_ITEMS] WHERE [id] = ' . $arrayItemInformation['ItemIndex']), 'name');
            if(empty($strNameOfItem)) {
                $strNameOfItem = $arrayItemInformation['ItemIndex'];
            }

            echo '
            <p class="items">
                <a href="search.php?search=' . $arrayItemInformation['m_szName'] . '&character=Charaktersuche">' . $arrayItemInformation['m_szName'] . '</a>:
                ' . $arrayItemInformation['ItemCnt'] . 'x
                ' . $strNameOfItem . ' <i>(Item-ID: <a href="search_item.php?itemID=' . $arrayItemInformation['ItemSerialNum'] . '">' . $arrayItemInformation['ItemSerialNum'] . '</a>)</i>
            </p>
            ';
        }

        echo '
        </div>';
    }

}
else {
    echo '<div class="fail">Dieser Charakter hat noch nicht gehandelt.</div>';
}
?>
</div>
<?php include('inc/footer.php'); ?>