package com.reallinkbilling.add;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
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 UpdateSalesmanDetails extends ActionSupport implements SessionAware {

	private static final long serialVersionUID = 556677889900112233L;
	private SessionMap<String,Object> sessionMap;

	private Integer salesmanId;
	private String salesmanName;
	private String mobileNumber;
	private String address;
	private Float commissionPercentage;
	private boolean active;

	public String execute() throws IOException{
		String result = "login";
		if(this.getSessionMap()==null || this.getSessionMap().get("login")==null){
			addActionMessage("Please Login First !!!");
			return "login";
		}

		String shopId = (String) this.getSessionMap().get("shopId");
		String shopCode = (String) this.getSessionMap().get("shopCode");
		String db = (String) this.getSessionMap().get("db");

		Connection connection = null;
		PreparedStatement ps = null;
		try{
			connection = GetDBConnection.getDBConnection(db);
			String sql = "UPDATE shop_salesman_master SET salesman_name=?, mobile_number=?, address=?, commission_percentage=?, active=? WHERE salesman_id=? AND shop_id=? AND shop_code=?";
			ps = connection.prepareStatement(sql);
			ps.setString(1, this.getSalesmanName().trim().toUpperCase());
			ps.setString(2, this.getMobileNumber().trim());
			ps.setString(3, this.getAddress()!=null? this.getAddress().trim().toUpperCase():"NA");
			ps.setFloat(4, this.getCommissionPercentage()!=null? this.getCommissionPercentage():0.0F);
			ps.setString(5, this.isActive()? "Y" : "N");
			ps.setInt(6, this.getSalesmanId());
			ps.setString(7, shopId);
			ps.setString(8, shopCode);
			int upd = ps.executeUpdate();
			if(upd>0){
				addActionMessage("Salesman details updated successfully!");
				result = SUCCESS;
			}else{
				addActionError("Salesman details not updated. Please check inputs.");
				result = ERROR;
			}
		}catch(SQLException e){
			addActionError(e.getErrorCode()+" || "+e.getMessage()+" || Salesman not updated!");
			result = ERROR;
		}finally{
			if(ps!=null){ try{ ps.close(); }catch(SQLException ignore){} }
			if(connection!=null){ try{ connection.close(); }catch(SQLException ignore){} }
		}
		return result;
	}

	@Override
	public void validate() {
		if(this.getSalesmanId()==null || this.getSalesmanId()<=0){
			addActionError("Invalid salesman.");
		}
		if(!hasActionErrors()){
			if(this.getSalesmanName()==null || this.getSalesmanName().trim().isEmpty()){
				addActionError("Please provide salesman name!");
			}
		}
		if(!hasActionErrors()){
			if(this.getMobileNumber()==null || this.getMobileNumber().trim().length()!=10){
				addActionError("Please provide valid 10-digit mobile number!");
			}
		}
		if(!hasActionErrors()){
			if(this.getCommissionPercentage()==null || this.getCommissionPercentage()<0){
				addActionError("Please provide valid commission percentage!");
			}
		}
	}

	public void setSession(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; }
}


