
<%
   import org.exoplatform.webui.core.UIComponent;
   import org.exoplatform.webui.form.UIForm;
   import java.text.DateFormat;
   import java.text.SimpleDateFormat;
   import org.gatein.common.text.EntityEncoder;
   import org.exoplatform.commons.utils.HTMLEntityEncoder;

   def rcontext = _ctx.getRequestContext();
   rcontext.getJavascriptManager().require("SHARED/bts_tooltip").require("SHARED/jquery", "\$")
   				 .addScripts("\$('[rel=\"tooltip\"]').tooltip();");
   
   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);

   def emptyMessage = _ctx.appRes(uicomponent.getId() + ".msg.empty");
   if("empty".equals(emptyMessage)) {
        emptyMessage = _ctx.appRes("UIGrid.msg.empty");
   }
%>
<div id="$uicomponent.id">
   <table class="uiGrid table table-hover table-striped" cellspacing="0" summary="Component $uicomponent.id">
      <thead>
      <tr>
         <% if (name != null)
         		{ for (field in beanFields)
         			{ %>
         				<th id="<%= field%>" scope="col"><%=_ctx.appRes(name + ".header." + field)%></th>
         		<% }
         			if (beanActions != null)
         			{ %>
         				<th class="center" id="actions" scope="col"><%=_ctx.appRes(name + ".header.action")%></th>
         		<% }
         		}
         		if (name == null)
         		{
         			for (field in beanFields)
         			{ %>
         				<th id="<%= field%>" scope="col"><%=_ctx.appRes(uiParent.getName() + ".header." + field)%></th>
         	 <% }
         			if (beanActions != null && beanActions.length > 0)
         			{ %>
         				<th class="center" id="actions" scope="col"><%=_ctx.appRes(uiParent.getName() + ".header.action")%></th>
         	 <% }
         		} %>
      </tr>
      </thead>
      <tbody>
      <% if (uicomponent.getUIPageIterator().getAvailable() < 1)
      { %>
      <tr>
      	 <td class="empty center" colspan="<%=colnum%>">
            <%=emptyMessage%>
         </td>
      </tr>
      <% }
      else
      {
         for (bean in uicomponent.getBeans())
         {
      %>
      <tr>
         <%
               EntityEncoder encoder = HTMLEntityEncoder.getInstance();
               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 = encoder.encode(fieldValue.toString());
                  println "<td headers=\"$field\"><span  class=\""+cssClass+"\" >"+value+"</span></td>";
               }
               if (beanActions != null && beanActions.length > 0)
               {
         %>
         <td headers="actions" class="center actionContainer">
           <%
                 def beanIdField = uicomponent.getBeanIdField();
                 def beanId = uicomponent.getBeanIdFor(bean);
                 for (action in beanActions)
                 {
                    if (action == null) continue;
                    String title = _ctx.appRes(org.exoplatform.webui.application.WebuiRequestContext.stripUUIDSuffix(uiParent.getName()) + ".action.title." + action);
                    String actionLink = "";
                    if (uiForm != null)
                    {
                       actionLink = uiForm.event(action, uiParent.getId(), beanId);
                    }
                    else
                    {
                       actionLink = uiParent.event(action, beanId);
                    }
           %>
              <a class="actionIcon" href="javascript:void(0)" onclick="$actionLink" rel="tooltip" data-placement="bottom" title="$title" >
               	<i class="uiIcon${action} uiIconLightGray"></i>
              </a>
           <% } %>
           
         </td>
         <%
               }
         %>
      </tr>
      <%
            }
         }
      %>
      </tbody>
   </table> <!--End UIGrid-->
<%
   if (uicomponent.getUIPageIterator().getAvailablePage() > 1)
   {
      _ctx.renderUIComponent(uicomponent.getUIPageIterator());
   }
%>
</div>
