<%@ page contentType="text/html;charset=GBK" %>
<%
/**
 *	$RCSfile: manageRewards.jsp,v $
 *	$Revision: 1.1.1.1 $
 *	$Date: 2002/09/09 13:50:17 $
 */
%>

<%@ page import="java.util.*,
				 java.text.*,
                 java.sql.*,
				 com.jivesoftware.util.*,
                 com.jivesoftware.forum.*,
				 com.jivesoftware.forum.database.*,
				 com.jivesoftware.forum.util.*"
    errorPage="error.jsp"
%>

<%@ include file="global.jsp" %>

<%! // Global vars, methods, etc

    private static final String USER_POINTS_SQL
        = "SELECT creationDate,rewardPoints,messageID,threadID FROM jiveReward WHERE userID=? ORDER BY creationDate DESC";
%>

<%	// get parameters
    long userID = ParamUtils.getLongParameter(request,"userID",-1L);
    String username = ParamUtils.getParameter(request,"username");
    boolean loadUser = ParamUtils.getBooleanParameter(request,"loadUser");
    int numPoints = ParamUtils.getIntParameter(request,"numPoints",0);
    boolean addPoints = ParamUtils.getBooleanParameter(request,"addPoints");
    int start = ParamUtils.getIntParameter(request,"start",0);
    int range = ParamUtils.getIntParameter(request,"range",2);

    boolean errors = false;
    String errorMessage = "";
    User user = null;
    if (loadUser) {
        UserManager userManager = forumFactory.getUserManager();
        try {
            user = userManager.getUser(userID);
        }
        catch (Exception ignored) {}
        if (user == null && username != null) {
            try {
                user = userManager.getUser(username);
            }
            catch (Exception ignored) {}
        }
        if (user == null) {
            errors = true;
            errorMessage = "没有找到指定用户。请确认输入了正确的用户名或者用户ID";
        }
    }

    int currentUserRewardPoints = 0;
	int totalUserRewardPoints = 0;
    if (!errors && user != null) {
        RewardManager rewardManager = forumFactory.getRewardManager();
        if (addPoints) {
            try {
                rewardManager.addPoints(user, numPoints);
            }
            catch (Exception e) {e.printStackTrace();}
            response.sendRedirect("manageRewards.jsp?userID=" + user.getID()
                + "&loadUser=true");
            return;
        }
        currentUserRewardPoints = rewardManager.getCurrentPoints(user);
		totalUserRewardPoints = rewardManager.getTotalPointsEarned(user);
    }
%>

<%@ include file="header.jsp" %>

<p>

<%  // Title of this page and breadcrumbs
    String title = "点数管理";
    String[][] breadcrumbs = {
        {"主页面", "main.jsp"},
        {title, "manageRewards.jsp"}
    };
%>
<%@ include file="title.jsp" %>

<font size="-1">
点数是用户间的虚拟货币，用于鼓励用户回答别人的问题。使用使用以下的设置可以控制这个特性，但是请注意这不是Jive的缺省功能。
</font>

<p>

