<%@ 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' />
    <c:if test="${not mailbox.features.calendar and mailbox.features.calendarUpsell}">
        <c:redirect url="/h/upsell">
            <c:param name="app" value="calendar"/>
        </c:redirect>
    </c:if>
    <c:if test="${not mailbox.features.calendar}">
        <c:redirect url="/h/search" />
    </c:if>
    <c:if test="${not empty param.st and param.st ne 'appointment'}">
        <c:redirect url="/h/search">
            <c:param name="sq" value="${param.sq}"/>
            <c:param name="st" value="${param.st}"/>
        </c:redirect>
    </c:if>

    <c:choose>
        <c:when test="${not empty param.tz}">
            <fmt:setTimeZone var="tz" value="${param.tz}" scope="request"/>
        </c:when>
        <c:otherwise>
            <c:set var="tz" value="${mailbox.prefs.timeZone}" scope="request"/>
        </c:otherwise>
    </c:choose>
    
    <c:choose>
        <c:when test="${not empty param.date}">
            <c:choose>
                <c:when test="${fn:contains(param.date, 'T')}">
                     <fmt:parseDate timeZone="${tz}" var="date" pattern="yyyyMMdd'T'HHmmss" value="${param.date}"/>
                </c:when>
                <c:otherwise>
                     <fmt:parseDate timeZone="${tz}" var="date" pattern="yyyyMMdd" value="${param.date}"/>
                </c:otherwise>
            </c:choose>
            <c:set scope="request" var="dateContext" value="${zm:getCalendar(date.time, tz)}"/>
        </c:when>
        <c:otherwise>
            <c:set scope="request" var="dateContext" value="${zm:getToday(tz)}"/>
        </c:otherwise>
    </c:choose>
    <c:set var="noComposeView" scope="session" value="${false}"/>
    <c:choose>
        <c:when test="${param.action eq 'compose'}">
            <app:composeCheck/>
            <c:set var="needViewAppt" value="${true}"/>            
        </c:when>
        <c:otherwise>
            <app:editapptCheck/>
        </c:otherwise>
    </c:choose>


    <c:set scope="request" var="calendarQuery" value="${param.sq}"/>
    
    <jsp:useBean id="expanded" scope="session" class="java.util.HashMap" />

     <c:if test="${not empty param.expand}">
         <c:set target="${sessionScope.expanded}" property="${param.expand}" value="expand"/>
     </c:if>
     <c:if test="${not empty param.collapse}">
         <c:set target="${sessionScope.expanded}" property="${param.collapse}" value="collapse"/>
     </c:if>


    <c:if test="${not empty param.check}">
        <zm:checkFolder checked="true" id="${param.check}"/>
    </c:if>
    <c:if test="${not empty param.uncheck}">
        <zm:checkFolder checked="false" id="${param.uncheck}"/>
    </c:if>

    <c:if test="${not empty param.refresh}">
        <zm:clearApptSummaryCache/>
    </c:if>

    <c:if test="${not empty param.sync}">
        <zm:syncFolder id="${param.sync}"/>
        <c:set var="folderName" value="${zm:getFolderName(pageContext, param.sync)}"/>
        <app:status>
            <fmt:message key="actionCalendarReloaded">
                <fmt:param value="${folderName}"/>
            </fmt:message>
        </app:status>
    </c:if>

    <c:if test="${param.action eq 'view'}">
        <c:set var="needViewAppt" value="${true}"/>
        <c:choose>
            <c:when test="${zm:actionSet(param,'actionGo')}">
                <c:set var="actionOp" value="${param.actionOp}"/>
                <c:set var="id" value="${param.invId}"/>
                <c:choose>
                    <c:when test="${actionOp eq 'flag' or actionOp eq 'unflag'}">
                        <zm:checkCrumb crumb="${param.crumb}"/>
                        <zm:flagItem var="result" id="${id}" flag="${actionOp eq 'flag'}"/>
                        <app:status>
                            <fmt:message key="${actionOp eq 'flag' ? 'actionApptFlag' : 'actionApptUnflag'}">
                                <fmt:param value="${result.idCount}"/>
                            </fmt:message>
                        </app:status>
                        ${zm:clearMessageCache(mailbox)}
                    </c:when>
                    <c:when test="${fn:startsWith(actionOp, 't:') or fn:startsWith(actionOp, 'u:')}">
                        <c:set var="tag" value="${fn:startsWith(actionOp, 't')}"/>
                        <c:set var="tagid" value="${fn:substring(actionOp, 2, -1)}"/>
                        <zm:checkCrumb crumb="${param.crumb}"/>
                        <zm:tagItem tagid="${tagid}" var="result" id="${id}" tag="${tag}"/>
                        <app:status>
                            <fmt:message key="${tag ? 'actionApptTag' : 'actionApptUntag'}">
                                <fmt:param value="${result.idCount}"/>
                                <fmt:param value="${zm:getTagName(pageContext, tagid)}"/>
                            </fmt:message>
                        </app:status>
                        ${zm:clearMessageCache(mailbox)}
                    </c:when>
                    <c:otherwise>
                        <app:status style="Warning"><fmt:message key="actionNoActionSelected"/></app:status>
                    </c:otherwise>
                </c:choose>
            </c:when>
            <c:when test="${zm:actionSet(param,'actionApptDelete')}">
                <zm:checkCrumb crumb="${param.crumb}"/>
                <c:choose>
                    <c:when test="${param.useInstance eq '1'}">
                        <c:set var="id" value="${not empty param.exInvId ? param.exInvId : param.invId}"/>
                        <c:set var="compNum" value="${empty param.exCompNum ? 0 : param.exCompNum}"/>
                        <c:set var="instance" value="${param.instStartTime}"/>
                    </c:when>
                    <c:otherwise>
                        <c:set var="id" value="${param.invId}"/>
                        <c:set var="compNum" value="${empty param.invCompNum ? 0 : param.invCompNum}"/>
                        <c:set var="instance" value="${0}"/>
                    </c:otherwise>
                </c:choose>
                <zm:getMessage var="message" id="${id}" markread="true" neuterimages="${empty param.xim}"/>
                <zm:cancelAppointment message="${message}" instance="${instance}"/>
                <c:set var="needViewAppt" value="${false}"/>
            </c:when>
        </c:choose>
    </c:if>

    <c:set var="view" value="${not empty param.view ? zm:cook(param.view) : mailbox.prefs.calendarInitialView}"/>
    <c:set var="checkedCalendars" value="${zm:getCheckedCalendarFolderIds(mailbox)}"/>
    <c:if test="${view eq 'list'}">
        <fmt:message var="dateFormat" key="CAL_APPT_EDIT_DATE_FORMAT"/>
        <c:if test="${not empty param.startDate}">
            <fmt:parseDate pattern="${dateFormat}" value="${param.startDate}" var="parsedStartDate" timeZone="${tz}"/>
            <fmt:formatDate value="${parsedStartDate}" pattern="yyyyMMdd'T'HHmmss" var="fmtStartDate" timeZone="${tz}"/>
            <fmt:parseDate value="${fmtStartDate}" pattern="yyyyMMdd'T'HHmmss" var="start" timeZone="${tz}" />
            <c:set scope="request" var="startDateContext" value="${zm:getCalendar(start.time, tz)}"/>
        </c:if>

        <c:if test="${not empty param.endDate}">
            <fmt:parseDate pattern="${dateFormat}" value="${param.endDate}" var="parsedEndDate" timeZone="${tz}"/>
            <fmt:formatDate value="${parsedEndDate}" pattern="yyyyMMdd'T'HHmmss" var="fmtEndDate" timeZone="${tz}"/>
            <fmt:parseDate value="${fmtEndDate}" pattern="yyyyMMdd'T'HHmmss" var="end" timeZone="${tz}"/>
            <c:set scope="request" var="endDateContext" value="${zm:getCalendar(end.time, tz)}"/>
        </c:if>
    </c:if>
