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 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 UpdateSalesmanDetailsJsp extends ActionSupport implements SessionAware{

	private static final long serialVersionUID = 1122112211221122L;
	private SessionMap<String,Object> sessionMap;
	private HttpSession session;

	private Integer salesmanId;
	private String salesmanName;
	private String mobileNumber;
	private String address;
	private Float commissionPercentage;
	private boolean active;

	public String execute(){
		String result = "login";
		session=ServletActionContext.getRequest().getSession(false);
		if(session==null || session.getAttribute("login")==null){
			addActionMessage("Please Login First !!!");
			result = "login";
		}else{
			// Load salesman details for prefill
			String db = (String) this.getSessionMap().get("db");
			String shopId = (String) this.getSessionMap().get("shopId");
			String shopCode = (String) this.getSessionMap().get("shopCode");
			Connection connection = null;
			PreparedStatement ps = null;
			ResultSet rs = null;
			try{
				if(this.getSalesmanId()==null){
					try{ this.setSalesmanId(Integer.parseInt(ServletActionContext.getRequest().getParameter("salesmanId"))); }catch(Exception ignore){}
				}
				if(this.getSalesmanId()!=null){
					connection = GetDBConnection.getDBConnection(db);
					ps = connection.prepareStatement("SELECT salesman_name,mobile_number,address,commission_percentage,active FROM shop_salesman_master WHERE shop_id=? AND shop_code=? AND salesman_id=?");
					ps.setString(1, shopId);
					ps.setString(2, shopCode);
					ps.setInt(3, this.getSalesmanId());
					rs = ps.executeQuery();
					if(rs.next()){
						this.setSalesmanName(rs.getString(1));
						this.setMobileNumber(rs.getString(2));
						this.setAddress(rs.getString(3));
						try{ this.setCommissionPercentage(rs.getFloat(4)); }catch(Exception ig){}
						this.setActive("Y".equalsIgnoreCase(rs.getString(5)));
					}
				}
				result = SUCCESS;
			}catch(SQLException e){
				addActionError(e.getErrorCode()+" || "+e.getMessage());
				result = SUCCESS; // still show page
			}catch(IOException e){
				addActionError(e.getMessage());
				result = SUCCESS; // still show page
			}finally{
				if(rs!=null){ try{ rs.close(); }catch(SQLException ig){} }
				if(ps!=null){ try{ ps.close(); }catch(SQLException ig){} }
				if(connection!=null){ try{ connection.close(); }catch(SQLException ig){} }
			}
		}
		return result;
	}

	public void setSession(java.util.Map<String, Object> map) {  sessionMap=(SessionMap<String, Object>)map; }
	public SessionMap<String, Object> getSessionMap() { return sessionMap; }

	public Integer getSalesmanId() { return salesmanId; }
	public void setSalesmanId(Integer salesmanId) { this.salesmanId = salesmanId; }
	public String getSalesmanName() { return salesmanName; }
	public void setSalesmanName(String salesmanName) { this.salesmanName = salesmanName; }
	public String getMobileNumber() { return mobileNumber; }
	public void setMobileNumber(String mobileNumber) { this.mobileNumber = mobileNumber; }
	public String getAddress() { return address; }
	public void setAddress(String address) { this.address = address; }
	public Float getCommissionPercentage() { return commissionPercentage; }
	public void setCommissionPercentage(Float commissionPercentage) { this.commissionPercentage = commissionPercentage; }
	public boolean isActive() { return active; }
	public void setActive(boolean active) { this.active = active; }
}


