<%@page contentType="text/html;charset=UTF-8" pageEncoding="ISO-8859-1" session="true"
%><%@page errorPage="Exception.jsp"
%><%@page import="com.fujitsu.iflow.model.workflow.WFSession"
%><%@page import="com.fujitsu.iflow.model.workflow.AttachmentRef"
%><%@page import="com.fujitsu.iflow.model.workflow.WorkItem"
%><%@page import="com.fujitsu.iflow.model.util.QuickForm"
%><%@page import="com.fujitsu.iflow.model.util.URLEncoder"
%>
<%@page import="java.io.*"
%>
<%request.setCharacterEncoding("UTF-8");request.getParameterNames(); //Need dummy call for WL9.1
%>
<script>
function viewAttach(name, pForm)
{
    pForm.cmd.value = "view Attachment";
    pForm.attachCheck.value=name;
    pForm.submit();
}

</script>
<%
    WFSession wfSession = (WFSession)session.getAttribute("ModelsessionID");

    if (wfSession == null) {
        response.sendRedirect("Login.jsp");
        return;
    }

    boolean isAcceptRequired = wfSession.getServerOption("TWFServerAcceptRequired").equalsIgnoreCase("true");

    String val = request.getParameter("Attachment");
    WorkItem task = (WorkItem)request.getAttribute("task");

    String disabled = "";

    if (isAcceptRequired) {
        disabled = "disabled";
    }

    if(task.getState() == task.STATE_ACCEPTED){
        disabled = "";
    }
    else if(task.getState() == task.STATE_DECLINED || task.getState() == task.STATE_COMPLETED){
        disabled = "disabled";
    }

    long taskId = task.getId();
    AttachmentRef[] attachments = task.getAttachments();
%>
<table align="center" width="650" class="tableClass">
<tr class="headrow">
    <td width="22"><img src="images/icons/attachment.gif" border="0"></td><td colspan="3" width="628">Attachments</td></tr>
<%
    if (attachments != null && attachments.length > 0)
    {
%>
    <tr class="prompt">
        <td align="left">Control</td>
        <td align="left">Attachment Title</td>
        <td align="left">Attachment</td>
        <td align="center">Delete</td>
    </tr>
<%
    String[] bcolor = {"evenrow","oddrow"};
    int nextColor = 0;
    String rowColor = bcolor[nextColor];
    for(int i=0; i<attachments.length; i++)
    {
        String attachName = attachments[i].getName();
        String attachPath1 = attachments[i].getPath();
        String attachPath;
        attachPath = attachPath1.substring(attachPath1.lastIndexOf(File.separator)+1);
        %>
        <tr class="<%=rowColor%>">
        <td>
        <%
        String vCmd = "View Attachment:" + attachName;
        %>
        <input type="image" name="<% QuickForm.writeHtml(out, vCmd); %>" src="images/view.gif" alt="Checkout for readonly" <%= disabled %>>&nbsp;
        <%
        vCmd = "Edit Attachment:" + attachName;
        %>
        <input type="image" name="<% QuickForm.writeHtml(out, vCmd); %>" src="images/edit.gif" alt="Checkout for edit" <%= disabled %>>&nbsp;
        <%
        vCmd = "Save Attachment:" + attachName;
        %>
        <a href="CheckIn.jsp?taskId=<%=taskId%>&documentName=<%=URLEncoder.encode(attachName,"UTF-8")%>">
            <img src="images/save.gif" alt="Checkin" border="0"></a>&nbsp;
        </td>
        <td align="left">
        <% QuickForm.writeHtml(out, attachName); %>
        </td>
        <td align="left">
        <% QuickForm.writeHtml(out, attachPath); %></a>
        </td>
        <td align="center">
        <input name="attachCheck" type="checkbox" value="<% QuickForm.writeHtml(out, attachName); %>" <%= disabled %>>
        </td>
        </tr>
        <%
        nextColor = (nextColor + 1)%2;
        rowColor = bcolor[nextColor];
    }
}
%>
    <tr align="center" class="evenrow">
        <td colspan="4" align="center">
            <input type="hidden" name="taskId" value="<%=taskId%>">
            <input class="submitClass" name="cmd" type="submit" value="Add" <%= disabled %>>&nbsp;
            <input class="submitClass" name="cmd" type="submit" value="Delete Selected" <%= disabled %>>
        </td>
    </tr>
    </table>
