package com.reallinkbilling.add;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.LinkedHashMap;
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.opensymphony.xwork2.ActionSupport;
import com.reallinkbilling.db.GetDBConnection;

public class AddEmployeeSalaryDetails extends ActionSupport implements SessionAware{

	/**
	 * 
	 */
	private static final long serialVersionUID = 5543099235530390172L;

	private SessionMap<String,Object> sessionMap;
	private String customerSearchOption;
	private String defaultValue;
	private Map<String,String> customerSearchOptionList = new HashMap<String,String>();
	private HashMap<String,String> typeOfBalanceAmountList = new LinkedHashMap<String,String>();
	
	private HttpSession session;
	private String typeOfSalaryPayment;
	private Float salaryAmount;
	private String employeeId;
	private String employeeName;
	private String typeOfOpeningBal;
	private Float balanceAmount;

	private HashMap<String,String> typeOfSalaryPaymentList = new LinkedHashMap<String,String>();
	
	public String execute() throws IOException{
		 
		String result = "login";
		session=ServletActionContext.getRequest().getSession(false); 
		
	        if(session==null || session.getAttribute("login")==null){ 
	        	addActionMessage("Please Login First !!!");
	        	result = "login";
	        }  
	        else{
	        	
	        	Connection connection = null;
				PreparedStatement preparedStatement= null;
			
				
				
				String shopId = (String) this.getSessionMap().get("shopId");
	    		String shopCode = (String) this.getSessionMap().get("shopCode");
	    	
	        	
				
				String sql = "";
				String db = (String) session.getAttribute("db");
				
				
				

				try
				{
					
					int rowAdded = 0;
					connection = GetDBConnection.getDBConnection(db);
					sql = "INSERT INTO "
							+ "shop_employee_salary_details "
							+ "(employee_id,"
							+ "shop_id,"
							+ "shop_code,"
							+ "employee_name,"
							+ "employee_salary_type,"
							+ "employee_salary_amount,"
							+ "opening_bal_type,"
							+ "opening_balance_amount) "
							+ "VALUES "
							+ "(?,?,?,?,?,?,?,?)";
					
					preparedStatement = connection.prepareStatement(sql);
					preparedStatement.setString(1, this.getEmployeeId());
					preparedStatement.setString(2, shopId);
					preparedStatement.setString(3, shopCode);
					preparedStatement.setString(4, this.getEmployeeName().trim().toUpperCase());
					preparedStatement.setString(5, this.getTypeOfSalaryPayment());
					preparedStatement.setFloat(6, this.getSalaryAmount());
					preparedStatement.setString(7, this.getTypeOfOpeningBal());
					preparedStatement.setFloat(8, this.getBalanceAmount());
					
					rowAdded = preparedStatement.executeUpdate();
							
					if(rowAdded >  0)
					{
						addActionMessage("Employee salary details added Successfully!");
						result = SUCCESS;				
					}
					else
					{
						addActionError("Employee salary details not added! Please try again!");
						result = ERROR;	
					}
					
				}
				catch (SQLException e) {
					//e.printStackTrace();
					result = ERROR;				
					addActionError(e.getErrorCode()+" || "+e.getMessage() +" || Employee salary details not added !");
				
				} catch (IOException e) {
					//e.printStackTrace();
					result = ERROR;			
					addActionError(e.getMessage()+" || "+e.getCause()+" || Employee salary details not added !");
				
				}finally {
					
					if(preparedStatement != null)
					{
						try {preparedStatement.close(); preparedStatement = null;} catch (SQLException e) {}
					}
					if(connection != null)
					{
						
						try {connection.close(); connection = null;}catch (SQLException e) {}
					}
					
				}
        		
					
	        }
	        
	        
	      return  result;
	}
	
