<?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\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;

?>
<form action="index.php?option=com_tassoscodesnippets&view=snippets"
      method="post"
      enctype="multipart/form-data"
      name="adminForm"
      id="adminForm"
      onsubmit="return submitform();">

    <div class="row">
        <div class="col-md-6">
            <div class="card">
                <div class="card-body">
                    <p><?php echo Text::_('COM_TASSOSCODESNIPPETS_IMPORT_DESC'); ?></p>

                    <div class="mb-3">
                        <label for="import-file" class="form-label">
                            <?php echo Text::_('COM_TASSOSCODESNIPPETS_IMPORT_FILE_LABEL'); ?>
                        </label>
                        <input type="file"
                               class="form-control"
                               name="file"
                               id="import-file"
                               accept=".tcs" />
                        <div class="form-text">
                            <?php echo Text::_('COM_TASSOSCODESNIPPETS_IMPORT_FILE_DESC'); ?>
                        </div>
                    </div>

                    <button type="submit" class="btn btn-primary">
                        <span class="icon-upload" aria-hidden="true"></span>
                        <?php echo Text::_('COM_TASSOSCODESNIPPETS_IMPORT'); ?>
                    </button>
                </div>
            </div>
        </div>
    </div>

    <input type="hidden" name="task" value="snippets.import" />
    <?php echo HTMLHelper::_('form.token'); ?>
</form>

<script>
function submitform() {
    var fileInput = document.getElementById('import-file');

    if (!fileInput || !fileInput.value) {
        alert('<?php echo Text::_('COM_TASSOSCODESNIPPETS_IMPORT_ERROR_NO_FILE', true); ?>');
        return false;
    }

    var filename = fileInput.value;
    var ext      = filename.substr(filename.lastIndexOf('.')).toLowerCase();

    if (ext !== '.tcs') {
        alert('<?php echo Text::_('COM_TASSOSCODESNIPPETS_IMPORT_ERROR_INVALID_FILE', true); ?>');
        return false;
    }

    return true;
}
</script>
