package com.reallinkbilling.add;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
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 AddNewSalesman extends ActionSupport implements SessionAware {

	private static final long serialVersionUID = 334132993497893432L;
	private SessionMap<String,Object> sessionMap;

	private String salesmanName;
	private String mobileNumber;
	private String address;
	private Float commissionPercentage;

	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 today = new SimpleDateFormat("dd-MM-yyyy").format(new Date());
			String sql = "INSERT INTO shop_salesman_master (shop_id,shop_code,add_date,salesman_name,mobile_number,address,commission_percentage,active) VALUES (?,?,STR_TO_DATE(?,'%d-%m-%Y'),?,?,?,?,?)";
			ps = connection.prepareStatement(sql);
			ps.setString(1, shopId);
			ps.setString(2, shopCode);
			ps.setString(3, today);
			ps.setString(4, this.getSalesmanName().trim().toUpperCase());
			ps.setString(5, this.getMobileNumber().trim());
			ps.setString(6, this.getAddress()!=null? this.getAddress().trim().toUpperCase():"NA");
			ps.setFloat(7, this.getCommissionPercentage()!=null? this.getCommissionPercentage():0.0F);
			ps.setString(8, "Y");
			int added = ps.executeUpdate();
			if(added>0){
				addActionMessage("Salesman added successfully!");
				result = SUCCESS;
			}else{
				addActionError("Salesman not added. Please try again.");
				result = ERROR;
			}
		}catch(SQLException e){
			addActionError(e.getErrorCode()+" || "+e.getMessage()+" || Salesman not added!");
			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.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 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; }
}


