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.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.SessionAware;

import com.reallinkbilling.cipher.EncryptionDecryption;
import com.reallinkbilling.db.GetDBConnection;
import com.reallinkbilling.utils.GetAccountListForDrCr;
import com.opensymphony.xwork2.ActionSupport;

public class AddEmployeePaymentJsp extends ActionSupport implements SessionAware{
	/**
	 * 
	 */
	private static final long serialVersionUID = 5721774390127479301L;
	private SessionMap<String,Object> sessionMap;  
	private String key;
	private String employeePaymentDate;
	private String employeeId;
	private String employeeName;
	private String employeeMobile;
	private Float employeeOutstandingAmount;
	
	private List<String> paymentTypeList = new ArrayList<String>();
	private Map<String,String> accountList = new LinkedHashMap<String,String>();
	private String addSideBarJs;
	private HttpSession session;
	public String execute() {
		 
		String result = "login";
		session=ServletActionContext.getRequest().getSession(false); 
	        if(this.getSessionMap()==null || this.getSessionMap().get("login")==null){ 
	        	addActionMessage("Please Logout and Login again !");
	        	result = "error";
	        }  
	        else{
	        	
	        	
	        	Connection connection = null;
				PreparedStatement preparedStatement= null;
				ResultSet  resultSet = null;
				
				String sql = "";
				String db = (String) this.getSessionMap().get("db");
				
				try
				{

		        	String shopId = (String) this.getSessionMap().get("shopId");
		    		String shopCode = (String) this.getSessionMap().get("shopCode");
		        
					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));
						
						
						Date d = new Date();
			        	SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
			        	this.setEmployeePaymentDate(dateFormat.format(d));
			        	paymentTypeList.add("CASH");
			        	paymentTypeList.add("UPI");
			        	paymentTypeList.add("RTGS");
			        	paymentTypeList.add("NEFT");
			        	paymentTypeList.add("CHEQUE");
			    		
			        	this.setAccountList(GetAccountListForDrCr.getAccountList(this.getSessionMap()));      
						
			        	result = SUCCESS;
						this.setAddSideBarJs("FALSE");
					}
					else
					{
						addActionError("Details not found! URL Tampered! Please try again! ");
						result = ERROR;
					}
				
				}catch (SQLException e) {
					result = ERROR;
					addActionError("Details not found! URL Tampered! Please try again! ");
				} catch (IOException e) {
					result = ERROR;
					addActionError("Details not found! URL Tampered! Please try again! ");
				}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 static long getSerialversionuid() {
		return serialVersionUID;
	}
	
	

	public List<String> getPaymentTypeList() {
		return paymentTypeList;
	}

	public void setPaymentTypeList(List<String> paymentTypeList) {
		this.paymentTypeList = paymentTypeList;
	}

	

	public HttpSession getSession() {
		return session;
	}

	public void setSession(HttpSession session) {
		this.session = session;
	}

	

	public String getEmployeePaymentDate() {
		return employeePaymentDate;
	}

	public void setEmployeePaymentDate(String employeePaymentDate) {
		this.employeePaymentDate = employeePaymentDate;
	}

	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 Float getEmployeeOutstandingAmount() {
		return employeeOutstandingAmount;
	}

	public void setEmployeeOutstandingAmount(Float employeeOutstandingAmount) {
		this.employeeOutstandingAmount = employeeOutstandingAmount;
	}

	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;  
	}

	public Map<String, String> getAccountList() {
		return accountList;
	}

	public void setAccountList(Map<String, String> accountList) {
		this.accountList = accountList;
	}
}
