#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package Admin::Mcps::Command::Edit;

use Data::Dumper;

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

use lib '/app/tools/rps/lib';
use RPS::Product::TypeList;
use RPS::CountryList;
use RPS::DB::Item::UKMechanicalRun;

use lib '/app/tools/admin/lib';
use Admin::Mcps::DefaultRoyaltyRateList;
use Admin::Mcps::ExportAllowanceList;
use Admin::Mcps::ExportUpliftHash;
use Admin::Mcps::RetentionRateList;
use Admin::Mcps::SalesPricingMode;
use Admin::Mcps::Form;
use Admin::Mcps::Command::Show;
use Admin::Command;
use base 'Admin::Command';

use constant kTemplate => '/app/tools/admin/templates/mcps/edit.xsl';

sub cmd  { return "edit"; }
sub area { return "mcps"; }

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

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

    my $form = Admin::Mcps::Form->new();

    if ( $self->getParam('formSubmit') ) {
        if ( $form->assignCGIParams( 'Form', $self->getParams() ) && $form->validate() && $openMcpsRuns == 0 ) {

            $form->save();

            # We want to change views, so we want to redirect to the Show command now.
            #
            my $successMessage = RPS::Message->new( type => "success", code => Common::FormObject::kSuccessFormSubmit );
            my $showParams = $self->getPassThroughParams();
            $showParams->{msg} = $successMessage->toString();

            my $command = Admin::Mcps::Command::Show->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}, kTemplate );

    return $response;
}

1;
