<& /Elements/Header, Title => loc("Assets") &>
<& /Elements/Tabs &>

% $m->callback(CallbackName => 'BeforeActionList', ARGSRef => \%ARGS, Assets => $assets, Actions => \@results);

<& /Elements/ListActions, actions => \@results &>

<form method="post" action="<% RT->Config->Get('WebPath') %>/Asset/Search/Bulk.html" enctype="multipart/form-data" name="BulkUpdate" id="BulkUpdate">
% foreach my $var ( @{$search{'PassArguments'}} )  {
<input type="hidden" class="hidden" name="<% $var %>" value="<% $ARGS{$var} || '' %>" />
% }
% foreach my $var (qw(Query Format OrderBy Order Rows Page Token)) {
<input type="hidden" class="hidden" name="<%$var%>" value="<%$ARGS{$var} || ''%>" />
%}
<& /Elements/CollectionList,
    %search,
    Collection      => $assets,
    AllowSorting    => 1,
    DisplayFormat   => $DisplayFormat,
    &>
% if (not $assets->Count) {
<em><&|/l&>No assets matching search criteria found.</&></em>
% }

<& /Elements/Submit,
    Name => 'Update',
    Label => loc('Update'),
    CheckboxNameRegex => '/^UpdateAsset(All)?$/',
&>
<div class="submit">
  <div class="buttons">
  <span class="caption"></span>
  <input type="submit" id="bulk-update-create-linked-ticket" name="CreateLinkedTicket"  value="Create Linked Ticket" class="button" alt="Create a ticket with checked assets linked" />
  </div>
</div>

<&| /Widgets/TitleBox, title => loc("Basics"), class => "asset-basics asset-bulk-basics", title_class => "inverse" &>
<table>
  <tr class="asset-catalog">
    <td class="label"><label for="UpdateCatalog"><&|/l&>Catalog</&></label></td>
    <td><& /Asset/Elements/SelectCatalog, Name => 'UpdateCatalog', UpdateSession => 0, ShowNullOption => 1 &></td>
  </tr>
  <tr class="asset-status">
    <td class="label"><label for="UpdateStatus"><&|/l&>Status</&></label></td>
    <td><& /Asset/Elements/SelectStatus, Name => 'UpdateStatus', DefaultValue => 1 &></td>
  </tr>
</table>
</&>

<&| /Widgets/TitleBox, title => loc("People"), class => "asset-people asset-bulk-people", title_class => "inverse" &>
<table>
% for my $rname ( $asset->Roles( ACLOnly => 0 ) ) {
% my $role = $asset->Role( $rname );
% if ( $role->{'Single'} ) {
% my $input = "SetRoleMember-$rname";
<tr class="full-width">
<td class="label"><label for="<% $input %>"><% loc($rname) %></label></td>
<td><input type="text" value="<% $ARGS{ $input } || '' %>" name="<% $input %>" id="<% $input %>" data-autocomplete="Users" data-autocomplete-return="Name" /></td>
</tr>
% } else {
<tr>
% my $input = "AddRoleMember-$rname";
<td class="label"><label for="<% $input %>"><% loc("Add [_1]", loc($rname)) %></label></td>
<td><input type="text" value="<% $ARGS{ $input } || '' %>" name="<% $input %>" id="<% $input %>" data-autocomplete="Users" data-autocomplete-return="Name" /></td>
</tr>

<tr>
% $input = "RemoveRoleMember-$rname";
<td class="label"><label for="<% $input %>"><% loc("Remove [_1]", loc($rname)) %></label></td>
<td>
  <input type="text" value="<% $ARGS{ $input } || '' %>" name="<% $input %>" id="<% $input %>" data-autocomplete="Users" data-autocomplete-return="Name" />
  <label>
    <input type="checkbox" name="RemoveAllRoleMembers-<% $rname %>" value="1"/>
    <em><&|/l&>(Check to delete all values)</&></em>
  </label>
</td>
</tr>
% }
% }
</table>
</&>

