<%@ page import="com.atlassian.jira.web.component.TableLayoutFactory"%>
<%@ page import="java.util.List"%>
<%@ page import="com.atlassian.jira.ComponentManager"%>
<%@ page import="com.atlassian.jira.web.component.IssueTableLayoutBean"%>
<%@ page import="com.opensymphony.user.User"%>
<%@ page import="com.atlassian.jira.web.component.IssueTableWebComponent"%>
<%@ page import="java.util.ArrayList"%>
<%@ page import="com.atlassian.jira.bean.SubTask"%>
<%@ page import="com.atlassian.jira.issue.Issue"%>
<%@ page import="com.atlassian.jira.bean.SubTaskBean"%>
<%@ page import="java.util.Collection"%>
<%@ page import="com.atlassian.jira.web.action.issue.ViewIssue"%>
<%@ taglib uri="webwork" prefix="webwork" %>
<%@ taglib uri="webwork" prefix="ui" %>


<%-- Ensure we retrive 'all' subtasks and see whetehr there are any of them, and not just sub-tasks for current view, as
if there are no sub-tasks for current view, vut still some exists, we should draw the subtasks table. See JRA-5087 --%>
<webwork:if test="/applicationProperties/option('jira.option.allowsubtasks')  == true">
<webwork:if test="/subTaskBean/subTasks('all')/empty == false || applicationProperties/option('jira.view.issue.show.quicksubtask.when.no.subtasks') == true">

<table cellpadding="2" cellspacing="0" border="0" width="100%" align="center">
<tr>
    <td width="1%" nowrap>
        <b><webwork:text name="'issue.field.subtasks'"/></b>:&nbsp;
    </td>
	<webwork:if test="/subTaskView == 'all'">
        <td bgcolor="bbbbbb" width="1%" nowrap align="center">
            &nbsp;<font color="ffffff"><b><webwork:text name="'viewissue.subtasks.tab.all.subtasks'"/></b></font>&nbsp;
        </td>
        <td nowrap width="1%" align="center">
            &nbsp;<b><a href="<%= request.getContextPath() %>/browse/<webwork:property value="/issue/string('key')"/>?subTaskView=unresolved"><webwork:text name="'viewissue.subtasks.tab.open.subtasks'"/></a></b>&nbsp;
        </td>
    </webwork:if>
    <webwork:else>
        <td nowrap width="1%" align="center">
            &nbsp;<b><a href="<%= request.getContextPath() %>/browse/<webwork:property value="/issue/string('key')"/>?subTaskView=all"><webwork:text name="'viewissue.subtasks.tab.all.subtasks'"/><a/></b>&nbsp;
        </td>
        <td bgcolor="bbbbbb" nowrap width="1%" align="center">
            &nbsp;<font color=ffffff><b><webwork:text name="'viewissue.subtasks.tab.open.subtasks'"/></b></font>&nbsp;
        </td>
    </webwork:else>

    <%-- Pad out the 'Sub-Task Progress' graph all the way to the right --%>
    <td width="100%">
        &nbsp;
    </td>
    <td style="border-width: 1px 1px 0 1px; border-style: solid; border-color: #bbb;" width="1%">
        <table height="6" valign="middle" align="right" cellspacing="0" cellpadding="0" border="0">
            <tr height="6">
                <td nowrap="true" valign="middle" align="right"><font size="1">&nbsp;<webwork:text name="'viewissue.subtasks.progress'"/>&nbsp;</font></td>
                <webwork:component template="percentagegraph.jsp" name="'/subTaskBean/subTaskProgress'" />
            </tr>
        </table>
    </td>
</tr>
</table>

<style>

#issuetable .nav
{
    font-size: 12px;
    vertical-align: top;
}

.stsequence
{
    width: 1%;
}
</style>

<table cellpadding="0" cellspacing="1" border="0" width="100%" bgcolor="bbbbbb" align="center"><tr><td>
<table cellpadding="2" cellspacing="0" border="0" width="100%">
<tr>
	<td bgcolor="ffffff" valign="top">

<webwork:if test="/subTaskCreatable == true">
    <form id="jiraform" action="<%= request.getContextPath() %>/secure/CreateSubTaskIssueDetails.jspa" method="post">
</webwork:if>


<webwork:property value="/subTaskBean/subTasks(/subTaskView)" id="subtasks"/>
<webwork:property value="/" id="viewIssueAction"/>
<webwork:if test="@subtasks != null && @subtasks/empty == false">
    <%
        ViewIssue viewIssue = (ViewIssue) pageContext.getAttribute("viewIssueAction");
        SubTaskBean subTaskBean = viewIssue.getSubTaskBean();
        String subTaskView = viewIssue.getSubTaskView();
        Collection issues = subTaskBean.getSubTasks(subTaskView);
        Issue parentIssue = viewIssue.getIssueObject();
        User user = viewIssue.getRemoteUser();

        List issueObjects = new ArrayList();
        for (java.util.Iterator iterator = issues.iterator(); iterator.hasNext();)
        {
            SubTask subTask = (SubTask) iterator.next();
            issueObjects.add(subTask.getSubTaskIssueObject());
        }
        IssueTableWebComponent issueTable = new IssueTableWebComponent();
        TableLayoutFactory layoutFactory = (TableLayoutFactory) ComponentManager.getComponentInstanceOfType(TableLayoutFactory.class);
        IssueTableLayoutBean layout = layoutFactory.getSubTaskIssuesLayout(user, parentIssue, subTaskBean, subTaskView);

        out.print(issueTable.getHtml(layout, issueObjects, null));
    %>
</webwork:if>
<webwork:else>
    <table cellpadding="3" cellspacing="0" border="0" width="100%">
        <tr>
            <td width="100%" align="left">
                <webwork:text name="'viewissue.subtasks.notasksforview'"/>
            </td>
        </tr>
    </table>

</webwork:else>
        <webwork:if test="/subTaskCreatable == true">
            <webwork:property value="/subTaskQuickCreationWebComponent/html()" escape="'false'" />
        </webwork:if>
<webwork:if test="/subTaskCreatable == true">
    </form>
</webwork:if>

	</td>
</tr>
</table>
</td></tr></table>

</webwork:if>
</webwork:if>
