<%
  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;

  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;
  }
      
  UIComponent uiParent = uicomponent.getParent();
  String name = uicomponent.getLabel();
  UIForm uiForm = uicomponent.getAncestorOfType(UIForm.class);
%>

<table id="$uicomponent.id" class="uiGrid table table-hover table-striped" cellspacing="0">
  <!--Template for "Add from template"	-->
  <% 
    if(uicomponent.getUIGridMode() == null || uicomponent.getUIGridMode().equals(uicomponent.TEMPLATE)) { 
  %>
    <tr>
      <th></th>
        <% 
          if (name != null) { 
            for (field in beanFields) { %>
              <th><%=_ctx.appRes(name + ".header." + field)%></th>
        <%  }
            if (beanActions != null) { %>
              <th><%=_ctx.appRes(name + ".header.action")%></th>
        <%  }
          }
          
          if (name == null) {
            for (field in beanFields) { %>
              <th><%=_ctx.appRes(uiParent.getName() + ".header." + field)%></th>
         <% }
            if (beanActions != null && beanActions.length > 0) { %>
              <th class="center"><%=_ctx.appRes(uiParent.getName() + ".header.action")%></th>
         <% }
          }
         %>
        </tr>
             
        <% if (uicomponent.getUIPageIterator().getAvailable() < 1) { %>
          <tr>
      	    <td class="center empty" colspan="<%=colnum + 1%>">
              <%=_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;
            def beanId = uicomponent.getBeanIdFor(bean);
        %>
          <tr>
            <td class="center">
	          <div class="uiRadio">
	            <input type="radio" name="objectId" value="$beanId"/>
	            <span></span>
              </div>
	        </td>
            <%
              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();
                println "<td><div class='" + cssClass + "'>" + fieldValue + "</div></td>";
              }
              
              if (beanActions != null && beanActions.length > 0) { %>
                <td class="center">                  
                  <%
                     def beanIdField = uicomponent.getBeanIdField();
                     for (action in beanActions) {
                       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" id="$actionId/$beanId" style="display:none;">&nbsp;</a>
                   <%  } else { %>
                         <a onclick="$actionLink" alt="$action" class="actionIcon"  rel="tooltip" data-placement="bottom" title="$title"><i class="uiIconPreview uiIconLightGray"></i></a>
                   <%  } 
                  } %>                 
               </td>
             <% } %>
           </tr>
           <% }
           }
           
           
           
    } else {%>
       
       
       
      <!--Template for "Space setting"-->
      <tr>         
        <% 
          if (name != null) { 
            for (field in beanFields) { %>
              <th><%=_ctx.appRes(name + ".header." + field)%></th>
        <%  }
        
            if (beanActions != null) { %>
              <th><%=_ctx.appRes(name + ".header.action")%></th>
        <%  }
          }
          
          if (name == null) {
            for (field in beanFields) { %>
              <th><%=_ctx.appRes(uiParent.getName() + ".header." + field)%></th>
        <%  }
        
            if (beanActions != null && beanActions.length > 0) { %>
              <th class="center"><%=_ctx.appRes(uiParent.getName() + ".header.action")%></th>
        <%  }
          } 
        %>
      </tr>
      
      <% 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();
                 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) {
                       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="eXo.wiki.UIWikiAjaxRequest.makeNewHash('$anchor/$beanId');" alt="$action" class="actionIcon"  rel="tooltip" data-placement="bottom" title="$title"><i class="uiIcon$action uiIconLightGray"></i></a>
                         <a onclick="$actionLink" id="$actionId/$beanId" style="display:none;">&nbsp;</a>
                 <%    } else { %>
                         <a onclick="$actionLink" alt="$action" class="actionIcon"  rel="tooltip" data-placement="bottom" title="$title"><i class="uiIcon$action uiIconLightGray"></i></a>
                 <%    } %>
                   <%} %>                 
               </td>
             <% } %>
           </tr>
         <% }
          }        
    }%>	
</table>

<%
   if (uicomponent.getUIPageIterator().getAvailablePage() > 1)
   {
      _ctx.renderUIComponent(uicomponent.getUIPageIterator());
   }
%>
