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

use lib '/app/tools/rps/lib';
use RPS::Region::Command::Edit;
use RPS::Region::Command::List;
use RPS::Region::Region;

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

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

sub cmd  { return 'delete'; }
sub area { return 'region'; }

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

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

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

    my $id = $self->getParam('RegionID');
    my $region;

    if ($id) {
        $region = RPS::Region::Region->new( regionID => $id );
    } else {
        my $command = RPS::Region::Command::List->new();
        return RSApache::Response::Redirect->new( $command->getRedirect() );
    }

    if ( $region->delete() ) {

        # Redirect to the Show command.
        #
        my $successMessage = RPS::Message->new( type => "success", code => Common::FormObject::kSuccessFormSubmit );
        my $command = RPS::Region::Command::List->new( msg => $successMessage->toString() );
        return RSApache::Response::Redirect->new( $command->getRedirect() );
    } else {
        my $errorMsg = RPS::Message->new( type => "error", code => Common::FormObject::kErrDelete );
        my %showParams = (
            RegionID => $id,
            msg      => $errorMsg->toString(),
        );
        my $command = RPS::Region::Command::Edit->new(%showParams);
        return RSApache::Response::Redirect->new( $command->getRedirect() );
    }
}

1;