</app:handleError>

<app:handleError>
<c:choose>
    <%--In case of send and cancel, noComposeView is set to true--%>
    <c:when test="${param.action eq 'compose' and noComposeView eq false}">
        <%--do nothing--%>
    </c:when>
    <c:when test="${zm:boolean(needViewAppt)}">
        <app:apptView/>
    </c:when>
    <c:when test="${view eq 'day'}">
        <app:multiDayView timezone="${tz}" date="${dateContext}" view='${view}' numdays="${not empty param.numdays ? param.numdays : 1}"/>
    </c:when>
    <c:when test="${view eq 'workWeek'}">
        <app:multiDayView timezone="${tz}" date="${dateContext}" view='${view}' numdays="7"/>
    </c:when>
    <c:when test="${view eq 'week'}">
        <app:multiDayView timezone="${tz}" date="${dateContext}" view='${view}' numdays="7"/>
    </c:when>
    <c:when test="${view eq 'schedule'}">
        <app:multiDayView timezone="${tz}" date="${dateContext}" numdays="1" view="${view}"/>
    </c:when>
    <c:when test="${view eq 'list'}">
        <app:listView timezone="${tz}" date="${not empty param.startDate? startDateContext : dateContext}" endDate="${not empty param.endDate ? endDateContext : ''}" view="${view}" numdays="7"/>
    </c:when>
    <c:otherwise>
        <app:monthView timezone="${tz}" date="${dateContext}"/>
    </c:otherwise>
</c:choose>
</app:handleError>
<script type="text/javascript">
    <!--
    var zprint = function(){
        try{ var idex = 0; var c =""; while (idex <= zrc ) { if(document.getElementById("C"+idex).checked) { cid = document.getElementById("C"+idex).value; c += cid + ","; } idex++ ; } }catch(ex){ }
        if (${view eq 'list'}) {
            window.open("/h/printcalendar?view=${view}&l=${checkedCalendars}&date=${fmtStartDate}&endDate=${fmtEndDate}&action=print&imc=true");
        } else {
            window.open("/h/printcalendar?view=${view}&l=${checkedCalendars}&date=${zm:cook(param.date)}&action=print&imc=true");
        }
    };
        //-->
</script>