<%@page contentType="text/html;charset=UTF-8" pageEncoding="ISO-8859-1" session="true"%>
<%@page errorPage="AdminException.jsp"%>
<%@page import="java.lang.Long"%>
<%@page import="com.fujitsu.iflow.model.bpm.BPMSession"%>
<%@page import="com.fujitsu.iflow.model.workflow.Dashboard"%>
<%@page import="com.fujitsu.iflow.model.workflow.KPIDefStruct"%>
<%
    request.setCharacterEncoding("UTF-8");request.getParameterNames(); //Need dummy call for WL9.1
    
    BPMSession bps = (BPMSession) session.getAttribute("BPMSession");
    if (bps == null) {
        %><jsp:include page="PasswordPanel.jsp" flush="true"/><%
        return;
    }
    
    // stop access of this page, if the logged in user is not a Administrator
    bps.getAdmin();
    
    Dashboard dash = (Dashboard)session.getAttribute("Dashboard");
    if(dash == null)
    {
        dash = bps.getDashboard();
        session.setAttribute("Dashboard",dash);
    }
    KPIDefStruct[] kpiThreshDefs = dash.getKPIThresholdDefs();
    String opnToPerform = request.getParameter("Modify");
    String displaySetting = request.getParameter("kpiSetDispl");
    if(opnToPerform != null)
    {
        long kpiId = Long.parseLong(request.getParameter("KPIId"));
        if(opnToPerform.equals("Change"))
        {
            for(int i=0; i<kpiThreshDefs.length; i++)
            {
                if(kpiThreshDefs[i].getKpiId() == kpiId)
                {
                    session.setAttribute("KPIDefStruct",kpiThreshDefs[i]);
                    session.setAttribute("opnToPerform",opnToPerform);
                    session.setAttribute("AddKPI",null);
                    break;
                }
            }
            response.sendRedirect(response.encodeRedirectURL("AddKPISettings.jsp"));
        }
        else if(opnToPerform.equals("Delete"))
        {
            dash.deleteKPIDef(kpiId);
            session.setAttribute("opnToPerform",opnToPerform);
            response.sendRedirect(response.encodeRedirectURL("KPISettings.jsp"));
        }
    }
    else if(displaySetting != null)
    {
        long kpiId = Long.parseLong(request.getParameter("KPIId"));
        int display = Integer.parseInt(displaySetting);
        KPIDefStruct kpi = null;
        for(int j=0; j<kpiThreshDefs.length; j++)
        {
            if(kpiThreshDefs[j].getKpiId() == kpiId)
            {
                kpiThreshDefs[j].setKpiDisplay(display);
                dash.updateKPIDef(kpiThreshDefs[j]);
                break;
            }
        }
    }
    String[] bcolor = {"evenrow","oddrow"};
    int nextColor = 0;
    String rowColor = bcolor[nextColor];
%>
<%!
    private
    long
    convertToMins(long timeVal)
    {
        // return the value in minutes
        return (timeVal/1000)/60;
    }
%>
<html>
<head>
<title>Interstage Business Process Manager | KPI Settings</title>
<jsp:include page="StyleSheet.jsp" flush="true"/>
<script language="javascript" src="styles/clockscripts.js"></script>
<script>
    function chkBoxClick(chkValue,kpiid)
    {
        if(chkValue == "checked")
        {
            document.ModifyKPIDefForm.kpiSetDispl.value = "0"
        }
        else
        {
            document.ModifyKPIDefForm.kpiSetDispl.value = "1"      
        }
        document.ModifyKPIDefForm.KPIId.value = kpiid
        document.ModifyKPIDefForm.submit();
    }
    
    function buttonClick(buttonOption,kpiid)
    {
        if(buttonOption == "Delete")
        {
            if(!confirm("Are you sure you want to delete this KPI?"))
                return;
        }
        document.ModifyKPIDefForm.KPIId.value = kpiid
        //document.ModifyKPIDefForm.submit();
        document.location.href="jsp/KPISettings.jsp?KPIId=" + kpiid +
                       "&kpiSetDispl=null&Display=" + document.ModifyKPIDefForm.Display.value +
                       "&Modify=Delete";
    }

</script>
</head>
<body class="bodyClass" onLoad="goforit();">
<%
    session.setAttribute("TabIndex","3");
