<?php

/**
 * @package         MailChimp Auto-Subscribe
 * @version         5.0.3 Pro
 *
 * @author          Tassos Marinos <info@tassos.gr>
 * @link            http://www.tassos.gr
 * @copyright       Copyright © 2021 Tassos Marinos All Rights Reserved
 * @license         GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later
 */

// No direct access to this file
defined('_JEXEC') or die;

require_once JPATH_PLUGINS . '/system/nrframework/fields/nrtoggle.php';

class JFormFieldLogging extends JFormFieldNRToggle
{
    public function getInput()
    {
        $parent  = parent::getInput();
        $logfile = JPath::clean(JFactory::getConfig()->get('log_path') . '/mailchimpAutoSubscribe.php');

        JFactory::getDocument()->addStyleDeclaration('
            .ts_logfile_viewer {
                display:flex;
                display:-webkit-flex;
                align-items:center;
                -webkit-align-items:center;
                margin:0 -8px;
            }
            .ts_logfile_viewer > * {
                padding:0 8px;
            }
            .ts_logfile_viewer .nrtoggle {
                display:block;
            }
        ');

        $html = '
            <div class="ts_logfile_viewer">
                <div>' . $parent . '</div>
                <div style="opacity:.7";>' . $logfile . '</div>
        ';

        if (JFile::exists($logfile))
        {
            echo JHtml::_('bootstrap.renderModal', 'viewLog', array(
                'url'         => 'index.php?option=com_ajax&format=raw&plugin=TM_MailChimpUserAutoAdd&group=user&task=viewlog&' . JSession::getFormToken() . '=1',
                'title'       => \JText::sprintf('MAS_VIEWING_LOG', $logfile),
                'closeButton' => true,
                'height'      => '100%',
                'width'       => '100%',
                'modalWidth'  => '70',
                'bodyHeight'  => '70',
                'footer'      => '<a role="button" class="btn" data-dismiss="modal" aria-hidden="true">' . JText::_('JLIB_HTML_BEHAVIOR_CLOSE') . '</a>',
            ));

            $html .= '
                <div><button class="btn btn-small" data-toggle="modal" data-target="#viewLog">' . JText::_('MAS_VIEW_LOG_FILE') . '</button></div>
            ';
        } else 
        {
            $html .= JText::_('MAS_VIEW_NO_EVENTS_TRACKED');
        }

        return $html .= '</div>';
    }
}