
<script type="text/javascript">

    function switchPage(pos) {
        document.getElementById("pos").value = pos;
        submitForm();
    }

</script>

<style>
    .table th{
        cursor: pointer;
    }
</style>

<form action="" method="post" name="hm" onsubmit="return false;" id="hm" enctype="multipart/form-data" class="form-horizontal form-label-left" >

    <input type="hidden" id="sort" name="sort" value="[% $sort %]" />
    <input type="hidden" id="sortorder" name="sortorder" value="[% $sortorder %]" />
    <input type="hidden" id="message" name="message" value="0" />
    <input type="hidden" id="type" name="type" value="" />
    <div class="card panel-fcs-border shadow-sm">
        <div class="card-header">
            <span class="fcs-card-title h2">[% $field.fieldname %] [% $user->gettext('pflegen') %]</span>
        </div>
        <div class="card-body">
            <div class="card panel-info shadow-sm rounded">
                <div class="card-body">
                    <div class="row">
                        <div class="col-lg-9" style="padding-left: 30px;">
                            <div class="row">
                                <div class="col-sm-6">
                                    <div class="form-group row mb-3">
                                        <label class="control-label col-sm-3">
                                            [% $user->gettext('Eintr&auml;ge pro Seite') %]
                                        </label>
                                        <div class="col-sm-9 text-left">
                                            <select style="width:15%;" name="entries_per_page" onchange="document.hm.submit();" class="form-control">
                                                <option value="10" [% if $entries_per_page == 10 %]selected="selected"[% /if %]>10</option>
                                                <option value="20" [% if $entries_per_page == 20 %]selected="selected"[% /if %]>20</option>
                                                <option value="50" [% if $entries_per_page == 50 %]selected="selected"[% /if %]>50</option>
                                                <option value="100" [% if $entries_per_page == 100 %]selected="selected"[% /if %]>100</option>
                                            </select>&nbsp;&nbsp;&nbsp;
                                        </div>

                                        <input type="hidden" name="pos" value="[% $pos %]" id="pos">
                                    </div>
                                </div>
                                <div class="col-sm-6">
                                    [%*
                                    <div class="form-group row mb-3">
                                    <label class="control-label col-sm-3">
                                    [% $user->gettext('Verwendet im Tickettyp') %]:
                                    </label>
                                    <div class="col-sm-9">
                                    <select  name="typeChoose" id ="typeChoose" onchange = "callType();" class="form-control">
                                    <option value="0" [% if $selectedType %]selected[% /if %]>[% $user->gettext('Alle') %]</option>
                                    [% foreach from=$types item=type%]
                                    <option value="[% $type.type_id %]" [% if $selectedType == $type.type_id %]selected[% /if %]>[% $type.type_name %]</option>
                                    [% /foreach %]
                                    </select>&nbsp;&nbsp;&nbsp;
                                    </div>
                                    <input type="hidden" name="pos" value="[% $pos %]" id="pos">
                                    </div>
                                    *%]
                                </div>
                            </div>


                        </div>
                        <div class="col-lg-3 text-end">
                            [% if $user->hasRight(36) || $user->hasRight(810) %]
                                <div class="btn-group">
                                    <a href="index.php?module=userfield&event=newFieldData&id=[% $field.userdeffieldid %]" class="btn btn-outline-primary">[% $field.fieldname %] [% $user->gettext('erstellen') %]</a>
                                    <a href="index.php?module=userfield" class="btn btn-outline-primary">[% $user->gettext('Liste aller Felder') %]</a>
                                </div>
                            [% /if %]
                        </div>
                    </div>
                </div>
            </div>
        


            <div class="card panel-info shadow-sm rounded">
                <div class="card-header">
                    <span class="fcs-card-title h3">
                        [% $entries %]

                        [% $field.fieldname %]
                        [% $user->gettext('gefunden') %]
                    </span>
                </div>
    </form>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-striped">
                            <thead>
                                <tr class="fields>">
                                    <th class="sort" id="fieldname">[% $user->gettext('Name') %]</th>
                                    <th class="sort" id="description">[% $user->gettext('Beschreibung') %]</th>
                                    <th class="sort" id="fieldcreatedby">[% $user->gettext('Ersteller') %]</th>
                                    <th class="sort" id="fieldcreateddate">[% $user->gettext('Erstellt am') %]</th>
                                    <th class="sort" id="fieldupdatedby">[% $user->gettext('Aktualisiert von') %]</th>
                                    <th class="sort" id="fieldupdateddate">[% $user->gettext('Aktualisiert am') %]</th>
                                    <th class="sort" id="">[% $user->gettext('Aktiv') %]</th>
                                    <th class="sort" id="valueorder">[% $user->gettext('Zuordnung') %]</th>
                                    <th></th>
                                </tr>
                            </thead>

                            <tbody>
                                [% foreach from=$fieldList item=field %]
                                    <tr>
                                        <td>[% $field.value  %]</td>
                                        <td class="center">
                                            <i>[% $field.valuedescription  %]</i>
                                        </td>
                                        <td>[% $field.createname %]</td>
                                        <td>[% $field.valuecreatedate|date_format:"%d.%m.%Y %H:%M" %]</td>
                                        <td>[% $field.updatename %]</td>
                                        <td>[% $field.valueupdatedate|date_format:"%d.%m.%Y %H:%M" %]</td>
                                        <td class="text-center"> 
                                            [% if $field.valueactive == 1 %]
                                                <span class="fa fa-check" style="color:green;"></span>
                                            [% else %]
                                                <span class="fa fa-times" style="color:red;"></span>
                                            [% /if %]
                                        </td>
                                        <td>[% $field.valueorder %]</td>
                                        <td >

                                            <a href="index.php?module=userfield&event=updateFieldData&id=[% $field.userdeffieldid %]&vid=[% $field.userdeffieldvalueid  %]" class="btn btn-outline-secondary"  title="[% $user->gettext('Bearbeiten') %]"><span class="fa fa-pencil"></span></a>
                                            [% if !$field.cnt %]
                                                <a href="javascript:deleteFieldData([% $field.userdeffieldvalueid  %])" class="btn btn-outline-secondary" title="[% $user->gettext('Löschen') %]"><span class="fa fa-trash" ></span></a>
                                            [% else %]
                                            <span title="[% $user->gettext('Der Eintrag kann nicht gelöscht werden, da er verwendet wird.') %]"><span class="btn btn-outline-secondary disabled" ><span class="fa fa-trash"></span></span>
                                            [% /if %]
                                            [% if $field.valueorder > $minOrder%]
                                            <form method="post" action="index.php?module=userfield&event=movevalueup" style="display:inline;">
                                                <input type="hidden" name="id" value="[% $field.valueorder %]">
                                                <input type="hidden" name="fieldid" value="[% $field.userdeffieldid %]">
                                                <button type="submit" class="btn btn-sm btn-outline-secondary">
                                                    <i class="fa fa-arrow-up"></i>
                                                </button>
                                            </form>
                                            
                                             [% /if %]
                                             [% if  $field.valueorder < $maxOrder%]
                                            <form method="post" action="index.php?module=userfield&event=movevaluedown" style="display:inline;">
                                                <input type="hidden" name="id" value="[% $field.valueorder %]">
                                                <input type="hidden" name="fieldid" value="[% $field.userdeffieldid %]">
                                                <button type="submit" class="btn btn-sm btn-outline-secondary">
                                                    <i class="fa fa-arrow-down"></i>
                                                </button>
                                            </form>
                                            [% /if %]
                                        </td>
                                    </tr>
                                [% foreachelse %]
                                    <tr class="even">
                                        <td colspan="9" style="text-align:center !important">[% $user->gettext('Es wurden keine Felder gefunden...') %]</td>
                                    </tr>
                                [% /foreach %]
                            </tbody>
                        </table>

                    </div>
                    <div class="col-lg-12 col-sm-12 hidden-sm hidden-xs">
                        <div class="col-lg-6 col-sm-6"></div>
                        <div class="col-lg-6 col-sm-6 text-end">
                            [% $Paging %]
                        </div>
                    </div>
                </div>
            </div>
        </div>

    </div>


