%# BEGIN BPS TAGGED BLOCK {{{
%#
%# COPYRIGHT:
%#
%# This software is Copyright (c) 1996-2021 Best Practical Solutions, LLC
%#                                          <sales@bestpractical.com>
%#
%# (Except where explicitly superseded by other copyright notices)
%#
%#
%# LICENSE:
%#
%# This work is made available to you under the terms of Version 2 of
%# the GNU General Public License. A copy of that license should have
%# been provided with this software, but in any event can be snarfed
%# from www.gnu.org.
%#
%# This work is distributed in the hope that it will be useful, but
%# WITHOUT ANY WARRANTY; without even the implied warranty of
%# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
%# General Public License for more details.
%#
%# You should have received a copy of the GNU General Public License
%# along with this program; if not, write to the Free Software
%# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
%# 02110-1301 or visit their web page on the internet at
%# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
%#
%#
%# CONTRIBUTION SUBMISSION POLICY:
%#
%# (The following paragraph is not intended to limit the rights granted
%# to you to modify and distribute this software under the terms of
%# the GNU General Public License and is only of importance to you if
%# you choose to contribute your changes and enhancements to the
%# community by submitting them to Best Practical Solutions, LLC.)
%#
%# By intentionally submitting any modifications, corrections or
%# derivatives to this work, or any other work intended for use with
%# Request Tracker, to Best Practical Solutions, LLC, you confirm that
%# you are the copyright holder for those contributions and you grant
%# Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
%# royalty-free, perpetual, license to use, copy, create derivative
%# works based on those contributions, and sublicense and distribute
%# those contributions and any derivatives thereof.
%#
%# END BPS TAGGED BLOCK }}}
% if ( $attachments ) {
<div class="form-row attachment"><div class="label col-3"><&|/l&>Attached file</&>:</div>
<div class="value col-9">
% foreach my $attach_name ( sort keys %$attachments ) {
    <div>
        <label for="DeleteAttach-<%$attach_name%>"><% $attach_name %></label>
%       $m->callback( ARGSRef => \%ARGS, CallbackName => 'BeforeDeleteLink', AttachmentName => $attach_name );
        (<a href="#" class="delete-attach" data-name="<%$attach_name%>"><&|/l&>Delete</&></a>)
    </div>
% } # end of foreach
</div>
</div>
% } # end of if

<div class="form-group">
        <div class="dropzone" id="attach-dropzone">
        </div>
<script type="text/javascript">
var MaxAttachmentSizeMiB = RT.Config.MaxAttachmentSize/(1024*1024);

