<%@ page buffer="8kb" autoFlush="true" %>
<%@ page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="zm" uri="com.zimbra.zm" %>
<%@ taglib prefix="app" uri="com.zimbra.htmlclient" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="fmt" uri="com.zimbra.i18n" %>
<app:handleError>
    <zm:getMailbox var="mailbox"/>
    <c:choose>
    <c:when test="${not empty mailbox.prefs.locale}">
        <fmt:setLocale value='${mailbox.prefs.locale}' scope='request' />
    </c:when>
    <c:otherwise>
        <fmt:setLocale value='${pageContext.request.locale}' scope='request' />
    </c:otherwise>
    </c:choose>
    <fmt:setBundle basename="/messages/ZhMsg" scope="request"/>

    <fmt:message var="title" key="newTask"/>
    <zm:composeUploader var="uploader"/>
    <c:set var="compose" value="${uploader.compose}"/>
    <fmt:message var="hourFmt" key="CAL_APPT_EDIT_HOUR_FORMAT"/>
</app:handleError>
<%--TODO: clean up this code --%>
<app:handleError>
    <c:choose>
        <c:when test="${param.useInstance eq '1' and not empty param.exInvId}">
            <c:set var="id" value="${param.exInvId}"/>
            <c:set var="compNum" value="${empty param.exCompNum ? 0 : param.exCompNum}"/>
            <zm:getMessage var="message" id="${id}" markread="true" neuterimages="${empty param.xim}" wanthtml="false"/>
        </c:when>
        <c:when test="${not empty param.id}">
            <c:set var="id" value="${param.id}"/>
            <c:set var="compNum" value="${empty param.invCompNum ? 0 : param.invCompNum}"/>
            <zm:getMessage var="message" id="${id}" markread="true" neuterimages="${empty param.xim}" wanthtml="false"/>
        </c:when>
        <c:otherwise>
            <c:set var="message" value="${null}"/>
        </c:otherwise>
    </c:choose>

    <c:set var="appt" value="${empty message ? null : message.invite.component}"/>

    <c:set var="canEditRecurrence" value="${(empty appt or not appt.exception) and not compose.useInstance}"/>
    <c:set var="canDelete" value="${not empty appt}"/>
    <c:set var="hasAttendees" value="${not empty appt and not empty appt.attendees}"/>


    <c:choose>
        <c:when test="${empty compose}">
            <zm:messageCompose var="compose" message="${message}" action="${empty message ? 'apptnew' : 'apptedit'}"
                               date="${requestScope.dateContext}"
                               inviteId="${param.id}"
                               exceptionInviteId="${empty param.exInvId? null : param.exInvId}"
                               useInstance="${param.useInstance eq '1'}"
                               istask="${true}"
                               instanceStartTime="${zm:nonNull(param.instStartTime)}"
                               instanceDuration="${zm:nonNull(param.instDuration)}"/>
        </c:when>
        <c:when test="${uploader.isUpload and not empty message}">
            <zm:fixupMessageCompose message="${message}" compose="${compose}" newattachments="${uploader.compose.hasFileItems}"/>
        </c:when>
    </c:choose>

    <c:choose>
        <c:when test="${empty compose.inviteId}">
            <c:set var="apptImage" value="tasks/ImgNewTask.png"/>
            <c:set var="apptImageAlt" value="ALT_NEW_TASK"/>
            <fmt:message var="apptSubject" key="newTask"/>
            <c:set var="isNewAppt" value="${true}"/>
        </c:when>
        <c:otherwise>
            <c:set var="apptImage" value="tasks/ImgTask.png"/>
            <c:set var="apptImageAlt" value="ALT_EDIT_TASK"/>
            <c:set var="apptSubject" value="${compose.subject}"/>
            <c:set var="closeImage" value="common/ImgClose.png"/>
            <c:set var="isNewAppt" value="${false}"/>
        </c:otherwise>
    </c:choose>

    <c:set var="isInstance" value="${not empty appt and (appt.exception or not empty appt.recurrence) and compose.useInstance}"/>

</app:handleError>

