# Decision ---------------------------------------------------------------------

# decision colors, titles
# note UC values .. unlocked, lc values .. locked for change
my @decisions = ( 'or', 'bo', 'po', 'bp', 're' );
my @ucdecisions = ( 'OR', 'BO', 'PO', 'BP', 'RE' );
my %cd = (  'or' => '#AAFFAA', 'bo' =>'#DDFFDD',
            'po' => '#AAAAFF', 'bp' => '#DDDDFF',
            're' => '#FFAAAA', 'un' => 'FFFF00' );
my %td = ( 'or' => 'oral',
           'bo' => 'border oral/poster',
           'po' => 'poster',
           'bp' => 'border poster/reject',
           're' => 'reject',
           'un' => 'undecided' );

sub SQL_PAPER_UN
{
    my ($where) = @_;
    my $select = 
    'SELECT paper.id,paper.title,accept_reject,s.recommendation AS ac ' .
        'FROM paper ' .
        'LEFT JOIN synthese AS s ON paper.id=s.paper_id ' .
        "WHERE NOT( paper.accept_reject IN ('" .
            join( "','", @decisions ) . "','" .
            uc(join( "','", @decisions )) . "') <=> 1)";

    $where and $select .= "\nAND $where";

    if( DEBUG() ) { dprintsql( 'SQL_PAPER_UN', $select ); }
    return $select;
}

sub SQL_COUNT_DEC
{
    my $ac = shift;
    my $tx = "SELECT ";
    ForAllDecisions( sub {
        $tx .= "COUNT(IF(BINARY accept_reject='$_[0]',1,NULL)) as cnt$_[0], ";
       $tx .= "COUNT(IF(BINARY accept_reject='" .uc($_[0])."',1,NULL)) as cnt" .
            uc($_[0]) .",\n";
    }  );

    $tx .= "COUNT(IF(BINARY accept_reject='un',1,NULL)) as cntun, ";

    
    $tx .= 'COUNT(1) as cnt FROM paper ';
    $ac and $tx .= ' LEFT JOIN synthese ON paper.id=synthese.paper_id ';
    $tx .= 'WHERE paper.confirm=1';
    $ac and $tx .= " AND synthese.editor_id='$ac'";

    if( DEBUG() ) { dprintsql( 'SQL_COUNT_DEC', $tx ); }
    
    return $tx;
}

sub CountDec
{
    my ( $sock, $ac ) = @_;
    my $sth = $sock->prepare( SQL_COUNT_DEC( $ac ) )
        or CgiDie( "ERROR: ".$sock->errstr );
    $sth->execute() or CgiDie( "ERROR: ".$sock->errstr );
    my $cnt = $sth->fetchrow_hashref() or CgiDie( "internal err" );
    my $n = 0;
    foreach my $k (keys %{$cnt})
    {
        $n += $cnt->{$k};
    }
    $cnt->{'cntUN'} = 2 * $cnt->{'cnt'} - $n;

    return $cnt;
}


sub Decisions { return \@decisions; }
sub UcDecisions { return \@ucdecisions; }

sub ForAllDecisions
{
    my $dispatch = shift;
    foreach my $d (@decisions)
    {
        &{$dispatch}( $d, $td{$d} );
    }
}

sub ForAllDecisionsUn
{
    my $dispatch = shift;
    foreach my $d (( @decisions, 'un' ))
    {
        &{$dispatch}( $d, $td{$d} );
    }
}

sub DecisionHead
{
    my ( $lock ) = @_;
    defined( $lock ) or $lock = 1;

    foreach my $d ( (@decisions, 'un' ) ) {
        print "<th bgcolor=$cd{$d}><a name='x'  title='$td{$d}'>", 
        substr($d,0,2), "</a>";
    }

    
    $lock and print "<th>Lck";
}

sub OneDecisionFull
{
    my ( $val ) = @_;

    $val or $val = 'UN';

    if( $val =~ /^[a-z]/ )
    {
        print "<span style='background-color:".$cd{$val}."'>";
        print $td{$val};
        print "</span>";
    } else {
        $val = lc( $val );
        print "<span style='background-color:".$cd{$val}."'>";
        print $td{$val};
        print "</span>";
    }
     

    
}

sub Decision
{
    my ( $id, $val, $lock ) = @_;

    defined( $lock ) or $lock = 1;
    defined( $val ) or $val = 'UN';

    # locked
    defined( $val ) and $val =~ /^[a-z]/ and do { 
        foreach my $d ( ( @decisions , 'un' ) ) {
            print "<td align='center'", $d eq $val ? " bgcolor=$cd{$d}>" : '>';
        }
        $lock>0 and print "<td align='center'><a title='unlock'>".
            "<input type=checkbox name=unlock$id></a>";
        return;
    };

    # unlocked
    defined( $val ) and $val = lc($val);

    foreach my $d ( ( @decisions, 'un' ) ) {
        print "<td align='center'", $d eq $val ? " bgcolor=$cd{$d}>" : '>';
        print "<a title='$td{$d}'>".
            "<input type=radio name='decision$id' value='".uc($d)."'",
            , $lock >= 0 ? '' : ' disabled', "></a>";
    }
    $lock>0 and print "<td align='center' bgcolor=$cd{'un'}><a title='lock'>".
        "<input type=checkbox name=lock$id></a>";
}


#  sub DecisionLocked
#  {
#      my ( $id, $val ) = @_;

#      foreach my $d ( @decisions ) {
#          print $d eq $val ? 
#              "<td bgcolor=$cd{$d}>&nbsp;" . substr($d,0,2). "&nbsp;" : "<td>";
#      }

#      print "<a name=\"set_$id\">";
#      print "<td><input type=checkbox name=unlock$id></a>";
#  }

#  sub DecisionBtns
#  {
#      my ( $id, $val, $enbl ) = @_;
#      $enbl = $enbl || '';

#      foreach my $d ( @decisions ) {
#          print "<td><input TYPE=RADIO alt = \"$d\" NAME=\"decision$id\" ",
#          "VALUE=\"$d\" ",($d eq $id ? "checked" : ''), " $enbl>";
#      }
#      print "<td>";
#  }

1;
