package main_package;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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 org.primefaces.context.RequestContext;

@ManagedBean(name="checkLoanRequest")
@ViewScoped
public class CheckLoanRequestBean implements Serializable
{
	private static final long serialVersionUID = 1L;
	ArrayList<LoanInfo> personalLoanList,vechileLoanList,propertyLoanList,grouplLoanList, secureLoanList, businessLoanList, finalList;
	ArrayList<LoanInfo> selectedFinalList=new ArrayList<>();
	LoanInfo selectedLoan; 
	Date approveDate=new Date(), emiDueDate=null, secEmiDueDate=null;
	boolean showDueDateField, showCenterName;
	DsaListInfo commissionInfo;
	double commission=0;
	String userId;
	DataBaseMethods obj=new DataBaseMethods();
	String newMember;
	List<String> autoCompList= new ArrayList<>();
	DataBaseMethodTaheer dbmt = new DataBaseMethodTaheer();
	
	
	
	public CheckLoanRequestBean() 
	{
		userId=obj.userId();
	  createList();
	  
	}
    
	public List<String> completeText(String name)
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss.getAttribute("branchId");

		autoCompList=new DataBaseAutoCompAndDuplicate().shareHolderAllNo(branchId,name);
     	return autoCompList;
	}

	public void createList()
	{
	   HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
	   int branchId = (int) ss.getAttribute("branchId");
	   finalList=new ArrayList<>();
	   personalLoanList=obj.pendingPersonalLoanList(branchId);
	   
	   vechileLoanList=obj.pendingVehicleLoanList(branchId);
	   propertyLoanList=obj.pendingPropertyLoanList(branchId);
	   grouplLoanList=obj.pendingGroupLoanList(branchId);
	   secureLoanList=obj.pendingSecureLoanList(branchId);
	   businessLoanList=obj.pendingBusinessLoanList(branchId);
	   
	   finalList.addAll(vechileLoanList);
	   finalList.addAll(personalLoanList);
	   finalList.addAll(propertyLoanList);
	   finalList.addAll(grouplLoanList);
	   finalList.addAll(secureLoanList);
	   finalList.addAll(businessLoanList);
	   
	   if (grouplLoanList.size() > 0) 
	   {
		   showCenterName = true;
	   }
	   else 
	   {
		   showCenterName = false;
	   }
	   	   
	   for(int i=0; i<finalList.size();i++)
	   {
		   finalList.get(i).setSno(i+1);
	   }
	}
	
	public void checkDueField()
	{
		showDueDateField=true;
		/*if(selectedLoan.getFrequency().equals("12"))
		{
			showDueDateField=true;
		}
		else
		{
			showDueDateField=false;
		}*/   //Need for only monthly
	}
	public String printSanctionLoan()
    {
		selectedLoan.setEmiPaidDate(approveDate);
        if(emiDueDate!=null)
		{
			selectedLoan.setEmiPaidDate(emiDueDate);
        }
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        ss.setAttribute("selectedLoan",selectedLoan);
        RequestContext.getCurrentInstance().execute("window.open('printFormatOfSanctionNote.xhtml')");
        
        RequestContext.getCurrentInstance().execute("window.open('printSanctionLetter.xhtml')");
        
        RequestContext.getCurrentInstance().execute("window.open('printRepaymentSchedule.xhtml')");
        
        return "";
    }
	
	
	
	public String approveLoan()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss.getAttribute("branchId");
		int i=0;
		Connection conn=DataBaseConnection.JAVAConnection();
		if(selectedFinalList.size() > 0)
		{
			for(LoanInfo tt : selectedFinalList)
			{
				commission=0;
				if(tt.getLoanType().equals("Personal Loan"))
				{
					i=obj.updateStatusForPersonalLoan(tt.getId(),"Approve",approveDate,tt.getLoanAmount(), emiDueDate, conn);
				}
				else if(tt.getLoanType().equals("Vehicle Loan"))
				{
					i=obj.updateStatusForVechileLoan(tt.getId(),"Approve",approveDate,tt.getLoanAmount(), emiDueDate, conn);
				}
				else if(tt.getLoanType().equals("Property Loan"))
				{
					i=obj.updateStatusForPropertyLoan(tt.getId(),"Approve",approveDate,tt.getLoanAmount(), emiDueDate, conn);
				}
				else if(tt.getLoanType().equals("Group Loan"))
				{
					i=obj.updateStatusForGroupLoan(tt.getId(),"Approve",approveDate,tt.getLoanAmount(), emiDueDate, tt.getLoanNo(), tt.getSubLoanNo(), conn);
				}
				else if(tt.getLoanType().equals("Secure Loan"))
				{
					i=obj.updateStatusForSecureLoan(tt.getId(),"Approve",approveDate,tt.getLoanAmount(), emiDueDate, conn);
				}
				else if(tt.getLoanType().equals("Business Loan"))
				{
					i=obj.updateStatusForBusinessLoan(tt.getId(),"Approve",approveDate,tt.getLoanAmount(), emiDueDate, conn);
				}
				
				String loanNo="";
				if(tt.getLoanType().equals("Group Loan") || tt.getLoanType().equals("grouploan"))
				{
					loanNo=tt.getSubLoanNo();
				}
				else
				{
					loanNo=tt.getLoanNo();
				}
				
				if(i>=1)
				{
					FacesContext fc=FacesContext.getCurrentInstance();
					fc.addMessage(null, new FacesMessage("Loan Approved Successfully"));
					
					if(tt.getFrequencyVal()==24)
					{
						obj.insertFifteenDayPlanGroupLoanEmi(branchId, loanNo, "Fifteen Days Plan", emiDueDate,tt.getTerms(),secEmiDueDate);
					}
					
					
					// File Charge Amount Add on Loan Approval
					conn=DataBaseConnection.JAVAConnection();
					String fcStatus=obj.checkSettingFromMasterSettingNewTable("File Charge", conn);
					if(fcStatus.equalsIgnoreCase("loan_approve"))
					{
						int z=obj.updateIncomeTableSelectedPendingEntryStatus("File Charge","active" , loanNo,conn,approveDate);
						if(z>0)
						{
							if(Double.valueOf(tt.getFileCharge())>0)
							{
								obj.increaseCompanyCapitalAmount(branchId,Double.valueOf(tt.getFileCharge()), conn);
								fc.addMessage(null, new FacesMessage("File Charge "+tt.getFileCharge() +" Rs. Add Sucessfully."));
							}
						}
					}	
					
					// Insurance Fee Amount Add on Loan Approval
					String insFeeStatus=obj.checkSettingFromMasterSettingNewTable("Insurance Fee", conn);
					if(insFeeStatus.equalsIgnoreCase("loan_approve"))
					{
						int z=obj.updateIncomeTableSelectedPendingEntryStatus("Insurance Fee","active" , loanNo,conn,approveDate);
						if(z>0)
						{
							if(Double.valueOf(tt.getInsuranceFee())>0)
							{
								obj.increaseCompanyCapitalAmount(branchId,Double.valueOf(tt.getInsuranceFee()), conn);
								fc.addMessage(null, new FacesMessage("Insurance Fee "+tt.getInsuranceFee() +" Rs. Add Sucessfully."));
							}
						}
					}	
					
					String stampStatus=obj.checkSettingFromMasterSettingNewTable("Stamp Fee", conn);
					if(stampStatus.equalsIgnoreCase("loan_approve"))
					{
						int z=obj.updateIncomeTableSelectedPendingEntryStatus("Stamp Fee","active" , loanNo,conn,approveDate);
						if(z>0)
						{
							if(Double.valueOf(tt.getStamp()) > 0)
							{
								obj.increaseCompanyCapitalAmount(branchId,Double.valueOf(tt.getStamp()), conn);
								fc.addMessage(null, new FacesMessage("Insurance Fee "+tt.getStamp() +" Rs. Add Sucessfully."));
							}
						}
					}
					
					
					sendMessageForMultiple(tt.getApplicantName(),tt.getLoanNo(),tt.getLoanType(),tt.getLoanAmount(),tt.getContactNo(),conn);
			//		printSanctionLoan();
					double amount=Double.parseDouble(tt.getLoanAmount());
					if(tt.getDsacode()==null || tt.getDsacode().equals(""))
					{
						commission=0;
					}
					else
					{
						calculateCommissionForMultiple(tt.getDsacode(),tt.getPlanNo(), tt, conn);
					}
					if(commission>0)
					{
						String emiNo="all";
						 boolean checkEntry=obj.checkDuplicacyInDsaCommissionTable(tt.getLoanNo(),emiNo);
						  if(checkEntry==false)
						  {
								String dsaName=obj.dsaNameById(tt.getDsacode(),conn);
								int j=obj.insertDataInDsaCommissionTable(branchId,tt.getLoanType(), loanNo, tt.getDsacode(), commission, approveDate, emiNo);
								if(j>0)
								{
									fc.addMessage(null, new FacesMessage("Commission Auto Credited to "+tt.getDsacode()+" - "+dsaName+" Rs.:- "+commission));
								}
						  }
					}
					//obj.increaseSharedCompanyAmount(amount);
					
					//createList();
				}
				else if(i<0)
				{
					 FacesContext fc=FacesContext.getCurrentInstance();
					 fc.addMessage(null, new FacesMessage("Sorry! Loan Request Can't Be Treated. Company's Capital Amount is '"+(-i)+"'"));
				}
				else
				{
					FacesContext fc=FacesContext.getCurrentInstance();
					fc.addMessage(null, new FacesMessage("An Error Occured... Please Try Again"));
				}
			}
			createList();
		}
		else if(selectedFinalList.size() ==0)
		{
			FacesContext fc=FacesContext.getCurrentInstance();
			fc.addMessage(null, new FacesMessage("Please Select Atleat One Loan No."));
		}
		
		return "checkLoanRequest";
	}

	public String approveForSingle()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss.getAttribute("branchId");
		int flag=1;
		if(selectedLoan.getFrequencyVal()==24)
		{
			if((emiDueDate.getDate()>=1 && emiDueDate.getDate()<=13) || (emiDueDate.getDate()>=17 && emiDueDate.getDate()<=28))
			{
				flag=1;
				if(selectedLoan.getLoanType().equalsIgnoreCase("grouploan") || selectedLoan.getLoanType().equals("Group Loan"))
				{
					obj.insertFifteenDayPlanGroupLoanEmi(branchId, selectedLoan.getSubLoanNo(), "Fifteen Days Plan", emiDueDate, selectedLoan.getTerms(),secEmiDueDate);
				}
				else
				{
					obj.insertFifteenDayPlanGroupLoanEmi(branchId, selectedLoan.getLoanNo(), "Fifteen Days Plan", emiDueDate, selectedLoan.getTerms(),secEmiDueDate);
				}
			}
			else
			{
				FacesContext fc=FacesContext.getCurrentInstance();
				fc.addMessage(null, new FacesMessage("Fifteen Days EMI Date must be between Either 1 to 13 or 17 to 28. Please select date Properly, as per given instruction. Thank you. "));
				flag=0;
			}
		}
		
		if(flag==1)
		{
			int i=0;
			Connection conn=DataBaseConnection.JAVAConnection();
			
			if(selectedLoan.getLoanType().equals("Personal Loan"))
			{
				i=obj.updateStatusForPersonalLoan(selectedLoan.getId(),"Approve",approveDate,selectedLoan.getLoanAmount(), emiDueDate, conn);
			}
			else if(selectedLoan.getLoanType().equals("Vehicle Loan"))
			{
				i=obj.updateStatusForVechileLoan(selectedLoan.getId(),"Approve",approveDate,selectedLoan.getLoanAmount(), emiDueDate, conn);
			}
			else if(selectedLoan.getLoanType().equals("Property Loan"))
			{
				i=obj.updateStatusForPropertyLoan(selectedLoan.getId(),"Approve",approveDate,selectedLoan.getLoanAmount(), emiDueDate, conn);
			}
			else if(selectedLoan.getLoanType().equals("Group Loan"))
			{
				i=obj.updateStatusForGroupLoan(selectedLoan.getId(),"Approve",approveDate,selectedLoan.getLoanAmount(), emiDueDate, selectedLoan.getLoanNo(), 
						selectedLoan.getSubLoanNo(), conn);
			}
			else if(selectedLoan.getLoanType().equals("Secure Loan"))
			{
				i=obj.updateStatusForSecureLoan(selectedLoan.getId(),"Approve",approveDate,selectedLoan.getLoanAmount(), emiDueDate, conn);
			}
			else if(selectedLoan.getLoanType().equals("Business Loan"))
			{
				i=obj.updateStatusForBusinessLoan(selectedLoan.getId(),"Approve",approveDate,selectedLoan.getLoanAmount(), emiDueDate, conn);
			}
			
			String loanNo="";
			if(selectedLoan.getLoanType().equals("Group Loan") || selectedLoan.getLoanType().equals("grouploan"))
			{
				loanNo=selectedLoan.getSubLoanNo();
			}
			else
			{
				loanNo=selectedLoan.getLoanNo();
			}
			
			if(i>=1)
			{
				FacesContext fc=FacesContext.getCurrentInstance();
				fc.addMessage(null, new FacesMessage("Loan Approved Successfully"));
				
				// File Charge Amount Add on Loan Approval
				String fcStatus=obj.checkSettingFromMasterSettingNewTable("File Charge", conn);
				if(fcStatus.equalsIgnoreCase("loan_approve"))
				{
					int z=obj.updateIncomeTableSelectedPendingEntryStatus("File Charge","active" , loanNo,conn,approveDate);
					if(z>0)
					{
						if(Double.valueOf(selectedLoan.getFileCharge())>0)
						{
							obj.increaseCompanyCapitalAmount(branchId,Double.valueOf(selectedLoan.getFileCharge()), conn);
							fc.addMessage(null, new FacesMessage("File Charge "+selectedLoan.getFileCharge() +" Rs. Add Sucessfully."));
						}
					}
				}	
				
				// Insurance Fee Amount Add on Loan Approval
				String insFeeStatus=obj.checkSettingFromMasterSettingNewTable("Insurance Fee", conn);
				if(insFeeStatus.equalsIgnoreCase("loan_approve"))
				{
					int z=obj.updateIncomeTableSelectedPendingEntryStatus("Insurance Fee","active" , loanNo,conn,approveDate);
					if(z>0)
					{
						if(Double.valueOf(selectedLoan.getInsuranceFee())>0)
						{
							obj.increaseCompanyCapitalAmount(branchId,Double.valueOf(selectedLoan.getInsuranceFee()), conn);
							fc.addMessage(null, new FacesMessage("Insurance Fee "+selectedLoan.getInsuranceFee() +" Rs. Add Sucessfully."));
						}
					}
				}	
				
				
				sendMessage(conn);
				printSanctionLoan();
				double amount=Double.parseDouble(selectedLoan.getLoanAmount());
				if(selectedLoan.getDsacode()==null || selectedLoan.getDsacode().equals(""))
				{
					commission=0;
				}
				else
				{
					calculateCommission(conn);
				}
				if(commission>0)
				{
					String emiNo="all";
					 boolean checkEntry=obj.checkDuplicacyInDsaCommissionTable(selectedLoan.getLoanNo(),emiNo);
					  if(checkEntry==false)
					  {
							String dsaName=obj.dsaNameById(selectedLoan.getDsacode(),conn);
							int j=obj.insertDataInDsaCommissionTable(branchId,selectedLoan.getLoanType(), selectedLoan.getLoanNo(), selectedLoan.getDsacode(), commission, approveDate, emiNo);
							if(j>0)
							{
								fc.addMessage(null, new FacesMessage("Commission Auto Credited to "+selectedLoan.getDsacode()+" - "+dsaName+" Rs.:- "+commission));
							}
					  }
					  try {
						conn.close();
					} catch (Exception e) {
						// TODO: handle exception
					}
				}
				//obj.increaseSharedCompanyAmount(amount);
				
				createList();
			}
			else if(i<0)
			{
				 FacesContext fc=FacesContext.getCurrentInstance();
				 fc.addMessage(null, new FacesMessage("Sorry! Loan Request Can't Be Treated. Company's Capital Amount is '"+(-i)+"'"));
			}
			else
			{
				FacesContext fc=FacesContext.getCurrentInstance();
				fc.addMessage(null, new FacesMessage("An Error Occured... Please Try Again"));
			}
		}
		return "";
	}
	
	
	private void sendMessage(Connection conn)  
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss.getAttribute("branchId");
		String check=obj.checkStatusInMessageSettingTable("Loan Approve");
		if(check.equals("active"))
		{	
			boolean isDLT = dbmt.isDLTMessageApi(conn);
			if (isDLT) 
			{
//				System.out.println("DLT MESSAGE API SELECTED");
				Map<String, String> variables = new HashMap<>();
				
			    variables.put("name", selectedLoan.getApplicantName());
			    variables.put("loantype", selectedLoan.getLoanType());
			    variables.put("loanamount", String.valueOf(selectedLoan.getLoanAmount()));
			    variables.put("loanno", selectedLoan.getLoanNo());
				
			    dbmt.sendDLTMessage(selectedLoan.getContactNo(), null, variables, conn, "loan approval");
			} 
			else 
			{
//				System.out.println("OLD MESSAGE API SELECTED");
				String typeMessage="Dear "+selectedLoan.getApplicantName()+", Your "+selectedLoan.getLoanType()+" No. "+selectedLoan.getLoanNo() +", Loan Amount "+selectedLoan.getLoanAmount()+",  is Approved Sucessfully.";
				obj.runMessageUrl(branchId, selectedLoan.getContactNo(), typeMessage, userId, "1207166054296301491",
						conn);
				typeMessage="";
			}
		}
		else
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Message For This Module is not Active. Please Go to In User Block and Active Message For This Module."));
		}
	}
	
	

	private void sendMessageForMultiple(String applicantName, String loanNo, String loanType, String loanAmount,
										String contactNo, Connection conn) 
	{
		HttpSession ss = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);

		int branchId = (int) ss.getAttribute("branchId");

		String check = obj.checkStatusInMessageSettingTable("Loan Approve");

		if ("active".equalsIgnoreCase(check)) 
		{
			boolean isDLT = dbmt.isDLTMessageApi(conn);
			if (isDLT) 
			{
				Map<String, String> variables = new HashMap<>();
				
			    variables.put("name", applicantName);
			    variables.put("loantype", loanType);	
			    variables.put("loanamount", loanAmount);
			    variables.put("loanno", loanNo);

				System.out.println("DLT MESSAGE API SELECTED");

				dbmt.sendDLTMessage(contactNo, null, variables, conn, "loan approval");

			} else {

				System.out.println("OLD MESSAGE API SELECTED");

				String typeMessage = "Dear " + applicantName + ", Your " + loanType + " No. " + loanNo
						+ ", Loan Amount " + loanAmount + ", is Approved Successfully.";

				obj.runMessageUrl(branchId, contactNo, typeMessage, userId, "1207166054296301491", conn);

				System.out.println("Message API call completed for Loan = " + loanNo);
			}

		} else {

			FacesContext.getCurrentInstance().addMessage(null,
					new FacesMessage("Message For This Module is not Active. "
							+ "Please Go to In User Block and Active Message " + "For This Module."));
		}
	}
	
	public void calculateCommission(Connection conn)
	{
		if(selectedLoan.getDsacode()==null || selectedLoan.getDsacode().equals(""))
		{
			commission=0;
		}
		else
		{
			commissionInfo=obj.checkPlanCommissionDetail(selectedLoan.getPlanNo(), conn);
			if(commissionInfo==null || commissionInfo.getStatusNullCheck().equals("0"))
			{
				// // System.out.println("null pointer: hello Loan Approve");
				commission=0;
			}
			else
			{
				// // System.out.println("method called:");
				double val=0;
				val=Double.parseDouble(selectedLoan.getLoanAmount());
				   if(commissionInfo.getCommissionBase().equalsIgnoreCase("approval"))
				   {
					   if(commissionInfo.getCommission_type().equalsIgnoreCase("percent"))
					   {
						   commission=(val/100)*commissionInfo.getCommission_value();
					   }
					   else
					   {
						   commission=commissionInfo.getCommission_value();
					   }
				   }
				   else
				   {
					   commission=0;
				   }
				   commission=Double.parseDouble(new DecimalFormat("##.##").format(commission));
			}
		}
	}
	
	
	public void calculateCommissionForMultiple(String dsaCode, String planNo, LoanInfo tt, Connection conn)
	{
		if(dsaCode==null || dsaCode.equals(""))
		{
			commission=0;
		}
		else
		{
			commissionInfo=obj.checkPlanCommissionDetail(planNo, conn);
			if(commissionInfo==null || commissionInfo.getStatusNullCheck().equals("0"))
			{
				commission=0;
			}
			else
			{
				double val=0;
				val=Double.parseDouble(tt.getLoanAmount());
			   if(commissionInfo.getCommissionBase().equalsIgnoreCase("approval"))
			   {
				   if(commissionInfo.getCommission_type().equalsIgnoreCase("percent"))
				   {
					   commission=(val/100)*commissionInfo.getCommission_value();
				   }
				   else
				   {
					   commission=commissionInfo.getCommission_value();
				   }
			   }
			   else
			   {
				   commission=0;
			   }
			   commission=Double.parseDouble(new DecimalFormat("##.##").format(commission));
			}
		}
	}
	public void denyLoan()
	{
		int i=0;
		Connection conn=DataBaseConnection.JAVAConnection();
		if(selectedLoan.getLoanType().equals("Personal Loan"))
		{
			i=obj.updateStatusForPersonalLoan(selectedLoan.getId(),"notApprove",approveDate,selectedLoan.getLoanAmount(), emiDueDate, conn);
		}
		else if(selectedLoan.getLoanType().equals("Vehicle Loan"))
		{
			i=obj.updateStatusForVechileLoan(selectedLoan.getId(),"notApprove",approveDate,selectedLoan.getLoanAmount(), emiDueDate, conn);
		}
		else if(selectedLoan.getLoanType().equals("Property Loan"))
		{
			i=obj.updateStatusForPropertyLoan(selectedLoan.getId(),"notApprove",approveDate,selectedLoan.getLoanAmount(), emiDueDate, conn);
		}
		else if(selectedLoan.getLoanType().equals("Group Loan"))
		{
			i=obj.updateStatusForGroupLoan(selectedLoan.getId(),"notApprove",approveDate,selectedLoan.getLoanAmount(), emiDueDate,selectedLoan.getLoanNo(),
					selectedLoan.getSubLoanNo(), conn);
		}
		else if(selectedLoan.getLoanType().equals("Secure Loan"))
		{
			i=obj.updateStatusForSecureLoan(selectedLoan.getId(),"notApprove",approveDate,selectedLoan.getLoanAmount(), emiDueDate, conn);
		}
		else if(selectedLoan.getLoanType().equals("Business Loan"))
		{
			i=obj.updateStatusForBusinessLoan(selectedLoan.getId(),"notApprove",approveDate,selectedLoan.getLoanAmount(), emiDueDate, conn);
		}
		
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		if(i>=1)
		{
			FacesContext fc=FacesContext.getCurrentInstance();
			fc.addMessage(null, new FacesMessage("Loan Denied SuccessFully"));
		    
			createList();
		}
		else
		{
			FacesContext fc=FacesContext.getCurrentInstance();
			fc.addMessage(null, new FacesMessage("An Error Occured... Please Try Again"));
		}
		
	}
	
	public String viewLoan()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		ss.setAttribute("selectedLoan",selectedLoan);
		if(selectedLoan.getLoanType().equals("Personal Loan"))
		{
			ss.setAttribute("pageName","editViewPersonalLoan.xhtml");
			return "viewPersonalLoan.xhtml";
		}
		else if(selectedLoan.getLoanType().equals("Vehicle Loan"))
		{
			ss.setAttribute("pageName","editViewVechileLoan.xhtml");
			return "viewVehicleLoan.xhtml";
		}
		else if(selectedLoan.getLoanType().equals("Property Loan"))
		{
			ss.setAttribute("pageName","editViewPropertyLoan.xhtml");
			return "viewPropertyLoan.xhtml";
		}
		else if(selectedLoan.getLoanType().equals("Group Loan"))
		{
			ss.setAttribute("pageName","editViewGroupLoan.xhtml");
			return "viewGroupLoan.xhtml";
		}
		else if(selectedLoan.getLoanType().equals("Secure Loan"))
		{
			ss.setAttribute("pageName","manageSecureLoan.xhtml");
			return "viewSecureLoan.xhtml";
		}
		
		else if(selectedLoan.getLoanType().equals("Business Loan"))
		{
			ss.setAttribute("pageName","editViewBusinessLoan.xhtml");
			return "viewBusinessLoan.xhtml";
		}
		
		return "";
	}
	
	public void editMember()
	{
		if(newMember.contains("/"))
		{
			newMember=newMember.substring(0,newMember.indexOf("/"));
		}
		FacesContext fc=FacesContext.getCurrentInstance();
		String loanNo=selectedLoan.getLoanNo();
		String ltp=obj.loanValueByLoanLabel(selectedLoan.getLoanType());
		if(ltp.equalsIgnoreCase("grouploan"))
		{
			//loanNo=selectedLoan.getSubLoanNo();
			fc.addMessage(null, new FacesMessage("This Option is Under Development for Group Loan..."));
		}
		else
		{
			
			int i=obj.editMemberInLoanTable(loanNo, ltp, newMember);
			
			if(i>0)
			{
				fc.addMessage(null, new FacesMessage("Member Edit Successfully"));
				createList();
				newMember="";
			}
			else
			{
				fc.addMessage(null, new FacesMessage("Error Occured"));
			}
		}
	}
	
	public LoanInfo getSelectedLoan() {
		return selectedLoan;
	}
	public void setSelectedLoan(LoanInfo selectedLoan) {
		this.selectedLoan = selectedLoan;
	}
	public ArrayList<LoanInfo> getFinalList() {
		return finalList;
	}
	public void setFinalList(ArrayList<LoanInfo> finalList) {
		this.finalList = finalList;
	}
	public Date getApproveDate() {
		return approveDate;
	}
	public void setApproveDate(Date approveDate) {
		this.approveDate = approveDate;
	}

	public Date getEmiDueDate() {
		return emiDueDate;
	}

	public void setEmiDueDate(Date emiDueDate) {
		this.emiDueDate = emiDueDate;
	}

	public boolean isShowDueDateField() {
		return showDueDateField;
	}

	public void setShowDueDateField(boolean showDueDateField) {
		this.showDueDateField = showDueDateField;
	}

	public ArrayList<LoanInfo> getSelectedFinalList() {
		return selectedFinalList;
	}

	public void setSelectedFinalList(ArrayList<LoanInfo> selectedFinalList) {
		this.selectedFinalList = selectedFinalList;
	}

	public Date getSecEmiDueDate() {
		return secEmiDueDate;
	}

	public void setSecEmiDueDate(Date secEmiDueDate) {
		this.secEmiDueDate = secEmiDueDate;
	}

	public boolean isShowCenterName() {
		return showCenterName;
	}

	public void setShowCenterName(boolean showCenterName) {
		this.showCenterName = showCenterName;
	}

	public String getNewMember() {
		return newMember;
	}

	public void setNewMember(String newMember) {
		this.newMember = newMember;
	}

	
	
	
	
}
