<?php
/**
 * @package     MefisAssetBridge
 * @subpackage  Joomla_Administrator_Component
 *
 * @copyright   Copyright (C) 2025 Goran Usljebrka. All rights reserved.
 * @license     Proprietary — No resale or incorporation into other projects without explicit written permission. see LICENSE.txt
 */

defined('_JEXEC') or die;

use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;

HTMLHelper::_('behavior.formvalidator');

?>

<div class="container-fluid">
    <div class="row mb-3">
        <div class="col-md-12">
            <h1><?php echo Text::_('COM_MEFISASSETBRIDGE_DASHBOARD_H1'); ?></h1>
        </div>
    </div>

    <div class="row">
        <div class="col-xxl-8">

            <h3><?php echo Text::_('COM_MEFISASSETBRIDGE_ACTIVE_API_KEYS'); ?></h3>

            <?php if (empty($this->activeTokens)) : ?>
                <div class="alert alert-info"><?php echo Text::_('COM_MEFISASSETBRIDGE_NO_ACTIVE_API_KEYS'); ?></div>
            <?php else : ?>
                <table class="table">
                    <thead>
                        <tr>
                            <th><?php echo Text::_('COM_MEFISASSETBRIDGE_API_KEY_NAME'); ?></th>
                            <th><?php echo Text::_('COM_MEFISASSETBRIDGE_STATUS'); ?></th>
                            <th><?php echo Text::_('COM_MEFISASSETBRIDGE_CREATED_AT'); ?></th>
                            <th><?php echo Text::_('COM_MEFISASSETBRIDGE_LAST_USED'); ?></th>
                            <th><?php echo Text::_('COM_MEFISASSETBRIDGE_DOMAINS'); ?></th>
                            <th><?php echo Text::_('COM_MEFISASSETBRIDGE_ACTIONS'); ?></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($this->activeTokens as $token) : ?>
                            <tr>
                                <td><?php echo htmlspecialchars($token['custom_name'] ?? ($token['token_id'] ?? ''), ENT_QUOTES, 'UTF-8'); ?></td>
                                <td><?php echo $token['status_label'] ?? ''; ?></td>
                                <td><?php echo htmlspecialchars($token['created_at'] ?? '', ENT_QUOTES, 'UTF-8'); ?></td>
                                <td><?php echo htmlspecialchars($token['last_used_at'] ?? '', ENT_QUOTES, 'UTF-8'); ?></td>
                                <td><?php echo htmlspecialchars($token['domains_list'] ?? '', ENT_QUOTES, 'UTF-8'); ?></td>
                                <td>
                                    <a class="btn btn-sm btn-danger" href="<?php echo $token['delete_link'] ?? '#'; ?>">
                                        <?php echo Text::_('COM_MEFISASSETBRIDGE_DELETE_API_KEY'); ?>
                                    </a>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            <?php endif; ?>
        </div>

        <div class="col-xxl-4">
            <h3><?php echo Text::_('COM_MEFISASSETBRIDGE_USER_INFO'); ?></h3>
            <table class="table">
                <tbody>
                    <tr>
                        <th><?php echo Text::_('COM_MEFISASSETBRIDGE_EMAIL'); ?></th>
                        <td><?php echo htmlspecialchars($this->userInfo['email'] ?? '', ENT_QUOTES, 'UTF-8'); ?></td>
                    </tr>
                    <tr>
                        <th><?php echo Text::_('COM_MEFISASSETBRIDGE_NAME'); ?></th>
                        <td><?php echo htmlspecialchars($this->userInfo['name'] ?? '', ENT_QUOTES, 'UTF-8'); ?></td>
                    </tr>
                    <tr>
                        <th><?php echo Text::_('COM_MEFISASSETBRIDGE_SUBSCRIPTION_LEVEL'); ?></th>
                        <td><?php echo $this->userInfo['level'] ?? ''; ?></td>
                    </tr>
                    <?php if (!empty($this->userInfo['subscription_date_label'])) : ?>
                    <tr>
                        <th><?php echo $this->userInfo['subscription_date_label']; ?></th>
                        <td><?php echo $this->userInfo['subscription_date_value']; ?></td>
                    </tr>
                    <?php endif; ?>
                    <tr>
                        <th><?php echo Text::_('COM_MEFISASSETBRIDGE_API_KEYS'); ?></th>
                        <td><?php echo $this->userInfo['api_keys_string'] ?? ''; ?></td>
                    </tr>
                    <tr>
                        <th><?php echo Text::_('COM_MEFISASSETBRIDGE_MAX_ASSETS_LABEL'); ?></th>
                        <td><?php echo $this->userInfo['max_assets_string'] ?? ''; ?></td>
                    </tr>
                    <?php if (($this->userInfo['level_name'] ?? 'free') === 'free') : ?>
                    <tr>
                        <th><?php echo Text::_('COM_MEFISASSETBRIDGE_SUBSCRIPTION_BUY_LABEL'); ?></th>
                        <td><a href="https://asset-bridge.ugoran.com/subscription" target="_blank"><?php echo Text::_('COM_MEFISASSETBRIDGE_SUBSCRIPTION_BUY_LINK_TEXT'); ?></a></td>
                    </tr>
                    <?php else : ?>
                    <tr>
                        <th><?php echo Text::_('COM_MEFISASSETBRIDGE_SUBSCRIPTION_MANAGE_LABEL'); ?></th>
                        <td><a href="https://asset-bridge.ugoran.com/dashboard" target="_blank"><?php echo Text::_('COM_MEFISASSETBRIDGE_SUBSCRIPTION_MANAGE_LINK_TEXT'); ?></a></td>
                    </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>
