<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.List" %>
<%@ page import="javax.jdo.PersistenceManager" %>
<%@ page import="com.google.appengine.api.users.User" %>
<%@ page import="com.google.appengine.api.users.UserService" %>
<%@ page import="com.google.appengine.api.users.UserServiceFactory" %>
<%@ page import="jp.sourceforge.tamanegisoul.er.ErrorReport" %>
<%@ page import="jp.sourceforge.tamanegisoul.er.PMF" %>

<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <title>Test Page</title>
  </head>

  <body>
    <h1>Entry Error Report </h1>
<%
    UserService userService = UserServiceFactory.getUserService();
    if(!userService.isUserLoggedIn() || !userService.isUserAdmin()){
%>
    <a href="<%= userService.createLoginURL("/") %>">管理者用</a><br/>
    <br/>
<%
    }
    if(!"all".equals(request.getParameter("view"))){
%>
    <a href="./index.jsp?view=all">解決済みも表示</a><br/>
<%
    }else{
%>
    <a href="./index.jsp">未解決のみ表示</a><br/>
<%
    }
    PersistenceManager pm = PMF.get().getPersistenceManager();
    String query = "select from " + ErrorReport.class.getName();
    if(!"all".equals(request.getParameter("view"))){
        query += " where fixed == false";
    }
    List<ErrorReport> reports = (List<ErrorReport>) pm.newQuery(query).execute();
    if (reports.isEmpty()) {
%>
    <p>The ErrorReport has no entries.</p>
<%
    } else {
%>
    <table border="1">
        <tr>
            <th>Fixed</th>
            <th>Date</th>
            <th>ID</th>
            <th>Device</th>
            <th>Model</th>
            <th>SDK Version</th>
            <th>App Name</th>
            <th>App Version</th>
            <th>Message</th>
            <th></th>
        </tr>
<%
        for (ErrorReport r : reports) {
%>
        <tr
<%
            if(Boolean.TRUE.equals(r.getFixed())){
%>
            style="background-color: #a9a9a9"
<%
            }
%>        
        >
            <form action="/errorreport">
<%
            if(Boolean.TRUE.equals(r.getFixed())){
%>
                <td><input name="fixed" type="checkbox" value="fixed" checked="true"/>
<%
            }else{
%>
            <td><input name="fixed" value="fixed" type="checkbox"/>
<%
            }
%>
                <td><%= r.getDate() %></td>
                <td><input name="id" type="hidden" value="<%= r.getId() %>"/><%= r.getId() %></td>
                <td><%= r.getDevice() %></td>
                <td><%= r.getModel() %></td>
                <td><%= r.getSdkVersion() %></td>
                <td><%= r.getAppName() %></td>
                <td><%= r.getAppVersion() %></td>
                <td><%= r.getMessage().getValue() %></td>
                <td>
<%
            if(userService.isUserLoggedIn() && userService.isUserAdmin()){
%>
                <input type="submit"/>
<%
            }
%>
                </td>
            </form>
        </tr>
<%
        }
    }
    pm.close();
%>
    </table>
  </body>
</html>