<app:view mailbox="${mailbox}" title="${not empty compose.subject ? compose.subject : title}" selected='tasks' tasks="true" context="${null}" keys="false" date="${requestScope.dateContext}" tags="true"
     onload="var e=document.getElementById('subject'); if (e) e.focus();">

        <table width="100%" style='height:100%;' cellpadding="0" cellspacing="0">
            <tr>
                <td class='TbTop'>
                    <table width="100%" cellspacing="0" cellpadding="0" class='Tb'>
                        <tr valign='middle'>
                            <td align="left" class="TbBt">
                                <table cellspacing="0" cellpadding="0" class='Tb'>
                                    <tr>
	                                    <td>
		                                    <a href="/h/search?st=task" ><app:img src="common/ImgCancel.png" altkey="refresh"/><span>&nbsp;<fmt:message key="close"/></span></a>

	                                    </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr>
                <td class='ZhAppContent'>
                    <table cellpadding="2" cellspacing="0" width="100%" class="Compose">
                        <tr  class='${zm:getFolder(pageContext, empty compose ? 10 : compose.apptFolderId).styleColor}Bg'>
                            <td colspan="2" class='ZhBottomSep'>
                                <table width="100%" cellpadding="0" cellspacing="0">
                                    <tr class='apptHeaderRow'>
                                        <td width="24"><center><app:img src="${apptImage}" altkey="${apptImageAlt}"/></center></td>
                                        <td class='apptHeader'>
                                            ${fn:escapeXml(apptSubject)}
                                        </td>
                                        <td width="1%" nowrap align='right' class='Tags' style='padding-right:5px'>
                                            <c:if test="${(message.hasTags and mailbox.features.tagging) or (message.isFlagged)}">
                                                <c:set var="tags" value="${zm:getTags(pageContext, message.tagIds)}"/>
                                                <c:forEach items="${tags}" var="tag">
                                                    <span style='white-space:nowrap;'>
                                                        <app:img altkey="${fn:escapeXml(tag.name)}" src="${tag.miniImage}"/>${fn:escapeXml(tag.name)} </span>
                                                </c:forEach>
                                                <c:if test="${message.isFlagged}">
                                                    <span style='white-space:nowrap;'>
                                                        <app:flagImage flagged="${message.isFlagged}"/>
                                                    </span>
                                                </c:if>
                                            </c:if>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                        <tr>
                        <td colspan="2">
                            <table cellpadding="2" cellspacing="0" width="100%">
                                <tr>
                                    <td valign='top' width="50%">
                                        <fieldset>
                                            <legend><fmt:message key="details"/></legend>
	                                        <table cellpadding="5" cellspacing="0">
		                                        <tr>
			                                        <td class="editViewLabel"><label for="subject"><fmt:message key="subject"/>:</label></td>
			                                        <td>${fn:escapeXml(compose.subject)}</td>
		                                        </tr>
		                                        <tr>
			                                        <td class="editViewLabel"><label for="location"><fmt:message key="location"/>:</label></td>
			                                        <td>${fn:escapeXml(compose.location)}</td>
		                                        </tr>
		                                        <tr>
			                                        <td class="editViewLabel"><label for="priority"><fmt:message key="priority"/>:</label></td>
			                                        <td>
				                                        <table cellpadding="0" cellspacing="0">
					                                        <tr>
						                                        <td>
							                                        <c:if test="${compose.taskPriority eq '9'}"><fmt:message key="taskPrioLow"/></c:if>
							                                        <c:if test="${compose.taskPriority eq '5'}"><fmt:message key="taskPrioNormal"/></c:if>
							                                        <c:if test="${compose.taskPriority eq '1'}"><fmt:message key="taskPrioHigh"/></c:if>
						                                        </td>
					                                        </tr>
				                                        </table>
			                                        </td>
		                                        </tr>
		                                        <tr>
			                                        <td colspan="2">&nbsp;</td>
		                                        </tr>
	                                        </table>
                                        </fieldset>
                                    </td>
                                    <td valign='top' width="50%">
                                        <fieldset>
                                            <legend><fmt:message key="date"/></legend>
                                            <table cellpadding="5" cellspacing="0">
                                            <tr>
                                                <td colspan="2">
                                                    <table cellpadding="2" cellspacing="2" >
                                                        <tr>
	                                                        <td><fmt:message key="status" /> : </td>
                                                            <td nowrap>
                                                                 <c:if test="${compose.taskStatus eq 'NEED'}"><fmt:message key="TASK_NEED"/></c:if>
                                                                 <c:if test="${compose.taskStatus eq 'COMP'}"><fmt:message key="TASK_COMP"/></c:if>
                                                                 <c:if test="${compose.taskStatus eq 'INPR'}"><fmt:message key="TASK_INPR"/></c:if>
                                                                 <c:if test="${compose.taskStatus eq 'WAITING'}"><fmt:message key="TASK_WAITING"/></c:if>
                                                                 <c:if test="${compose.taskStatus eq 'DEFERRED'}"><fmt:message key="TASK_DEFERRED"/></c:if>
                                                            </td>
                                                            <td>&nbsp;</td>
                                                            <td nowrap>
                                                                    <c:if test="${compose.taskPercentComplete eq '0'}">0%</c:if>
                                                                    <c:if test="${compose.taskPercentComplete eq '10'}">10%</c:if>
                                                                    <c:if test="${compose.taskPercentComplete eq '20'}">20%</c:if>
                                                                    <c:if test="${compose.taskPercentComplete eq '30'}">30%</c:if>
                                                                    <c:if test="${compose.taskPercentComplete eq '40'}">40%</c:if>
                                                                    <c:if test="${compose.taskPercentComplete eq '50'}">50%</c:if>
                                                                    <c:if test="${compose.taskPercentComplete eq '60'}">60%</c:if>
                                                                    <c:if test="${compose.taskPercentComplete eq '70'}">70%</c:if>
                                                                    <c:if test="${compose.taskPercentComplete eq '80'}">80%</c:if>
                                                                    <c:if test="${compose.taskPercentComplete eq '90'}">90%</c:if>
                                                                    <c:if test="${compose.taskPercentComplete eq '100'}">100%</c:if>
                                                            </td>
                                                        </tr>
                                                    </table>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td width="1%" align="right" nowrap><label for="start"><fmt:message key="startDate"/>:</label></td>
                                                <td>
                                                    <table cellpadding="0" cellspacing="0">
                                                        <tr>
                                                            <td>
                                                                ${fn:escapeXml(compose.startDate)}
                                                            </td>
                                                        </tr>
                                                    </table>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td width="1%" align="right" nowrap><label for="end"><fmt:message key="taskDueDate"/>:</label></td>
                                                <td>
                                                    <table cellpadding="0" cellspacing="0">
                                                        <tr>
                                                            <td>
                                                                ${fn:escapeXml(compose.endDate)}
                                                            </td>
                                                        </tr>
                                                    </table>
                                                </td>
                                            </tr>
                                            </table>
                                        </fieldset>
                                    </td>
                                </tr>
                            </table>
                        </td>

                        </tr>
                        <c:set var="firstAttachment" value="${true}"/>
                        <c:if test="${!empty compose.messageAttachments}">
                                <c:forEach var="ma" items="${compose.messageAttachments}" varStatus="status">
                                    <tr>
                                        <td align='right'>
                                            <c:if test="${firstAttachment}">
                                                <app:img altkey="ALT_ATTACHMENT" src="startup/ImgAttachment.png"/>
                                                <c:set var="firstAttachment" value="${false}"/>
                                            </c:if>
                                        </td>
                                        <td colspan=2>
                                            <c:choose>
                                                <c:when test="${empty ma.subject}"><fmt:message var="subj" key="noSubject"/></c:when>
                                                <c:otherwise><c:set var="subj" value="${zm:truncate(ma.subject,100,true)}"/></c:otherwise>
                                            </c:choose>
                                                ${fn:escapeXml(subj)}
                                            <input type="hidden" name="messageAttachment" value="${ma.id}:${fn:escapeXml(ma.subject)}"/>
                                        </td>
                                    </tr>
                                </c:forEach>
                            </c:if>
                        <c:if test="${!empty compose.originalAttachments}">
                                <c:forEach var="part" items="${compose.originalAttachments}" varStatus="status">
                                    <tr class="CompOrigAtt" valign="middle">
                                        <td align='right'>
                                            <c:if test="${firstAttachment}">
                                                <app:img altkey="ALT_ATTACHMENT" src="startup/ImgAttachment.png"/>
                                                <c:set var="firstAttachment" value="${false}"/>
                                            </c:if>
                                        </td>
                                        <c:set var="url" value="/service/home/~/?id=${message.id}&part=${part.partName}&auth=co"/>
                                        <c:set var="pname" value="${part.displayName}"/>
                                        <c:if test="${empty pname}">
                                            <fmt:message key="unknownContentType" var="pname">
                                                <fmt:param value="${part.contentType}"/>
                                            </fmt:message>
                                        </c:if>
                                        <td>
                                            <table cellpadding=0 cellspacing=0 border="0">
                                                <tr valign="middle">
                                                    <td>
                                                        <a target="_blank" href="${url}&disp=i">${fn:escapeXml(pname)}</a>&nbsp;(${part.displaySize})
                                                    </td>
                                                </tr>
                                            </table>
                                        </td>
                                    </tr>
                                </c:forEach>
                        </c:if>
                        <tr>
                            <td colspan="3" style='padding:10px;' width="100%" >
		                            <div class='MsgCompose'  style='width:100%;border:1px solid #ccc;padding:7px;'>${fn:escapeXml(empty compose.content ? compose.description : compose.content)}</div>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr>
                <td class='TbBottom'>
                    &nbsp;
                        <c:if test="${not empty param.debug}">
                            <pre>${fn:escapeXml(appt)}</pre>
                        </c:if>
                </td>
            </tr>
        </table>
</app:view>
