<%@ 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="${!empty param.doAction}">
        <zm:checkCrumb crumb="${param.crumb}"/>
        <app:tagAction/>
    </c:if>

    <c:choose>
        <c:when test="${not empty requestScope.newlyCreatedTagName}">
             <zm:forEachTag var="tag">
                <c:if test="${requestScope.newlyCreatedTagName eq tag.name}"><c:set var="selectedTag" value="${tag}"/></c:if>
            </zm:forEachTag>
        </c:when>
        <c:when test="${not empty param.id}">
            <c:set var="selectedTag" value="${zm:getTag(pageContext, param.id)}"/>
        </c:when>
        <c:when test="${not empty param.sti}">
            <c:set var="selectedTag" value="${zm:getTag(pageContext, param.sti)}"/>            
        </c:when>
    </c:choose>

    <c:set var="newTag" value="${zm:actionSet(param, 'actionNewTag')}"/>

    <c:if test="${empty selectedTag and not newTag}">
        <zm:forEachTag var="tag">
            <c:if test="${empty selectedTag}">
                <c:set var="selectedTag" value="${tag}"/>
            </c:if>
        </zm:forEachTag>
    </c:if>

    <c:set var="toolbar">

    </c:set>
    <fmt:message var="title" key="manageTags"/>

    <c:set var="selectedRow" value="${-1}"/>    
</app:handleError>

<app:view mailbox="${mailbox}" title="${title}" context="${null}" selected='folders' tags="true" editmode="true" keys="true"
            onload="var e=document.getElementById('name'); if (e) e.focus();">

    <form action="" method="post" accept-charset="utf-8">
        <table width="100%" cellspacing="0" cellpadding="0">
            <tr>
                <td class='TbTop'>
                    <table width="100%" cellspacing="0">
                        <tr>
                            <td class='TbBt'>
                                <table cellspacing="0" cellpadding="0" class='Tb'>
                                    <tr>
                                        <td>
                                            <a id="OPCLOSE" href="<c:url value="/h/search"/>">
                                                <app:img src="common/ImgClose.png" alt="close"/><span style='padding-left:5px'><fmt:message key="close"/></span>
                                            </a>
                                        </td>
                                        <td><div class='vertSep'></div></td>
                                        <app:button id="OPNEWTAG" name="actionNewTag" src="startup/ImgNewTag.png" tooltip="tagNew" text="tagNew"/>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr>
                <td class='ZhAppContent'>
                    <table border="0" cellpadding="0" cellspacing="0" width="100%">
                        <tr>
                            <td width="200" class='List' valign='top'>
                                <table width="100%" cellpadding="2" cellspacing="0">
                                    <c:set var="index" value="${0}"/>
                                    <zm:forEachTag var="tag">
                                        <c:set var="hasTags" value="${true}"/>
                                        <c:if test="${selectedTag.id eq tag.id and not newTag}"><c:set var="selectedRow" value="${index}"/></c:if>

                                        <tr <c:if test="${index eq selectedRow}">class='RowSelected'</c:if>>
                                            <td width="1%">
                                                &nbsp;
                                            </td>
                                            <td nowrap colspan="3" class='Folder ${tag.hasUnread ? ' Unread':''}'>
                                                <c:url var="selectUrl" value="">
                                                    <c:param name="id" value="${tag.id}"/>
                                                </c:url>
                                                <a id="A${index}" href="${fn:escapeXml(selectUrl)}">
                                                    <app:img src="${tag.image}" alt="tag"/>
                                                        <span style='vertical-align:middle;'>${fn:escapeXml(tag.name)}
                                                            <c:if test="${tag.hasUnread}"> (${tag.unreadCount}) </c:if>
                                                        </span>
                                                </a>
                                            </td>
                                        </tr>
                                        <c:set var="index" value="${index+1}"/>
                                    </zm:forEachTag>
                                    <c:if test="${not hasTags}">
                                        <tr>
                                            <td>
                                                <div class='NoResults'>
                                                    <fmt:message key="tagNoTags"/>
                                                </div>
                                            </td>
                                        </tr>
                                    </c:if>
                                </table>
                            </td>
                            <td class='ZhEditTagContent' valign='top'>
                                <c:choose>
                                    <c:when test="${zm:actionSet(param, 'actionNewTag')}">
                                        <app:newTag/>
                                    </c:when>
                                    <c:when test="${not empty selectedTag}">
                                        <app:editTag tag="${selectedTag}"/>
                                    </c:when>
                                    <c:otherwise>
                                        &nbsp;
                                    </c:otherwise>
                                </c:choose>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>

        </table>
        <input type="hidden" name="doAction" value="1"/>
        <input type="hidden" name="crumb" value="${fn:escapeXml(mailbox.accountInfo.crumb)}"/>
    </form>
     <SCRIPT TYPE="text/javascript">
    <!--
    var zrc = ${index};
    var zindex = ${selectedRow};
    var zclick = function(id) { var e2 = document.getElementById(id); if (e2) e2.click(); }
    var zget = function(i) { var e = document.getElementById("A"+i); if (e && e.href) window.location = e.href; }
    var zprev = function() { if (zindex > 0) zget(zindex-1);}
    var znext = function() { if (zindex+1 < zrc) zget(zindex+1);}
    //-->
    </SCRIPT>
    <app:keyboard cache="mail.mtags" mailbox="${mailbox}" globals="true" passspecial="true">
        <zm:bindKey message="global.Cancel" id="OPCLOSE"/>
        <zm:bindKey message="mtags.NewTag" func="function() {zclick('SOPNEWTAG');}"/>
        <zm:bindKey message="global.PreviousItem" func="zprev"/>
        <zm:bindKey message="global.NextItem" func="znext"/>
        <zm:bindKey message="mail.MarkAllRead" func="function() {zclick('OPMARKALLREAD');}"/>
        <zm:bindKey message="mtags.Save" func="function() {zclick('OPSAVE');}"/>
    </app:keyboard>
</app:view>
