%# No one can make you feel inferior without your consent.
%#		-- Eleanor Roosevelt
<& /Edit/Elements/PopHeader, Title => loc('Pick'), $IsWork ? (BG => '#C0EBEB') : () &>

<input type="hidden" name="Queue" value="<% $Queue %>">
<input type="hidden" name="CurrentUser" value="<% $CurrentUser %>">

<table border="0" cellspacing="0" cellpadding="5" class="poptable" align="center">
  <tr> 
      <td height="18"> 
      <table border="0" cellspacing="2" cellpadding="0" align="center">
        <tr> 
          <td> 
            <select name="Group" style="width:250px" onchange="this.form.submit()">
              <option value=''>(<&|/l&>Group Name</&>)</option>
% foreach my $g (@$List) {
%   my $selected = 'SELECTED' if $Group eq $g->Id;
                <option <% $selected %> value="<% $g->Id %>"><% $g->Name %></option>
% }
            </select>
          </td>
        </tr>
        <tr>
          <td>
            <select name="Job" style="width:250px" onchange="this.form.submit()">
              <option value=''>(<&|/l&>Position Name</&>)</option>
% foreach my $j (@$Jobs) {
%   my $selected = 'SELECTED' if $Job eq $J{$j};
                <option <% $selected %> value="<% $J{$j} %>"><% $j %></option>
% }
            </select>
          </td>
        </tr>
        <tr>
          <td> 
            <select name="User" style="width:250px; height:300px" size="22">
% while (my $User = $Users->Next) {
%     next if $Job and $User->val('job') ne $Job;
%     next unless $User->PrincipalObj->HasRight( Right => 'OwnTicket', Object => $QueueObj);
		<option value="<% $User->Id %>"><% $User->get('no') %> <% $User->RealName %> <% $User->get('job') %></option>
% }
            </select>
          </td>
        </tr>
      </table>
      </td>
    </tr>
  </table>

<& /Edit/Elements/PopFooter &>
<%INIT>

my $QueueObj = RT::Queue->new($session{'CurrentUser'});
$QueueObj->Load($Queue) or Abort("Cannot load queue $Queue");

if ($User) {
    foreach my $User (@{$QueueObj->AdminCc->UserMembersObj->ItemsArrayRef}) {
	$QueueObj->AdminCc->DeleteMember($User->PrincipalId);
    }
    if ($User) {
	my ($val, $msg) = $QueueObj->AdminCc->AddMember($User);
	Abort($msg) unless $val;
    }
    $QueueObj->UpdateCompany if $QueueObj->can('UpdateCompany');
    $m->print("<script>opener.location.reload();window.close();</script>");
    $m->abort;
}

my $U = $session{'CurrentUser'}->UserObj;
my $J = $U->table($U->normalize($U->table('employee'), 'job'));
my $company = $U->val('company') || 1;
my %J = map {($_->{job_cname}, $_->{job_id})}
	grep { $_->{company_id} eq $company }
	values %$J;
my $Jobs;
@$Jobs = sort keys %J;

my $Groups = RT::Groups->new($session{'CurrentUser'});
$Groups->LimitToUserDefinedGroups();
my $List = $Groups->ItemsArrayRef;
@$List = ( map { $_->[0] } sort { $a->[1] cmp $b->[1] } map { [ $_ => 
    (int($_->Description) ? 1 : 0) . $_->Name
] } grep {
    $_->Description >= 0
} grep { $_->Name } @$List );

my $Users = $RT::Nothing;
if ($Group or $Job) {
    $Users = RT::Users->new($session{'CurrentUser'});
    $Users->Limit( FIELD => 'ExternalAuthId', OPERATOR => '!=', VALUE =>'0');
    $Users->MemberOfGroup($Group) if $Group;
}

</%INIT>
<%ARGS>
$User		=> 0
$IsWork		=> 0
$Queue		=> ''
$Group		=> 0
$Job		=> 0
$CurrentUser	=> $session{'CurrentUser'}->UserObj->PrincipalId
</%ARGS>
