#!/usr/bin/perl
# $Id: search_safety_task.cgi,v 1.2 2016/06/17 12:44:45 sqko Exp $
############################################################
############################################################
###                                                      ###
###   Originally designed by Soeren Koch                 ###
###                                                      ###
###   Impelemented by Soeren Koch                        ###
###                                                      ###
###   Copyright by Soeren Koch 2016                      ###
###                                                      ###
############################################################
############################################################

use strict;
use warnings;
use CGI qw(:standard :netscape);
use RFC::Main;
use RFC::Rig;
use RFC::RFCCGI qw(:cgi :html);
use RFC::Header;

my $acmode = get_cv('global','access_mode');
$acmode = 'celltest' unless ($acmode);

my $r = param('rig');
unless ($r)
{
    print header();
    print "No rig specified!\n";
    exit;
}
my $rig = RFC::Rig->new($r);
my $rig_setup = $rig->get_cv('main','main_page');
if ($rig_setup && -e '/home/http/cgi-bin/celltest/'.$rig_setup)
{
    &print_header('Redirecting',-onload=>'document.redirect.submit()');
    print start_form(-name=>'redirect',-action=>$rig_setup);
    &print_hidden_rig();
    print end_form();
    print end_html;
    exit;
}

my $readmode = undef;
my $usermode = undef;
my $modifymode = undef;

if (!defined(param))
{
    &print_header("Please log in to the $name");
    print '<center>';
    RFC::RFCCGI::login();
    print '</center>';
    &print_end;
}
else
{
    if (login_ok())
    { 
	&print_header("Rig ".$rig->name);
        print RFC_start_html(-title=>"Show missing safety clearances for ".$rig->name, -style=>{src=>'/celltest/RFCcontrol.css'});
        top_nav_bar_start;
	top_no_button;
	top_no_button;
	top_no_button;
	tab_newrow;
	&js_back();
	top_js_return('#006600','white');
	top_no_button;
	top_no_button;
	top_no_button;
	top_nav_bar_button('celltest/main.cgi','','','To menu','s4');
	end_top_bar;
	&main_page;
	print_end;
    }
    else 
    {
	&not_auth;
    }
}

$rig = undef;    
exit;



sub main_page
{
    print '<center>';
    my $rig = &get_CGI_value_clean('rig');
    if (@_)
    {
        my $r = shift;
        if (ref($r) eq 'RFC::Rig')
        {
            $rig = $r->name;
        }
        else
        {
            $rig = $r;
        }
    }
    $rig =~ s/[\r\n]*//g if (defined($rig));
    my $user = &get_user();
    if ($rig && $rig =~ /(\d+)/)
    {
	$rig = $1;
    }
    my $taskid = &get_cv('safety_task_access','rig'.$rig);
    if ($taskid && $taskid!~/NONE/)
    {
	print h1('The safet clerances nescesary for this rig is:');
	print '<table border=0 cellspacing=10>';		
	foreach my $id (split(/\s*\,\s*/,$taskid)) 
	{
	    my $ok = &RFC::Main::passwd_client('check_task_status',$user,$id);
	    my $n = &RFC::Main::passwd_client('task_name',&get_user,&get_pass(),$id);
	    print '<tr><th>';
	    print $id;
	    print '</th><th>';
	    print $n;
	    print '</th><th>';
	    if ($ok)
	    {
		print 'OK';
	    }
	    else
	    {
		print 'Not authorised';
	    }
	    print '</th></tr>';
	}
	print '</table>';
    }
    print '</center>';
}    