<div class="modal fade" id="removeModal" tabindex="-1" role="dialog" aria-labelledby="removeModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-bs-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body modalContent">
                ...
            </div>
        </div>
    </div>
</div>

<script>

    $('.select2').select2({theme: 'bootstrap-5'});

    function deleteFieldData(id) {
        bootbox.confirm("[% $user->gettext('Daten wirklich löschen?')%]", function (result) {
            if (result) {
                $.post('index.php?module=userfield&event=deleteFieldData&id=' + id, function (data) {

                    if (data == 1) {
                        location.reload();
                    } else {
                        noty({
                            text: "[% $user->gettext('Daten können nicht gelöscht werden.') %]",
                            type: 'error',
                            timeout: notifyTimeout
                        });
                    }
                })
            }
        })
    }



    var current_sort = $("#sort").val();
    var current_sortorder = $("#sortorder").val();

    showSortIcon();



    $(".sort").on("click", function (e) {
        var sort = $(this).attr('id');
        var sortorder = 'asc';
        if (current_sort === sort) {
            if (current_sortorder === 'asc') {
                sortorder = 'desc';
            } else {
                sortorder = 'asc';
            }
        }
        $("#sort").val(sort);
        $("#sortorder").val(sortorder);
        submitForm();
    })

    function showSortIcon() {
        var content = $("#" + current_sort).html();
        if (current_sortorder == 'desc') {
            content += '<span class="fa fa-chevron-down"></span>';

        } else {
            content += '<span class="fa fa-chevron-up"></span>';
        }
        $("#" + current_sort).html(content);
    }

    function setActive(id) {
        var isChecked = 0;
        var info = "";
        if ($('#checbox_active_' + id).is(":checked")) {
            isChecked = 1
        }
        ;
        if (isChecked == 1) {
            info = "[% $user->gettext('aktiviert')%]";
        } else {
            info = "[% $user->gettext('deaktiviert')%]";
        }
        $.post('index.php?module=field&event=setActive', {id: id, active: isChecked}, function (data) {
            if (data == 1) {
                noty({
                    text: "[% $user->gettext('Feld " +info+ ".') %]",
                    type: 'success',
                    timeout: notifyTimeout
                });
            } else {
                noty({
                    text: "[% $user->gettext('Feld kann momentan nicht aktualisiert werden.') %]",
                    type: 'error',
                    timeout: notifyTimeout
                });
            }

        })
    }
</script>
