<!-- Author : Jagbir Singh -->
<!-- Debug : Amanbir Singh -->
<!-- Tested by : Swapnil Sarode , Siddhesh Surve -->

 <%
    response.setHeader("Cache-Control","no-store");
    response.setHeader("Pragma","no-cache");
    response.setDateHeader ("Expires", 0); 
    %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@page language="java"%>
    <%@page import="java.sql.*, javax.sql.DataSource, javax.naming.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%
DataSource pool;
int flag =0;

  Connection conn = null;
  
  PreparedStatement pstmt = null;
 
  String id = (String)session.getAttribute("sUserName");
 
  String active = (String)session.getAttribute("active");
  String loginQuery = null;
  double bmi = 0;

  try {

	InitialContext ctx = new InitialContext();
	pool = (DataSource)ctx.lookup("java:comp/env/jdbc/TestDB");
	if (pool == null)
	   throw new SQLException("Unknown DataSource 'jdbc/TestDB'");
	conn = pool.getConnection();

	  String drname;
	  String spclization;
	  String pswd2 = request.getParameter("pswd2");
	  String fname = request.getParameter("firstname");
	  String lname = request.getParameter("surname");
	  String email = request.getParameter("email");
	  
	  String sex = null;
	  String age =null;
	  String phnumber =null;
	  String height=null;
	  String weight=null;
	  String Occupation=null;
	  if(active.equals("actpatient")){
	   sex = request.getParameter("SEX(M/F)");
	  age = request.getParameter("age");
	  phnumber = request.getParameter("phNumber");
	  height = request.getParameter("height");
	   weight = request.getParameter("weight");
	   Occupation = request.getParameter("Occupation");
	  }
	  if(active.equals("actpatient")){
	  double heightInch = Double.parseDouble(height);
	  double weightLbs = Double.parseDouble(weight);
	  		heightInch = (heightInch/2.5);
	  		 bmi = ((weightLbs/(heightInch*heightInch))*703);
	  			bmi = bmi * 100;
	  			bmi = Math.round(bmi);
	  			bmi = bmi/100;
	  }
	  String add2 = request.getParameter("address2");
	  
	  
	  if(active.equals("actpatient")){
	   drname = request.getParameter("DoctorName");}
	  else{
		   spclization = request.getParameter("spclization");
	  }
	
	
	if(active.equals("actpatient")){		//updating patients profile details
		loginQuery = "UPDATE userInfo set passWord = ?, email = ?, firstName = ?, lastName = ?,conNumber = ?, addressPerm = ?, seX = ?, age = ?, height = ?, weight = ?, occupation = ?, BMI = ? where user_id = ?";
	}
	else
	loginQuery = "UPDATE drInfo set passWord = ?, email = ?, firstName = ?, lastName = ?,  conNumber = ?, addressPerm = ? where doc_id = ?";
	
	pstmt = conn.prepareStatement(loginQuery);
	pstmt.setString(1, pswd2);
	pstmt.setString(2, email);
	pstmt.setString(3, fname);
	pstmt.setString(4, lname);
	pstmt.setString(5, phnumber);
	pstmt.setString(6, add2);
	if(active.equals("actpatient")){
		pstmt.setString(7, sex);
		pstmt.setString(8, age);
		
		pstmt.setString(9, height);
		pstmt.setString(10, weight);
		pstmt.setString(11, Occupation);
		pstmt.setDouble(12, bmi);
		pstmt.setString(13, id);
	} else {
		pstmt.setString(7, id);
	}
	
	pstmt.executeUpdate();
	}
	catch(Exception e){
		e.printStackTrace();
	} finally {
		if(pstmt!= null){
			pstmt.close();
		}
		if(conn != null){
			conn.close();
		}
	}
 
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Updated profile</title>
</head>
<form name="home" " action="welcome.jsp">
<input type="image" src="home.jpg" alt="Submit" align="left" width="70" height="60">
</form>
<form name="graph" " action="logout.jsp">
<input type="image" src="logout.jpg" alt="Submit" align="right" width="60" height="60">
</form>
<br><br><br>
<body bgcolor="#1ECCC9">
<p> Your Profile is Successfully updated</p>
</body>
</html>