<?php
declare(strict_types=1);

/**
 * @var \Magento\Framework\View\Element\Template $block
 * @var \Magento\Framework\Escaper $escaper
 * @var \ClawRock\Debug\Model\View\Profiler $viewModel
 * @var \ClawRock\Debug\Model\Collector\DatabaseCollector $collector
 */
$viewModel = $block->getViewModel();
$collector = $block->getCollector();
?>
<div style="float:left; width: 50%">
    <h2><?= $escaper->escapeHtml(__('Query metrics')) ?></h2>
    <div class="metrics">
        <div class="metric">
            <span class="value"><?= /* @noEscape */ $collector->getQueriesCount() ?></span>
            <span class="label"><?= $escaper->escapeHtml(__('Database queries')) ?></span>
        </div>
        <div class="metric">
            <span class="value"><?= count($collector->getDuplicatedQueries()); ?></span>
            <span class="label"><?= $escaper->escapeHtml(__('Duplicated queries')) ?></span>
        </div>
        <div class="metric">
            <span class="value"><?= /* @noEscape */ $collector->getTotalTime() ?> ms</span>
            <span class="label"><?= $escaper->escapeHtml(__('Query time')) ?></span>
        </div>
    </div>
</div>
<div style="clear: both;"></div>
<h2><?= $escaper->escapeHtml(__('Queries')) ?></h2>
<div class="sf-tabs">
    <div class="tab">
        <h3 class="tab-title">
            <?= $escaper->escapeHtml(__('All')) ?>
            <span class="badge"><?= /* @noEscape */ $collector->getQueriesCount() ?></span>
        </h3>
        <div class="tab-content">
            <?= /* @noEscape */ $viewModel->renderQueryList($collector->getAllQueries()); ?>
        </div>
    </div>
    <?php if (count($collector->getDuplicatedQueries())): ?>
        <div class="tab">
            <h3 class="tab-title">
                <?= $escaper->escapeHtml(__('Duplicated queries')) ?>
                <span class="badge"><?= count($collector->getDuplicatedQueries()); ?></span>
            </h3>
            <div class="tab-content">
                <?= /* @noEscape */ $viewModel->renderQueryList($collector->getDuplicatedQueries()); ?>
            </div>
        </div>
    <?php endif; ?>
    <?php if (count($collector->getSelectQueries())): ?>
        <div class="tab">
            <h3 class="tab-title">SELECT <span class="badge"><?= count($collector->getSelectQueries()); ?></span></h3>
            <div class="tab-content">
                <?= /* @noEscape */ $viewModel->renderQueryList($collector->getSelectQueries()); ?>
            </div>
        </div>
    <?php endif; ?>
    <?php if (count($collector->getInsertQueries())): ?>
        <div class="tab">
            <h3 class="tab-title">INSERT <span class="badge"><?= count($collector->getInsertQueries()); ?></span></h3>
            <div class="tab-content">
                <?= /* @noEscape */ $viewModel->renderQueryList($collector->getInsertQueries()); ?>
            </div>
        </div>
    <?php endif; ?>
    <?php if (count($collector->getUpdateQueries())): ?>
        <div class="tab">
            <h3 class="tab-title">UPDATE <span class="badge"><?= count($collector->getUpdateQueries()); ?></span></h3>
            <div class="tab-content">
                <?= /* @noEscape */ $viewModel->renderQueryList($collector->getUpdateQueries()); ?>
            </div>
        </div>
    <?php endif; ?>
    <?php if (count($collector->getDeleteQueries())): ?>
        <div class="tab">
            <h3 class="tab-title">DELETE <span class="badge"><?= count($collector->getDeleteQueries()); ?></span></h3>
            <div class="tab-content">
                <?= /* @noEscape */ $viewModel->renderQueryList($collector->getDeleteQueries()); ?>
            </div>
        </div>
    <?php endif; ?>
    <?php if (count($collector->getOtherQueries())): ?>
        <div class="tab">
            <h3 class="tab-title">OTHER <span class="badge"><?= count($collector->getOtherQueries()); ?></span></h3>
            <div class="tab-content">
                <?= /* @noEscape */ $viewModel->renderQueryList($collector->getOtherQueries()); ?>
            </div>
        </div>
    <?php endif; ?>
</div>
<style>
    tr.sf-toggle-content.sf-toggle-visible {
        display: table-row !important;
    }
    td > .context > strong {
        display: none;
    }
    .hidden {
        display: none;
    }
    .queries-table td, .queries-table th {
        vertical-align: top;
    }
    .queries-table td > div {
        margin-bottom: 6px;
    }
    .highlight pre {
        margin: 0;
        white-space: pre-wrap;
    }
    .highlight .keyword {
        color: #8959A8;
        font-weight: bold;
    }
    .highlight .word {
        color: #222222;
    }
    .highlight .variable {
        color: #916319;
    }
    .highlight .symbol {
        color: #222222;
    }
    .highlight .comment {
        color: #999999;
    }
    .highlight .backtick {
        color: #718C00;
    }
    .highlight .string {
        color: #718C00;
    }
    .highlight .number {
        color: #F5871F;
        font-weight: bold;
    }
    .highlight .error {
        color: #C82829;
    }
</style>
