<script type="text/javascript" src="includes/js/jquery-ui-timepicker-addon.js"></script>
<script type="text/javascript" src="includes/js/script.js"></script>
<link rel="stylesheet" type="text/css" href="styles/[% $CSSTEMPLATE %]/css/jquery-ui-timepicker-addon.css" />
<form action="[% $action %]" method="post" name="hm" onsubmit="return false;" id="hm" enctype="multipart/form-data" class="form-horizontal form-label-left">

    <input type="hidden" name="save" value="1" />
    <input type="hidden" name="close" value="0" id="close" />
    <input type="hidden" name="done" value="0" id="done" />
    <input type="hidden" name="take" value="0" id="take" />
    <input type="hidden" name="ActivityID" value="[% $Activity->ActivityID %]" id="ActivityID" />

    <input type="hidden" name="oldstatus" value="[% $status %]" />

    <div class="row">

        <div class="col-lg-12   sticky-top" id="UpperRow" style="z-index: 1;">
            <div class="card panel-fcs-content shadow-sm MovableItems" id="ActionItem" >
                <div class="card-header movable">
                    <div class="row">
                        <div class="col-11">
                            [% $user->gettext('Aktionen') %]
                        </div>
                        <div class="col-1 text-end">
                            <span class="fa fa-chevron-up" id="ActionItemIcon" onclick="ToggleItem('ActionItem');"></span>
                        </div>
                    </div>

                </div>
                <div class="card-body " id="ActionItemBody">
                    <div class="btn-group">
                        <input type="button" class="btn btn-outline-primary" value="[% $user->gettext('Zur&uuml;ck') %]" onclick="saveForm(true);" />
                        [% if $ticket.ticketstatusid != 3 && $ticket.ticketstatusid != 11 && $Activity->TaskType != 5 %]

                            [% if $hmcompany != 'VERSORGUNGSBETRIEBE HOYERSWERDA GMBH' || $user->hasRight(703) %]
                                <input id="btnActivityNotPossible" type="button" class="btn btn-outline-primary"

                                [% if $hmcompany == 'VERSORGUNGSBETRIEBE HOYERSWERDA GMBH' %]
                                    value="[% $user->gettext('Abbrechen') %]"
                                [% elseif $hmcompany == 'STADT NETPHEN' %]
                                    value="[% $user->gettext('Vorgang abbrechen') %]"
                                [% elseif $hmcompany == 'BERUFSBILDUNGSZENTRUM SCHLESWIG' %]
                                    value="[% $user->gettext('Arbeitsschritt abbrechen') %]"
                                [% else %]
                                    value="[% $user->gettext('Aktivität nicht möglich') %]"
                                [% /if %] onclick="saveForm(false);
                                        $('#ActivityImposibleLink').click();"  />
                                <a href="index.php?module=ticket&event=event&tid=[% $ticket.id %]&activity=1&impos=1"  data-fancybox-type="iframe" class="dialog" id="ActivityImposibleLink" style="display:none;">[% $user->gettext('Nicht möglich') %]</a>
                            [% /if %]

                            <input id="btnActivitySuccess" type="button" class="btn btn-outline-success-full"

                                   [% if $hmcompany == 'VERSORGUNGSBETRIEBE HOYERSWERDA GMBH' %]
                                        value="[% $user->gettext('OK') %]"
                                    [% elseif $hmcompany == 'BADEN-WüRTTEMBERGISCHER GENOSSENSCHAFTSVERBAND E.V' %]
                                        value="[% $user->gettext('Speichern') %]"
                                    [% elseif $hmcompany == 'STADT NETPHEN' %]
                                        value="[% $user->gettext('Vorgang erledigt') %]"
                                    [% elseif $hmcompany == 'BERUFSBILDUNGSZENTRUM SCHLESWIG' %]
                                        value="[% $user->gettext('Arbeitsschritt abschließen') %]"
                                    [% else %]
                                        value="[% $user->gettext('Aktivität erledigt') %]"
                                    [% /if %] onclick="
                                   [% if $Activity->TaskType == 2 %]
                                    var formcheck = validateForm();
                                    if (formcheck) {
                                        $('#activityForm').submit();
                                        bootbox.confirm('[% $user->gettext('Aktivität wirklich erledigt?') %]', function (result) {
                                            if (result) {
                                                document.getElementById('close').value = 1;
                                                submitForm();
                                            }
                                        })
                                    }
                                   [% else %]
                                    bootbox.confirm('[% $user->gettext('Aktivität wirklich erledigt?') %]', function (result) {
                                        if (result) {
                                            document.getElementById('close').value = 1;
                                            submitForm();
                                        }
                                    })
                                   [% /if %]"
                                   />

                            [% if $Activity->StartConID == 1 && $user->hasRight(703) %]
                                <input id="btnActivityEnd" type="button" class="btn btn-outline-danger"
                                value="[% if $hmcompany == 'BERUFSBILDUNGSZENTRUM SCHLESWIG' %][% $user->gettext('Workflow abbrechen') %][% else %][% $user->gettext('Workflow beenden') %][% /if %]" onclick="
                                    bootbox.confirm('[% $user->gettext('Möchten Sie den Workflow wirklich abbrechen?') %]', function (result) {
                                        if (result) {
                                            window.location.replace('index.php?module=workflow&event=stopWorkflow&showtid=[%$ticket.id %]&id=[% $Activity->WFIID %]');
                                        }
                                    })"  />
                            [% /if %]

                        [% /if %]
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-6 SortableContainer" id="LeftCol">
            <div class="card panel-info shadow-sm rounded MovableItems" id="FormDataItem">
                <div class="card-header movable">
                    <div class="row">
                        <div class="col-11">
                            [% $user->gettext('Formular') %]
                        </div>
                        <div class="col-1 text-end">
                            <span class="fa fa-chevron-up" id="FormDataItemIcon" onclick="ToggleItem('FormDataItem');"></span>
                        </div>
                    </div>

                </div>
                <div class="card-body" id="FormDataItemBody">
                    [% if $Activity->TaskType == 2 %]
                        <!-- Formulartabelle -->
                        <div class="holds-the-iframeaa">
                            <div id="Formular_[% $Activity->ActivityID %]">
                                <img src="images/ajax-loader.gif" alt="loading..."/>
                            </div>
                        </div>
                        <!-- Formulartabelle Ende-->
                    [% elseif $Activity->TaskType == 5 %]
                        [% $WFInfoText %]

                        <h2>[% $user->gettext('Aktivitäten') %]</h2>
                        <table class="table">
                            <tr>
                                <th>[% $user->gettext('Kurzbeschreibung (Titel)') %]</th>
                                <th>[% $user->gettext('Status') %]</th>
                                <th>[% $user->gettext('Adressat') %]</th>

                                <th>[% $user->gettext('Formulardaten') %]</th>
                            </tr>
                            [% foreach from=$startedWFI.aGantt item=Activity %]
                                <tr>
                                    <td>[% $Activity.Descr %] (<a href="index.php?module=ticket&event=show&id=[% $Activity.ticketid %]" target="_blank">(#[% $Activity.ticketid %])</a>)</td>
                                    <td> [% $user->gettext($Activity.Status) %]</td>
                                    [% if $Activity.O->TargetType == 1 || $Activity.O->TargetType == 3 %]
                                        <td>[% $Activity.O->TargetUsers %] </td>
                                    [% elseif $Activity.O->TargetType == 2 %]
                                        <td>[% $user->gettext('Gruppe') %]: [% $Activity.O->TargetGroups %]</td>
                                    [% /if %]

                                    <td>[% $Activity.O->FormClass->showVisibleFormContent() %]</td>
                                </tr>
                            [% /foreach %]
                        </table>
                    [% else %]
                        [% $Activity->TaskText %]
                    [% /if %]
                </div>
            </div>

            [% if $isStartedByWF && ($startActivity->ShowOtherInformations || $startActivity->ShowDirectPrevActivity)%]
                <div class="card panel-info shadow-sm rounded MovableItems" id="PreviousActivityItem">
                    <div class="card-header movable">
                        <div class="row">
                            <div class="col-11">
                                [% $user->gettext('Daten von Vorgänger-WF') %] <strong>[% $startActivity->WFIDescr %]</strong>
                            </div>
                            <div class="col-1 text-end">
                                <span class="fa fa-chevron-up" id="PreviousActivityItemIcon" onclick="ToggleItem('PreviousActivityItem');"></span>
                            </div>
                        </div>
                    </div>

                    <div class="card-body" id="PreviousActivityItemBody">

                        [% foreach from=$startActivity->FormClass->getPreviusForms([% $startActivity->ShowDirectPrevActivity %]) item=Form key=key %]
                            [% foreach from=$Form item=PrevForm key=key %]
                                <div class="well well-sm" [% $user->gettext('Daten aus Vorgänger-WF') %] <strong>[% $key %]:</strong></div>

                                [% $PrevForm.FormData %]

                                [% if $Tools->FCS_Count($PrevForm.Attachments) %]

                                    [% if $Tools->FCS_Count($PrevForm.Attachments) == 1 %]
                                        <b>[% $user->gettext('Dokument') %]</b>'[% $key %]'
                                    [% else %]
                                        <b>[% $user->gettext('Dokumente') %]</b>'[% $key %]'
                                    [% /if %]:

                                    [% foreach from=$PrevForm.Attachments item=attachment %]

                                        [% if $attachment.attachmenttypid != 2 %]
                                            <a href="index.php?module=upload&event=ShowImage&name=[% $attachment.changedfilename %]" class="dialog" target="top">
                                            [% else %]
                                                <a href="[% $attachment.url %]" target="top">
                                                [% /if %]
                                                <img src="[% if $attachment.thumbnail != '' %]index.php?module=upload&event=ShowImage&name=[% $attachment.thumbnail %][% else %][% $attachment.mimetyp %][% /if %]"
                                                     width="20" height="20" border="0" title="[% $attachment.shortdescr %][% if $attachment.attachmenttypid == 2%]: [% $attachment.url %][% /if %]"  />
                                            </a>
                                        [% /foreach %]
                                    [% /if %]
                                [% /foreach %]
                            [% /foreach %]
                    </div>
                </div>
            [% /if %]

            [% if $Activity->ShowOtherInformations || $Activity->ShowDirectPrevActivity %]
                <div class="card panel-info shadow-sm rounded MovableItems"  id="PreviousActivityItem2">
                    <div class="card-header movable">
                        <div class="row">
                            <div class="col-11">
                                [% $user->gettext('Daten von Vorgängern') %]
                            </div>
                            <div class="col-1 text-end">
                                <span class="fa fa-chevron-up" id="PreviousActivityItem2Icon" onclick="ToggleItem('PreviousActivityItem2');"></span>
                            </div>
                        </div>
                    </div>

                    <div class="card-body" id="PreviousActivityItem2Body">
                        [% foreach from=$Activity->FormClass->getPreviusForms([% $Activity->ShowDirectPrevActivity %]) item=Form key=key %]
                            [% foreach from=$Form item=PrevForm key=key %]
                                <div class="well well-sm" [% $user->gettext('Daten aus Vorgängeraktivität') %] <strong>[% $key %]:</strong></div>

                                [% $PrevForm.FormData %]

                                [% if $Tools->FCS_Count($PrevForm.Attachments) %]

                                    [% if $Tools->FCS_Count($PrevForm.Attachments) == 1 %]
                                        <b>[% $user->gettext('Dokument') %]</b>'[% $key %]'
                                    [% else %]
                                        <b>[% $user->gettext('Dokumente') %]</b>'[% $key %]'
                                    [% /if %]:

                                    [% foreach from=$PrevForm.Attachments item=attachment %]

                                        [% if $attachment.attachmenttypid != 2 %]

                                            <a href="index.php?module=upload&event=ShowImage&name=[% $attachment.changedfilename %]" class="dialog" target="top">

                                            [% else %]
                                                <a href="[% $attachment.url %]" target="top">
                                                [% /if %]
                                                <img src="[% if $attachment.thumbnail != '' %]index.php?module=upload&event=ShowImage&name=[% $attachment.thumbnail %][% else %][% $attachment.mimetyp %][% /if %]"
                                                     width="20" height="20" border="0" title="[% $attachment.shortdescr %][% if $attachment.attachmenttypid == 2%]: [% $attachment.url %][% /if %]"  />
                                            </a>
                                        [% /foreach %]
                                    [% /if %]
                                [% /foreach %]
                            [% /foreach %]
                    </div>
                </div>
            [% /if %]

        </div> <!--Ende Linke Spalte -->

        <div class="col-md-6 SortableContainer" id="RightCol">
            <div class="card panel-info shadow-sm rounded MovableItems" id="WFInfoItem">
                <div class="card-header movable">
                    <div class="row">
                        <div class="col-11">
                            [% $user->gettext('Allgemeine Daten') %]
                        </div>
                        <div class="col-1 text-end">
                            <span class="fa fa-chevron-up" id="WFInfoItemIcon" onclick="ToggleItem('WFInfoItem');"></span>
                        </div>
                    </div>
                </div>
                <div class="card-body" id="WFInfoItemBody">
                    <div class="form-group row mb-3">
                        <label class="col-lg-3 control-label">[% $user->gettext('Initiator') %]</label>
                        <div class="col-lg-9">
                            <p class="form-control-static">[% $Activity->IUsername %] ([% $Activity->IName %])</p>
                        </div>
                    </div>

                    <div class="form-group row mb-3">
                        <label class="col-lg-3 control-label">[% $user->gettext('Adressaten') %]</label>
                        <div class="col-lg-7">
                            [% if $Activity->TargetType != 2 %]
                                <p class="form-control-static">
                                    [% $user->gettext('Benutzer') %]:
                                    [% $Activity->TargetUsers %]
                                </p>
                            [% else %]
                                <p class="form-control-static">
                                    [% $user->gettext('Gruppe') %]:
                                    [% $Activity->TargetGroups %]
                                </p>
                            [% /if %]
                            <div class="col-lg-2">
                                [% if $Activity->TargetType == 2 && $ticket.ticketstatusid != 3 && $ticket.ticketstatusid != 11 %]
                                    <a href ="JavaScript:void(0);" onclick="saveForm(false);
                                            document.getElementById('take').value = 1;
                                            submitForm();" class="btn btn-outline-primary" title="[% $user->gettext('Aktivität übernehmen: Benutzer wird statt der Gruppe als Adressat der Aktivität eingetragen.') %]" >
                                        <span class="fa fa-user" id="TicketTakeOverIcon"></span>
                                        [% $user->gettext('Aktivität übernehmen') %]
                                    </a>
                                [% /if %]
                            </div>
                        </div>
                    </div>

                    <div class="form-group row mb-3">
                        <label class="col-lg-3 control-label">[% $user->gettext('Dauer') %]</label>
                        <div class="col-lg-9">
                            <p class="form-control-static">
                                [% $Activity->Duration %]
                                [% $user->gettext('Tage') %]
                            </p>
                        </div>
                    </div>
                    <div class="form-group row mb-3">
                        <label class="col-lg-3 control-label">[% $user->gettext('Fällig') %]</label>
                        <div class="col-lg-9">
                            <p class="form-control-static">[% $ticket.desiredate|date_format:"%d.%m.%Y %H:%M" %]</p>
                        </div>
                    </div>

                    [% if $project_id > 0 %]
                        <div class="form-group row mb-3">
                            <label class="col-lg-4 control-label">[% $user->gettext('Projekt') %]</label>
                            <div class="col-lg-8">
                                [% $projects[$project_id] %]
                            </div>
                        </div>
                    [% /if %]

                    [% if ($Activity->NotificationType == 2 && $Activity->NGroupName != '') || ($Activity->NotificationType == 1 && $Activity->NUsername != '') %]
                        <div class="form-group row mb-3">
                            <label class="col-lg-3 control-label">[% $user->gettext('Benachrichtigung') %]</label>
                            <div class="col-lg-9">
                                [% if $Activity->NotificationType != 2 %]
                                    [% $user->gettext('Benutzer') %]:
                                    [% $Activity->NUsername %] ([% $Activity->NName %])
                                [% else %]
                                    [% $user->gettext('Gruppe') %]:
                                    [% $Activity->NGroupName %]
                                [% /if %]
                            </div>
                        </div>
                    [% /if %]
                </div>
            </div>
            <div class="card panel-info shadow-sm rounded MovableItems" id="OnHoldItem">
                <div class="card-header movable">
                    <div class="row">
                        <div class="col-11">
                            [% $user->gettext('Zurückstellung') %]
                        </div>
                        <div class="col-1 text-end">
                            <span class="fa fa-chevron-up" id="OnHoldItemIcon" onclick="ToggleItem('OnHoldItem');"></span>
                        </div>
                    </div>
                </div>
                <div class="card-body" id="OnHoldItemBody">
                    <div class='form-group row mb-3'>
                        <label class='control-label col-lg-3'>[% $user->gettext('Zurückgestellt bis')%]</label>
                        <div class='col-lg-3'>
                            <input type='text' class='form-control datetimepicker' id='OnHold' value="[% $ticket.onhold %]">

                        </div>
                        <div class='col-lg-3'>
                            <a href='javascript: setOnholdDate();' class='btn btn-outline-primary'>[% $user->gettext('Zurückstellen')%]</a>

                        </div>
                    </div>
                </div>
            </div>
            <div class="card panel-info shadow-sm rounded MovableItems" id="EventItem">
                <div class="card-header movable">

                    <div class="row">
                        <div class="col-sm-4"><h3 class="card-title">[% $user->gettext('Bearbeitungen') %]</h3></div>
                        <div class="col-sm-7 text-end">
                            <div class="btn-group">
                                [% if $ticket.ticketstatusid != 3 && $ticket.ticketstatusid != 11 %]
                                    <a href="index.php?module=ticket&event=event&tid=[%$ticket.id%]&activity=1" onclick="saveForm(false);" class="btn btn-outline-primary btn-sm openInBigIFrameModal">[% $user->gettext('Neuer Kommentar') %]</a>
                                [% /if %]
                                [% if !$user->hasRight(243) %]
                                    <span class="btn btn-outline-secondary btn-sm" data-toggle="button" aria-pressed="false" autocomplete="off" title="[% $user->gettext('Protokolleintr&auml;ge anzeigen') %]" onclick="$('.protokoll').toggleClass('hidden');">[% $user->gettext('Protokolleintr&auml;ge anzeigen') %]</span>
                                [% /if %]
                            </div>
                        </div>
                        <div class="col-sm-1 text-end">
                            <span class="fa fa-chevron-up" id="EventItemIcon" onclick="ToggleItem('EventItem');"></span>
                        </div>
                    </div>
                </div>
                <div class="card-body" id="EventItemBody">
                    [% include file=$modulePath|cat:"/details.includeHistory.tpl" %]
                </div>
            </div>
            
                     [% if $user->hasRight(219) %]
                    <!--Tags-->
                    <div id="TagsItem" class="card panel-info shadow-sm rounded" [% if ($type.type_name && !$Tools->FCS_InArray('tag', $show)) %]style="display:none;"[% /if %]>
                        <div class="card-header" role="tab" id="headingTags">
                            <div class="row">
                                <div class="col-lg-11">
                                    <div class="row">
                                        <div class="col-4">
                                            <h6 class="card-title">
                                                [% $user->gettext('Schlagworte') %]
                                            </h6>
                                        </div>
                                        <div class="col-8 text-end">
                                            [% foreach from=$TicketTags item=Tag %]
                                                <span class="badge bg-secondary">[% $Tag %]</span>
                                            [% /foreach %]
                                        </div>
                                    </div>
                                </div>
                                <div class="col-lg-1 text-end">
                                    <a role="button" href="#collTags" data-toggle="collapse" data-parent="#DEACTIVEleftAccordion" class="collapsed2"  id="collapse_TagsItem" onclick="toggleCollapseIcon('collapse_TagsItem');" aria-expanded="false" aria-controls="collapseContact"><span class="fa fa-chevron-down" id="icon_collapse_TagsItem"></span></a>
                                </div>
                            </div>
                        </div>
                        <div id="collTags" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingTwo">
                            <div class="card-body">
                                <div  class="form-group row mb-3">
                                    <div class=" col-sm-12">
                                        <select name="TicketTags[]" id="TicketTags" class="form-control select2" multiple="multiple" style="width:100%;">
                                            [% html_options options=$AllTags selected=$TicketTags2 %]
                                        </select>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                [% /if %]


            <div class="card panel-info shadow-sm rounded MovableItems" id="NotDisplayItem"  [% if !$user->hasRight(1020)  %] style="visibility:hidden;"  [% /if %]>
                <div class="card-header movable">

                    <div class="row">
                        <div class="col-sm-11"><h3 class="card-title">[% $user->gettext('Nicht angezeigte Objekte') %]</h3></div>

                        <div class="col-sm-1 text-end">
                            <span class="fa fa-chevron-up" id="NotDisplayItemIcon" onclick="ToggleItem('NotDisplayItem');"></span>
                        </div>
                    </div>
                </div>
                <div class="card-body SortableContainer" id="NotDisplayItemBody">

                </div>
            </div>
        </div><!--Ende rechte Spalte -->
    </div>
</form>

<script>

                    [% if $user->hasRight(219) %]
                    let tags = $("#TicketTags").select2({theme:'bootstrap-5'}/*{
                                                    tags: true,
                                                    tokenSeparators: [',', ';'],
                                                    createTag: function (params) {
                                                    var term = $.trim(params.term);
                                                    if (term === '') {
                                                    return null;
                        }
                        
                        return {
                                                            id: term,
                                                            text: term,
                                                            newTag: true // add additional parameters
                    }
                    }
                    }*/).on('change', function(e) {
                                                            /*
                                                            var isNew = $(this).find('[data-select2-tag="true"]');
                                                            if (isNew.length){
                                                            
                                                            $.ajax({
                                                            type: "POST",
                                                            url: "index.php?module=ticket&event=addTag",
                                                            data: {tid: TicketID, tag: isNew.val()}
                                                            }).done(function (data) {
                                                            if (data) {
                                                            
                                                            //Neuer Tag angelegt und verknüpft
                                                            isNew.replaceWith('<option selected value="' + data + '">' + isNew.val() + '</option>');
                                                            $("#TicketTags").trigger('change'); //update select2
                                                            
                                                            
                                                            } else {
                                                            //Fehler beim Speichern
                                                            }
                                                            });
                                                            }
                                                            */
                                                            var data = $("#TicketTags").trigger('change.select2').val();
                                                    $.ajax({
                                                    type: "POST",
                                                            url: "index.php?module=ticket&event=changeTicketTags",
                                                            data: {tid: [% $ticket.id %], tagids: data}
                                                    })
                                                    });
                                    [% /if %]
                                    

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

    /**
     * setOnholdDate
     */
    function setOnholdDate() {
        var onhold = $("#OnHold").val();
        //console.log(onhold);
        if (onhold !== '') {
            $.post('index.php?module=ticket&event=RESTUpdateTicketOnHoldDate&id=[% $ticket.id %]', {"OnHoldDate": onhold}, function (data) {
                if (!data.error) {
                    noty({
                        text: data.message[0],
                        //layout: 'top',
                        //theme: 'defaultTheme', // or 'relax'
                        //closeWith: ['click', 'hover'],
                        type: 'success',
                        timeout: notifyTimeout
                    });
                } else {
                    noty({
                        text: data.message[0],
                        //layout: 'top',
                        //theme: 'defaultTheme', // or 'relax'
                        //closeWith: ['click', 'hover'],
                        type: 'error',
                        timeout: notifyTimeout
                    });
                }
            });
        } else {
            $.ajax({
                type: "POST",
                url: "index.php?module=ticket&event=setOnHold",
                data: {tid: [% $ticket.id %], save: 1, remove: 1, OnHoldDate: ''}
            }).done(function (msg) {
                if (1 != msg) {
                    $("#OnHold").addClass("textboxerror");
                    $("#OnHold").before('<p>' + msg + '</p>');
                } else {
                    noty({
                        text: "[% $user->gettext('OnHold Datum entfernt!') %]",
                        //layout: 'top',
                        //theme: 'defaultTheme', // or 'relax'
                        //closeWith: ['click', 'hover'],
                        type: 'success',
                        timeout: notifyTimeout
                    });
                }
            });
        }
    }

    function ToggleItem(ItemID) {
    }
</script>
