#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package RPS::Mechanical::UK::Command::EditMcpsLicense;
use strict;
use warnings;

use Data::Dumper;

use lib '/app/tools/rps/lib';
use RPS::Mechanical::UK::McpsLicenseForm;

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

use RPS::Catalog::Command::Show;
use RPS::Mechanical::UK::Command::ShowMcpsLicense;
use RPS::DB::Item::UKMechanicalRun;
use RPS::Command;
use RPS::CommandLog;
use base 'RPS::Command';

sub cmd  { return 'edit_mcps_license'; }
sub area { return 'uk_mech'; }

use constant kTemplate => "/app/tools/rps/templates/uk_mech/edit_mcps_license.xsl";

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


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

	my $productID = $self->getParam('ProductID');
	my $form;
	if($productID)
	{
		$form = RPS::Mechanical::UK::McpsLicenseForm->new(productID => $productID);
	}


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


			$form->save();


            # Command succeeded, log it.
            #
            RPS::CommandLog::Log($self, $productID);


			# 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->{AlbumID} = $form->Product()->AssetID();
            $showParams->{msg} = $successMessage->toString();

            # Try to land on the UK License tab.
            #
            $showParams->{tab} = RPS::Catalog::Command::Show::kTabUKLicenses;

			my $command = RPS::Catalog::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;