%>
<jsp:include page="Header.jsp" flush="true"/><br>

<table border="0" class="tableClass" width="90%" align="center">
   <tr>
   <td align="right" valign="top" height="35">
   
   <table border="0" cellpadding="0" cellspacing="0"><tr><td>
<table border="0" cellspacing="0" cellpadding="0">
  <tr background="images/buttons/com_button_bg.gif">
    <td width="15"><img src="images/buttons/but_back_dashboard.gif"></td>
    <td background="images/buttons/com_button_bg.gif" nowrap><a href="Dashboard.jsp"><div align="center" class="buttonlink"> Dashboard</div></a></td>
    <td width="9"><div align="right"><img src="images/buttons/com_button_right.gif" width="9" height="24"></div></td>
  </tr>
</table>
</td><td>&nbsp;</td><td>
<table border="0" cellspacing="0" cellpadding="0">
  <tr background="images/buttons/com_button_bg.gif">
    <td width="15"><img src="images/buttons/add_kpi.gif"></td>
    <td background="images/buttons/com_button_bg.gif" nowrap><a href="AddKPISettings.jsp?AddKPI=Add+KPI"><div align="center" class="buttonlink"> Add KPI</div></a></td>
    <td width="9"><div align="right"><img src="images/buttons/com_button_right.gif" width="9" height="24"></div></td>
  </tr>
</table>

   </td>
   </tr>
  </table> 
 </td></tr></table>

    <TABLE bgcolor="#CCCCCC" border="0" cellpadding="4" cellspacing="1" class="tableClass" width="90%" align="center">
      <tr align="center" class="headrow">
        <td colspan="7" bgcolor="#EEEEEE" style="filter:progid:DXImageTransform.Microsoft.Gradient(startColorStr='#FFFFFF', endColorStr='#6699CC', gradientType='1')"><font color="#000000" size="2">Key Performance Indicators</font></td>
      </tr>
    <form name="ModifyKPIDefForm" enctype="multipart/form-data" method="get" action="<%= response.encodeURL("KPISettings.jsp") %>">
    <input type="hidden" name="KPIId" value="-1">
    <input type="hidden" name="kpiSetDispl" value="null">
      <tr align="center" class="headrow" bgcolor="#0066CC">
        <th> KPI Name </th>
        <th> Target </th>
        <th> Initial </th>
        <th> Critical </th>
        <th> Display </th>
        <th> </th>
        <th> </th>
      </tr>
      <%
      if(kpiThreshDefs != null)
      {
        for(int i=0; i<kpiThreshDefs.length; i++)
        {
            KPIDefStruct kpiDef = kpiThreshDefs[i];
            long targetVal = kpiDef.getTargetValue();
            if(kpiDef.getKpiMeasure() != 1)
                targetVal = convertToMins(targetVal);
                
            rowColor = bcolor[nextColor];
            nextColor = (nextColor + 1)%2;
      %>
        <tr class="<%=rowColor%>">
        <td><%= kpiDef.getName() %> </td> 
        <td><%= targetVal %> </td>
        <%
            for(int j=0; j<kpiDef.getThresholds().length; j++)
            {
                long thresholdVal = kpiDef.getThresholds()[j].thresholdValue;
                if(kpiDef.getKpiMeasure() != 1)
                    thresholdVal = convertToMins(thresholdVal);
        %>
                <td><%= thresholdVal %> </td>
        <%
            }
        %>
        <td>
        <%
            String chkValue ="";
            if(kpiDef.getKpiDisplay() == 1)
            {
                chkValue = "checked";
            }
        %>
            <INPUT TYPE="checkbox" NAME="Display" VALUE="<%= chkValue %>" onclick="chkBoxClick(this.value,<%= kpiDef.getKpiId() %>)" <%= chkValue %> >
        </td>
            <td><input value="Change" type="Submit" name="Modify" onclick="buttonClick(this.value,<%= kpiDef.getKpiId() %>)" class="core-button"></td>
            <td><input value="Delete" type="Submit" name="Modify" onclick="buttonClick(this.value,<%= kpiDef.getKpiId() %>)" class="core-button"></td>
        </tr>
      <%
        }
       }
      %>
      </form>
    </TABLE>
    </body>
</html>