<%  if (user == null) { %>

    <form action="manageRewards.jsp">
    <input type="hidden" name="loadUser" value="true">

    <%  if (errors) { %>
        <i><%= errorMessage %></i>
        <p>
    <%  } %>

    <font size="-1">
    请输入你要管理的用户的用户名 <b>或者</b> 用户ID:
    <p>
    </font>
    <table cellpadding="2" cellspacing="0" border="0" width="100%">
    <tr>
        <td width="10%" rowspan="99">&nbsp;</td>
        <td width="10%">
            <font size="-1">用户名</font>
        </td>
        <td width="80%"><input type="text" name="username" value="" size="30"></td>
    </tr>
    <tr>
        <td width="10%">&nbsp;</td>
        <td width="80%"><font size="-1">或者</font></td>
    </tr>
    <tr>
        <td width="10%">
            <font size="-1">用户ID</font>
        </td>
        <td width="80%"><input type="text" name="userID" value="" size="6"></td>
    </tr>
    <tr>
        <td width="10%">&nbsp;</td>
        <td width="80%"><input type="submit" value="管理"></td>
    </tr>
    </table>

<%  } else { %>

    <font size="-1">
    <b><%= user.getUsername() %></b>的点数:
    <p>

    <ul>
        当前点数: <b><%= currentUserRewardPoints %></b><br>
		总点数: <b><%= totalUserRewardPoints %>
    </ul>

    <form action="manageRewards.jsp">
    <input type="hidden" name="userID" value="<%= user.getID() %>">
    <input type="hidden" name="loadUser" value="true">
    <input type="hidden" name="addPoints" value="true">

    给予此用户的点数:
    <ul>
        增加 <input type="text" name="numPoints" value="" size="5"> 点(可以为负值)。
        <input type="submit" value="给予">
    </ul>
    </form>

    用户点数记录:
    <p>
    </font>
    <ul>
    <table bgcolor="<%= tblBorderColor %>" cellpadding="0" cellspacing="0" border="0" width="">
    <tr><td>
    <table bgcolor="<%= tblBorderColor %>" cellpadding="3" cellspacing="1" border="0" width="100%">
    <tr bgcolor="#eeeeee">
        <td><font size="-2">&nbsp;</font></td>
        <td align="center"><font size="-2" face="verdana"><b>点数</b></font></td>
        <td align="center"><font size="-2" face="verdana"><b>日期</b></font></td>
        <td align="center"><font size="-2" face="verdana"><b>主题ID</b></font></td>
        <td align="center"><font size="-2" face="verdana"><b>消息ID</b></font></td>
    </tr>
<%  Connection con = null;
    PreparedStatement pstmt = null;
    boolean less = false;
    boolean more = false;
    try {
        con = ConnectionManager.getConnection();
        pstmt = con.prepareStatement(USER_POINTS_SQL);
        pstmt.setLong(1, user.getID());
        ResultSet rs = pstmt.executeQuery();
        int count = 0;
        while (count < start) {
            count++;
            rs.next();
            less = true;
        }
        while (rs.next() && (count < (start+range))) {
            long date = rs.getLong(1);
            int points = rs.getInt(2);
            long mID = rs.getLong(3);
            if (rs.wasNull()) {
                mID = -1L;
            }
            long tID = rs.getLong(4);
            if (rs.wasNull()) {
                tID = -1L;
            }

%>
    <tr bgcolor="#ffffff">
        <td><font size="-1"><%= ++count %></font></td>
        <td align="center"><font size="-1"><%= points %></font></td>
        <td align="center"><font size="-1">&nbsp;<%= JiveGlobals.formatDateTime(new java.util.Date(date)) %>&nbsp;</font></td>
        <td align="center"><font size="-1">
            <%  if (tID != -1L) { %>
                <%= tID %>
            <%  } else { %>
                &nbsp;
            <%  } %>
        </font></td>
        <td align="center"><font size="-1">
            <%  if (mID != -1L) { %>
                <%= mID %>
            <%  } else { %>
                &nbsp;
            <%  } %>
        </font></td>
    </tr>
<%
        }
        if (rs.next()) {
            more = true;
        }
    }
    catch (SQLException sqle) {
        sqle.printStackTrace();
    }
    finally {
        try {  pstmt.close();   }
        catch (Exception e) { e.printStackTrace(); }
        try {  con.close();   }
        catch (Exception e) { e.printStackTrace(); }
    }
%>
    </table>
    </td></tr>
    </table>
    </ul>

    <br>

    <table cellpadding="3" cellspacing="0" border="0" width="80%" align="center">
    <tr>
        <td width="1%" nowrap>
        <%  if (less) { %>
        &laquo;
        <a href="manageRewards.jsp?userID=<%= user.getID() %>&loadUser=true&start=<%= (start-range) %>&range=<%= range %>"
        ><font size="-1">前 <%= range %> 条记录</font></a>
        <%  } %>
        </td>
        <td width="98%">&nbsp;</td>
        <td width="1%" nowrap>
        <%  if (more) { %>
        <a href="manageRewards.jsp?userID=<%= user.getID() %>&loadUser=true&start=<%= (start+range) %>&range=<%= range %>"
        ><font size="-1">后 <%= range %> 条记录</font></a>
        &raquo;
        <%  } %>
        </td>
    </tr>
    </table>

<%  } %>

</body>
</html>


