<%@page import="project.ConnectionProvider"%>
<%@page import="java.sql.*"%>
<%@ page import="java.util.Properties" %>
<%@ page import="javax.mail.*" %>
<%@ page import="javax.mail.internet.*" %>
<%@include file="adminHeader.jsp" %>

<%
    String vid = request.getParameter("req_id");
    String status = request.getParameter("status");

    try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/CLINIHR", "root", "Clinica123");
        Statement st = con.createStatement();
        
        if (status.equals("Approved") || status.equals("Rejected") || status.equals("Discussion")) {
            st.executeUpdate("update leave_app set LM_approved='" + status + "' where req_id='" + vid + "'");

            ResultSet rs = st.executeQuery("select * from leave_app where req_id='" + vid + "'");
            if (rs.next()) {
                String emp_code = rs.getString("emp_code");
                ResultSet rsEmp = st.executeQuery("select email from users where emp_code='" + emp_code + "'");
                if (rsEmp.next()) {
                    String to = rsEmp.getString("email");
                    final String HREmail = "anita.sarkar@clinicaresearch.com";
                    String subject = "Status update - Leave Application";
                    String messageText = "Dear Employee,\n\n";
                    String messageTextHR = ""; // Initialize HR message text
                    
                    if (status.equals("Approved")) {
                        messageText += "Your leave application has been approved by LM.\n";
                        messageTextHR = "Dear HR,\n\n"
                                        + "A leave request has been approved by the Line Manager.\n"
                                        + "Please login to CLINIHR to view details and resume further procedure.";
                    } else if (status.equals("Rejected")) {
                        messageText += "Your leave application has been rejected by LM.\n";
                    } else if (status.equals("Discussion")) {
                        messageText += "LM says: Let's Discuss about your leave application.\n";
                    }
                    messageText += "Please login to CLINIHR to view details.";

                    // Sender's email credentials
                    final String from = "dipti.yelve@clinicaresearch.com";
                    final String pass = "Clinica@068";

                    // Define host
                    String host = "mail.clinicaresearch.com";

                    // Create Properties object
                    Properties props = new Properties();
                    props.put("mail.smtp.host", host);
                    props.put("mail.transport.protocol", "smtp");
                    props.put("mail.smtp.auth", "true");
                    props.put("mail.smtp.starttls.enable", "true");
                    props.put("mail.user", from);
                    props.put("mail.password", pass);
                    props.put("mail.smtp.port", "587");
                    props.put("mail.smtp.ssl.trust", "*");

                    // Get Session object
                    Session mailSession = Session.getInstance(props,
                        new javax.mail.Authenticator() {
                            @Override
                            protected PasswordAuthentication getPasswordAuthentication() {
                                return new PasswordAuthentication(from, pass);
                            }
                        });

                    // Create MimeMessage object
                    MimeMessage message = new MimeMessage(mailSession);
                    MimeMessage messageHR = new MimeMessage(mailSession);

                    try {
                        // Set From and all required fields for employee email
                        message.setFrom(new InternetAddress(from));
                        message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
                        message.setSubject(subject);
                        message.setText(messageText);

                        // Set From and all required fields for HR email
                        messageHR.setFrom(new InternetAddress(from));
                        messageHR.addRecipient(Message.RecipientType.TO, new InternetAddress(HREmail));
                        messageHR.setSubject(subject);
                        messageHR.setText(messageTextHR);

                        // Send messages using Transport class
                        Transport.send(message);
                        Transport.send(messageHR);

                        // Display successful message
                        response.sendRedirect("allLeaveAppLM.jsp?msg=done");
                    } catch (MessagingException e) {
                        // Handle messaging exception
                        response.sendRedirect("allLeaveAppLM.jsp?msg=wrong");
                    }
                }
            }
        } 
    } catch (Exception e) {
        out.println(e);
        // Handle exception
        response.sendRedirect("allLeaveAppLM.jsp?msg=wrong");
    }
%>
