<%
   import org.exoplatform.webui.core.UIComponent;
   import org.exoplatform.webui.form.UIForm;
   import java.text.DateFormat;
   import java.text.SimpleDateFormat;
   import org.exoplatform.wiki.webui.UIWikiPortlet;
   import org.exoplatform.wiki.commons.Utils;
   import org.exoplatform.wiki.webui.bean.DraftBean;

   String[] beanFields = uicomponent.getBeanFields();
   String[] beanActions = uicomponent.getBeanActions();
   int colnum = 0;
   if(beanFields != null && beanFields.length > 0 && beanActions != null && beanActions.length > 0) {
      colnum = beanFields.length + 1;
   } else if(beanFields != null && beanFields.length > 0) {
      colnum = beanFields.length;
   }
   
   def fieldToDisplayBreadCrumb = uicomponent.getFieldToDisplayBreadCrumb();
   UIComponent uiParent = uicomponent.getParent();
   String name = uicomponent.getLabel();
   if (name == null) {
     name = uiParent.getName();
   }
   UIForm uiForm = uicomponent.getAncestorOfType(UIForm.class);
   
   String sortField = uicomponent.getSortField();
   boolean isASC = !uicomponent.isASC();
%>
<div id="$uicomponent.id" class="uiWikiDraftGrid">
   <table class="uiGrid  table table-hover table-striped" >
      <thead>
      <tr>
        <% for (field in beanFields) {
          String orderParam = "ASC";
          if (field.equals(sortField) && !isASC) {
            orderParam = "DESC";
          }
          String fileSortParam = field + "_" + orderParam;
          String sortActionLink = uiForm.event(uiParent.ACTION_SORT, uiParent.getId(), fileSortParam);
          String sortTitle = _ctx.appRes(uiParent.getName() + ".action.title." + uiParent.ACTION_SORT);
          
          String clazz = "uiIconArrowUp";
          if (field.equals(sortField)) {
            clazz = "uiIconArrowDown uiIconBlue ";
            if ("DESC".equals(orderParam)) {
              clazz = "uiIconArrowUp uiIconBlue ";
            }
          } else {
            clazz = "uiIconArrowDown uiIconLightGray";
            if ("DESC".equals(orderParam)) {
              clazz = "uiIconArrowUp uiIconLightGray";
            }
          }
        %>
     	  <th onclick="$sortActionLink" style="cursor: pointer;" >
			<i class="$clazz pull-right"></i>
     	    <%=_ctx.appRes(name + ".header." + field)%>     	    
     	  </th>
     	<% } %>
     	<% if (beanActions != null) { %>
     	  <th class="center"><%=_ctx.appRes(name + ".header.action")%></th>
     	<% } %>
      </tr>
      </thead>
      <tbody>
      <% 
      if (uicomponent.getUIPageIterator().getAvailable() < 1) {
      %>
        <tr>
      	  <td class="center empty " colspan="<%=colnum%>">
            <%=_ctx.appRes("UIGrid.msg.empty")%>
          </td>
        </tr>
      <% } else {
        def rowClass = null;
        boolean even = true;
        for (bean in uicomponent.getBeans()) {
          if (even) rowClass = "EvenRow";
          else rowClass = "OddRow";
          even = !even;
      %>
      <tr >
         <%
           for (field in beanFields) {
		      def fieldValue = uicomponent.getFieldValue(bean, field);
              def cssClass = "";
              if(fieldValue != null) {
                 def fieldClass = fieldValue.getClass();
                 if(fieldClass == Integer.class) {
                   cssClass = "number";
                 } else if(java.util.Date.class.isAssignableFrom(fieldClass)) {
                    def dateFormat = new SimpleDateFormat("HH:mm:ss yyyy-MM-dd");
                    cssClass = "datetime";
                    fieldValue = dateFormat.format(fieldValue);
                 } else {
                   cssClass = "text";
                 }
              } else {
                 fieldValue = "";
              }
              String value = fieldValue.toString();
              
              String actionOfField = uicomponent.getActionForField(field);
              def beanId = uicomponent.getBeanIdFor(bean);
              if (actionOfField != null) {
                String title2 = _ctx.appRes(uiParent.getName() + ".action.title." + actionOfField);
                String actionLink2 = uiForm.getActionLink(actionOfField, beanId);
                println "<td class='fieldName'><div class=\"" + cssClass + "\">";
				%>
				  <a data-placement="bottom" rel="tooltip" href="javaScript:void(0)" onclick="$actionLink2"  title="$title2">$fieldValue</a>
				<%
				println "</div></td>";
              } else {
                if (field.equals(fieldToDisplayBreadCrumb)) {
                  String tooltipText = uicomponent.getBreadCrumb(beanId);
                  String wikiId = uicomponent.getWikiName(beanId);
                  %>
                  <td class='breadcrum'><div class="$cssClass" title="$tooltipText" alt="$tooltipText">$wikiId</div></td>
                  <%
                } else {
                  println "<td><div class='$cssClass'>" + fieldValue + "</div></td>";
                }
              }
           }
           
           if (beanActions != null && beanActions.length > 0) {
         %>
         <td class="center">
               <%
                 def beanIdField = uicomponent.getBeanIdField();
                 def beanId = uicomponent.getBeanIdFor(bean);
                 for (action in beanActions) {
                    if (action == null) continue;
                    String title = _ctx.appRes(uiParent.getName() + ".action.title." + action);
                    String actionLink = "";
                    if (uiForm != null) {
                       actionLink = uiForm.event(action, uiParent.getId(), beanId);
                    } else {
                       actionLink = uiParent.event(action, beanId);
                    }
                    String actionId = UIWikiPortlet.WIKI_PORTLET_ACTION_PREFIX + action;
                    String anchor = "#" + action;
               %>
               
               <% if (Utils.getModeFromAction(action)){ %>
			   <a onclick="$actionLink" data-placement="bottom" rel="tooltip" title="$title" class="actionIcon"><i class="uiIcon${action} uiIconLightGray"></i></a>
               <% } else { %>
			   <a onclick="$actionLink" data-placement="bottom" rel="tooltip" title="$title" class="actionIcon"><i class="uiIcon${action} uiIconLightGray"></i></a>
               <% } %>
              <% } %>
         </td>
         <%
               }
         %>
      </tr>
      <%
            }
         }
      %>
      </tbody>
   </table> <!--End UIGrid-->
<%
   if (uicomponent.getUIPageIterator().getAvailablePage() > 1) {
      _ctx.renderUIComponent(uicomponent.getUIPageIterator());
   }
%>
</div>
