[% if $tid || $tid === 0 %]
    <html>
        <head>
        <script>
            [% if $TRefID  && !$iframe %]
            function saveTRefID(tid) {

                [% if $tid == 0 %]
                if (typeof (self.parent.document.getElementById('TicketRefId')) == "object") {
                    self.parent.document.getElementById('TicketRefId').value = tid;
                }
                window.close();
                [% else %]
                document.getElementById('TRefID').value = tid;
                document.getElementById('save').value = 1;
                        var export = document.getElementById("Export");
                        if (export) export.value = "none";
                submitForm();
                [% /if %]
            }

            function closing() {
                self.parent.location.href = "index.php?module=ticket&event=show&id=[% $tid %]";
                self.parent.$.fancybox.close();
            }

            [% /if %]

        </script>
    </head>

    <body id="PopUpForm" >

    <div id="formLoad">
        <form action="[% $action %]" method="POST" name="hm" onsubmit="return false;" id="GeneratetForm[% $fid %]" class="form-label-left form-horizontal">

            <input type="hidden" name="tid" value="[% $tid %]" />
            <input type="hidden" name="fid" value="[% $fid %]" />
            [% if $editable %]<input type="hidden" name="action" value="store" />[% /if %]

            <input type="hidden" id="GeneratetForm[% $fid %]_required" name="required" value="[% $form.required %]" />

        [% else %]
            <form action="index.php?module=formwizard&event=viewFormular" class="form-horizontal form-label-left wizardForm" onsubmit="return false;"  id="GeneratetForm[% $fid %]">
                <script language="JavaScript" src="modules/formwizard/javascript/validation.js" type="text/JavaScript"></script>
                <link rel="stylesheet" type="text/css" href="styles/[% $CSSTEMPLATE %]/css/jquery-ui.css" media="screen" />
                <link rel="stylesheet" type="text/css" href="styles/[% $CSSTEMPLATE %]/css/jquery-ui-timepicker-addon.css" media="screen">
                <script type="text/javascript" src="includes/js/jquery-ui-timepicker-addon.js"></script>
                <div class="card panel-fcs-border shadow-sm" style="width: 600px;">
                    <div class="card-header">
                        <div class="row">
                            <div class="col-3">
                                <h3 class="card-title-fcs">[% $user->gettext('Vorschau') %]</h3>
                            </div>
                            <div class="col-9">
                                [% $user->gettext('Folgende Felder werden im Ticket angezeigt') %]
                            </div>
                        </div>
                    </div>
                    <div class="card-body">


            [% /if %]
            <!-- ###CONTENT### begin -->
    [% foreach from=$form.fields item=field name=fieldlist %]
        [% assign var=lfdnr value=$smarty.foreach.fieldlist.iteration %]

         [% assign var=FieldData value=$data[$field.sqlname] %]


        <div class="form-group row mb-3 [% if $field.required && $field.required == "Y" %]required[% /if %]">
            [% if $field.type == "seperator" %]
                <hr>
            [% else %]
                <label class="control-label col-lg-4">
                    [% $field.title %]
                </label>
                <div class="col-lg-8">
                    [% if $field.type == "text" %]
                        <input type="[% if $field.password == "Y" %]password[% else %]text[% /if %]" id="field_[% $field.name %]" name="[% $field.name %]" value="[% $data[$field.sqlname] %]" class="form-control" [% if $field.required && $field.required=="Y"%]required="true"[% /if %][% if !$editable && $tid > 0 %]readonly="readonly"[% /if %] />
                    [% /if %]
                    [% if $field.type == "textarea" %]
                        <textarea name="[% $field.name %]" id="field_[% $field.name %]"rows="[% $field.max %]" class="form-control" [% if !$editable && $tid > 0 %]readonly="readonly"[% /if %]>[% $data[$field.sqlname] %]</textarea>
                    [% /if %]
                    [% if $field.type == "date" %]
                        <input type="text" name="[% $field.name %]" value="[% $data[$field.sqlname] %]"  id="field_[% $field.name %]" class="form-control [% if $editable %]datepicker[% /if %]" [% if !$editable && $tid > 0 %]readonly="readonly"[% /if %] />
                    [% /if %]
                    [% if $field.type == "datetime" %]
                        <input type="text" name="[% $field.name %]" value="[% $data[$field.sqlname] %]" id="field_[% $field.name %]" class="form-control [% if $editable %]datetimepicker[% /if %]" [% if !$editable && $tid > 0 %]readonly="readonly"[% /if %] />
                    [% /if %]
                    [% if $field.type == "integer" %]
                        <input type="text" name="[% $field.name %]" id="field_[% $field.name %]" value="[% $data[$field.sqlname] %]" class="form-control" [% if !$editable && $tid > 0 %]readonly="readonly"[% /if %] />
                    [% /if %]
                    [% if $field.type == "checkbox" || $field.type == "checklist" %]
                        [% foreach from=$field.ele item=ele name=checklist %]
                            <label><input type="checkbox" name="[% $field.name %][]" value="[% $ele %]" [% if $Tools->FCS_InArray($ele, $data[$field.sqlname] ) %]checked="checked"[% /if %]  /> &nbsp;[% $ele %]</label><br>
                        [% /foreach %]
                    [% /if %]
                    [% if $field.type == "radio" %]
                        [% foreach from=$field.ele item=ele name=checklist %]
                           <label for="[% $field.name %]"><input type="radio" name="[% $field.name %]" value="[% $ele %]" [% if $ele == $data[$field.sqlname] %]checked="checked"[% /if %] />&nbsp;[% $ele %]</label><br />
                        [% /foreach %]
                    [% /if %]
                    [% if $field.type == "select" %]
                        <select name="[% $field.name %]" id="field_[% $field.name %]" class="form-control" [% if !$editable && $tid > 0 %]readonly="readonly"[% /if %]>
                            [% foreach from=$field.ele item=ele %]
                                [% html_options values=$ele output=$ele selected=$data[$field.sqlname] %]
                            [% /foreach %]
                        </select>
                    [% /if %]
                    [% if $field.type == "notice" %]
                        <p class="form-control-static">[% $field.descr|nl2br %]</p>
                    [% /if %]

                [% if $field.type == "userdata" %]
                    <input type="hidden" name="[% $field.name %][userid]" value="[% $user->UserID %]" />
                    <ul class="list-unstyled">
                        [% foreach from=$field.userdata item=Userdata key=Dataname %]
                            <li class="option">[% $user->gettext($Dataname) %]: <strong>[% $data[$field.sqlname][$Dataname] %]</strong></li>
                            [% /foreach %]
                    </ul>
                [% /if %]


                [% if $field.type == "contactdata" %]
                    <input type="hidden" name="[% $field.name %][contactid]" />
                    <ul class="list-unstyled">
                        [% foreach from=$field.contactdata item=Contactdata key=Dataname %]
                            <li class="option">[% $user->gettext($Dataname) %]: <strong>[% $data[$field.sqlname][$Dataname] %]</strong></li>
                            [% /foreach %]
                    </ul>
                [% /if %]

                [% if $field.type == "selecteduserdata" %]

                    <select name="[% $field.name %][userid]" id="field_[% $field.name %]" size="1" class="form-control FormularUserContact" [% if $field.required == "Y" && $editable %]required[% /if %] [% if !$editable && $aid > 0 %]readonly="readonly"[% /if %] style="width:100%;">
                        <option hidden>--[% $user->gettext('Bitte wählen') %]--</option>

                        [% html_options options=$field.UserSelection selected=$data[$field.sqlname]['userid'] %]
                    </select>

                    <ul class="list-unstyled">
                        [% foreach from=$field.userdata item=Userdata key=Dataname %]
                            <li class="option">[% $user->gettext($Dataname) %]: <strong>[% $data[$field.sqlname][$Dataname] %]</strong></li>
                            [% /foreach %]
                    </ul>
                [% /if %]
                [% if $field.type == "selectedcontactdata" %]

                    <select name="[% $field.name %][contactid]" id="field_[% $field.name %]" size="1" class="form-control FormularUserContact" [% if $field.required == "Y" && $editable %]required[% /if %] [% if !$editable && $aid > 0 %]readonly="readonly"[% /if %] style="width:100%;">
                        <option hidden>--[% $user->gettext('Bitte wählen') %]--</option>

                        [% html_options options=$HMContacts selected=$data[$field.sqlname]['contactid'] %]
                    </select>

                    <ul class="list-unstyled">
                        [% foreach from=$field.contactdata item=ContactData key=Dataname %]
                            <li class="option">[% $user->gettext($Dataname) %]: <strong>[% $data[$field.sqlname][$Dataname] %]</strong></li>
                            [% /foreach %]
                    </ul>
                [% /if %]


                    <div id="field_error_[% $field.name %]" style="display:none;" class="formwizard_error help-block"></div>
                </div>
            [% /if %]
        </div>


    [% /foreach %]


