<?php

declare(strict_types=1);

use app\models\Search;
use app\models\Settings;

if (isset($history) && $history) {
    // Отображение истории

    if (yii::$app->user->isGuest) {
        // Не аутентифицирован
    } else if ($rows = Search::searchByEdge(
        from: 'account',
        to: 'search',
        sort: ['DESC'],
        where: ['search.type' => 'order']
    )) {
        // История поиска существует

        preg_match_all('/UTC([\+\-0-9:]*)/', $account->zone ?? Settings::searchActive()['timezone_default'] ?? 'UTC+3', $timezone);
        $timezone = $timezone[1][0];

        foreach ($rows as $row) {
            // Инициализация
            $time = $row->jrnl;
            $date = empty($time) ? '' : (new DateTime())->setTimestamp(end($time)['date'])->setTimezone(new DateTimeZone($timezone))->format('d.m.y');

            echo <<<HTML
                <a class="dropdown-item d-flex button_white text-dark" href="/search?type=product&q=$row->text">$row->text<span class="ml-auto">$date</span></a>
            HTML;
        }
    }
}
?>
