<?php
/**
 * Copyright © Magefan (support@magefan.com). All rights reserved.
 * Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
 */

/**
 * @var $block \Magefan\SeoCrosslink\Block\Adminhtml\Stats
 * @var $escaper \Magento\Framework\Escaper
 */
?>
<style>
    /* palette shared with Magefan Page Cache Warmer; the amber is darkened to stay legible */
    .mfseo-stats { clear: both; margin: 14px 0 22px; background: #fff; border-radius: 8px;
        box-shadow: 0 1px 3px rgba(16, 101, 101, .07), 0 0 0 1px rgba(0, 0, 0, .05); }
    .mfseo-stats .mfseo-cols { display: grid; grid-template-columns: 1fr 1fr 1.3fr; align-items: start; }
    .mfseo-stats .mfseo-col { padding: 22px 26px; border-left: 1px solid #f1f1f1; }
    .mfseo-stats .mfseo-col:first-child { border-left: 0; }
    .mfseo-stats .mfseo-eyebrow { font-size: 10.5px; font-weight: 600; letter-spacing: .11em;
        text-transform: uppercase; color: #9b9b9b; margin: 0 0 14px; }
    .mfseo-stats .mfseo-num { font-size: 38px; font-weight: 400; line-height: 1;
        letter-spacing: -.02em; color: #106565; }
    .mfseo-stats .mfseo-num small { font-size: 17px; color: #a9a9a9; }
    .mfseo-stats .mfseo-sub { font-size: 12px; color: #6a6a6a; margin: 9px 0 0; line-height: 1.7; }
    .mfseo-stats .mfseo-sub b { color: #303030; font-weight: 600; }

    .mfseo-stats .mfseo-flag { border-top: 1px solid #f1f1f1; margin-top: 16px; padding-top: 14px; }
    .mfseo-stats .mfseo-flag-h { font-size: 12px; font-weight: 600; color: #303030; margin-bottom: 7px; }
    .mfseo-stats .mfseo-flag-h i { font-style: normal; margin-right: 7px; }
    .mfseo-stats .mfseo-flag-h .crit { color: #ff6384; }
    .mfseo-stats .mfseo-flag-h .warn { color: #dda000; }
    .mfseo-stats .mfseo-flag-h .info { color: #9b9b9b; }
    .mfseo-stats .mfseo-flag ul { margin: 0; padding: 0; list-style: none; }
    .mfseo-stats .mfseo-flag li { font-size: 12px; line-height: 1.75; color: #6a6a6a;
        overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
    .mfseo-stats .mfseo-flag li a { color: #007bdb; }
    .mfseo-stats .mfseo-crit { color: #ff6384; font-weight: 600; }

    .mfseo-stats .mfseo-stack { display: flex; gap: 2px; height: 10px; margin: 14px 0 12px; }
    .mfseo-stats .mfseo-stack i { display: block; border-radius: 3px; }
    .mfseo-stats .mfseo-legend { margin: 0; padding: 0; list-style: none; }
    .mfseo-stats .mfseo-legend li { display: grid; grid-template-columns: 10px 1fr auto;
        gap: 9px; align-items: center; font-size: 12px; color: #6a6a6a; line-height: 2; }
    .mfseo-stats .mfseo-legend i { width: 10px; height: 10px; border-radius: 3px; }
    .mfseo-stats .mfseo-legend b { color: #303030; font-weight: 600; }

    .mfseo-stats .mfseo-bar { display: grid; grid-template-columns: minmax(90px, 45%) 1fr 26px;
        gap: 14px; align-items: center; line-height: 2.3; }
    .mfseo-stats .mfseo-bar .lbl { font-size: 12px; color: #303030;
        overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
    .mfseo-stats .mfseo-bar .trk { background: #f4f4f4; border-radius: 3px; height: 10px; }
    .mfseo-stats .mfseo-bar .b { display: block; height: 10px; border-radius: 3px; background: #4bc0c0; }
    .mfseo-stats .mfseo-bar .b.crit { background: #ff6384; }
    .mfseo-stats .mfseo-bar .val { font-size: 12px; color: #303030; text-align: right; font-weight: 600; }
    .mfseo-stats .mfseo-note { font-size: 11px; color: #9b9b9b; margin: 14px 0 0; }

    .mfseo-stats .mfseo-off { padding: 34px 26px; text-align: center; color: #6a6a6a; }
    .mfseo-stats .mfseo-off b { display: block; color: #303030; font-size: 14px; margin-bottom: 6px; }
    @media (max-width: 1100px) {
        .mfseo-stats .mfseo-cols { grid-template-columns: 1fr; }
        .mfseo-stats .mfseo-col { border-left: 0; border-top: 1px solid #f1f1f1; }
        .mfseo-stats .mfseo-col:first-child { border-top: 0; }
    }
</style>

<div class="mfseo-stats">
    <?php if (!$block->isHistoryEnabled()): ?>
        <div class="mfseo-off">
            <b><?= $escaper->escapeHtml(__('Cross Links History is off')) ?></b>
            <?= $escaper->escapeHtml(
                __('Turn it on in the extension configuration to start collecting where your links are placed.')
            ) ?>
            <div style="margin-top:12px;">
                <a href="<?= $escaper->escapeUrl($block->getConfigUrl()) ?>">
                    <?= $escaper->escapeHtml(__('Open settings')) ?>
                </a>
            </div>
        </div>
    <?php else:
        $stats = $block->getStats();
        $rules = $stats['rules'];
        $coverage = $stats['coverage'];
        $topPages = $stats['top_pages'];
        $blocks = $stats['blocks'];
        $threshold = $block->getOverLinkedThreshold();
        $maxLinks = $topPages ? (int)$topPages[0]['links'] : 0;
        $placements = (int)$coverage['placements'];
        $colors = ['#4bc0c0', '#dda000', '#36a2eb', '#9966ff'];
        ?>
        <div class="mfseo-cols">

            <div class="mfseo-col">
                <p class="mfseo-eyebrow"><?= $escaper->escapeHtml(__('Rules')) ?></p>
                <div class="mfseo-num">
                    <?= (int)$rules['applied'] ?><small> / <?= (int)$rules['total'] ?></small>
                </div>
                <p class="mfseo-sub"><?= $escaper->escapeHtml(__('active rules with recorded placements')) ?></p>

                <?php if ($rules['never']): ?>
                    <div class="mfseo-flag">
                        <div class="mfseo-flag-h"><i class="info">&#9675;</i>
                            <?= $escaper->escapeHtml(__('%1 with no recorded placements', count($rules['never']))) ?>
                        </div>
                        <ul>
                            <?php foreach ($rules['never'] as $rule): ?>
                                <li>
                                    <a href="<?= $escaper->escapeUrl($block->getRuleUrl((int)$rule['id'])) ?>"
                                       title="<?= $escaper->escapeHtmlAttr($rule['keyword']) ?>">
                                        <?= $escaper->escapeHtml($rule['keyword']) ?></a>
                                </li>
                            <?php endforeach; ?>
                        </ul>
                        <p class="mfseo-note">
                            <?= $escaper->escapeHtml(
                                __('Their pages may not have been visited yet, or the history was cleared.')
                            ) ?>
                        </p>
                    </div>
                <?php endif; ?>

                <?php if ($rules['broad']): ?>
                    <div class="mfseo-flag">
                        <div class="mfseo-flag-h"><i class="warn">&#9650;</i>
                            <?= $escaper->escapeHtml(__('%1 too broad', count($rules['broad']))) ?>
                        </div>
                        <ul>
                            <?php foreach ($rules['broad'] as $rule):
                                $share = __('%1 of %2 pages', $rule['pages'], $coverage['pages']); ?>
                                <li>
                                    <a href="<?= $escaper->escapeUrl($block->getRuleUrl((int)$rule['id'])) ?>"
                                       title="<?= $escaper->escapeHtmlAttr($rule['keyword']) ?>">
                                        <?= $escaper->escapeHtml($rule['keyword']) ?></a>
                                    &mdash; <?= $escaper->escapeHtml($share) ?>
                                </li>
                            <?php endforeach; ?>
                        </ul>
                        <p class="mfseo-note">
                            <?= $escaper->escapeHtml(__(
                                'On %1% or more of all pages with links. Narrow its keyword, blocks or categories.',
                                $block->getTooBroadPercent()
                            )) ?>
                        </p>
                    </div>
                <?php endif; ?>
            </div>

            <div class="mfseo-col">
                <p class="mfseo-eyebrow"><?= $escaper->escapeHtml(__('Coverage')) ?></p>
                <div class="mfseo-num"><?= (int)$placements ?></div>
                <p class="mfseo-sub">
                    <?= $escaper->escapeHtml(__('Placements')) ?>
                    &middot; <b><?= (int)$coverage['pages'] ?></b>
                    <?= $escaper->escapeHtml(__('Pages with links')) ?>
                    &middot; <b><?= $escaper->escapeHtml((string)$coverage['avg']) ?></b>
                    <?= $escaper->escapeHtml(__('Average per page')) ?>
                </p>

                <?php if ($coverage['over_linked']): ?>
                    <div class="mfseo-flag">
                        <div class="mfseo-flag-h">
                            <i class="crit">&#9650;</i>
                            <span class="mfseo-crit"><?= (int)$coverage['over_linked'] ?></span>
                            <?= $escaper->escapeHtml(__('Pages over %1 links', $threshold)) ?>
                        </div>
                    </div>
                <?php endif; ?>

                <?php if ($blocks): ?>
                    <div class="mfseo-flag">
                        <div class="mfseo-flag-h"><?= $escaper->escapeHtml(__('Where the links land')) ?></div>
                        <div class="mfseo-stack">
                            <?php foreach (array_slice($blocks, 0, 4) as $i => $row):
                                $share = $placements ? $row['links'] / $placements * 100 : 0; ?>
                                <i style="background:<?= $escaper->escapeHtmlAttr($colors[$i]) ?>;
                                    width:<?= $escaper->escapeHtmlAttr((string)round($share, 2)) ?>%"></i>
                            <?php endforeach; ?>
                        </div>
                        <ul class="mfseo-legend">
                            <?php foreach (array_slice($blocks, 0, 4) as $i => $row): ?>
                                <li>
                                    <i style="background:<?= $escaper->escapeHtmlAttr($colors[$i]) ?>"></i>
                                    <span><?= $escaper->escapeHtml($row['block'] ?: __('(no block)')) ?></span>
                                    <b><?= (int)$row['links'] ?></b>
                                </li>
                            <?php endforeach; ?>
                        </ul>
                    </div>
                <?php endif; ?>
            </div>

            <div class="mfseo-col">
                <p class="mfseo-eyebrow"><?= $escaper->escapeHtml(__('Most linked pages')) ?></p>
                <?php foreach ($topPages as $page):
                    $links = (int)$page['links'];
                    $width = (string)$block->getBarWidth($links, $maxLinks); ?>
                    <div class="mfseo-bar">
                        <span class="lbl" title="<?= $escaper->escapeHtmlAttr($page['page_url']) ?>">
                            <?= $escaper->escapeHtml($block->getPagePath($page['page_url'])) ?>
                        </span>
                        <span class="trk">
                            <span class="b <?= $links > $threshold ? 'crit' : '' ?>"
                                  style="width:<?= $escaper->escapeHtmlAttr($width) ?>%"></span>
                        </span>
                        <span class="val"><?= (int)$links ?></span>
                    </div>
                <?php endforeach; ?>
                <?php if (!$topPages): ?>
                    <p class="mfseo-note"><?= $escaper->escapeHtml(__('No links recorded yet.')) ?></p>
                <?php endif; ?>
                <p class="mfseo-note">
                    <?= $escaper->escapeHtml(
                        __('Red marks pages above the %1 link threshold. Based on visited pages only.', $threshold)
                    ) ?>
                </p>
            </div>

        </div>
    <?php endif; ?>
</div>
