package main_package;

import java.io.Serializable;
import java.util.ArrayList;

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;

@ManagedBean(name="manageNachReg")
@ViewScoped
public class ManageNachRegBean implements Serializable
{
	ArrayList<NachInfo> list;
	int total;
	boolean show;
	String searchType, umrnNo="", remark="";
	DataBaseMethods dbm=new DataBaseMethods();
	NachInfo selectedUser;
	
	public void searchData()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId =(int) ss.getAttribute("branchId");
		list=dbm.viewNachRegData(searchType,branchId);
		total=list.size();
		show=true;
	}

	public void approveNach()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId =(int) ss.getAttribute("branchId");
		boolean check=dbm.checkUmrnNoDuplicacy(umrnNo,branchId);
		if(check==true)
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Duplicacy Found!!! This UMRN No. Already allot to Existing Member. Please Check???"));
		}
		else
		{
			int i=dbm.updateNachMandateForm(umrnNo, remark, "active", selectedUser.getId());
			if(i>0)
			{
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("NACH Mandate Form of "+selectedUser.getName()+" Approve Successfully"));
				list=dbm.viewNachRegData(searchType,branchId);
				total=list.size();
				umrnNo="";
			}
			else
			{
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Error in Approval of NACH Mandate form of "+selectedUser.getName()));
			}
		}
	}
	
	public void rejectNach()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId =(int) ss.getAttribute("branchId");
		int i=dbm.updateNachMandateForm(umrnNo, remark, "inactive", selectedUser.getId());
		if(i>0)
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("NACH Mandate Form of "+selectedUser.getName()+" Reject Successfully"));
			list=dbm.viewNachRegData(searchType,branchId);
			total=list.size();
			remark="";
		}
		else
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Error in Reject of NACH Mandate form of "+selectedUser.getName()));
		}
	}
	
	
	public ArrayList<NachInfo> getList() {
		return list;
	}

	public void setList(ArrayList<NachInfo> list) {
		this.list = list;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public boolean isShow() {
		return show;
	}

	public void setShow(boolean show) {
		this.show = show;
	}

	public String getSearchType() {
		return searchType;
	}

	public void setSearchType(String searchType) {
		this.searchType = searchType;
	}

	public NachInfo getSelectedUser() {
		return selectedUser;
	}

	public void setSelectedUser(NachInfo selectedUser) {
		this.selectedUser = selectedUser;
	}

	public String getUmrnNo() {
		return umrnNo;
	}

	public void setUmrnNo(String umrnNo) {
		this.umrnNo = umrnNo;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}
	
}
