#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package RPS::License::Command::BaseAccountEdit;

use strict;
use warnings;
use Data::Dumper;

use lib '/app/tools/rps/lib';
use RPS::Catalog::Command::Search;

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

use RPS::Command;
use base 'RPS::Command';

sub cmd { return 'account'; }

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

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

    my $trackLicenseID = $self->getParam('TrackLicenseID');
    my $form;

    if ($trackLicenseID) {
        $form = $self->_form()->new( trackLicenseID => $trackLicenseID );
    } else {
        my $command = RPS::Catalog::Command::Search->new();
        return RSApache::Response::Redirect->new( $command->getRedirect() );
    }

    if ( $self->getParam('formSubmit') ) {
        if ( $form->assignCGIParams( 'Form', $self->getParams() ) && $form->validate() ) {
            $form->save();

            # Redirect to the Show command.
            #
            my $command;
            my $successMessage = RPS::Message->new( type => "success", code => Common::FormObject::kSuccessFormSubmit );
            my %showParams = (
                TrackLicenseID => $form->trackLicenseID(),
                msg            => $successMessage->toString(),
            );
            $command = $self->_command()->new(%showParams);
            return RSApache::Response::Redirect->new( $command->getRedirect() );
        } else {
            $form->setError(Common::FormObject::kErrFormSubmit);
            $self->addMessageXML( type => "error", code => Common::FormObject::kErrFormSubmit );
        }
    }

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

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

    return $response;
}

1;
