<%
  import org.exoplatform.webui.core.UIComponent;
  import org.exoplatform.webui.form.UIForm;
  import java.text.DateFormat;
  import java.text.SimpleDateFormat;

  String[] beanFields = uicomponent.getBeanFields();  
  String[] beanActions =  uicomponent.getBeanActions();
  UIComponent uiParent = uicomponent.getParent();
  String name = uicomponent.getLabel();
  UIForm uiForm = uicomponent.getAncestorOfType(UIForm.class); 
  
  DateFormat dateFormat = null;
%>
<div id="$uicomponent.id">
  <table class="UIGrid" cellspacing="0">
    <thead>
      <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><%=_ctx.appRes(uiParent.getName()+".header.action")%></th>
          <%}%>
        <%}%>
      </tr>
    </thead>
    <tbody>
    <%if(uicomponent.getUIPageIterator().getAvailable() < 1) {%>
      <tr>
        <td style="font-style:italic; color: #FF5604; text-align: center;" colspan="<%=beanFields.length+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;
    %>
      <tr class="$rowClass">
    <%
          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)) {
                if(dateFormat == null) 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+"\" title='$fieldValue'>"+fieldValue+"</div></td>";
          } 
          if(beanActions != null && beanActions.length > 0) { 
    %>
        <td>
          <div class="ActionContainer">
              <%
                def beanIdField = uicomponent.getBeanIdField();          
                for(action in beanActions) { 
                  def beanId = uicomponent.getFieldValue(bean, beanIdField) ;
                  if(action == null) continue;
                  String title = _ctx.appRes(uicomponent.getParent().getName() + ".action.title." + action);
                  String actionLink = "";
                  if(uiForm != null){
                    actionLink = uiForm.event(action, uicomponent.getParent().getId(), beanId);
                  } else {
                    actionLink = uiParent.event(action, beanId);
                  }
              %>             
                  <img onclick="$actionLink" alt="$title" title="$title" src="/eXoResources/skin/sharedImages/Blank.gif" class="${action}Icon" />
              <%}%>                
          </div>
        </td>
    <%
          }
    %>  
      </tr>
    <%
        }
      }
    %> 
    </tbody>
  </table> <!--End UIGrid-->
  <%
    _ctx.renderUIComponent(uicomponent.getUIPageIterator());
  %>
</div>
