package main_package;

import java.io.IOException;
import java.io.Serializable;
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;

@ManagedBean(name="maturedRDBean")
@ViewScoped
public class MaturedRDBean implements Serializable
{
	DataBaseMethods obj=new DataBaseMethods();
	private static final long serialVersionUID = 1L;
	UserInfo selectedUser;
	ArrayList<UserInfo> list;
	String user_no;
	Date date=new Date();
	boolean showTable=false;
	
	public void maturedRD()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId =(int) ss.getAttribute("branchId");
		list=obj.showUserByMaturityDate(branchId,date, "all");
		if(list.size()>0)
		{
			showTable=true;
		}
		else
		{
			showTable=false;
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("No Record Found"));
		}
	}
	
	
	public String checkInstallmentStatus() throws IOException
	{
		
		user_no=selectedUser.getUser_no();
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		ss.setAttribute("selected", selectedUser);
		int n=obj.showInstallmentStatus(user_no);
		if(n>=1)
		{
			boolean check=obj.existingValueInReturnedAmountTable(user_no);
			if(check==true)
			{
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("A Cheque has Already Given To This User Which Is Still Uncleared.Please Check 'Due Cheque Debit'!!"));
			}
			else
			{
				FacesContext.getCurrentInstance().getExternalContext().redirect("returnMaturedAmount.xhtml");
				//return "returnMaturedAmount";
			}
		}	
		else
		{
			FacesContext fc=FacesContext.getCurrentInstance();
			fc.addMessage(null, new FacesMessage("User "+user_no+" Has Pending Installments. Unable To Receive Maturity Amount."));
		}
		
		return "";
	}
	
	public void closeSchemeMethod() throws IOException
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		ss.setAttribute("userNo", selectedUser.getUser_no());
		FacesContext.getCurrentInstance().getExternalContext().redirect("closeSchemePage.xhtml");
	}
	
	public Date getDate() {
		return date;
	}
	public void setDate(Date date) {
		this.date = date;
	}
	public String getUser_no() {
		return user_no;
	}
	public void setUser_no(String user_no) {
		this.user_no = user_no;
	}
	public UserInfo getSelectedUser() {
		return selectedUser;
	}
	public void setSelectedUser(UserInfo selectedUser) {
		this.selectedUser = selectedUser;
	}
	public ArrayList<UserInfo> getList() {
		return list;
	}
	public void setList(ArrayList<UserInfo> list) {
		this.list = list;
	}


	public boolean isShowTable() {
		return showTable;
	}


	public void setShowTable(boolean showTable) {
		this.showTable = showTable;
	}
}
