package main_package;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;



@ManagedBean(name="secureLoanOtherDetail")
@ViewScoped
public class SecureLoanOtherDetailBean implements Serializable
{
	String loanNo, loanType="secure_loan";
	UserInfo user_info;
	LoanInfo loan_info;
	List<String> nameList;
	DataBaseMethods obj=new DataBaseMethods();
	boolean show;
	ArrayList<LoanInfo> otherList;
	int x1;
	
	public List<String> completeLoanNo(String name)
	{
		nameList=new DataBaseAutoCompAndDuplicate().showLoanNoByLoanTypePaidUnpaid(name,"secure_loan", obj.userId());
		return nameList;
	}

	public void searchDetail()
	{
		Connection conn=DataBaseConnection.JAVAConnection();
		show=true;
		if(loanNo.contains("/"))
		{
			loanNo=loanNo.substring(0,loanNo.indexOf("/"));
		}
		
		user_info=obj.userInfoByLoanNoLoanType(loanNo,loanType,conn);
		loan_info=obj.loanInfoByLoanNoLoanType(loanNo,loanType, conn);
		
		otherList=obj.showOhterListValues(loanNo);	
		if(otherList.size()==0)
		{
			x1=1;
			otherList=new ArrayList<>();
			if(otherList.size()<1)
			{
				for(int i=1;i<=1;i++)
				{
					LoanInfo info=new LoanInfo();
					info.setSno(i);
					otherList.add(info);
				}
			}
		}
		
		
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	public void addMore()
	{
		x1=otherList.size()+1;
		for(int i=1;i<=1;i++)
		{
			LoanInfo info=new LoanInfo();
			info.setSno(x1);
			otherList.add(info);
		}
	}
	
	public String addOtherDetail()
	{
		if(otherList.size()>0)
		{
			obj.deleteOtherDetailByLoanNo(loanNo);
			int i=obj.insertLoanOtherDetail(otherList, loanNo);	
			if(i>0)
			{
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Data has been Update Sucessfully !!!"));
			}
			else
			{
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Error Occured!!!"));
			}
		}
		return "secureLoanOtherDetail.xhtml";
	}
	
	public String getLoanNo() {
		return loanNo;
	}

	public void setLoanNo(String loanNo) {
		this.loanNo = loanNo;
	}

	public List<String> getNameList() {
		return nameList;
	}

	public void setNameList(List<String> nameList) {
		this.nameList = nameList;
	}

	public String getLoanType() {
		return loanType;
	}

	public void setLoanType(String loanType) {
		this.loanType = loanType;
	}

	public UserInfo getUser_info() {
		return user_info;
	}

	public void setUser_info(UserInfo user_info) {
		this.user_info = user_info;
	}

	public LoanInfo getLoan_info() {
		return loan_info;
	}

	public void setLoan_info(LoanInfo loan_info) {
		this.loan_info = loan_info;
	}

	public boolean isShow() {
		return show;
	}

	public void setShow(boolean show) {
		this.show = show;
	}

	public ArrayList<LoanInfo> getOtherList() {
		return otherList;
	}

	public void setOtherList(ArrayList<LoanInfo> otherList) {
		this.otherList = otherList;
	}
}
