<?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;
use Joomla\CMS\Router\Route;
?>

<h3><?php echo Text::_('COM_MEFISASSETBRIDGE_AUTH_REGISTER_TITLE'); ?></h3>
<p class="lead-"><?php echo Text::_('COM_MEFISASSETBRIDGE_AUTH_REGISTER_DESC'); ?></p>

<form action="<?php echo Route::_('index.php?option=com_mefisassetbridge&task=auth.register'); ?>" method="post" name="registerForm" id="register-form" class="form-validate">

    <div class="mb-3">
        <label for="name" class="form-label"><?php echo Text::_('COM_MEFISASSETBRIDGE_AUTH_NAME_LABEL'); ?></label>
        <input type="text" name="name" id="name" class="form-control required" required
               value="<?php echo $this->escape($this->app->input->get('name', '', 'raw')); ?>">
    </div>

    <div class="mb-3">
        <label for="register_email" class="form-label"><?php echo Text::_('COM_MEFISASSETBRIDGE_AUTH_EMAIL_LABEL'); ?></label>
        <input type="email" name="email" id="register_email" class="form-control required" required
               value="<?php echo $this->escape($this->app->input->get('email', '', 'raw')); ?>">
    </div>

    <div class="mb-3">
        <label for="register_password" class="form-label"><?php echo Text::_('COM_MEFISASSETBRIDGE_AUTH_PASSWORD_LABEL'); ?></label>
        <input type="password" name="password" id="register_password" class="form-control required" required minlength="8">
        <div class="form-text"><?php echo Text::_('COM_MEFISASSETBRIDGE_AUTH_PASSWORD_REQUIREMENTS'); ?></div>
    </div>

    <div class="mb-3">
        <label for="password_confirm" class="form-label"><?php echo Text::_('COM_MEFISASSETBRIDGE_AUTH_PASSWORD_CONFIRM_LABEL'); ?></label>
        <input type="password" name="password_confirm" id="password_confirm" class="form-control required" required equals="register_password">
    </div>

    <div class="mb-3 form-check">
        <input type="checkbox" class="form-check-input required" name="terms" id="terms" required>
        <label class="form-check-label" for="terms"><?php echo Text::sprintf('COM_MEFISASSETBRIDGE_AUTH_TERMS_LABEL', '<a href="https://asset-bridge.ugoran.com/page/terms" target="_blank">', '</a>'); ?></label>
    </div>

    <div class="mb-3">
        <button type="submit" class="btn btn-primary">
            <?php echo Text::_('COM_MEFISASSETBRIDGE_AUTH_REGISTER_BUTTON'); ?>
        </button>
    </div>

    <?php echo HTMLHelper::_('form.token'); ?>
</form>

<div class="mt-5">
    <p><?php echo Text::_('COM_MEFISASSETBRIDGE_AUTH_ALREADY_ACCOUNT'); ?>
    <button class="btn btn-link ps-0" id="switch-to-login"><?php echo Text::_('COM_MEFISASSETBRIDGE_AUTH_GO_TO_LOGIN'); ?></button></p>

</div>

<script>
document.getElementById('switch-to-login').addEventListener('click', function() {
    document.getElementById('login-tab').click();
});

// Client-side password confirmation validation
document.getElementById('register-form').addEventListener('submit', function(e) {
    const password = document.getElementById('register_password').value;
    const confirmPassword = document.getElementById('password_confirm').value;

    if (password !== confirmPassword) {
        e.preventDefault();
        Joomla.renderMessages({'error': ['<?php echo Text::_('COM_MEFISASSETBRIDGE_AUTH_PASSWORD_MISMATCH'); ?>']});
    }
});
</script>