<%
  import org.apache.commons.lang.StringEscapeUtils;
  import java.util.List;
  import java.util.ArrayList;
  import java.util.Locale;
  import java.text.DateFormat;
  import org.exoplatform.portal.webui.util.Util;
  import org.exoplatform.wiki.mow.api.PageVersion;
  import org.exoplatform.wiki.webui.UIWikiPortlet;
  import org.exoplatform.wiki.webui.UIWikiBottomArea;
  import org.exoplatform.wiki.commons.Utils;
%>
<%
	def rcontext = _ctx.getRequestContext() ;
  def requireJs = rcontext.getJavascriptManager().getRequireJS();
  requireJs.require("SHARED/UIWikiPageVersionsList", "UIWikiPageVersionsList");
  requireJs.addScripts("UIWikiPageVersionsList.init(\""+uicomponent.id+"\");");
  
  Locale currentLocale = Util.getPortalRequestContext().getLocale();
  List<PageVersion> versionsList = uicomponent.getVersionsList();
  if(versionsList == null){
    versionsList = new ArrayList<PageVersion>();
  }
  
  String compareAction = uicomponent.COMPARE_ACTION;
  
  String anchor = "#" + compareAction;
  String compareActionId = UIWikiPortlet.WIKI_PORTLET_ACTION_PREFIX + compareAction;
  String compareActionLink = uicomponent.event(compareAction);
%>
<div class="uiWikiPageVersionsList uiAttachmentUploadContainer">
 <% uiform.begin(); %>
  <h4 class="historyLabel"><%= _ctx.appRes("UIWikiPageVersionsList.label.HistoryLabel"); %></h4>
	<div class="compareSelect">
		<button type="button"  class="btn btn-primary refreshModeTarget"><%= _ctx.appRes("UIWikiPageVersionsList.label.CompareSelected"); %></button>
		<a onclick="$compareActionLink" id="$compareActionId" style="display:none;"></a>	
	</div>
    <table class="uiFormGrid uiGrid table table-hover table-striped">
      <thead >
        <tr>
          <th ></th>
          <th ><%= _ctx.appRes("UIWikiPageVersionsList.label.Version"); %></th>
          <th ><%= _ctx.appRes("UIWikiPageVersionsList.label.Date"); %></th>
          <th ><%= _ctx.appRes("UIWikiPageVersionsList.label.Author"); %></th>
          <th ><%= _ctx.appRes("UIWikiPageVersionsList.label.Summary"); %></th>
          <th class="center"><%= _ctx.appRes("UIWikiPageVersionsList.label.Action"); %></th>
        </tr>
      </thead>
      <tbody>
      <%
          int index = 0;
          boolean isCurrentVersion = true;
          DateFormat df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT, currentLocale);
          for(version in versionsList){
            index++;
            if(index != 1){
              isCurrentVersion = false;
            }
            String versionNumber = version.getName();
            String vertionId = uicomponent.VERSION_NAME_PREFIX + "_" + versionNumber;
            String versionLabel = _ctx.appRes("UIWikiPageInfoArea.label.version-short").toLowerCase()+"." + versionNumber;
            
            if(isCurrentVersion){
              versionLabel = _ctx.appRes("UIWikiPageVersionsList.label.Current") + " ( " + _ctx.appRes("UIWikiPageInfoArea.label.version-short").toLowerCase()+"." + versionNumber + ")";
            }
            String author = StringEscapeUtils.escapeHtml(Utils.getFullName(version.getAuthor()));
            String comment = StringEscapeUtils.escapeHtml(version.getComment());
            String viewLink = uicomponent.event(uicomponent.VIEW_REVISION, versionNumber);
            String viewLinkId=  UIWikiPortlet.WIKI_PORTLET_ACTION_PREFIX + uicomponent.VIEW_REVISION + "_" + versionNumber;
        %>
        <tr>
          <td class="center">
            <%
              uicomponent.renderField(vertionId);
            %>
          </td>
          <td >
            <label for="<%=versionNumber%>">
	          	<a onclick="javascript:eXo.wiki.UIWikiAjaxRequest.makeNewHash('#<%=uicomponent.VIEW_REVISION%>_<%=versionNumber%>')" rel="tooltip" data-placement="bottom" title="<%= _ctx.appRes("UIWikiPageVersionsList.label.ViewVersion"); %>" ><%= versionLabel %></a>
	          	<a id="$viewLinkId" onclick="$viewLink" style="display:none"></a>
          	</label>  
          </td>
          <td><%=df.format(version.getUpdatedDate())%></td>
          <td><%=author%></td>
          <td><%=((comment != null) ? comment : "")%></td>
          <td class="center">
          	<% uicomponent.renderRestoreRevisionActions(versionNumber); %>
          </td>
        </tr>
        <%}%>
      </tbody>
    </table>
 <% uiform.end();%>
</div>