#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package RPS::LabelRoyalty::Command::Create;
use strict;
use warnings;
use Data::Dumper;

use lib '/app/tools/rps/lib';
use RPS::LabelRoyalty::Command::Show;
use RPS::LabelRoyalty::Command::List;
use RPS::Command;
use RPS::Payor::Payor;
use RPS::LabelRoyalty::CreateRunForm;
use base 'RPS::Command';

use lib '/app/tools/common/lib';
use RSApache::Response::XSLT;

sub cmd  { return 'create'; }
sub area { return 'label_royalty'; }

use constant kTemplate => "/app/tools/rps/templates/label_royalty/index.xsl";

sub execute {
    my ($self) = @_;

    my $response = $self->SUPER::execute();
    return $response if $response;

    my $form = RPS::LabelRoyalty::CreateRunForm->new();

    if ( $self->getParam('formSubmit') ) {

        # Apply cgi parameters, then save changes if all went well.
        #
        if ( $form->assignCGIParams( 'Form', $self->getParams() ) && $form->validate() ) {

            # Not a 'save' form.
            # Instead, we want to queue the run.
            #
            $form->save();

            # on a 'regular' save, we will refresh back to the Detail page.
            # on a 'save and new', we refresh back to a blank Edit screen (with a success message).
            #
            my $successMessage = RPS::Message->new( type => "success", code => Common::FormObject::kSuccessFormSubmit );

            my $command = RPS::LabelRoyalty::Command::List->new( msg => $successMessage->toString, );

            my $redirectResponse = RSApache::Response::Redirect->new( $command->getRedirect() );
            return $redirectResponse;

        } else {

            # We stay on the same page, and display our errors.
            #
            $form->setError(Common::FormObject::kErrFormSubmit);
            $self->addMessageXML( type => "error", code => Common::FormObject::kErrFormSubmit );
        }
    }

    $self->{xml}{Form} = $form;

    $response = RSApache::Response::XSLT->new( $self->{xml}, kTemplate );
    return $response;
}

1;
