<style>
    .lock{
        color: red;
    }

    .unlocl{
        color: #337ab7;
    }
</style>
<div class="table-responsive">
    <table class="table table-striped">
        <thead>
            <tr>
                <th></th>
                    [% if $Activity %]
                    <th>[% $user->gettext('Aktivit&auml;t') %]</th>
                    [% /if %]
                <th>#</th>
                <th>[% $user->gettext('Icons') %]</th>
                <th>[% $user->gettext('Titel') %]</th>
                    [% if $hasAttachmentsFromChildtickets %]
                    <th>[% $user->gettext('Von RefTicket') %]</th>
                    [% /if %]
                <th>[% $user->gettext('Dateigr&ouml;&szlig;e') %]</th>
                <th>[% $user->gettext('Bild-Vorschau') %]</th>
                    [% if $user->hasRight(230) %]
                    <th>[% $user->gettext('Dateipfad') %]</th>
                    [% /if %]

                <th>[% $user->gettext('Erstellt am') %]</th>
               [% if $user->hasRight(231) %] <th>[% $user->gettext('Benutzer') %]</th>[% /if %]
            </tr>
        </thead>
        <tbody>
            [% foreach from=$Attachments item=Att %]
                [% if !$Att.eventid  || ($Att.eventid && $Att.viewable) %]
                    <tr id="AttID_[% $Att.attachmentid %]" title="">
                        <td><input type="checkbox" name="AttCollection[[% $Att.attachmentid %]]" value="[% $Att.attachmentid %]" class="AttCollection" /> </td>
                            [% if $Activity %]
                            <td>[% $Att.descriptionname %] ([% $Att.activityid %] ) </td>
                        [% /if %]
                        <td>
                            [% if $Att.attachmenttypid == 1 OR $Att.filename != '' %]
                                <a href="index.php?module=upload&event=ShowImage&name=[% $Att.changedfilename %]" title="Download [% $Att.filename %]"><span class="fa fa-download"></span></a>
                                [% elseif $Att.attachmenttypid == 2 OR $Att.url != '' %]
                                <a href="[% $Att.url %]" target="top">[% $Att.url %]</a>

                            [% else %]
                                &nbsp;
                            [% /if %]
                        </td>
                        [% if !$Att.video %]
                            <td><img src="[% $Att.mimetyp %]" /></td>
                            [% else %]
                            <td><a href="index.php?module=upload&event=showVideo&name=[% $Att.changedfilename %]" id="" data-fancybox-type="iframe" class="video-diaglog btn btn-outline-primary"><span class="fa fa-play"></span></a></td>
                                [% /if %]
                        <td>
                            [% if $Att.shortdescr|trim != '' %]
                                [% $Att.shortdescr %]
                            [% else %]
                                [% $Att.filename %]
                            [% /if %]
                        </td>

                        [% if $hasAttachmentsFromChildtickets %]
                            <td>
                                [% if $Att.fromchildticket %]
                                    #[% $Att.ticketid %]
                                [% /if %]
                            </td>
                        [% /if %]
                        <td>[% $Att.filesize %] KB</td>
                        <td>
                            [% if $Att.thumbnail %]
                                <a href="index.php?module=upload&event=ShowImage&name=[% $Att.changedfilename %]"  rel="gallery1" class="ImageDialog image" title="[% $Att.filename %]">
                                    <img src="index.php?module=upload&event=ShowImage&name=[% $Att.thumbnail %]" border="0" title="[% if $Att.longdescr != '' %][% $Att.longdescr %][% /if %] ([% $Att.imagewidth %] x [% $Att.imageheight %] px) " [% if $Att.longdescr != '' %]alt='[% $Att.longdescr %]'[% /if %]/>
                                </a>
                            [% /if %]
                        </td>
                        [% if $user->hasRight(230) %]
                            <td><a href="[% $UncUploadPath %][% $Att.movedFilename %]" target='_blank' >[% $Att.changedfilename %]</a> </td>
                        [% /if %]
                        <td>[% $Att.createdate %]</td>
                        [% if $user->hasRight(231) %]<td>[% $Att.createuser %]</td>[% /if %]
                        <td>
                            [% if $Att.attachmenttypid == 1 || $Att.attachmenttypid == 2 || $Att.attachmenttypid == 3 %]
                                <a href="index.php?module=upload&event=FileDetails&id=[% $Att.attachmentid %]"  class=" openInLargeModal">
                                    <span class="fa fa-info" title="[% $user->gettext('Dokument-Details anzeigen')%]"></span>
                                </a>&nbsp;
                            [% /if %]
                            [% if $Att.remove %]
                                <a href="index.php?module=upload&event=editFileDetails&amp;id=[% $Att.attachmentid %]&tid=[% $TicketID %]"  class=" openInLargeModal">
                                    <span class="fa fa-pencil" title="[% $user->gettext('Dokument-Details bearbeiten')%]"></span>
                                </a>
                                &nbsp;
                                <a href="JavaScript:void(0);" onclick="RemoveAttachment([% $Att.attachmentid %]);">
                                    <span class="fa fa-times" title="[% $user->gettext('Dokument entfernen')%]"></span>
                                </a>
                            [% /if %]
                            [% if ($user->isSupporter() || $user->inGroup(1)) %]
                                &nbsp;
                                [% if $Att.createdatebl %]
                                    <span class="fa fa-triangle-exclamation" title="[% $user->gettext('Das Dokument wurde der Mail2Ticket-Blacklist zugefügt und wird derzeit nicht mehr als Mailanhang gespeichert') %]"></span>
                                    &nbsp;
                                [% else %]
                                    <a href="JavaScript:void(0);" onclick="AddToBlacklist([% $Att.attachmentid %]);">
                                        <span class="fa fa-ban" title="[% $user->gettext('Dokument zur Blacklist hinzufügen')%]"></span>
                                    </a>
                                    &nbsp;
                                [% /if %]
                                [% if !$forProject  && $Sennebogen %]
                                    <a href="JavaScript:void(0);" onclick="SetAsPublic([% $Att.attachmentid %]);" title="[% if $Att.documentnotpublic  %][% $user->gettext('Dokument intern - klick um das Dokument für alle sichtbar zu machen') %][% else %][% $user->gettext('Dokument öffentlich - klick um es intern zu machen') %][% /if %]">
                                        <span class="fa fa-lock[% if $Att.documentnotpublic  %]  [% else %]-open[% /if %]" id="lock_[% $Att.attachmentid %]" ></span>
                                    </a>
                                [% /if %]
                            [% /if %]
                        </td>
                    </tr>
                [% /if %]
            [% /foreach %]
        </tbody>
        <tfoot>
            <tr>

                <td colspan="10">
                    [% if $Tools->FCS_Count($Attachments) %]
                        <span class="pull-left">
                            <input type="checkbox" id="SelectAll" />
                            
                            <span class="btn btn-outline-secondary disabled DocumentCollectionButton" [% if $existZipArchiv %]id="DownloadCollection" title="[% $user->gettext('Dokumentenauswahl herunterladen') %]"[% else %] title="[% $user->gettext('Benötigte PHP-Extension Zip nicht aktivert') %]" style="opacity: .65;"[% /if %]><span class="fa fa-download"></span></span>
                            
                            <span class="btn btn-outline-secondary disabled DocumentCollectionButton" id="BlacklistCollection" title="[% $user->gettext('Dokumentenauswahl auf Blacklist setzten') %]"><span class="fa fa-ban"></span></span>
                            <span class="btn btn-outline-secondary disabled DocumentCollectionButton" id="DeleteCollection" title="[% $user->gettext('Ausgewählte Dokumente löschen') %]"><span class="fa fa-times" ></span></span>
                            [% if $WarantyRequestData.CallID %]
                            <span class="btn btn-outline-secondary disabled DocumentCollectionButton" id="SendCollectionToPA" title="[% $user->gettext('Ausgewählte Dokumente an ProAlpha Call senden') %]"><span id="SendCollectionToPAIcon" class="fa fa-file-export"></span></span>
                            [% /if %]
                        </span>
                    [% /if %]
                    [% $Paging %]
                </td>
            </tr>
        </tfoot>
    </table>
