package com.reallinkbilling.report;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.reallinkbilling.DAO.EmployeePresentDetailsDAO;
import com.reallinkbilling.cipher.EncryptionDecryption;
import com.reallinkbilling.db.GetDBConnection;

public class GetEmployeePresentEntryReport  extends ActionSupport implements SessionAware{

	
	/**
	 * 
	 */
	private static final long serialVersionUID = 663470450572349982L;
	private SessionMap<String,Object> sessionMap;  
	
	private String fromDate;
	private String toDate;
	private String employeeName;
	
	List<EmployeePresentDetailsDAO> employeePresentDetailsDAOList = new ArrayList<EmployeePresentDetailsDAO>();
	
	public String execute() {
		 
		String result = "login";
	
		
	        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 shopId = (String) this.getSessionMap().get("shopId");
	    		String shopCode = (String) this.getSessionMap().get("shopCode");
	        
				String sql = "";
				String db = (String)  this.getSessionMap().get("db");
				
				try
				{
					connection = GetDBConnection.getDBConnection(db);
					
					sql = "SELECT "
					        + "entry_id,"
					        + "DATE_FORMAT(marking_date,'%d-%m-%Y'),"
					        + "employee_id,"
					        + "employee_name,"
					        + "type_of_marking,"
					        + "details,"
					        + "entry_by "
					        + "FROM "
					        + "shop_employee_presenty_details "
					        + "WHERE "
					        + "marking_date BETWEEN "
					        + "STR_TO_DATE(?,'%d-%m-%Y') AND STR_TO_DATE(?,'%d-%m-%Y') "
					        + "AND "
					        + "shop_id = ? "
					        + "AND "
					        + "shop_code = ? "
					        + "AND "
					        + "employee_name LIKE ? ";
					
					preparedStatement = connection.prepareStatement(sql);
					
					preparedStatement.setString(1, this.getFromDate());
					preparedStatement.setString(2, this.getToDate());
					preparedStatement.setString(3, shopId);
					preparedStatement.setString(4, shopCode);
					
					if (this.getEmployeeName() == null || this.getEmployeeName().trim().isEmpty()) {
					    preparedStatement.setString(5, "%");
					} else {
					    preparedStatement.setString(5, "%" + this.getEmployeeName().trim() + "%");
					}
					
					
					resultSet = preparedStatement.executeQuery();
					
					resultSet.last();
					
					int rowCount = resultSet.getRow();
					
					resultSet.beforeFirst();
					
					if(rowCount > 0)
					{
						while(resultSet.next())
						{
							EmployeePresentDetailsDAO detailsDAO = new EmployeePresentDetailsDAO();
							
							detailsDAO.setEntryId(resultSet.getString(1));
							detailsDAO.setMarkingDate(resultSet.getString(2));
							detailsDAO.setEmployeeId(resultSet.getString(3));
							detailsDAO.setEmployeeName(resultSet.getString(4));
							detailsDAO.setTypeOfMarking(resultSet.getString(5));
							detailsDAO.setDetails(resultSet.getString(6));
							detailsDAO.setEntryBy(resultSet.getString(7));
							detailsDAO.setKey(EncryptionDecryption.encrypt(resultSet.getString(1)+"#"+resultSet.getString(3)));
							this.getEmployeePresentDetailsDAOList().add(detailsDAO);
						}
						
						result = SUCCESS;
					}
					else
					{
						result = ERROR;
						addActionError("Details not found for provided dates!");
					}
				}
				catch (SQLException e) {
					result = "error";
					
					addActionError(e.getErrorCode()+" || "+e.getMessage() +" || Present entry details not added !");
				}
				catch (Exception e) {
						result = "error";
					
					addActionError(e.getMessage() +" || Present entry not added !");
				}
				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 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 List<EmployeePresentDetailsDAO> getEmployeePresentDetailsDAOList() {
		return employeePresentDetailsDAOList;
	}


	public void setEmployeePresentDetailsDAOList(List<EmployeePresentDetailsDAO> employeePresentDetailsDAOList) {
		this.employeePresentDetailsDAOList = employeePresentDetailsDAOList;
	}


	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 String getEmployeeName() {
		return employeeName;
	}


	public void setEmployeeName(String employeeName) {
		this.employeeName = employeeName;
	}
	

}