<script>
    $(".FormularUserContact").select2({theme: 'bootstrap-5'});
    var ErrorMessages = new Array();
    ErrorMessages[101] = "[% $user->gettext('Es wurde kein Eintrag selektiert.') %]";
    ErrorMessages[102] = "[% $user->gettext('Es wurden nicht alle Felder selektiert.') %]";
    ErrorMessages[201] = "[% $user->gettext('Keine g&uuml;ltige Zahl.') %]";
    ErrorMessages[202] = "[% $user->gettext('Keine Gleitkommazahl.') %]";
    ErrorMessages[301] = "[% $user->gettext('Ung&uuml;ltiges Datumsformat.') %]";
    ErrorMessages[302] = "[% $user->gettext('Ung&uuml;ltiges Datumsformat.') %]";
    ErrorMessages[303] = "[% $user->gettext('Ung&uuml;ltiges Zeitformat.') %]";
    ErrorMessages[304] = "[% $user->gettext('Keine Jahreszahl.') %]";
    ErrorMessages[404] = "[% $user->gettext('Formularfeld muss ausgef&uuml;llt werden.') %]";
    ErrorMessages[405] = "[% $user->gettext('Das Feld darf nur aus Buchstaben und Zahlen bestehen.') %]";
    ErrorMessages[406] = "[% $user->gettext('Nur Grossbuchstaben erlaubt') %]";
    ErrorMessages[407] = "[% $user->gettext('Nur Kleinbuchstaben erlaubt.') %]";
    ErrorMessages[408] = "[% $user->gettext('Keine g&uuml;ltige URL.') %]";
    ErrorMessages[409] = "[% $user->gettext('Keine g&uuml;ltige e-Mail.') %]";

    [% foreach from=$form.fields item=field %]
        [% if $field.type != "seperator" && $field.type != "notice" %]
    fields.push(new field("[% $field.type %]", "[% $field.name %]", "[% $field.required %]", "[% $field.eval %]"));
        [% /if %]
    [% /foreach %]

     forms.push(new form("GeneratetForm[% $fid %]", fields));

    $(".datepicker").datepicker();
    $(".datetimepicker").datetimepicker({
        currentText: 'Jetzt',
        closeText: 'OK',
        timeText: 'Zeit',
        hourText: 'Stunde'
    });


$(document).on('submit', '#GeneratetForm[% $fid %]', function(e) {

    e.preventDefault(); // <-- add this

    var data = $('#GeneratetForm[% $fid %]').serialize();
    var url = "index.php?module=formwizard&event=viewFormular";

    $.post(url, data, function(){
        console.log("Formular [#GeneratetForm[% $fid %]] wurde gespeichert...");
        // readyCheckforms ist ein Globales Array welches in der Datei ticket-newticket.js deklariert wird.
        //Dort werden alle Formulare die noch zu prüfen sind mit dem Wert FALSE eingetragen.
        //Nach dem Senden wird der Wert hier auf TRUE gesetzt.
        readyCheckforms[formid]=true;
    });
})
</script>

            [% if $tid || $tid === 0 %]
            </form>
    </div>
    </body>
</html>
[% else %]

</form>
                        </div>
                </div>
[% /if %]
