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

public class EditPersonal extends HttpServlet
{
	// define a few things we'll need
	private HttpSession session = null;
	private Connection conn = null;
	private Statement stmt = null;
	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);
			stmt = conn.createStatement();
		}

		catch (ClassNotFoundException ce)
		{
			System.out.println(ce);
		}
		catch (SQLException sqe)
		{
			sqe.printStackTrace();
		}
		System.out.println("EditPersonal: Init Successful");
	}

	public void doGet (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");

		res.setContentType ("text/html");
		
		try
		{
			ResultSet rs = stmt.executeQuery ("SELECT passwd from LOGIN WHERE name='"+username+"'");
			String temp="";
			while(rs.next())
			temp=rs.getString(1);	
			
			if (password.equals (temp))
			{
				//get old info and show it to the person
				ResultSet rs1 = stmt.executeQuery ("select name, job, contact_info, room_no, building from COMPLAINTEE where user_id='"+username+"'");
				
				while (rs1.next())
				{
				//TODO: ENTER JAVASCRIPT FOR OTHER VALUES	
				out.println("<html><head><title>Change user preferences</title>"+
				"<script language=\"javascript\">"+
				"function filled(field){ if (field.value ==\"\" || field.value == null) return false; else return true; }"+
				"function canSubmit(form) {"+
				"if(!filled(form.name)) {alert(\"Please enter your name.\"); form.name.focus(); return false;}"+
				"if(!filled(form.job)) {alert(\"Please enter your job.\"); form.job.focus(); return false;}"+
				"if(!filled(contact_info.job)) {alert(\"Please enter your contact information.\"); form.contact_info.focus(); return false;}"+
				"if(!filled(room_no.job)) {alert(\"Please enter your room number.\"); form.room_no.focus(); return false;}"+
				"if(!filled(building.job)) {alert(\"Please enter your building.\"); form.building.focus(); return false;}"+
				"}</script></head><body bgcolor=#ffffff><h1>Edit Personal Info</h1>"+
				"<form action=\"" + cfg.ServletDir + "EditPersonal\" method=\"POST\""+
				" onSubmit=\"return canSubmit(this)\">"+
				"<input type=\"hidden\" name=\"username\" value=\"" + username + "\">"+
				"<input type=\"hidden\" name=\"password\" value=\"" + password + "\">"+
				"<table border=0>"+
				"<tr><td>Name:</td><td><input type=\"TEXT\" name=\"name\" value=" + rs1.getString(1) +
				"></td></tr>" +
				"<tr><td>Job:</td><td><input type=\"TEXT\" name=\"job\" value=" + rs1.getString(2) +
				"></td></tr>" +
				"<tr><td>Contact Info:</td><td><input type=\"TEXT\" name=\"contact_info\" value=" +
				rs1.getString(3) + "></td></tr>" +
				"<tr><td>Room Number:</td><td><input name=\"room_no\" type=\"TEXT\" value=" +
				rs1.getString(4) + "></td></tr>" +
				"<tr><td>Building:</td><td><input name=\"building\" type=\"TEXT\" value=" +
				rs1.getString(5) + "></td></tr></table>" +
				"<input type=\"submit\" value=\"Update my Information\">" +
				"</form></body></html>");
				}
			}
			else
			{
				out.println("Sorry. You are not authorised for this operation or have been logged out.");
			}
		}
		catch (Exception e)
		{
			out.println(e);
		}
	}


	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");
		String name = req.getParameter ("name");
		String job = req.getParameter ("job");
		String contact_info = req.getParameter ("contact_info");
		String room_no = req.getParameter ("room_no");
		String building = req.getParameter ("building");
		
		res.setContentType ("text/html");
		
		try
		{
			ResultSet rs = stmt.executeQuery ("SELECT passwd from LOGIN WHERE name='"+username+"'");
			String temp="";
			while(rs.next())
			temp=rs.getString(1);	
			
			if(temp.equals(password))
			{
				stmt.executeQuery ("update COMPLAINTEE set name='" + name + "', job='"+job+
				"', contact_info='"+contact_info+"', room_no='"+room_no+"',building='"+
				building+"' where user_id='"+username+"'");

				out.println("<html><body bgcolor=#ffffff><h2>Your info has been updated</h2>"+
				"<p><form action=\""+cfg.ServletDir+"Complaintee\" method=\"POST\">"+
				"<input type=\"hidden\" name=\"username\" value=\""+username+"\">"+
				"<input type=\"hidden\" name=\"password\" value=\""+password+"\">"+
				"<input type=\"submit\" value=\"Main Page\">"+
				"</form></body></html>");
			}
			else
			{
				out.println("Sorry. You are not authorised for this operation or have been logged out.");
			}
		}
		catch (Exception e)
		{
			out.println(e);
		}
	}
}
