//Complaintee.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class Complaintee extends HttpServlet
{
	// define a few things we'll need
	private HttpSession session = null;
	private Connection conn = null;
	private Configure cfg = new Configure();

	public void init (ServletConfig config)
		throws ServletException
	{
		try
		{
			Class.forName("oracle.jdbc.driver.OracleDriver");
			conn = DriverManager.getConnection (cfg.dbPath, cfg.dbUsername, cfg.dbPassword);
		}

		catch (SQLException sqe)
		{
			sqe.printStackTrace();
		}

		catch (ClassNotFoundException ce)
		{
			System.out.println(ce);
		}
		System.out.println("Complaintee: Init Successful");
	}
	
	// Not implemented yet
	public void doGet (HttpServletRequest req, HttpServletResponse res)
		throws ServletException, IOException
	{
		doPost (req, res);
	}

	// Frankly i dont know what this does. but what the heck
	public void doDelete (HttpServletRequest req, HttpServletResponse res)
		throws ServletException, IOException
	{
	}
	public void doPost (HttpServletRequest req, HttpServletResponse res)
		throws ServletException, IOException
	{
		session = req.getSession (true);
		PrintWriter out = res.getWriter();
		String username = req.getParameter ("username");
		String password = req.getParameter ("password");
		session.putValue (session.getId(), username);
		session = null;

		res.setContentType ("text/html");
		try
		{
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery ("SELECT passwd from LOGIN WHERE name='"+username+"'");
						
			String temp="";
			while(rs.next())
				temp=rs.getString(1);	
			if (password.equals (temp))
			{
				//User logged in
				session = req.getSession (true);
				out.println("<html><head><title>Welcome "+username+"</title></head>");
				out.println("<body bgcolor=\"#ffffff\"<p align=\"right\">"+
				"<a href=\"" + cfg.ServletDir + "ChangePassword?username="+username+
				"\">Change Password</a> | "+
				"<a href=\"" + cfg.ServletDir + "EditPersonal?username="+
				username+"&password="+password+"\">Edit Personal Info</a> | " + 
				"<a href=\""+cfg.ServletDir+"Logout\">Logout</a>"
				+"<h2>Welcome "+username+"</h2>");
				
				
				String query = "select complaint.complaint_id, complaint.title,complaint.description, complaint.date_lodged from complaint,register,complaintee where complaint.complaint_id = register.complaint_id and complaintee.user_id = register.user_id and complaintee.user_id ='" + username+"' and complaint.start_date is null";
				
				stmt = conn.createStatement ();
				rs = stmt.executeQuery (query);
				
				out.println("<table border=0 cellspacing=2 width=100%>");
				out.println("<tr bgcolor=\"#8888EE\"><td colspan=2 align=\"center\"><b>Complaints not yet looked at<td></tr>");

				while (rs.next())
				{
					out.println("<tr bgcolor=\"#8888EE\"><td valign=\"top\" width=70%><b>"+
					rs.getString(1)+"</b> - "+rs.getString(3)+
					"</td><td valign=\"top\" width=30%>Date Lodged :"+rs.getString(4)+
					"</td></tr>");
				}
				out.println("</table><p>");
				//Get his previous unresolved complaints

				query = "select complaint.complaint_id, complaint.title,complaint.description, complaint.start_date,complaint.status from complaint,register,complaintee where complaint.complaint_id = register.complaint_id and complaintee.user_id = register.user_id and complaintee.user_id ='" + username+"' and complaint.start_date is not null and complaint.end_date is null";
				
				stmt = conn.createStatement ();
				rs = stmt.executeQuery (query);

				
				out.println("<table border=0 cellspacing=2 width=100%>");
				out.println("<tr bgcolor=\"#88EE88\"><td colspan=2 align=\"center\"><b>Complaints being resolved</td></tr>");

				while (rs.next())
				{
					out.println("<tr bgcolor=\"#88EE88\"><td valign=\"top\" width=70%><b>"+
					rs.getString(1)+":"+rs.getString(2)+"</b><br>"+rs.getString(3)+
					"</td><td valign=\"top\" width=30%>Date Started :"+rs.getString(4)+
					"<br>Status :"+rs.getString(5)+"</td></tr>");
				}
				out.println("</table><p>");
				
				//Show the resolved complaints	
				query = "select complaint.complaint_id, complaint.title,complaint.description, complaint.start_date,complaint.end_date from complaint,register,complaintee where complaint.complaint_id = register.complaint_id and complaintee.user_id = register.user_id and complaintee.user_id ='" +
				username+"'  and complaint.end_date is not null and complaint.rating is null";
				stmt = conn.createStatement ();
				rs = stmt.executeQuery (query);
//				rsmd = rs.getMetaData();
				out.println("<table border=0 cellspacing=2 width=100%>");
				out.println("<tr bgcolor=\"#EEEE88\"><td colspan=2 align=\"center\"><b>Complaints resolved - Rate Them Please</td></tr>");
				while (rs.next())
				{
					out.println("<tr bgcolor=\"#EEEE88\"><td valign=\"top\" width=70%><b>"+
					rs.getString(1)+":"+rs.getString(2)+"</b><br>"+rs.getString(3)+
					"<br><a href=\""+cfg.ServletDir+"ComplainteeRate?" +
					"username=" + username + "&password=" + password + "&complaint_id=" + 
					rs.getString(1) + "\">Rate It/ Give Feedback</a>"+
					"<td valign=\"top\" width=30%>Start Date :"+rs.getString(4)+
					"<br>End Date :"+rs.getString(5));
				}
				out.println("</table>");
				out.println("<h2>Register New Complaint <a href=\""+cfg.ServletDir+"NewComplaint?username="+username+"&password="+password+"\">Here</a></h2>");
//				out.println("<h2><form action="+cfg.ServletDir+"NewComplaint method=\"GET\">"+
//				"<input type=\"hidden\" name=\"username\" value=\""+username+"\">" +
//				"<input type=\"hidden\" name=\"password\" value=\""+password+"\">" +
//				"<input type=\"submit\" value=\"Register New Complaint\"></form>");
				out.println("<h3>Look at all complaints by location. <a href=\""+cfg.ServletDir+"ShowByLocation\">Click here</a></h3>");
				out.println("</body></html>");
			}	
			else
			{
				out.println("<html><head><title>Login Failure</title></head>");
				out.println("<body bgcolor=#ffffff>Login Failure. Please Try again</body></html>");
			}
		}
		catch (Exception e)
		{
			out.println(e);
		}
	}
}
