<?php

/**
 * @package         Tassos Code Snippets
 * @version         1.0.1-RC2 Free
 *
 * @author          Tassos Marinos <info@tassos.gr>
 * @link            https://www.tassos.gr
 * @copyright       Copyright © 2026 Tassos Marinos All Rights Reserved
 * @license         GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later
 */

defined('_JEXEC') or die;

use Joomla\CMS\Factory;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Router\Route;

/** @var \Tassos\CodeSnippets\Administrator\View\Logs\HtmlView $this */

$app   = Factory::getApplication();
$token = $app->getFormToken();
?>

<div class="row">
    <div class="col-md-12">
        <div id="j-main-container" class="j-main-container">

            <?php if (empty($this->logFiles)) : ?>
                <div class="alert alert-info">
                    <span class="icon-info-circle" aria-hidden="true"></span>
                    <?php echo Text::_('COM_TASSOSCODESNIPPETS_LOGS_NO_LOGS'); ?>
                </div>
            <?php else : ?>

                <!-- File selector and Clear button -->
                <div class="d-flex align-items-center gap-3 mb-3">
                    <form method="get" action="<?php echo Route::_('index.php'); ?>" class="d-flex align-items-center gap-2 flex-grow-1">
                        <input type="hidden" name="option" value="com_tassoscodesnippets">
                        <input type="hidden" name="view" value="logs">

                        <label for="logfile-select" class="fw-bold text-nowrap">
                            <?php echo Text::_('COM_TASSOSCODESNIPPETS_LOGS_SELECT_FILE'); ?>:
                        </label>

                        <select id="logfile-select"
                                name="logfile"
                                class="form-select"
                                style="max-width:420px;"
                                onchange="this.form.submit()">
                            <?php foreach ($this->logFiles as $logFilePath) :
                                $basename = basename($logFilePath);
                                // Derive a human-readable label from the filename (snippet_[alias]_[id].log → alias)
                                $alias = preg_replace('/^snippet_/', '', $basename);
                                $alias = preg_replace('/_\d+\.log$/', '', $alias);
                                $modified = date('Y-m-d H:i', filemtime($logFilePath));
                            ?>
                                <option value="<?php echo $this->escape($basename); ?>"
                                    <?php echo ($basename === $this->selectedFile) ? 'selected' : ''; ?>>
                                    <?php echo $this->escape($alias); ?> — <?php echo $modified; ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </form>

                    <?php if ($this->selectedFile) : ?>
                        <form method="post"
                              action="<?php echo Route::_('index.php?option=com_tassoscodesnippets&task=logs.clear'); ?>">
                            <input type="hidden" name="logfile" value="<?php echo $this->escape($this->selectedFile); ?>">
                            <?php echo HTMLHelper::_('form.token'); ?>
                            <button type="submit"
                                    class="btn btn-danger"
                                    onclick="return confirm('<?php echo Text::_('JGLOBAL_CONFIRM_DELETE'); ?>')">
                                <span class="icon-trash" aria-hidden="true"></span>
                                <?php echo Text::_('COM_TASSOSCODESNIPPETS_LOGS_CLEAR'); ?>
                            </button>
                        </form>
                    <?php endif; ?>
                </div>

                <!-- Log content -->
                <?php if ($this->selectedFile && $this->logContent !== '') : ?>
                    <div class="card">
                        <div class="card-header bg-dark text-white d-flex justify-content-between align-items-center">
                            <code style="font-size:13px;"><?php echo $this->escape($this->selectedFile); ?></code>
                            <small class="text-light opacity-75"><?php echo Text::_('COM_TASSOSCODESNIPPETS_LOGS_TITLE'); ?></small>
                        </div>
                        <div class="card-body p-0">
                            <pre style="background:#1e1e1e;color:#d4d4d4;padding:16px;margin:0;font-size:12px;line-height:1.6;overflow:auto;max-height:72vh;white-space:pre-wrap;word-break:break-all;"><?php echo $this->escape($this->logContent); ?></pre>
                        </div>
                    </div>
                <?php elseif ($this->selectedFile) : ?>
                    <div class="alert alert-info">
                        <span class="icon-info-circle" aria-hidden="true"></span>
                        <?php echo Text::_('JGLOBAL_NO_MATCHING_RESULTS'); ?>
                    </div>
                <?php endif; ?>

            <?php endif; ?>

        </div>
    </div>
</div>
