/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Hashtable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author arvind raghavan
 */
public class ViewInvites extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            response.setContentType("text/html;charset=UTF-8");
            String id = (String) request.getSession().getAttribute("id");
            String agenda = request.getParameter("agenda");
            Connection con = DataBase.getConnection();
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery("select * from scheduledmeetings where empid='" + id + "' " +
                    "and status =0 and agenda='" + agenda + "'");
            Hashtable ht = new Hashtable();
            String[] datetimepairs = new String[4];
            int count = 0;
            while (rs.next()) {
                String topic = rs.getString("Agenda");
                int isActive = rs.getInt("is_active");
                ht.put("agenda", topic);
                ht.put("isactive", isActive);
                Timestamp startdatetime = rs.getTimestamp("startDateTime");
                Timestamp enddatetime = rs.getTimestamp("endDateTime");
                SimpleDateFormat sdf = new SimpleDateFormat("EEE, MMM d, yyyy [HH:mm]");
                String temp = sdf.format(startdatetime);
                String tmp = sdf.format(enddatetime);
                datetimepairs[count] = temp + "-" + tmp;
                ++count;

            }
            ht.put("datetimepairs", datetimepairs);
            request.getSession().setAttribute("entries", ht);

            Integer isactive = (Integer) ht.get("isactive");
            System.out.println("isActive" + isactive);
            int isActive = isactive.intValue();
            if (isActive == 1) {
                String[] equiplist = new String[200];
                int equipcount = 0;
                ResultSet equiprs = st.executeQuery("select equipmentName from equipmentlist");
                while (equiprs.next()) {
                    equiplist[equipcount] = equiprs.getString("equipmentName");
                    ++equipcount;
                }
                ht.put("equipments", equiplist);
            }
            response.sendRedirect("accept.jsp");

        } catch (SQLException ex) {
            Logger.getLogger(ViewInvites.class.getName()).log(Level.SEVERE, null, ex);
        }


    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
