package main_package;

import java.io.Serializable;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Date;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

@ViewScoped
@ManagedBean(name="vedPromotors")
public class ViewEditDeletePromotorsBean implements Serializable
{
	ArrayList<DsaListInfo> llist;
	DsaListInfo selectedVar;
	String id,address,name,code,mobile;
	int branchId;
	DataBaseMethods obj=new DataBaseMethods();
	double interestRate, totalAmount;
	Date openDate=new Date();
	int totalRecord;
	
	public ViewEditDeletePromotorsBean()
	{
		checkData();
		dsaCode();
	}
	
	public void checkData()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		branchId = (int) ss.getAttribute("branchId");
		totalAmount=0;
		llist = new DataBaseMethods().viewPromotors(branchId);
		totalRecord=llist.size();
		if(llist.size()>0)
		{
			for(DsaListInfo dd: llist)
			{
				totalAmount+=dd.getAmount();
			}
		}
		
	}
	
	public void edit()
	{
		address=selectedVar.getAddress();
		name=selectedVar.getName();
		code=selectedVar.getCode();
		mobile=selectedVar.getMobile();
		id=selectedVar.getId();
	}

	private void dsaCode() 
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		branchId = (int) ss.getAttribute("branchId");
		int getcode1 = obj.getPromotorCode(branchId);
		getcode1=getcode1+1;
		String getcode = String.valueOf(getcode1);
		code="";
		if(Integer.parseInt( getcode)<10)
		{
			code = code.concat("00"+getcode);
		}
		else if(Integer.parseInt( getcode)<100)
		{
			code = code.concat("0"+getcode);
		}
		else
		{
			code = code.concat(""+getcode);
		}
	}

	public void add()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		branchId = (int) ss.getAttribute("branchId");
		Connection conn=DataBaseConnection.JAVAConnection();
		int re = obj.addPromotors(code,name,address,mobile,openDate, interestRate,branchId,conn);
		if(re>0)
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Promotors Add Sucessfully"));
			address=name=code=mobile="";
			interestRate=0;
			dsaCode();
			checkData();
		}
		else
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Error Occured!!!"));
		}
	}
	public void update()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		branchId = (int) ss.getAttribute("branchId");
		int i = new DataBaseMethods().updatePromotors(id, name, mobile, address);
		 if(i>0)
		 {
			 FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Promotors Detail update Sucessfully"));
			 llist = new DataBaseMethods().viewPromotors(branchId);
		 }
		 else
		 {
			 FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Error Occuered!!!"));
		 }
	}
	
	public void delete()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		branchId = (int) ss.getAttribute("branchId");
		 String idp = selectedVar.getId();
		 int i = new DataBaseMethods().deletePromotors(idp);
		 if(i>0)
		 {
			 FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Delete Sucessfully"));
			 llist = new DataBaseMethods().viewPromotors(branchId);
		 }
		 else
		 {
			 FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Error Occuered!!!"));
		 }
	}
	
	public ArrayList<DsaListInfo> getLlist() {
		return llist;
	}
	public void setLlist(ArrayList<DsaListInfo> llist) {
		this.llist = llist;
	}
	
	public DsaListInfo getSelectedVar() {
		return selectedVar;
	}
	public void setSelectedVar(DsaListInfo selectedVar) {
		this.selectedVar = selectedVar;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}




	public Date getOpenDate() {
		return openDate;
	}

	public void setOpenDate(Date openDate) {
		this.openDate = openDate;
	}

	public double getInterestRate() {
		return interestRate;
	}

	public void setInterestRate(double interestRate) {
		this.interestRate = interestRate;
	}

	public int getTotalRecord() {
		return totalRecord;
	}

	public void setTotalRecord(int totalRecord) {
		this.totalRecord = totalRecord;
	}

	public double getTotalAmount() {
		return totalAmount;
	}

	public void setTotalAmount(double totalAmount) {
		this.totalAmount = totalAmount;
	}
}
