<?php
/**
 * @package   Essentials YOOtheme Pro 2.2.16 build 0813.1608
 * @author    ZOOlanders https://www.zoolanders.com
 * @copyright Copyright (C) Joolanders, SL
 * @license   http://www.gnu.org/licenses/gpl.html GNU/GPL
 */

use function YOOtheme\app;
use YOOtheme\Metadata;

/** @var Metadata $metadata */
$metadata = app(Metadata::class);

$config = [
    'html5validation' => $node->form->config['html5validation'] ?? true,
    'reset_after_submit' => $node->form->config['reset_after_submit'] ?? true,
    'errors_display' => (object) array_filter([
        'modal' => $node->form->config['errors_display_modal'] ?? false,
        'modal_content' => $node->form->config['errors_display_modal_content'] ?? false,
        'modal_center' => $node->form->config['errors_display_modal_center'] ?? false,
    ]),
];

$form = $this->el('form', [
    'id' => $node->form->domId,

    'name' => $node->form->domName,

    'action' => $node->form->action,

    'method' => $node->form->method,

    'validate-action' => $node->form->validateAction,

    'data-uk-yooessentials-form' => json_encode(compact('config')),

    'data-yooessentials-formid' => $node->id,

    'novalidate' => true,

    'class' => ['uk-form', $node->form->domClass],
]);

$errors = $this->el(
    'div',
    [
        'class' => ['uk-text-danger', 'uk-text-small'],
        'data-yooessentials-form-errors' => true,
    ],
    ''
);

$scripts = array_filter($node->form->hooks ?? []);

if (isset($scripts['onsubmit'])) {
    $metadata->set(
        "script:yooessentials-form-{$node->id}-onsubmit",
        sprintf(
            "UIkit.util.on(document, 'yooessentials-form:submit', function (e, ctx) {
                var form = ctx.form;
                if (form.dataset.yooessentialsFormid === '%s') {
                    %s
                }
            });",
            $node->id,
            $scripts['onsubmit']
        ),
        [
            'defer' => true
        ]
    );
}

if (isset($scripts['onsubmitted'])) {
    $metadata->set(
        "script:yooessentials-form-{$node->id}-onsubmitted",
        sprintf(
            "UIkit.util.on(document, 'yooessentials-form:submitted', function (e, ctx) {
                var form = ctx.form;
                var data = ctx.data;
                var response = ctx.response;

                if (form.dataset.yooessentialsFormid === '%s') {
                    %s
                }
            });",
            $node->id,
            $scripts['onsubmitted']
        ),
        [
            'defer' => true
        ]
    );
}

if (isset($scripts['onvalidationerror'])) {
    $metadata->set(
        "script:yooessentials-form-{$node->id}-onvalidationerror",
        sprintf(
            "UIkit.util.on(document, 'yooessentials-form:validation-error', function (e, ctx) {
                var form = ctx.form;
                var data = ctx.data;

                if (form.dataset.yooessentialsFormid === '%s') {
                    %s
                }
            });",
            $node->id,
            $scripts['onvalidationerror']
        )
    );
}

if (isset($scripts['onsubmissionerror'])) {
    $metadata->set(
        "script:yooessentials-form-{$node->id}-onsubmissionerror",
        sprintf(
            "UIkit.util.on(document, 'yooessentials-form:submission-error', function (e, ctx) {
                var form = ctx.form;
                var data = ctx.data;
                var error = ctx.error;
                var errors = ctx.errors;
                var validation = ctx.validation;

                if (form.dataset.yooessentialsFormid === '%s') {
                    %s
                }
            });",
            $node->id,
            $scripts['onsubmissionerror']
        )
    );
}
?>

<?= $form($props, array_filter($attrs)) ?>
    <?= $builder->render($children) ?>
    <input type="hidden" name="formid" value="<?= $node->id ?>"/>
    <?= $node->form->csrf ?>
    <?= $errors() ?>
<?= $form->end() ?>