% for my $group ( RT::CustomField->CustomGroupings( 'RT::Asset' ), '' ) {
%   my $cfs = $catalog_obj->AssetCustomFields;
%   $cfs->LimitToGrouping( 'RT::Asset' => $group);
%   if ( $cfs->Count ) {
<&| /Widgets/TitleBox, class=>'asset-bulk-grouping asset-bulk-cfs', title => loc('Edit [_1]', ($group? loc($group) : loc('Custom Fields')) ) &>
<& /Elements/BulkCustomFields, CustomFields => $cfs, &>
</&>
%   }
% }

<&|/Widgets/TitleBox, title => loc('Edit Links'), color => "#336633"&>
<& /Elements/BulkLinks, Collection => $assets, %ARGS &>
</&>

<& /Elements/Submit, Label => loc('Update'), Name => 'Update' &>
</form>

<%INIT>
my @results;
$m->callback(ARGSRef => \%ARGS, Results => \@results, CallbackName => 'Initial');

my $catalog_obj = LoadDefaultCatalog($ARGS{'Catalog'} || '');
$ARGS{'Catalog'} = $catalog_obj->Id;

my $assets = RT::Assets->new($session{CurrentUser});
my %search;
if ( $ARGS{Query} ) {
    $assets->FromSQL($ARGS{Query});
}
else {
    %search = ProcessAssetsSearchArguments(
        Assets => $assets, Catalog => $catalog_obj, ARGSRef => \%ARGS,
    );
}
$search{Format} ||= ProcessAssetSearchFormatConfig;

my $DisplayFormat = "'__CheckBox.{UpdateAsset}__',". ($ARGS{Format} || $search{'Format'});
$DisplayFormat =~ s/\s*,\s*('?__NEWLINE__'?)/,$1,''/gi;

my $asset = RT::Asset->new( $session{'CurrentUser'} );

delete $ARGS{$_} foreach grep { $ARGS{$_} =~ /^$/ } keys %ARGS;

$DECODED_ARGS->{'UpdateAssetAll'} = 1 unless @UpdateAsset;

if ( $ARGS{'CreateLinkedTicket'} ){
    my $url = RT->Config->Get('WebURL') . "Asset/CreateLinkedTicket.html";
    $url .= '?'. $m->comp( '/Elements/QueryString', 'Assets' => \@UpdateAsset );
    RT::Interface::Web::Redirect($url);
}
elsif ( $ARGS{Update} ) {
    my @attributes  = $asset->WritableAttributes;
    @attributes = grep exists $ARGS{ 'Update'. $_ }, @attributes;
    my %basics = map { $_ => $ARGS{ 'Update'. $_ } } @attributes;

    foreach my $aid ( @UpdateAsset ) {
        my $asset = LoadAsset($aid);

        my @tmp_res;
        push @tmp_res, UpdateRecordObject(
            Object          => $asset,
            AttributesRef   => \@attributes,
            ARGSRef         => \%basics,
        );
        push @tmp_res, ProcessAssetRoleMembers( $asset => %ARGS );
        push @tmp_res, ProcessObjectCustomFieldUpdates( Object => $asset, ARGSRef => \%ARGS );
        push @tmp_res, ProcessRecordLinks( RecordObj => $asset, RecordId => 'Asset', ARGSRef => \%ARGS );
        push @tmp_res, ProcessRecordBulkCustomFields( RecordObj => $asset, ARGSRef => \%ARGS );
        push @results, map { loc( "Asset #[_1]: [_2]", $asset->id, $_ ) } @tmp_res;
    }

    MaybeRedirectForResults(
        Actions     => \@results,
        Arguments   => { map { $_ => $ARGS{$_} } grep { defined $ARGS{$_} } @{$search{'PassArguments'}}, qw(Query Format OrderBy Order Rows Page Token) },
    );
}
</%INIT>
<%ARGS>
@UpdateAsset => ()
</%ARGS>