package com.reallinkbilling.jspRedirect;


import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.reallinkbilling.cipher.EncryptionDecryption;
import com.reallinkbilling.db.GetDBConnection;

public class ViewEmployeeAccountDetailsJsp extends ActionSupport implements SessionAware{

	/**
	 * 
	 */
	private static final long serialVersionUID = -1137449839377712427L;
	
	private String key;
	private String employeeId;
	private String employeeName;
	private String employeeMobile;
	private String fromDate;
	private String toDate;
	private String addSideBarJs;
	private SessionMap<String,Object> sessionMap;
	public String execute(){
		
		String result = "login";
	        if(this.getSessionMap()==null || this.getSessionMap().get("login")==null){ 
	        	addActionMessage("Please Login First !!!");
	        	result = "login";
	        }  
	        else{
	        	
	        	Date d = new Date();
	        	SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
	        	this.setToDate(dateFormat.format(d));
	        	this.setFromDate("01-04-2022");
	        	
	     		
	        	
					String shopId = (String) this.getSessionMap().get("shopId");
		    		String shopCode = (String) this.getSessionMap().get("shopCode");
		    
	        
	        	this.setAddSideBarJs("FALSE");
	        	Connection connection = null;
	       		PreparedStatement preparedStatement= null;
	       		ResultSet resultSet = null;
	       		
	       		String sql = "";
	       		String db = (String) this.getSessionMap().get("db");
	       		try {
					
					connection = GetDBConnection.getDBConnection(db);
					
					String employeeId = EncryptionDecryption.decrypt(this.getKey());
					
					sql = "SELECT "
							+ "employee_name,"
							+ "employee_mobile_number "
							+ "FROM "
							+ "shop_employee_master "
							+ "WHERE "
							+ "employee_id = ? "
							+ "AND "
							+ "shop_id = ? "
							+ "AND "
							+ "shop_code = ?";
					
					preparedStatement = connection.prepareStatement(sql);
					preparedStatement.setString(1, employeeId);
					preparedStatement.setString(2, shopId);
					preparedStatement.setString(3, shopCode);
					
					
					resultSet = preparedStatement.executeQuery();
					
					if(resultSet.next())
					{
						this.setEmployeeName(resultSet.getString(1));
						this.setEmployeeMobile(resultSet.getString(2));
						
						result = SUCCESS;
					}
					else
					{
						addActionError("Details not found! URL Tampered! Please try again! ");
						result = ERROR;
					}
				
				}catch (SQLException e) {
	       			result = "error";
	    			addActionError(e.getErrorCode()+" || "+e.getMessage() +" || DETAILS NOT FOUND !");
	    		} catch (IOException e) {
	    			
	    			result = "error";
	    			addActionError(e.getMessage()+" || "+e.getCause()+" || DETAILS NOT FOUND !");
	    		}finally {
	    			if(resultSet != null)
    				{
    					try {
    						resultSet.close(); resultSet = null;
    					} catch (SQLException e) {
    						
    					}
    				}
	    			if(preparedStatement != null)
	    			{
	    				try {preparedStatement.close(); preparedStatement = null;} catch (SQLException e) {}
	    			}
	    			if(connection != null)
	    			{
	    				
	    				try {connection.close(); connection = null;}catch (SQLException e) {}
	    			}
	    			
	    		}
	        	
	        	
	        	
	        }
	        return result;
}

	

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	
	public String getEmployeeId() {
		return employeeId;
	}

	public void setEmployeeId(String employeeId) {
		this.employeeId = employeeId;
	}

	public String getEmployeeName() {
		return employeeName;
	}

	public void setEmployeeName(String employeeName) {
		this.employeeName = employeeName;
	}

	public String getEmployeeMobile() {
		return employeeMobile;
	}

	public void setEmployeeMobile(String employeeMobile) {
		this.employeeMobile = employeeMobile;
	}

	public String getFromDate() {
		return fromDate;
	}

	public void setFromDate(String fromDate) {
		this.fromDate = fromDate;
	}

	public String getToDate() {
		return toDate;
	}

	public void setToDate(String toDate) {
		this.toDate = toDate;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public String getAddSideBarJs() {
		return addSideBarJs;
	}

	public void setAddSideBarJs(String addSideBarJs) {
		this.addSideBarJs = addSideBarJs;
	}

	public SessionMap<String, Object> getSessionMap() {
		return sessionMap;
	}

	public void setSessionMap(SessionMap<String, Object> sessionMap) {
		this.sessionMap = sessionMap;
	}

	public void setSession(Map<String, Object> map) {  
	    sessionMap=(SessionMap<String, Object>)map;  
	}
	
	

}