	@Override
	public void validate() {
		if(this.getSalaryAmount() == null || this.getSalaryAmount().equals(""))
		{
			addActionError("Please provide correct salary amount!");
		}
		
		if(this.getTypeOfSalaryPayment() == null || this.getTypeOfSalaryPayment().equals("")){
			addActionError("Please select type of salary payment!");
		}
		
		if(!hasActionErrors())
		{
			if(this.getTypeOfOpeningBal() == null || this.getTypeOfOpeningBal().equals(""))
			{
				addActionError("Please select type of opening balance type!");
			}
		}
		if(!hasActionErrors())
		{
			if(this.getBalanceAmount() == null || this.getBalanceAmount().equals(""))
			{
				addActionError("Please provide correct balance amount!");
			}
		}
		
		this.getTypeOfSalaryPaymentList().put("MONTHLY", "MONTHLY");
    	this.getTypeOfSalaryPaymentList().put("PERHOURBASIS", "PER HOUR BASIS");
    	this.getTypeOfSalaryPaymentList().put("DAILY", "DAILY");
    	this.getTypeOfSalaryPaymentList().put("WORKBASIS", "WORK BASIS");
    	this.getTypeOfSalaryPaymentList().put("WEEKLY", "WEEKLY");
    	
    	
    	this.getTypeOfBalanceAmountList().put("ADVANCE", "ADVANCE");
    	this.getTypeOfBalanceAmountList().put("PENDING", "PENDING");
	}
	
	public String getCustomerSearchOption() {
		return customerSearchOption;
	}


	public void setCustomerSearchOption(String customerSearchOption) {
		this.customerSearchOption = customerSearchOption;
	}


	public String getDefaultValue() {
		return defaultValue;
	}


	public void setDefaultValue(String defaultValue) {
		this.defaultValue = defaultValue;
	}


	public Map<String, String> getCustomerSearchOptionList() {
		return customerSearchOptionList;
	}


	public void setCustomerSearchOptionList(Map<String, String> customerSearchOptionList) {
		this.customerSearchOptionList = customerSearchOptionList;
	}


	public HttpSession getSession() {
		return session;
	}


	public void setSession(HttpSession session) {
		this.session = session;
	}


	public static long getSerialversionuid() {
		return serialVersionUID;
	}


	public String getTypeOfSalaryPayment() {
		return typeOfSalaryPayment;
	}


	public void setTypeOfSalaryPayment(String typeOfSalaryPayment) {
		this.typeOfSalaryPayment = typeOfSalaryPayment;
	}


	public Float getSalaryAmount() {
		return salaryAmount;
	}


	public void setSalaryAmount(Float salaryAmount) {
		this.salaryAmount = salaryAmount;
	}



	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 HashMap<String, String> getTypeOfSalaryPaymentList() {
		return typeOfSalaryPaymentList;
	}

	public void setTypeOfSalaryPaymentList(HashMap<String, String> typeOfSalaryPaymentList) {
		this.typeOfSalaryPaymentList = typeOfSalaryPaymentList;
	}

	public HashMap<String, String> getTypeOfBalanceAmountList() {
		return typeOfBalanceAmountList;
	}

	public void setTypeOfBalanceAmountList(HashMap<String, String> typeOfBalanceAmountList) {
		this.typeOfBalanceAmountList = typeOfBalanceAmountList;
	}

	public String getTypeOfOpeningBal() {
		return typeOfOpeningBal;
	}

	public void setTypeOfOpeningBal(String typeOfOpeningBal) {
		this.typeOfOpeningBal = typeOfOpeningBal;
	}


	public Float getBalanceAmount() {
		return balanceAmount;
	}

	public void setBalanceAmount(Float balanceAmount) {
		this.balanceAmount = balanceAmount;
	}

	public void setSession(Map<String, Object> map) {  
	    sessionMap=(SessionMap<String, Object>)map;  
	}


	public SessionMap<String, Object> getSessionMap() {
		return sessionMap;
	}

	public void setSessionMap(SessionMap<String, Object> sessionMap) {
		this.sessionMap = sessionMap;
	}
}
