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

package Controller;

import LoginDb.DbHandler;
import java.io.IOException;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.http.HttpSession;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author 1004242
 */
public class ModifyServicesServlet extends HttpServlet {
   
    
    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

        HttpSession session = request.getSession(false);
        Connection connection = null;
        ResultSet rs = null;
        Statement statement = null;
        CallableStatement proc = null;
        int searchFound = 0;
        String message = "";
        String pacsId = (String) session.getAttribute("pacsId");
         String headPacsId = (String) session.getAttribute("headPacsId");
       String tellerId=(String)session.getAttribute("user");
        String JSP_Name = request.getParameter("screenName");
       // String acc_no = request.getParameter("accountNo");

        String sms = request.getParameter("smsService");
       // String mob_no = request.getParameter("mob_no");

        String mail = request.getParameter("mailservice");
       // String mail_id = request.getParameter("mail_id");

        String miss_call = request.getParameter("miss_call");

        System.out.println(acc_no+" "+sms+" "+mob_no+" "+mail+" "+mail_id+" "+miss_call);


        if( sms==null && mail == null && miss_call == null && mob_no == null ) {
        //search
            try{
            connection = DbHandler.getDBConnection();

        String sql = "select sms_flag,mobile_no,email_flag,email_id,misdcl_flag from ACCT_SERVICES p where p.key_1='"+acc_no+"' and p.pacs_id='"+pacsId+"'";
        int sms_flag = 0;
        int email_flag = 0;
        int msdcl_flag = 0;
        System.out.println(sms_flag);

        statement = connection.createStatement();
        //rs = statement.executeQuery(sql);

        while (rs.next()) {
            sms_flag = rs.getInt("sms_flag");
            email_flag = rs.getInt("email_flag");
            msdcl_flag = rs.getInt("misdcl_flag");
            mob_no = rs.getString("mobile_no");
            mail_id = rs.getString("email_id");
        }
            request.setAttribute("sms_flag", ""+sms_flag);
            request.setAttribute("email_flag", ""+email_flag);
            request.setAttribute("misdcl_flag", ""+msdcl_flag);
            request.setAttribute("mob_no", mob_no);
            request.setAttribute("mail_id", mail_id);
            request.setAttribute("acc_no", acc_no);
            request.setAttribute("displayFlag", "Y");
            request.setAttribute("deact_flag", "N");

        } catch(Exception ex){
            //ex.printStackTrace();
            System.out.println("Error occurred during processing.");
        } finally {
            try {
                connection.close();
            } catch (SQLException e) {
               // System.out.println(e.toString());
               System.out.println("Error occurred during connection close.");
            }
        }
    }
        else{
        //update
            try{
                connection = DbHandler.getDBConnection();
                proc = connection.prepareCall("{ call operations1.ACCT_SERVICES_UPDATE(?,?,?,?,?,?,?,?,?) }");

                 proc.setString(1, pacsId);
                 proc.setString(2, acc_no);
                 proc.setString(3, tellerId);

                 if(sms == null){
                     proc.setInt(4, 0);
                 }
                 else{
                     proc.setInt(4, 1);
                 }
                 proc.setString(5, mob_no);

                 if(mail == null){
                     proc.setInt(6, 0);
                 }
                 else{
                     proc.setInt(6, 1);
                 }
                 proc.setString(7, mail_id);

                 if(miss_call == null){
                     proc.setInt(8, 0);
                 }
                 else{
                     proc.setInt(8, 1);
                 }

                 proc.registerOutParameter(9, java.sql.Types.VARCHAR);

                 proc.executeUpdate();
                // message = proc.getString(9);
                 request.setAttribute("message", message);

            }catch(Exception e){
                message = "Error Occurred during processing.";
                // message = "Error Occurred: "+e.getMessage()+"Please contact Administrator";
                 request.setAttribute("message", message);
               // e.printStackTrace();               
            } finally {
                try {
                connection.close();
            } catch (SQLException e) {
                System.out.println("Error occured during connection close.");
            }
        }
            request.setAttribute("displayFlag", "N");
        }
         request.getRequestDispatcher("/Deposit/EditServices.jsp").forward(request, response);
    }

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

}
