%# BEGIN BPS TAGGED BLOCK {{{
%#
%# COPYRIGHT:
%#
%# This software is Copyright (c) 1996-2025 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 }}}
<& /Admin/Elements/Header, Title => $title &>
<& /Elements/Tabs &>
<& /Elements/ListActions, actions => \@results &>
<div class="row mt-2">
  <div class="col-12">
    <p><&|/l&>Create statuses for your lifecycle and define the transitions allowed between them.</&></p>
  </div>
</div>

<script type="text/javascript" src="<%RT->Config->Get('WebPath')%>/static/js/d3.min.js"></script>
<script type="text/javascript" src="<%RT->Config->Get('WebPath')%>/static/js/lifecycleui-editor.js"></script>

<form action="<%RT->Config->Get('WebPath')%>/Admin/Lifecycles/Modify.html" name="ModifyLifecycle" method="post" enctype="multipart/form-data" class="mx-auto max-width-xl">
  <input type="hidden" class="hidden" name="Name" value="<% $Name %>" />
  <input type="hidden" class="hidden" name="Type" value="<% $Type %>" />
  <input type="hidden" class="hidden" name="Update" value="1" />
  <input type="hidden" class="hidden" name="Config" />
  <input type="hidden" class="hidden" name="Layout" />
  <input type="hidden" class="hidden" name="Maps" />

  <& /Elements/Lifecycle/Graph, Name => $Name, Config => $Config, Layout => $Layout, Maps => $Maps &>
  <& /Elements/Submit, Name => 'Update', Label => loc('Save Changes'), FullWidth => 1 &>
</form>
<%INIT>

my ($title, @results);
my $LifecycleObj = RT::Lifecycle->new();
$LifecycleObj->Load(Name => $Name, Type => $Type);
Abort( loc("Invalid lifecycle") ) unless $LifecycleObj->Name && $LifecycleObj->{data}{type} eq $Type;

$title = loc("Modify lifecycle [_1]", $LifecycleObj->Name);

if ( $Update ) {
    my ( $ok, $msg ) = RT::Lifecycle->UpdateLifecycleLayout(
        CurrentUser  => $session{CurrentUser},
        LifecycleObj => $LifecycleObj,
        NewLayout    => $Layout ? JSON::from_json($Layout) : undef,
    );


    if ( $ok ) {
        push @results, loc('Lifecycle layout updated');
    }
    elsif ($msg
        && $msg ne loc('That is already the current value')
        && $msg ne loc( '[_1] update: Nothing changed', "LifecycleLayout-$Name" ) )
    {
        # This is just svg layout change, no need to notify user if nothing changed.
        push @results, $msg;
    }

    my $lifecycle = JSON::from_json($Config);
    $lifecycle->{type} = $Type;

    my ( $valid, @warnings )
        = $LifecycleObj->ValidateLifecycle( Lifecycle => $lifecycle, CurrentUser => $session{CurrentUser} );

    if ($valid) {
        my ( $ok, $msg ) = RT::Lifecycle->UpdateLifecycle(
            CurrentUser  => $session{CurrentUser},
            LifecycleObj => $LifecycleObj,
            NewConfig    => $lifecycle,
            Maps         => JSON::from_json($Maps),
        );
        if ($ok) {
            push @results, loc('Lifecycle updated');
        }
        else {
            push @results, $msg;
        }
    }
    else {
        push @results, @warnings;
    }
}
else {
    $Config ||= JSON( RT->Config->Get('Lifecycles')->{$Name} );
    $Maps ||= JSON( RT->Config->Get('Lifecycles')->{__maps__} || {} );

    unless ($Layout) {
        my $conf = RT::Configuration->new( $session{CurrentUser} );
        $conf->LoadByCols( 'Name' => "LifecycleLayout-$Name", Disabled => 0 );
        $Layout = $conf->Id ? JSON( $conf->_DeserializeContent( $conf->Content ) ) : '';
    }
}

# This code does automatic redirection if any updates happen.
MaybeRedirectForResults(
    Actions   => \@results,
    Arguments => { Name => $Name, Type => $Type, Config => $Config },
);
</%INIT>
<%ARGS>
$Name   => undef
$Type   => undef
$Config => undef
$Layout => undef
$Maps   => undef
$Update => undef
</%ARGS>
