

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

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

    </head>
    <body>


        <%

            //String workedHrsWeek = request.getParameter("worked_hrs_week");
            //String totalWorkedHrs = request.getParameter("total_worked_hrs");
            //String regularHrs = request.getParameter("regular_hrs");
            //String overtime = request.getParameter("overtime");
            String emp_code = request.getParameter("emp_code");
            String workedHrsDay = "08:00";
            String regularHrs = "08:00";
            String date = request.getParameter("dates");
            String timeIn = request.getParameter("time_in");
            String lunchStart = request.getParameter("lunch_start");
            String lunchEnd = request.getParameter("lunch_end");
            String timeOut = request.getParameter("time_out");
            String workPerformed = request.getParameter("work_performed");
            String projectNumber = request.getParameter("project_number");

            try {
                // Parse time strings into LocalTime objects
                LocalTime timeInLocal = LocalTime.parse(timeIn);
                LocalTime timeOutLocal = LocalTime.parse(timeOut);

                // Calculate total worked hours
                LocalTime totalWorkedHours = timeInLocal.plusHours(timeOutLocal.getHour()).plusMinutes(timeOutLocal.getMinute());
                
                // Format total worked hours as "HH:mm"
                String totalWorkedHoursString = totalWorkedHours.format(DateTimeFormatter.ofPattern("HH:mm"));
                
                
                // Calculate overtime hours if worked hours exceed regular hours
                Duration overtimeDuration = totalWorkedHoursString.minus(regularHrs).abs();

                
                // Format overtime hours as "HH:mm"
                String overtimeHoursString = String.format("%02d:%02d", overtimeDuration.toHours(), overtimeDuration.toMinutesPart());


                Class.forName("com.mysql.jdbc.Driver");
                Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/CLINIHR", "root", "Clinica123");
                PreparedStatement pst = con.prepareStatement("insert into employee_timesheet(emp_code, worked_hrs_day, total_worked_hrs, regular_hrs, overtime_hrs, date, time_in, lunch_start, lunch_end, time_out, details, project_number) values(?,?,?,?,?,?,?,?,?,?,?,?)");
                pst.setString(1, emp_code); 
                pst.setString(2, workedHrsDay); // worked_hrs_week
                pst.setString(3, totalWorkedHoursString); // total_worked_hrs
                pst.setString(4, regularHrs); // regular_hrs
                pst.setString(5, overtimeHoursString ); // overtime
                pst.setString(6, date);
                pst.setString(7, timeIn);
                pst.setString(8, lunchStart);
                pst.setString(9, lunchEnd);
                pst.setString(10, timeOut);
                pst.setString(11, workPerformed);
                pst.setString(12, projectNumber);
                pst.executeUpdate();
                response.sendRedirect("employeeTimesheet.jsp?msg=valid");
            } catch (Exception e) {
                out.println(e);
                //response.sendRedirect("registerEmployee.jsp?msg=invalid"); 
            }
        %>



    </body>


</html>