<%
  import java.util.List;
  import org.apache.commons.lang.StringEscapeUtils;
  
  uicomponent.checkToDisplayNotification();
  def rcontext = _ctx.getRequestContext() ;    
  def saveDraftSuccessMessage = StringEscapeUtils.escapeJavaScript(_ctx.appRes("DraftPage.msg.save-draft-success"));
  def discardDraftConfirmMessage = StringEscapeUtils.escapeJavaScript(_ctx.appRes("DraftPage.msg.keep-draft-question"));
  
  // Resource for cancel action
  def cancelDraftConfirmTitle = StringEscapeUtils.escapeJavaScript(_ctx.appRes("UIConfirmBox.label.Confirmation"));
  def cancelDraftConfirmMessage = StringEscapeUtils.escapeJavaScript(_ctx.appRes("DraftPage.msg.cancel-draft"));
  def yesButtonLabel = StringEscapeUtils.escapeJavaScript(_ctx.appRes("DraftPage.label.yes-button"));
  def noButtonLabel = StringEscapeUtils.escapeJavaScript(_ctx.appRes("DraftPage.label.no-button"));
  def removeDraftRestUrl = uicomponent.getRemoveDraftRestUrl();
  
  def removeDraftAction = uicomponent.getCancelDraftEvent();
  def saveDraftRestUrl = uicomponent.getSaveDraftRestUrl();
  def wikiType = uicomponent.getWikiType();
  def wikiOwner = uicomponent.getWikiOwner();
  def pageId = uicomponent.getCurrentPageId();
  def initDraftName = uicomponent.getInitDraftName();
  def isFirstTimeRenderEdit = uicomponent.isFirstTimeRenderEdit();
  uicomponent.setFirstTimeRenderEdit(false);
  def pageRevision = uicomponent.getCurrentPageRevision();
  boolean isRunAutoSave = uicomponent.isRunAutoSave();
  boolean isDraftForNewPage = uicomponent.isNewPage();
  long getAutoSaveSequenceTime = uicomponent.getAutoSaveSequenceTime();
  List notificationMessages = uicomponent.getNotificationMessages();
  String untitledLabel = StringEscapeUtils.escapeJavaScript(_ctx.appRes("UIWikiPageTitleControlArea.label.Untitled"));
  
  def requireJs = rcontext.getJavascriptManager().getRequireJS();
  requireJs.require("SHARED/UIWikiPageEditForm", "UIWikiPageEditForm");
  requireJs.require("SHARED/UIWikiAjaxRequest", "UIWikiAjaxRequest");
  requireJs.require("SHARED/UIForm", "UIForm");
  requireJs.addScripts("UIWikiPageEditForm.init('" + uicomponent.id + "', '" + saveDraftRestUrl + "', " + isRunAutoSave + ", '" + pageRevision + "', " + isDraftForNewPage + ", " + getAutoSaveSequenceTime + ", '" + untitledLabel + "');");
  requireJs.addScripts("UIWikiPageEditForm.setMessageResource('" + saveDraftSuccessMessage + "', '" + discardDraftConfirmMessage + "');");
  requireJs.addScripts("UIWikiPageEditForm.setCancelDraftAction('" + removeDraftRestUrl + "', '" + cancelDraftConfirmTitle + "', '" + cancelDraftConfirmMessage + "', '" + yesButtonLabel + "', '" + noButtonLabel + "');");
  if (isFirstTimeRenderEdit) {
    requireJs.addScripts("UIWikiPageEditForm.setDraftName('" + initDraftName + "');");
  }
  requireJs.addScripts("UIWikiPageEditForm.setRestParam('" + wikiType + "', '" + wikiOwner + "', '" + pageId + "');");
  requireJs.addScripts("UIWikiPageEditForm.synPublishActivityCheckboxesStatus('" + uiform.FIELD_PUBLISH_ACTIVITY_UPPER + "','" + uiform.FIELD_PUBLISH_ACTIVITY_BOTTOM + "');");

  if (uicomponent.isInTemplateMode()) {
    String defaultTitleOfTemplate = _ctx.appRes("UIWikiPageEditForm.label.SampleTemplateTitle");
    String defaultDescriptionOfTemplate = _ctx.appRes("UIWikiPageEditForm.label.Description");
    requireJs.addScripts("UIWikiPageEditForm.decorateInputOfTemplate('" + defaultTitleOfTemplate + "', '" + defaultDescriptionOfTemplate + "');");
  }
  
  if (uicomponent.isInRichTextEditor()) {
    requireJs.addScripts("UIWikiPageEditForm.checkToRemoveEditorMenu();");
  }
  requireJs.addScripts("UIWikiPageEditForm.fixWikiNotificationTimeZone();");
