<?php

namespace WHMCS\Module\Addon\OnestreamReseller;

use WHMCS\Module\Addon\OnestreamReseller\Config;

class AdminArea
{
    public function handle($action, $vars)
    {
        if (empty($action) || $action == 'index') {
            return $this->index($vars);
        } else if ($action == 'show') {
            return $this->show($vars);
        }

        return 'NOT FOUND: ' . json_encode($vars);
    }

    public function index($vars)
    {
        $modulelink = $vars['modulelink'];
        $version = $vars['version'];
        
        $panels = Config::getConfiguredPanels();

        $html = <<<EOF
<p>The currently installed version is: <strong>{$version}</strong></p>

<h4>Configured Panels</h4>

{{PANELS}}

EOF;

// <p>
//     <a href="{$modulelink}&action=show" class="btn btn-success">
//         <i class="fa fa-check"></i>
//         Visit valid action link
//     </a>
//     <a href="{$modulelink}&action=invalid" class="btn btn-default">
//         <i class="fa fa-times"></i>
//         Visit invalid action link
//     </a>
// </p>

        $panelsHtml = '<table class="table">';

        $panelsHtml .= '<tr>';
        $panelsHtml .= '<th></th>';
        $panelsHtml .= '<th>Config</th>';
        $panelsHtml .= '</tr>';

        foreach ($panels as $index => $values) {
            if (empty($values['panel_url'])) {
                continue;
            }
            $panelsHtml .= '<tr>';
            $panelsHtml .= '<td>';
            $panelsHtml .= 'Panel ' . $index . '<br>' . htmlentities($values['panel_url']);
            $panelsHtml .= '</td>';
            $panelsHtml .= '<td>';
            $panelsHtml .= '<p>Api Token: ' . htmlentities($values['api_token']) . '</p>';
            $panelsHtml .= '<p>User Token: ' . htmlentities($values['user_token']) . '</p>';
            $panelsHtml .= '</td>';
            $panelsHtml .= '</tr>';
        }

        $panelsHtml .= '</table>';

        $html = str_replace('{{PANELS}}', $panelsHtml, $html);

        return $html;
    }

    public function show($vars)
    {
        return 'SHOW: ' . json_encode($vars);
    }
}