import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.lang.*;
import java.sql.*;
import java.util.*;
import Configure;

public class EditWorker extends HttpServlet {
    Connection myCom;
    Statement stmt=null;
    Statement stmt1=null;
    Statement stmt2=null;
    public void doGet (HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException {
	try {
	HttpSession session = req.getSession(true);
	String name = (String)session.getValue(session.getId());
	//	System.out.println("user name " + name);
	DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
	myCom = DriverManager.getConnection(Configure.HOST_NAME,Configure.LOGIN,Configure.PASSWD);
	PrintWriter out;
	String title;
	res.setContentType("text/html");
	out = res.getWriter();
	if (name == null) {
	    out.println("You are already logged out");
	}
	
	String logout = req.getParameter("logout");
	String edit = req.getParameter("edit");
	if (edit == null) {
	    String name1 = req.getParameter ("name");
	    String company = req.getParameter ("company");
	    String designation = req.getParameter ("designation");
	    String contact_info = req.getParameter ("contact_info");
	    String room_no = req.getParameter ("room_no");
	    String building = req.getParameter ("building");
	    
	    if ((logout==null) && (!(name==null))) {
		stmt1 = myCom.createStatement();
		stmt1.executeUpdate ("update WORKER set name='" + name1 + "', company='"+company+ "', designation='"+designation+ "', contact_info='"+contact_info+"', room_no='"+room_no+"',building='"+ building+"'where worker_id='" + name + "'");
	    }
	}
	if (!(logout==null)) {
	    title = "logout db";
	    session.invalidate();
	    out.println("<HTML><HEAD><TITLE>");
	    out.println(title);
	    out.println("</TITLE></HEAD><BODY>");
	    out.println("<h3> You are logged out successfully </br><h3>");
	}
	else if (!(name == null)) {
	
	    title = "View jobs";
	    out.println("<HTML><HEAD><TITLE>");
	    out.println(title);
	    out.println("</TITLE></HEAD><BODY>");
	    out.println("<table cellpadding=4 border=2>");

	    stmt = myCom.createStatement();
	    
	    ResultSet rs1 = stmt.executeQuery ("select name, company, designation, contact_info, room_no, building from WORKER where worker_id='"+ name +"'");
	    
	    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=\"" + Configure.SERVLET_SERVER + ":" + Configure.SERVLET_PORT + "/" + Configure.SERVLET_PATH + "/EditWorker\" method=\"GET\" onSubmit=\"return canSubmit(this)\">" +
			    "<table border=0>"+
			    "<tr><td>Name:</td><td><input type=\"TEXT\" name=\"name\" value=" + rs1.getString(1) +
			    "></td></tr>" +
			    "<tr><td>Company:</td><td><input type=\"TEXT\" name=\"company\" value=" + rs1.getString(2) +
			    "></td></tr>" +
			    "<tr><td>Designation:</td><td><input type=\"TEXT\" name=\"designation\" value=" + rs1.getString(3) +
			    "></td></tr>" +
			    "<tr><td>Contact Info:</td><td><input type=\"TEXT\" name=\"contact_info\" value=" +
			    rs1.getString(4) + "></td></tr>" +
			    "<tr><td>Room Number:</td><td><input name=\"room_no\" type=\"TEXT\" value=" +
			    rs1.getString(5) + "></td></tr>" +
			    "<tr><td>Building:</td><td><input name=\"building\" type=\"TEXT\" value=" +
			    rs1.getString(6) + "></td></tr></table>" +
			    "<input type=\"submit\" value=\"Update my Information\" name=\"some\">" +
			    "</form>");
	    }
	    out.println("<FORM ACTION=\"" + Configure.SERVLET_SERVER + ":" + Configure.SERVLET_PORT + "/" + Configure.SERVLET_PATH + "/Worker\" method =\"GET\">");
	    out.println("<br><INPUT TYPE=\"submit\" NAME=\"view\" VALUE=\"View all jobs\" >");
	    out.println("<br><INPUT TYPE=\"submit\" NAME=\"logout\" VALUE=\"Logout\" >");
	    out.println("</FORM>");
	    out.println("</body></html>");
	    out.close();
	    
	}
	} catch (SQLException sqle) {
	    sqle.printStackTrace();
	}
    }   
}










