<%@ 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="http://java.sun.com/jsp/jstl/fmt" %>
<fmt:setBundle basename="/msgs/ZhMsg" scope="session"/>
<app:handleError>
    <c:set var="selected" value="${empty param.selected ? 'general' : param.selected}"/>

    <c:choose>
        <c:when test="${zm:actionSet(param, 'actionSave')}">
            <app:optAction selected="${selected}"/>
        </c:when>
        <c:when test="${zm:actionSet(param, 'actionFilterCancel')}">
            <app:status>
                <fmt:message key="${zm:actionSet(param, 'actionNewFilter') ? 'filterCancelCreate' : 'filterCancelModify'}"/>
            </app:status>
        </c:when>
        <c:when test="${zm:actionSet(param, 'actionDeleteFilter') and not empty param.ruleName}">
            <zm:deleteFilterRule name="${param.ruleName}"/>
        </c:when>
        <c:when test="${zm:actionSet(param, 'actionMoveFilterUp') and not empty param.ruleName}">
            <zm:moveFilterRule direction="up" name="${param.ruleName}"/>
        </c:when>
        <c:when test="${zm:actionSet(param, 'actionMoveFilterDown') and not empty param.ruleName}">
            <zm:moveFilterRule direction="down" name="${param.ruleName}"/>
        </c:when>
        <c:when test="${zm:actionSet(param, 'actionFilterSave') and not empty param.frompost}">
            <c:set var="filterSave" value="fail" scope="request"/>
            <app:constructRule var="postRule"/>
            <c:choose>
                <c:when test="${empty postRule.name}">
                    <app:status>
                        <fmt:message key="actionRuleNoName"/>
                    </app:status>
                </c:when>
                <c:when test="${empty postRule.conditions}">
                    <app:status>
                        <fmt:message key="actionRuleNoConditions"/>
                    </app:status>
                </c:when>
                <c:when test="${empty postRule.actions}">
                    <app:status>
                        <fmt:message key="actionRuleNoActions"/>
                    </app:status>
                </c:when>
                <c:when test="${zm:actionSet(param, 'actionNewFilter')}">
                    <zm:createFilterRule rule="${postRule}"/>
                    <c:set var="filterSave" value="success" scope="request"/>
                </c:when>
                <c:when test="${zm:actionSet(param, 'actionEditFilter')}">
                    <zm:modifyFilterRule originalname="${param.origname}"  rule="${postRule}"/>
                    <c:set var="filterSave" value="success" scope="request"/>
                </c:when>
            </c:choose>
        </c:when>
    </c:choose>

    <zm:getMailbox var="mailbox"/>
</app:handleError>

<c:choose>
    <%------------------- GENERAL ------------------%>
    <c:when test="${selected eq 'general'}">
        <c:set var="subKey" value="general"/>
    </c:when>
    <%------------------- MAIL ------------------%>
    <c:when test="${selected eq 'mail'}">
        <c:set var="subKey" value="mail"/>
    </c:when>
    <%------------------- MAIL IDENTITY ------------------%>
    <c:when test="${selected eq 'identity'}">
        <c:set var="subKey" value="mailIdentity"/>
    </c:when>
    <%------------------- MAIL FILTERS ------------------%>
    <c:when test="${selected eq 'filters' and mailbox.features.filters}">
        <c:set var="subKey" value="mailFilters"/>
    </c:when>
    <%------------------- ADDRESS BOOK ------------------%>
    <c:when test="${selected eq 'addressbook'}">
        <c:set var="subKey" value="addressBook"/>
    </c:when>
    <%------------------- SHORTCUTS ------------------%>
    <c:when test="${selected eq 'shortcuts'}">
        <c:set var="subKey" value="shortcuts"/>
    </c:when>
    <c:otherwise>
        <c:set var="subKey" value=""/>
    </c:otherwise>
</c:choose>

<c:set var="title">
    <fmt:message key="options"/>
    <c:if test="${not empty subKey}">:
        <fmt:message key="${subKey}"/>
    </c:if>
</c:set>

<app:view title="${title}" context="${null}" selected='options' folders="true" searches="true" tags="true" keys="true">

    <form action="" method="post">
        <table width=100% cellpadding="0" cellspacing="0">
            <tr>
                <td class='TbTop'>
                    <table cellspacing=0 class='Tb'>
                        <tr>
                            <td align=left class=TbBt>
                                <input type=hidden class='tbButton' name="zzz">&nbsp;
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr>
                <td class='ZhAppContent'>
                    <table width=100% cellpadding=0 cellspacing=0>
                        <tr>
                            <td>
                                <app:optTabs mailbox="${mailbox}" selected="${selected}"/>
                            </td>
                        </tr>
                        <tr>
                            <td class='TbTop'>
                                <app:optSubToolbar selected="${selected}"/>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <c:choose>
                                    <%------------------- GENERAL ------------------%>
                                    <c:when test="${selected eq 'general'}">
                                        <app:optGeneral mailbox="${mailbox}"/>
                                    </c:when>
                                    <%------------------- MAIL ------------------%>
                                    <c:when test="${selected eq 'mail'}">
                                        <app:optMail mailbox="${mailbox}"/>
                                    </c:when>
                                    <%------------------- MAIL IDENTITY ------------------%>
                                    <c:when test="${selected eq 'identity'}">
                                        <app:optMailIdentity mailbox="${mailbox}"/>
                                    </c:when>
                                    <%------------------- MAIL FILTER ------------------%>
                                    <c:when test="${selected eq 'filter' and mailbox.features.filters}">
                                        <app:optMailFilter mailbox="${mailbox}"/>
                                    </c:when>
                                    <%------------------- ADDRESS BOOK ------------------%>
                                    <c:when test="${selected eq 'addressbook'}">
                                        <app:optAddressBook mailbox="${mailbox}"/>
                                    </c:when>
                                    <%------------------- SHORTCUTS ------------------%>
                                    <c:when test="${selected eq 'shortcuts'}">
                                        <app:optShortcuts mailbox="${mailbox}"/>
                                    </c:when>
                                </c:choose>
                            </td>
                        </tr>
                        <br>
                    </table>
                </td>
            </tr>
        </table>
        <input type="hidden" name="doAction" value="1"/>
    </form>
</app:view>