Dropzone.autoDiscover = false;
jQuery( function() {
    var attachDropzone = new Dropzone('#attach-dropzone', {
        url: RT.Config.WebHomePath + '/Helpers/Upload/Add?Token=' + jQuery('#attach-dropzone').closest('form').find('input[name=Token]').val(),
        paramName: "Attach",
        dictDefaultMessage: <% loc("Drop files here or click to attach") |n,j %>,
        maxFilesize: MaxAttachmentSizeMiB,
        parallelUploads: 1,
        forceFallback: <% RT->Config->Get('PreferDropzone', $session{CurrentUser}) ? 0 : 1 %>,
        previewTemplate: '' +
            '<div class="dz-preview dz-file-preview">' +
            '    <div class="dz-remove-mark pointer-events" data-dz-remove>' +
            '        <svg width="54px" height="54px" viewBox="0 0 54 54" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:sketch="http://www.bohemiancoding.com/sketch/ns">' +
            '            <title>' + loc_key('remove') + '</title>' +
            '            <defs></defs>' +
            '            <g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd" sketch:type="MSPage">' +
            '                <g style="fill:#d9534f" id="Check-+-Oval-2" sketch:type="MSLayerGroup" stroke="#747474" stroke-opacity="0.198794158" fill="#FFFFFF" fill-opacity="0.816519475">' +
            '                    <path d="M32.6568542,29 L38.3106978,23.3461564 C39.8771021,21.7797521 39.8758057,19.2483887 38.3137085,17.6862915 C36.7547899,16.1273729 34.2176035,16.1255422 32.6538436,17.6893022 L27,23.3431458' +
            '                    L21.3461564,17.6893022 C19.7823965,16.1255422 17.2452101,16.1273729 15.6862915,17.6862915 C14.1241943,19.2483887 14.1228979,21.7797521 15.6893022,23.3461564 L21.3431458,29 L15.6893022,34.6538436' +
            '                    C14.1228979,36.2202479 14.1241943,38.7516113 15.6862915,40.3137085 C17.2452101,41.8726271 19.7823965,41.8744578 21.3461564,40.3106978 L27,34.6568542 L32.6538436,40.3106978 C34.2176035,41.8744578' +
            '                    36.7547899,41.8726271 38.3137085,40.3137085 C39.8758057,38.7516113 39.8771021,36.2202479 38.3106978,34.6538436 L32.6568542,29 Z M27,53 C41.3594035,53 53,41.3594035 53,27 C53,12.6405965 41.3594035,1 27,1' +
            '                    C12.6405965,1 1,12.6405965 1,27 C1,41.3594035 12.6405965,53 27,53 Z" id="Oval-2" sketch:type="MSShapeGroup"></path>' +
            '                </g>' +
            '            </g>' +
            '        </svg>' +
            '    </div>' +
            '    <div class="dz-image pointer-events"><img data-dz-thumbnail /></div>' +
            '    <div class="dz-details">' +
            '        <div class="dz-filename"><span data-dz-name></span></div>' +
            '    </div>' +
            '    <div class="dz-progress"><span class="dz-upload" data-dz-uploadprogress></span></div>' +
            '    <div class="dz-error-message"><span data-dz-errormessage></span></div>' +
            '    <div class="dz-success-mark">' +
            '        <svg width="54px" height="54px" viewBox="0 0 54 54" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:sketch="http://www.bohemiancoding.com/sketch/ns">' + 
            '            <title>' + loc_key('check') + '</title>' +
            '            <defs></defs>' +
            '            <g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd" sketch:type="MSPage">' +
            '                <path d="M23.5,31.8431458 L17.5852419,25.9283877 C16.0248253,24.3679711 13.4910294,24.366835 11.9289322,25.9289322 C10.3700136,27.4878508 10.3665912,30.0234455 11.9283877,31.5852419 L20.4147581,40.0716123' +
            '                C20.5133999,40.1702541 20.6159315,40.2626649 20.7218615,40.3488435 C22.2835669,41.8725651 24.794234,41.8626202 26.3461564,40.3106978 L43.3106978,23.3461564 C44.8771021,21.7797521 44.8758057,19.2483887' +
            '                43.3137085,17.6862915 C41.7547899,16.1273729 39.2176035,16.1255422 37.6538436,17.6893022 L23.5,31.8431458 Z M27,53 C41.3594035,53 53,41.3594035 53,27 C53,12.6405965 41.3594035,1 27,1 C12.6405965,1 1,12.6405965' +
            '                1,27 C1,41.3594035 12.6405965,53 27,53 Z" id="Oval-2" stroke-opacity="0.198794158" stroke="#747474" fill-opacity="0.816519475" fill="#FFFFFF" sketch:type="MSShapeGroup"></path>' +
            '            </g>' +
            '        </svg>' +
            '    </div>' +
            '    <div class="dz-error-mark">' +
            '        <svg width="54px" height="54px" viewBox="0 0 54 54" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:sketch="http://www.bohemiancoding.com/sketch/ns">' +
            '            <title>' + loc_key('error') + '</title>' +
            '            <defs></defs>' +
            '            <g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd" sketch:type="MSPage">' +
            '                <g id="Check-+-Oval-2" sketch:type="MSLayerGroup" stroke="#747474" stroke-opacity="0.198794158" fill="#BE2626" fill-opacity="0.816519475">' +
            '                    <path d="M32.6568542,29 L38.3106978,23.3461564 C39.8771021,21.7797521 39.8758057,19.2483887 38.3137085,17.6862915 C36.7547899,16.1273729 34.2176035,16.1255422 32.6538436,17.6893022 L27,23.3431458' +
            '                    L21.3461564,17.6893022 C19.7823965,16.1255422 17.2452101,16.1273729 15.6862915,17.6862915 C14.1241943,19.2483887 14.1228979,21.7797521 15.6893022,23.3461564 L21.3431458,29 L15.6893022,34.6538436' +
            '                    C14.1228979,36.2202479 14.1241943,38.7516113 15.6862915,40.3137085 C17.2452101,41.8726271 19.7823965,41.8744578 21.3461564,40.3106978 L27,34.6568542 L32.6538436,40.3106978 C34.2176035,41.8744578' +
            '                    36.7547899,41.8726271 38.3137085,40.3137085 C39.8758057,38.7516113 39.8771021,36.2202479 38.3106978,34.6538436 L32.6568542,29 Z M27,53 C41.3594035,53 53,41.3594035 53,27 C53,12.6405965 41.3594035,1 27,1' +
            '                    C12.6405965,1 1,12.6405965 1,27 C1,41.3594035 12.6405965,53 27,53 Z" id="Oval-2" sketch:type="MSShapeGroup"></path>' +
            '                </g>' +
            '            </g>' +
            '        </svg>' +
            '    </div>' +
            '</div>',
        init: function() {
            jQuery('#attach-dropzone').triggerHandler('dropzone-init');
            jQuery('#attach-dropzone').addClass('dropzone-init');
        },
        fallback: function() {
            jQuery('#attach-dropzone').triggerHandler('dropzone-fallback');
            jQuery('#attach-dropzone').addClass('hidden');
            jQuery('#attach-dropzone').siblings('div.old-attach').removeClass('hidden');
        }
    });
    attachDropzone.on('removedfile', function(file) {
        jQuery.post( RT.Config.WebHomePath + '/Helpers/Upload/Delete', { Name: file.name, Token: jQuery('#attach-dropzone').closest('form').find('input[name=Token]').val() }, 'json');
    });
    attachDropzone.on('dragenter', function() {
        jQuery('#attach-dropzone').addClass('scaled');
    });
    attachDropzone.on('dragleave', function() {
        jQuery('#attach-dropzone').removeClass('scaled');
    });
    attachDropzone.on('reset', function() {
        jQuery('#attach-dropzone').removeClass('scaled has-attachments');
        jQuery('#attach-dropzone').triggerHandler('attachment-change');
    });
    var submit_input = jQuery('#attach-dropzone').closest('form').find('div.submit :submit:visible:last');
    var submit_input_label = submit_input.attr('value');
    attachDropzone.on('addedfile', function() {
        jQuery('#attach-dropzone').addClass('has-attachments');
        jQuery('#attach-dropzone').triggerHandler('attachment-change');

        submit_input.prop('disabled', true).attr('value', <% loc('Uploading...') |n,j %>);
    });
    attachDropzone.on('queuecomplete', function() {
        submit_input.prop('disabled', false).attr('value', submit_input_label);
    });
});
</script>
        <div class="hidden old-attach">
          <div class="custom-file">
            <input type="file" id="Attach" name="Attach" class="custom-file-input" />
            <label class="custom-file-label" for="Attach">
              <input type="submit" class="button" name="AddMoreAttach" value="<&|/l&>Add More Files</&>" />
            </label>
          </div>
        </div>
</div>
% if ($HasExisting) {
<div class="form-row">
  <div class="label col-3"><&|/l&>Include attachments</&>:</div>
  <div class="value col-9" id="reuse-attachments">
    <& /Ticket/Elements/ShowAttachments,
      Ticket       => $TicketObj,
      Selectable   => 1,
      HideTitleBox => 1,
      Checked      => \@AttachExisting,
      Count        => RT->Config->Get('AttachmentListCount'),
    &>
  </div>
</div>
% }
% $m->callback( %ARGS, CallbackName => 'End' );
<%ARGS>
$Token => ''
@AttachExisting => ()
$QuoteTransaction => ''
$TicketObj => undef
</%ARGS>
<%INIT>
my $attachments;
if ( exists $session{'Attachments'}{ $Token } && keys %{ $session{'Attachments'}{ $Token } } ) {
    $attachments = $session{'Attachments'}{ $Token };
}

my $HasExisting = 0;

if ($TicketObj && $TicketObj->id) {
    my $Existing = $TicketObj->Attachments;
    $Existing->LimitHasFilename;
    $HasExisting = 1 if $Existing->Count;
}
</%INIT>
