<%-- 
    Document   : conditionsList
    Created on : 21.1.2018, 19:59:33
    Author     : themi
--%>
<%@page import="cz.csu.sdmx.constants.SESSION"%>
<%@page import="java.util.List"%>
<%@page import="cz.csu.sdmx.validation.model.LogicCondition"%>
<%@page import="cz.csu.sdmx.web.security.User"%>
<%@page import="cz.csu.sdmx.constants.URL"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix = "fn" uri = "http://java.sun.com/jsp/jstl/functions" %>
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<jsp:include page="reusable/header.jsp">
    <jsp:param name="scripts" value="jquery-ui.js" />
    <jsp:param name="stylesheets" value="jquery-ui.css" />
    <jsp:param name="title" value="Conditions" />
</jsp:include>
<div style="float:left; width: 49%">
    <h2>Export</h2>
    <a class="btn btn-default" href="<c:url value="<%= URL.CONDITIONS_EXPORT %>" />">Export all conditions</a>
    <a class="btn btn-default" href="<c:url value="<%= URL.CONDITIONS_EXPORT_ALLOWED %>" />">Export allowed conditions</a>
</div>
<div style="clear: both;"></div>
<hr style="margin: 20px 0 20px 0" class="hr-primary">
<h2>Conditions list</h2>
<script>
    $( function() {
      var availableTags = [
      <c:forEach var="dsd" items="${dsds}" varStatus="status">
                "${dsd}"<c:if test="${!status.last}">,</c:if>
      </c:forEach>
      ];
      $("#dsds").autocomplete({
        source: availableTags,
        close: function() {
            $("#searchConditionsForm").submit();
        }
      });
    } );
</script>
<script>
$(document).ready(function(){
    $('[data-toggle="tooltip"]').tooltip(); 
});
</script>
<form id="searchConditionsForm" action="<c:url value="<%= URL.CONDITIONS_FILTER%>" />" method="GET">
  <div class="input-group col-lg-9">
        <input type="text" id="dsds" name="dsd" class="form-control" placeholder="DSD" value="${dsd}">
    <div class="input-group-btn">
      <button class="btn btn-default" style="width: 100px;" type="submit">
        <i class="glyphicon glyphicon-search"></i>
      </button>
        <a data-toggle="tooltip" title="Download dsd related conditions" data-placement="top" href="<c:url value="<%= URL.CONDITIONS_EXPORT %>" />/${dsd}" class="btn btn-default"><span class="glyphicon glyphicon-download"></span></a>
    </div>
  </div>
</form>
<% 
    pageContext.setAttribute("conditionPermission", false);
    
    List<LogicCondition> lcList = (List<LogicCondition>) request.getAttribute("conditions");
    if(lcList != null && lcList.size() != 0){
        User currentUser = (User) session.getAttribute(SESSION.USER);
        LogicCondition lc = lcList.get(0);
        boolean canEdit = currentUser.hasUrn(lc.getDsdName());

        pageContext.setAttribute("conditionPermission", canEdit);
    }
%>
<table class="col-sm-12 tableList">
    <tr><th class="col-lg-1">ID</th><th class="col-lg-2">Obs attr value name</th><th class="col-lg-3">Condition</th><th class="col-lg-4">Common dimensions</th><th class="col-lg-1"></th><c:if test="${conditionPermission}" ><th class="col-lg-1"></th></c:if></tr>
    <c:forEach var="condition" items="${conditions}">
        <tr>
            <td><c:out value="${condition.id}"/></td>
            <td><c:out value="${condition.obsValAttrName}"/></td>
            <td><c:out value="${condition.expression}"/></td>
            <td>
                <c:forEach items="${condition.commonDimensions}" var="dimension" varStatus="status">
                    ${dimension}<c:if test="${!status.last}">,</c:if>
                </c:forEach>
            </td>
            <td>
                <a href="<c:url value="<%= URL.CONDITION_EXPORT %>" />/${condition.id}" class="cleanButton"><span class="glyphicon glyphicon-download"></span></a>
            </td>
            <c:if test="${conditionPermission}" >
                <td>
                    <form class="deleteConditionForm" action="<c:url value="<%=URL.CONDITION_DELETE%>" />" method="POST">
                        <input name="id" type="hidden" value="${condition.id}" />
                        <input name="dsdUri" type="hidden" value="${condition.dsdName}" />
                        <button  class="cleanButton">
                            <span class="glyphicon glyphicon-trash" aria-hidden="true"></span>
                        </button>
                    </form>            
                </td>            
            </c:if>
        </tr>
    </c:forEach>        
</table>
<jsp:include page="reusable/footer.jsp" />