</div>

<form action="index.php?module=upload&event=AttachmentCollectionDownload" method="POST" name="AttachmentCollectionDownload" id="AttachmentCollectionDownload" enctype="multipart/form-data" >
    <input type="hidden" name="AttachmentIDs" id="AttachmentCollectionAttachmentIDs" value="" />
    <input type="hidden" name="TicketID" value="[% $TicketID %]" />
    <input type="hidden" name="action" id="AttachmentCollectionAction" value="" />
</form>
<script>
    /*
    $('.lazyImgDocumentList').Lazy({
        // your configuration goes here
        //scrollDirection: 'vertical',
        //effect: 'fadeIn',
        //visibleOnly: true,
        onError: function(element) {
//            console.log('error loading ' + element.data('src'));
        }
    });
    */

    $(".video-diaglog").fancybox({
        maxWidth: 600,
        maxHeight: 400,
        closeClick: false,
        keys: {
            close: [27] //escape key
        },
        afterShow: function () {
            //this.wrap.draggable();
        }
    });
    $('#DocumentCount').html([% $entries %]);

    function switchPage(page) {
        getAttachments([% $TicketID %], page);
    }

    /**
     * Set Attachment as Public/Non-public
     */
    [% if !$forProject && ($user->isSupporter() || $user->inGroup(1)) && $Sennebogen %]
    function SetAsPublic(AttID, Public) {
        $.post('index.php?module=upload&event=SetAttachmentAsPublic', {AttachmentID: AttID, Public: Public}, function (data) {
            if (parseInt(data) === 1) {
                $("#lock_" + AttID).removeClass('fa-lock-open');
                $("#lock_" + AttID).addClass('fa-lock');
                $("#lock_" + AttID).parent().prop('title', "[% $user->gettext('Dokument intern - klick um das Dokument für alle sichtbar zu machen') %]");
            } else {
                $("#lock_" + AttID).removeClass('fa-lock');
                $("#lock_" + AttID).addClass('fa-lock-open');
                $("#lock_" + AttID).parent().prop('title', "[% $user->gettext('Dokument öffentlich - klick um es intern zu machen') %]");
            }

        });
    }
    [% /if %]


    $('#DownloadCollection').on('click', function (e) {
        getAttCollection()
        if (counter > 0) {

            $('#AttachmentCollectionAttachmentIDs').val(sList);
            $('#AttachmentCollectionAction').val('download');
            $('#AttachmentCollectionDownload').submit();
        }

    })

    $('#BlacklistCollection').on('click', function (e) {
        getAttCollection()

        if (counter > 0) {
            bootbox.confirm("[% $user->gettext('Ausgewählte Dokumente auf die Blacklist setzten?') %]", function (result) {
                if (result) {
                    $.post('index.php?module=upload&event=AttachmentCollectionDownload', {AttachmentIDs: sList, action: 'blacklist'}, function (data) {
                        getAttachments([% $TicketID %]);
                    })
                }
            })
        }
    })
    $('#DeleteCollection').on('click', function (e) {

        getAttCollection()

        if (counter > 0) {
            bootbox.confirm("[% $user->gettext('Ausgewählte Dokumente löschen?') %]", function (result) {
                if (result) {
                    $.post('index.php?module=upload&event=AttachmentCollectionDownload', {AttachmentIDs: sList, action: 'delete'}, function (data) {
                        getAttachments([% $TicketID %]);
                    })
                }
            })
        }
    })

    $('#SendCollectionToPA').on('click', function (e) {

        getAttCollection()

        if (counter > 0) {
            bootbox.confirm("[% $user->gettext('Ausgewählte Dokumente an ProAlpha Call senden?') %]", function (result) {
                if (result) {
                    $('#SendCollectionToPAIcon').toggleClass('fa-file-export fa-spinner');
                    $.post('index.php?module=upload&event=AttachmentCollectionDownload', { AttachmentIDs: sList, action: 'sendToPA', CallID: [% $WarantyRequestData.CallID|default:0 %] }, function (json) {
                        $('#SendCollectionToPAIcon').toggleClass('fa-file-export fa-spinner');
                        alert(json.message);
                        getAttachments([% $TicketID %]);
                    })
                }
            })
        }
    })

    var counter = 0;
    var sList = "";
    /**
     * Comment
     */
    function getAttCollection() {
        sList = ""
        counter = 0;
        $('.AttCollection').each(function () {
            if (this.checked) {
                counter++;
                sList += (sList == "" ? $(this).val() : "," + $(this).val());
            }
        });
    }

    $('.AttCollection').on('click', function (e) {
        getAttCollection();
        if (counter) {
            $('.DocumentCollectionButton').removeClass('disabled');
        } else {
            $('.DocumentCollectionButton').addClass('disabled');
        }
    })

    $('#SelectAll').on("click", function () {
        if ($('#SelectAll').prop('checked')) {
            $('.AttCollection:visible').prop('checked', true);
            $('.DocumentCollectionButton').removeClass('disabled');
        } else {
            $('.AttCollection:visible').prop('checked', false);
            $('.DocumentCollectionButton').addClass('disabled');
        }
    })

    $(".ImageDialog").fancybox();

</script>
