#import koji
#from kojiweb import util

#def printChildren($taskID, $childMap)
  #set $iter = 0
  #set $children = $childMap[$str($taskID)]
  #if $children
  <ul>
    #for $child in $children
    #set $iter += 1
    #if $iter < $len($children)
    <li class="sibling">
    #else
    <li>
    #end if
      #set $childState = $util.taskState($child.state)
      <span class="treeBranch">
        <span class="treeLabel">
          <a href="taskinfo?taskID=$child.id" class="task$childState" title="$childState">$koji.taskLabel($child)</a>
        </span>
      </span>
    $printChildren($child.id, $childMap)
    </li>
    #end for
  </ul>
  #end if
#end def

#def headerPrefix($state)
  #if $state == 'active'
Active
  #elif $state == 'all'
All
  #else
#echo $state.capitalize()
  #end if
#end def

#attr _PASSTHROUGH = ['owner', 'state', 'view', 'method', 'hostID', 'channelID', 'order']

#include "includes/header.chtml"

  <h4>$headerPrefix($state) #if $view == 'toplevel' then 'toplevel' else ''# #if $method != 'all' then $method else ''# Tasks#if $ownerObj then ' owned by <a href="userinfo?userID=%i">%s</a>' % ($ownerObj.id, $ownerObj.name) else ''##if $host then ' on host <a href="hostinfo?hostID=%i">%s</a>' % ($host.id, $host.name) else ''# #if $channel then ' in channel <a href="channelinfo?channelID=%i">%s</a>' % ($channel.id, $channel.name) else ''#</h4>

  <table class="data-list">
    <tr>
      <td colspan="6">
        <form action="tasks">
        <table class="nested">
        <tr><td>
        <strong>State</strong>:
        </td><td>
        <select name="state" class="filterlist" onchange="javascript: window.location = 'tasks?state=' + this.value + '$util.passthrough_except($self, 'state')';">
          <option value="active" $util.toggleSelected($self, $state, 'active')>active</option>
          <option value="all" $util.toggleSelected($self, $state, 'all')>all</option>
          <option value="free" #if $state == 'free' then 'selected="selected"' else ''#>free</option>
          <option value="open" #if $state == 'open' then 'selected="selected"' else ''#>open</option>
          <option value="closed" #if $state == 'closed' then 'selected="selected"' else ''#>closed</option>
          <option value="failed" #if $state == 'failed' then 'selected="selected"' else ''#>failed</option>
          <option value="canceled" #if $state == 'canceled' then 'selected="selected"' else ''#>canceled</option>
          <option value="assigned" #if $state == 'assigned' then 'selected="selected"' else ''#>assigned</option> 
        </select>
        </td><td>
        <strong>Owner</strong>:
        </td><td>
        <select name="owner" class="filterlist" onchange="javascript: window.location = 'tasks?owner=' + this.value + '$util.passthrough_except($self, 'owner')';">
          <option value="" #if not $owner then 'selected="selected"' else ''#>everyone</option>
          #if $loggedInUser
          <option value="$loggedInUser.name">me</option>
          #end if
          #for $user in $users
          <option value="$user.name" #if $user.name == $owner then 'selected="selected"' else ''#>$user.name</option>
          #end for
        </select>
        </td></tr>
        <tr><td>
        <strong>Method</strong>:
        </td><td>
        <select name="method" class="filterlist" onchange="javascript: window.location = 'tasks?method=' + this.value + '$util.passthrough_except($self, 'method')';">
          <option value="all" $util.toggleSelected($self, $method, 'all')>all</option>
          #for $task_type in $alltasks
          #if $task_type in ('maven', 'buildMaven') and not $mavenEnabled
          #continue
          #elif $task_type in ('winbuild', 'vmExec') and not $winEnabled
          #continue
          #elif $task_type == 'wrapperRPM' and not ($mavenEnabled or $winEnabled)
          #continue
          #else
          <option value="$task_type" #if $method == $task_type then 'selected="selected"' else ''#>$task_type</option>
          #end if
          #end for
        </select>
        </td><td>
        <strong>View</strong>:
        </td><td>
        <select name="view" class="filterlist" onchange="javascript: window.location = 'tasks?view=' + this.value + '$util.passthrough_except($self, 'view')';">
          <option value="tree" $util.toggleSelected($self, $view, 'tree') #if not $treeEnabled then 'disabled="disabled"' else ''#>tree</option>
          <option value="toplevel" $util.toggleSelected($self, $view, 'toplevel') #if not $toplevelEnabled then 'disabled="disabled"' else ''#>toplevel</option>
          <option value="flat" $util.toggleSelected($self, $view, 'flat')>flat</option>
        </select>
        </td></tr>
        </table>
        </form>
      </td>
    </tr>
    <tr>
      <td class="paginate" colspan="6">
        #if $len($taskPages) > 1
        <form class="pageJump" action="">
          Page:
          <select onchange="javascript: window.location = 'tasks?start=' + this.value * $taskRange + '$util.passthrough_except($self)';">
            #for $pageNum in $taskPages
            <option value="$pageNum"#if $pageNum == $taskCurrentPage then ' selected="selected"' else ''#>#echo $pageNum + 1#</option>
            #end for
          </select>
        </form>
        #end if
        #if $taskStart > 0
        <a href="tasks?start=#echo $taskStart - $taskRange #$util.passthrough_except($self)">&lt;&lt;&lt;</a>
        #end if
        #if $totalTasks != 0
        <strong>Tasks #echo $taskStart + 1 # through #echo $taskStart + $taskCount # of $totalTasks</strong>
        #end if
        #if $taskStart + $taskCount < $totalTasks
        <a href="tasks?start=#echo $taskStart + $taskRange#$util.passthrough_except($self)">&gt;&gt;&gt;</a>
        #end if
      </td>
    </tr>
    <tr class="list-header">
      <th><a href="tasks?order=$util.toggleOrder($self, 'id')$util.passthrough_except($self, 'order')">ID</a> $util.sortImage($self, 'id')</th>
      <th><a href="tasks?order=$util.toggleOrder($self, 'method')$util.passthrough_except($self, 'order')">Type</a> $util.sortImage($self, 'method')</th>
      <th><a href="tasks?order=$util.toggleOrder($self, 'owner')$util.passthrough_except($self, 'order')">Owner</a> $util.sortImage($self, 'owner')</th>
      <th><a href="tasks?order=$util.toggleOrder($self, 'arch')$util.passthrough_except($self, 'order')">Arch</a> $util.sortImage($self, 'arch')</th>
      <th><a href="tasks?order=$util.toggleOrder($self, 'completion_time')$util.passthrough_except($self, 'order')">Finished</a> $util.sortImage($self, 'completion_time')</th>
      <th><a href="tasks?order=$util.toggleOrder($self, 'state')$util.passthrough_except($self, 'order')">State</a> $util.sortImage($self, 'state')</th>
    </tr>
    #if $len($tasks) > 0
      #for $task in $tasks
        <tr class="$util.rowToggle($self)">
          #set $taskState = $util.taskState($task.state)
          <td>$task.id</td>
          <td#if $treeDisplay then ' class="tree"' else ''#>
            #if $treeDisplay then '&nbsp;' else ''#<a href="taskinfo?taskID=$task.id" class="task$taskState" title="$taskState">$koji.taskLabel($task)</a>
            #if $treeDisplay
            $printChildren($task.id, $task.descendents)
            #end if
          </td>
          <td class="user-$task.owner_name">
            #if $task.owner_type == $koji.USERTYPES['HOST']
            <a href="hostinfo?userID=$task.owner">$task.owner_name</a>
            #else
            <a href="userinfo?userID=$task.owner">$task.owner_name</a>
            #end if
          </td>
          <td>$task.arch</td>
          <td>$util.formatTime($task.completion_ts)</td>
          <td class="task$state">$util.imageTag($taskState)</td>
        </tr>
      #end for
    #else
      <tr class="row-odd">
        <td colspan="6">No tasks</td>
      </tr>
    #end if
    <tr>
      <td class="paginate" colspan="6">
        #if $len($taskPages) > 1
        <form class="pageJump" action="">
          Page:
          <select onchange="javascript: window.location = 'tasks?start=' + this.value * $taskRange + '$util.passthrough_except($self)';">
            #for $pageNum in $taskPages
            <option value="$pageNum"#if $pageNum == $taskCurrentPage then ' selected="selected"' else ''#>#echo $pageNum + 1#</option>
            #end for
          </select>
        </form>
        #end if
        #if $taskStart > 0
        <a href="tasks?start=#echo $taskStart - $taskRange #$util.passthrough_except($self)">&lt;&lt;&lt;</a>
        #end if
        #if $totalTasks != 0
        <strong>Tasks #echo $taskStart + 1 # through #echo $taskStart + $taskCount # of $totalTasks</strong>
        #end if
        #if $taskStart + $taskCount < $totalTasks
        <a href="tasks?start=#echo $taskStart + $taskRange#$util.passthrough_except($self)">&gt;&gt;&gt;</a>
        #end if
      </td>
    </tr>
  </table>

#include "includes/footer.chtml"
