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

<%
    String emp_code = request.getParameter("emp_code");
    String LMemail = request.getParameter("LMemail");
    String nature_of_leave = request.getParameter("nature_of_leave");
    String days_applied = request.getParameter("days_applied");
    String date_from = request.getParameter("date_from");
    String date_to = request.getParameter("date_to");
    String half_day = request.getParameter("half_day");
    String reason = request.getParameter("reason");
    String contact_number = request.getParameter("contact_number");
    String contact_address = request.getParameter("contact_address");
    String allocated_person = request.getParameter("allocated_person");
    String LM_approved = "Approval Pending";
    String HR_approved = "Approval Pending";

    try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/CLINIHR", "root", "Clinica123");
        PreparedStatement ps = con.prepareStatement("insert into leave_app (emp_code, LMemail, nature_of_leave, days_applied, date_from, date_to, half_day, reason, contact_number, contact_address, allocated_person, LM_approved, HR_approved) values(?,?,?,?,?,?,?,?,?,?,?,?,?)");
        ps.setString(1, emp_code);
        ps.setString(2, LMemail);
        ps.setString(3, nature_of_leave);
        ps.setString(4, days_applied);
        ps.setString(5, date_from);
        ps.setString(6, date_to);
        ps.setString(7, half_day);
        ps.setString(8, reason);
        ps.setString(9, contact_number);
        ps.setString(10, contact_address);
        ps.setString(11, allocated_person);
        ps.setString(12, LM_approved);
        ps.setString(13, HR_approved);
        ps.executeUpdate();
        // Step 1: Get fields from index.jsp
        final String to = request.getParameter("LMemail");
        out.println(to);
        final String subject = "Leave request// leave Application!";
        final String messageText = "Dear Line Manager,\n"
                + "You have a new Leave request \n"
                + "Please login to CLINIHR to view details.";

        // Step 2: Sender's email ID and password
        final String from = "dipti.yelve@clinicaresearch.com";
        final String pass = "Clinica@068";

        // Step 3: Define host
        String host = "mail.clinicaresearch.com";

        // Step 4: Create Properties class 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", "*");

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

        // Step 6: Create MimeMessage object
        MimeMessage message = new MimeMessage(mailSession);

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

            // Step 9: Send message using Transport class
            Transport.send(message);

            // Step 10: Display successful message
            response.sendRedirect("leaveApp.jsp?msg=done");
        } catch (MessagingException e) {
            //throw new RuntimeException(e);
            response.sendRedirect("leaveApp.jsp?msg=wrong");
        }
        response.sendRedirect("leaveApp.jsp?msg=wrong.jsp");
    } catch (Exception e) {
        //out.println(e);
        response.sendRedirect("leaveApp.jsp?msg=wrong");
    }
%>