%>
<div class="uiWikiPageEditForm">
<% uiform.begin();%>
  <% uiform.renderChild(uiform.HELP_PANEL);%>
  <%
    def isHelpPanelRendered = uicomponent.isSidePanelRendered();
    String className = "";
    if(isHelpPanelRendered) className = "class='uiColsLeftsEditForm'";
  %>
  <div $className> 
    <div class="uiEditUpper">
      <% uiform.renderChild(uiform.TITLE_CONTROL); %>
    </div>
	<% uiform.renderChild(uiform.FIELD_TEMPLATEDESCTIPTION);%>
    <div class="actionContainer ">
      <div class="pull-right">
        <%
          uiform.renderChild(uiform.SUBMIT_TOOLBAR_UPPER);
          if(uiform.getChildById(uiform.FIELD_PUBLISH_ACTIVITY_UPPER).isRendered()) {
        %>
            <label class="publishActivityCheckBoxTitle pull-right">
              <%uiform.renderField(uiform.FIELD_PUBLISH_ACTIVITY_UPPER);%>
            </label>
        <% } %>
      </div>
 
      <div class="btn-toolbar ">
        <% uiform.renderChild(uiform.EDITOR_TABS);%>
		<div class="uiWikiPageEditForm_MessageArea "></div>
      </div>

      
    </div>
    <div class="uiWikiNotificationContainer">
      <% for(String message : notificationMessages) { %>
        <%=message%> 
      <% } %>
    </div>

    <div class="uiWikiPageContainer clearfix">
    <%
      textAreaInput = uiform.getUIFormTextAreaInput(uiform.FIELD_CONTENT)
      textAreaInput.setRows(25);
      if(isHelpPanelRendered && uiform.getUIFormTextAreaInput(uiform.FIELD_CONTENT).isRendered()) {
        textAreaInput.setColumns(92);
    %>
        <div class="uiWikiPageContentInputContainer">
          <% uiform.renderField(uiform.FIELD_CONTENT); %>
        </div>
	  
    <% } else if (uiform.getUIFormTextAreaInput(uiform.FIELD_CONTENT).isRendered()) {
      textAreaInput.setColumns(128);
    %>
	    <div class="uiWikiPageContentInputContainer">
	      <% uiform.renderField(uiform.FIELD_CONTENT); %>
	    </div>
    <%} %>
	  
    <% uiform.renderChild(uiform.RICHTEXT_AREA); %>
  
    <% if(uiform.getChildById(uiform.FIELD_COMMENT).isRendered() && uiform.getChildById(uiform.FIELD_PUBLISH_ACTIVITY_BOTTOM).isRendered()) { %>
      <div class="form-horizontal clearfix">
        <div class="control-group">
          <% if(uiform.getChildById(uiform.FIELD_COMMENT).isRendered()) { %>
            <label class="control-label" for="<%=uiform.FIELD_COMMENT%>"><%= _ctx.appRes("UIWikiPageEditForm.label.CommentTitle"); %></label>
          <% } %>
 
          <% if (uiform.getChildById(uiform.FIELD_PUBLISH_ACTIVITY_BOTTOM).isRendered()) { %>	
            <label class="publishActivityCheckBoxTitle pull-right">
              <%uiform.renderField(uiform.FIELD_PUBLISH_ACTIVITY_BOTTOM);%>
            </label>
          <% } %>
           <% if(uiform.getChildById(uiform.FIELD_COMMENT).isRendered()) { %>
            <div class="controls">
              <% uiform.renderField(uiform.FIELD_COMMENT);%>
            </div>
          <% } %>
        </div>
      </div>
    <% } %>
    </div>
    <% uiform.renderChild(uiform.SUBMIT_TOOLBAR_BOTTOM); %>
  </div>
<% uiform.end();%>
</div>
