#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
#------------------------------------------------------------
package Support::JobQueue::Command::EditRules;
use strict;
use warnings;

use Data::Dumper;

use lib '/app/tools/support/lib';
use Support::JobQueue::SkipRuleForm;
use Support::JobQueue::RunRuleForm;
use Support::JobQueue::HostClassForm;

use base 'Support::Command::Submit';

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

use lib '/app/tools/job/lib';

sub cmd  { return 'rules'; }
sub area { return 'job_queue'; }

use constant kTemplate => "/app/tools/support/templates/job_queue/editrules.xsl";

sub write_command { 1; }

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

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

    # Going to have 2 forms on this page.
    # - The global 'skip rules'.
    # - The host-specific 'run rules'.

    my $hostname = $self->getParam('host');

    # !!! I will have _multiple_ submits, one for each form.
    # !!! Just want to verify the actual form that was updated...
    #
    # ... we'll set that up later.
    #
    my $skipForm      = Support::JobQueue::SkipRuleForm->new();
    my $runForm       = Support::JobQueue::RunRuleForm->new( hostname => $hostname );
    my $hostClassForm = Support::JobQueue::HostClassForm->new( hostname => $hostname );

    # Which form are we updating?
    #
    my $action = $self->getParam('action');
    if ( 'skip' eq $action ) {
        if ( $skipForm->assignCGIParams( 'SkipRuleForm', $self->getParams() ) && $skipForm->validate() ) {
            $skipForm->save();
            $self->{xml}{Message} = "Update Successful";
        } else {
            $self->{xml}{Error} = "Validation Failed";
        }
    }

    # We'll want to list all the hosts that have any entries of any sort in the job_run_rule table.
    #
    #    my @hosts = Job::DB::Item::JobRunRule->GetAllHosts();
    #    $self->{xml}{HostList}{Host} = \@hosts;

    $self->{xml}{SkipRuleForm}  = $skipForm;
    $self->{xml}{RunRuleForm}   = $runForm;
    $self->{xml}{HostClassForm} = $hostClassForm;

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

    return $response;
}

1;
