<form method='post' action="index.php?module=tickettype" id="hm2">
    <input type="hidden" name="meldung" value="4">
</form>
<form method='post' action="index.php?module=tickettype&event=updateStandard" class="form-horizontal form-label-left">
    <div class="card panel-fcs-border shadow-sm">
        <div class="card-header">
            <div class="row">
                <div class="col-sm-6">
                    <span class="fcs-card-title h1">
                        [% $name %] [% $user->gettext('aktualisieren') %]
                    </span>
                </div>
                <div class="col-sm-6 text-end">
                    <a onclick="setStandard();" class="btn btn-outline-primary">[% $user->gettext('Aktualisieren')%]</a>
                    <a onclick="window.location.href = 'index.php?module=tickettype';" class="btn btn-outline-primary">[% $user->gettext('Zurück')%]</a>
                </div>
            </div>

        </div>
        <div class="card-body">
            <div class="col-sm-8 col-sm-offset-2">
                <div class="form-group row mb-3">
                    <label class="col-sm-3 control-label">
                        [% $user->gettext('Name') %]
                    </label>
                    <div class="col-sm-9">
                        <input type="text" class="form-control" name="name" id="name" value="[% $name %]"/>
                    </div>
                </div>
                <div class="form-group row mb-3">
                    <label class="col-sm-3 control-label">
                        [% $user->gettext('Icon') %]
                    </label>
                    <div class="col-sm-9">
                        <select name="icon" id="icon" class="form-control" style="max-width: 55px;">

                        </select>
                    </div>
                </div>
            </div>

        </div>
    </div>
</form>

<script>
    $.get('index.php?module=tickettype&event=getIconsAsJSONSetting', function (data) {
    [% if $id == 1 %]
        $('#icon').select2({theme: 'bootstrap-5',
            data: JSON.parse(data),
            templateResult: formatIcon,
            templateSelection: formatIcon
        });

        $('#icon').val("[% $smarty.const.STANDARD_ICON %]").trigger('change');
    [% else %]
        $('#icon').select2({theme: 'bootstrap-5',
            data: JSON.parse(data),
            templateResult: formatIcon,
            templateSelection: formatIcon
        });
        $('#icon').val("[% $smarty.const.WF_ICON %]").trigger('change');
    [% /if %]
    })

    function formatIcon(icon) {
        if (!icon.id || icon.id == 0) {

            return icon.text;

        }
        var $icon = $(
                '<span><img src="index.php?module=upload&class=download&event=ShowImage&name=' + icon.path + '" style="max-height:16px;"  /> ' + icon.text + '</span>'
                );

        return $icon;
    }
    ;

    function setStandard() {
        if ($('#name').val().trim() == '') {
            noty({
                text: "[% $user->gettext('Tickettyp muss ein Name haben.') %]",
                type: 'error',
                timeout: notifyTimeout
            });
            return;
        }
        $.post('index.php?module=settings&event=setStandard', {type:[% $id %], name: $('#name').val(), icon: $('#icon').val()}, function (data) {
            $("#hm2").submit();
        })
    }
</script>
