<?php

namespace Nette\Bridges\DatabaseTracy;

use Nette,
	Tracy,
	Nette\Database\Helpers;

?>
<style class="tracy-debug">#tracy-debug td.nette-DbConnectionPanel-sql{background:white!important}#tracy-debug .nette-DbConnectionPanel-source{color:#BBB!important}</style>

<h1 title="<?= htmlSpecialChars($connection->getDsn(), ENT_QUOTES, 'UTF-8') ?>">Queries: <?php
	echo $count, ($totalTime ? sprintf(', time: %0.3f ms', $totalTime * 1000) : ''), ', ', htmlSpecialChars($name, ENT_NOQUOTES, 'UTF-8') ?></h1>

<div class="tracy-inner">
	<table>
		<tr><th>Time&nbsp;ms</th><th>SQL Query</th><th>Rows</th></tr>
		<?php
		foreach ($queries as $query):
			list($connection, $sql, $params, $source, $time, $rows, $error, $explain) = $query;
		?>
		<tr>
		<td>
		<?php if ($error): ?>
			<span title="<?= htmlSpecialChars($error, ENT_IGNORE | ENT_QUOTES, 'UTF-8') ?>">ERROR</span>
		<?php elseif ($time !== NULL): echo sprintf('%0.3f', $time * 1000); endif ?>
		<?php if ($explain): ?>
			<br /><a class="tracy-toggle tracy-collapsed" data-tracy-ref="^tr .nette-DbConnectionPanel-explain">explain</a>
		<?php endif ?>
        </td>
        <td class="nette-DbConnectionPanel-sql"><?= Helpers::dumpSql($sql, $params, $connection) ?>
		<?php if ($explain): ?>
			<table class="tracy-collapsed nette-DbConnectionPanel-explain">
			<tr>
			<?php foreach ($explain[0] as $col => $foo): ?>
				<th><?= htmlSpecialChars($col, ENT_NOQUOTES, 'UTF-8') ?></th>
			<?php endforeach ?>
			</tr>
			<?php foreach ($explain as $row): ?>
			<tr>
			<?php foreach ($row as $col): ?>
				<td><?= htmlSpecialChars($col, ENT_NOQUOTES, 'UTF-8') ?></td>
			<?php endforeach ?>
			</tr>
			<?php endforeach ?>
			</table>
		<?php endif ?>
		<?php if ($source) echo substr_replace(Tracy\Helpers::editorLink($source[0], $source[1]), ' class="nette-DbConnectionPanel-source"', 2, 0); ?>
		</td>
		<td><?= $rows ?></td>
		</tr>
		<?php endforeach ?>
	</table>
	<?php if (count($queries) < $count): ?><p>...and more</p><?php endif ?>
</div>
