package other_package;

import java.io.IOException;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
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;

import main_package.DataBaseConnection;
import main_package.DataBaseMethods;
import main_package.LoanInfo;
import main_package.UserInfo;

@ManagedBean(name="parLoan")
@ViewScoped
public class ParticularLoanBean implements Serializable
{
	private static final long serialVersionUID = 1L;
	ArrayList<LoanInfo> allLoanList;
	String shareId,name,contact;
	LoanInfo selectedLoan;
	boolean showPenaltyField;
	
	public ParticularLoanBean()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		shareId=(String) ss.getAttribute("shareId");
		allLoanList = new DataBaseMethodsOther().allLoanList(shareId);
		
		Connection conn=DataBaseConnection.JAVAConnection();
		UserInfo info=new DataBaseMethods().userInfoByShareHolderNo(shareId, conn);
		name=info.getFirst_applicantName();
		contact=info.getContactNo();
		
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	// This Method Show Complete Detail Of Loan. 
	public void viewLoanDetails() throws IOException
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		ss.setAttribute("selectedLoan",selectedLoan);
		ss.setAttribute("pageName","particularLoan.xhtml");
		if(selectedLoan.getLoanType().equals("Personal Loan"))
		{
			FacesContext.getCurrentInstance().getExternalContext().redirect("viewPersonalLoan.xhtml");
		}
		else if(selectedLoan.getLoanType().equals("Vehicle Loan"))
		{
			FacesContext.getCurrentInstance().getExternalContext().redirect ("viewVehicleLoan.xhtml");
		}
		else if(selectedLoan.getLoanType().equals("Property Loan"))
		{
			FacesContext.getCurrentInstance().getExternalContext().redirect("viewPropertyLoan.xhtml");
		}
		else if(selectedLoan.getLoanType().equals("Group Loan"))
		{
			FacesContext.getCurrentInstance().getExternalContext().redirect( "viewGroupLoan.xhtml");
		}
	}
	
	public void searchReport() throws IOException
	{
		if(!selectedLoan.getStatus().equalsIgnoreCase("Approve"))
		{
			FacesContext.getCurrentInstance().addMessage(null,new FacesMessage("Please Approve Loan First"));
		}
		else
		{
			HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
			ss.setAttribute("selectedLoan",selectedLoan);
			ss.setAttribute("showPen",showPenaltyField);
			ss.setAttribute("pageName","particularLoan.xhtml");
			
			FacesContext.getCurrentInstance().getExternalContext().redirect("loanReportParticular.xhtml");
		}
	}
	

	// Method for Loan EMI Payment. This Method Firstly check Loan is Approved than after it return on Pay Loan EMI Page.
	public void payment() throws IOException
	{
		if(!selectedLoan.getStatus().equalsIgnoreCase("Approve"))
		{
			FacesContext.getCurrentInstance().addMessage(null,new FacesMessage("Please Approve Loan First"));
		}
		else
		{
			HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
			ss.setAttribute("selectedLoan",selectedLoan);
			ss.setAttribute("pageName","particularLoan");
			FacesContext.getCurrentInstance().getExternalContext().redirect( "payLoanEMI.xhtml");
		}
	}
	
	public LoanInfo getSelectedLoan() {
		return selectedLoan;
	}
	public void setSelectedLoan(LoanInfo selectedLoan) {
		this.selectedLoan = selectedLoan;
	}
	public ArrayList<LoanInfo> getAllLoanList() {
		return allLoanList;
	}
	public void setAllLoanList(ArrayList<LoanInfo> allLoanList) {
		this.allLoanList = allLoanList;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getContact() {
		return contact;
	}
	public void setContact(String contact) {
		this.contact = contact;
	}


	public String getShareId() {
		return shareId;
	}


	public void setShareId(String shareId) {
		this.shareId = shareId;
	}


	public boolean isShowPenaltyField() {
		return showPenaltyField;
	}


	public void setShowPenaltyField(boolean showPenaltyField) {
		this.showPenaltyField = showPenaltyField;
	}
	
	
}
