


<%@page import="java.text.SimpleDateFormat"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="project.ConnectionProvider"%>
<%@page import="project.ValidationFunctions"%>
<%@page import="java.sql.*"%>
<!DOCTYPE html>

<html>
    <head>
        <meta name="viewport" content="width=device-width, initial-scale=1">

    </head>
    <body>


        <%
            //1
            String first_name = request.getParameter("first_name");
            //2
            String middle_name = "";
            //3
            String last_name = request.getParameter("last_name");
            //4
            //String DOB="";
            
            //String input = "";
            //SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss");
            //java.util.Date dt = sdf.parse(input);
            
            //5
            String gender = "";
            //6
            String address = "";
            //7
            String city = "";
            //8
            String state = "";
            //9
            String country = "";
            //10
            String email = request.getParameter("email");
            //11
            String personal_email = "";
            //12
            String mobile_number = "";
            //13
            String bank_name = "";
            //14
            String bank_account_number = "";
            //15
            String bank_account_type = "";
            //16
            String IFSC_code = "";
            //17
            String PAN_card = "";
            //18
            String emp_code = request.getParameter("emp_code");
            //19
            String joining_date = request.getParameter("joining_date");
            //20
            String emp_type = request.getParameter("emp_type");
            //21
            String designation = request.getParameter("designation");
            //22
            String department = request.getParameter("department");
            //23
            String line_manager = request.getParameter("line_manager");
            String LMemail = request.getParameter("LMemail");
            //24
            String qualification = "";
            //25
            String experience = "";
            //26
            String password = request.getParameter("password");
            //27
            String security_question = "";
            //28
            String answer = "";
            //29
            String role = request.getParameter("role");
            //30
            String status = request.getParameter("status");
            try {
                Class.forName("com.mysql.jdbc.Driver");
                Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/CLINIHR", "root", "Clinica123");
                PreparedStatement ps = con.prepareStatement("insert into users(first_name, middle_name, last_name, gender, address, city, state, country, email, personal_email, phone_number, bank_name, bank_account_number, bank_account_type, IFSC_code, PAN_card, emp_code, joining_date, emp_type, designation, department, line_manager, LMemail, qualification, experience, password, security_question, answer, role, status) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
                ps.setString(1, first_name);
                ps.setString(2, middle_name);
                ps.setString(3, last_name);
                //ps.setString(4, DOB);
                ps.setString(4, gender);
                ps.setString(5, address);
                ps.setString(6, city);
                ps.setString(7, state);
                ps.setString(8, country);
                ps.setString(9, email);
                ps.setString(10, personal_email);
                ps.setString(11, mobile_number);
                ps.setString(12, bank_name);
                ps.setString(13, bank_account_number);
                ps.setString(14, bank_account_type);
                ps.setString(15, IFSC_code);
                ps.setString(16, PAN_card);
                ps.setString(17, emp_code);
                ps.setString(18, joining_date);
                ps.setString(19, emp_type);
                ps.setString(20, designation);
                ps.setString(21, department);
                ps.setString(22, line_manager);
                ps.setString(23, LMemail);
                ps.setString(24, qualification);
                ps.setString(25, experience);
                ps.setString(26, password);
                ps.setString(27, security_question);
                ps.setString(28, answer);
                ps.setString(29, role);
                ps.setString(20, status);
                // Perform validation
    boolean isValid = true;
    String errorMsg = "";

    // Example validation: Check if any field is empty
    if (first_name.isEmpty() || last_name.isEmpty() || emp_code.isEmpty() || joining_date.isEmpty() || emp_type.isEmpty() || designation.isEmpty() || department.isEmpty() || line_manager.isEmpty() || status.isEmpty() || role.isEmpty() || email.isEmpty() || password.isEmpty()) {
        isValid = false;
        errorMsg = "All fields must be filled out";
    } else {
        // Email validation
        if (!ValidationFunctions.isValidEmail(email)) {
            isValid = false;
            errorMsg = "Invalid email address";
            response.sendRedirect("registerEmployee.jsp?msg=invalidEmail&error=" + java.net.URLEncoder.encode(errorMsg, "UTF-8"));
        }
/*
        // Mobile number validation
        if (!ValidationFunctions.isValidMobileNumber(mobile_number)) {
            isValid = false;
            errorMsg = "Invalid mobile number";
        response.sendRedirect("registerEmployee.jsp?msg=invalidNumber&error=" + java.net.URLEncoder.encode(errorMsg, "UTF-8"));
        }*/

        //Password validation
        if (!ValidationFunctions.isValidPassword(password)) {
            isValid = false;
            errorMsg = "Invalid password";
            response.sendRedirect("registerEmployee.jsp?msg=invalidPassword&error=" + java.net.URLEncoder.encode(errorMsg, "UTF-8"));
        }
    }

    if (isValid) {
        // Proceed with registration
        // This is where you would insert the data into the database
        // Redirect or display success message
        ps.executeUpdate();
        response.sendRedirect("registerEmployee.jsp?msg=valid");
    } else {
        // Display error message
        response.sendRedirect("registerEmployee.jsp?msg=invalid&error=" + java.net.URLEncoder.encode(errorMsg, "UTF-8"));
    }
                
            } catch (Exception e) {
                out.println(e);
                //response.sendRedirect("registerEmployee.jsp?msg=invalid"); 
            }
        %>



    </body>


</html>