package com.reallinkbilling.ajax;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.reallinkbilling.db.GetDBConnection;

public class SalesmanListInfo extends ActionSupport implements SessionAware{

	private static final long serialVersionUID = 112233445566778899L;
	private String term;
	private Map<String, String> salesmanList = new HashMap<String, String>();
	private SessionMap<String,Object> sessionMap;  

	public String execute() {
		String result = "login";
		if(this.getSessionMap()==null || this.getSessionMap().get("login")==null){ 
	    	addActionMessage("Please Login First !!!");
	    	return "login";  
	    }  
	    
		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);
			sql = "SELECT salesman_id, salesman_name, mobile_number, commission_percentage FROM shop_salesman_master WHERE shop_id=? AND shop_code=? AND active='Y' AND (salesman_name like '%"+term.trim()+"%' OR mobile_number like '%"+term.trim()+"%')";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, shopId);
			preparedStatement.setString(2, shopCode);
			resultSet = preparedStatement.executeQuery();
			resultSet.last();
			int rows=resultSet.getRow();
			resultSet.beforeFirst();
			if(rows>0){
				while(resultSet.next()){
					String key = resultSet.getInt(1)+"-"+resultSet.getString(2)+"-"+resultSet.getString(3)+"-"+resultSet.getBigDecimal(4);
					this.getSalesmanList().put(key, key);
				}
			}else{
				this.getSalesmanList().put("Salesman_Not_Found", "Salesman Not Found");
			}
			result = SUCCESS;
		}catch (SQLException | IOException e) {
			result = SUCCESS; // return empty list rather than erroring UI
		}finally{
			if(resultSet != null){ try {resultSet.close();} catch (SQLException e) {} }
			if(preparedStatement != null){ try {preparedStatement.close();} catch (SQLException e) {} }
			if(connection != null){ try {connection.close();} catch (SQLException e) {} }
		}
		return result;     
	}

	public String getTerm() { return term; }
	public void setTerm(String term) { this.term = term; }
	public Map<String, String> getSalesmanList() { return salesmanList; }
	public void setSalesmanList(Map<String, String> salesmanList) { this.salesmanList = salesmanList; }
	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;  }
}


