package main_package;

import java.io.IOException;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
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.faces.model.SelectItem;
import javax.servlet.http.HttpSession;

import org.primefaces.context.RequestContext;
import org.primefaces.model.UploadedFile;

import report_package.BorrowerLenderInfo;

@ManagedBean(name="editViewGroupLoan")
@ViewScoped
public class EditViewGroupLoanBean implements Serializable
{
	DataBaseMethods obj=new DataBaseMethods();
	private static final long serialVersionUID = 1L;
	ArrayList<LoanInfo> grpList,multiSelectList;
	LoanInfo selectedLoan;
	ArrayList<SelectItem> centerList, dsaList;
	String center,newDsaCode, remark, chqFrom ,chqNo,bankName, chqName ,bankIfsc ,bankBranch,bankAccNo;
	Date emiDate, chargesDate=new Date();
	int totalLoan, chargesAmount;
	boolean printOne, printTwo, printThree, printFour, printFive, printSix, printSeven, printEight, printNine, printTenth, printEleventh;
	ArrayList<BorrowerLenderInfo> manualChargesList;
	BorrowerLenderInfo selectedRow;
	ArrayList<ChequeDetailsEntryInfo> chqList;
    private String companyName;
    boolean printInsuranseFormat;
    

	
	public EditViewGroupLoanBean()  
	{
		 HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		 int branchId = (int) ss.getAttribute("branchId");
		 
		ss.getAttribute("companyName");
		Connection conn=DataBaseConnection.JAVAConnection();
		centerList=obj.allcenter(branchId,"all",conn);
		dsaList=obj.allDsaList(branchId,"Loan",conn);
		try
		{
			conn.close();
		}
		catch (SQLException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void searchData()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		 int branchId = (int) ss.getAttribute("branchId");
		grpList = obj.groupLoanList(branchId,center);
		totalLoan=grpList.size();
	}
	
	public void firstEmiDueDate()
	{
		emiDate=selectedLoan.getEmiDate();
	}
	
	public void printJlgMethod()
	{
		if(center!=null && !center.equalsIgnoreCase("") && !center.equalsIgnoreCase("all"))
		{
			if(multiSelectList.size()>0)
			{
				HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		        ss.setAttribute("printGrpList",multiSelectList);
		        
		        RequestContext.getCurrentInstance().execute("window.open('printJlg.xhtml')");
		        RequestContext.getCurrentInstance().execute("window.open('printJlg2.xhtml')");
		        RequestContext.getCurrentInstance().execute("window.open('printJlg3.xhtml')");
			}
		}
		else
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Please select one Center For Print this Letter."));
		}   
	}
	
	public void checkDetails() {
		
	  System.out.println("checkDetails Initiate !!");
		DataBaseMethodRahul dbr = new DataBaseMethodRahul();
		
		chqList = new ArrayList<>();
		
	   dbr.checkDetailByshareHolderNo(chqList, selectedLoan.getShareHolderNo());
		
	}
	

	
	public void printCenterDisbursement()
	{
		if(center!=null && !center.equalsIgnoreCase("") && !center.equalsIgnoreCase("all"))
		{
			HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
	        ss.setAttribute("printGrpList",multiSelectList);
	        ss.setAttribute("printCenter",center);
	        
	        RequestContext.getCurrentInstance().execute("window.open('printCenterDisbursementLetter.xhtml')");
		}
		else
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Please select one Center For Print this Letter."));
		}
	}

	public void manualChargesData()
	{
		manualChargesList=obj.manualChargesDataList(selectedLoan.getSubLoanNo(), "grouploan");
	}
	
	public void deleteEntry()
	{
		int i=obj.deleteManualChargeEntry(selectedRow.getId());
		if(i>0)
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Manual Charges Deleted Sucessfully..."));
			manualChargesData();
		}
		else
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Error Occurred!!! Please Try Again...."));
		}
	}
	
	
	public void addCharges()
	{
		if(chargesAmount>0)
		{
			int i=obj.addManualCharges(selectedLoan.getSubLoanNo(), "grouploan", chargesDate, chargesAmount, remark);
			if(i>0)
			{
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Manual Charges added Sucessfully..."));
			}
			else
			{
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Error Occurred!!! Please Try Again...."));
			}
		}
		else
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Dear User, Charges Must be Greater Than ZERO. Please Try Again...."));
		}
	}
	
	public void sub()
	{
		if(multiSelectList.size()>0)
		{
			ArrayList<UserInfo> list1=new ArrayList<>();
			Connection conn=DataBaseConnection.JAVAConnection();
			int i=0;
			 NumberToWord nm = new NumberToWord();
			for(LoanInfo li: multiSelectList)
			{
				//list1.get(i).setLoan(li.getLoanAmount());
				UserInfo ff=new UserInfo();
				
				ff=obj.memberBasicDetail(li.getShareHolderNo(), conn);
				ff.setLoan(String.valueOf(li.getLoanAmtInt()));
		          ff.setWord(nm.convertNumberToWords(li.getLoanAmtInt()));
		          ff.setReason(li.getLoanPurpose());
				list1.add(ff);
				i++;
			}
			HttpSession ss1=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
	        ss1.setAttribute("list", list1);
	        ss1.setAttribute("center",center);
	        
	        try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		RequestContext.getCurrentInstance().execute("window.open('selectedAcccount.xhtml')");
	}
	
	
	public void updateEmiDueDate()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		 int branchId = (int) ss.getAttribute("branchId");
		Connection conn=DataBaseConnection.JAVAConnection();
		if(emiDate.equals(selectedLoan.getEmiDate()))
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Dear User, You Can't Update Same FIRST EMI DATE Again. Please Try Again...."));
		}
		else
		{
			int i=obj.updateFirstEMIDate("grouploan", selectedLoan.getSubLoanNo(),emiDate, conn);
			if(i>0)
			{
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("FIRST EMI DATE Update Sucessfully."));
				grpList = obj.groupLoanList(branchId,center);
			}
			else
			{
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Error in updation."));
			}
		}
		
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void updateDsa()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		 int branchId = (int) ss.getAttribute("branchId");
		if(multiSelectList.size()>0)
		{
			int count=0;
			Connection conn=DataBaseConnection.JAVAConnection();
			for(LoanInfo uu: multiSelectList)
			{
				if(!uu.getDsacode().equals(newDsaCode))
				{
					count+=obj.updateDsaInLoanAccount(uu.getId(), newDsaCode, "grouploan", conn);
				}
			}
			
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(count+" Account Has been Successfully Tranfer in the Code of "+newDsaCode));
			grpList = obj.groupLoanList(branchId,center);
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Dear User, Please Select at least one Loan Account for Transfer"));
		}
	}
	
	public void viewGroupLoan() 
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		ss.setAttribute("selectedLoan", selectedLoan);
		ss.setAttribute("pageName","editViewGroupLoan.xhtml");
		
		RequestContext.getCurrentInstance().execute("window.open('viewGroupLoan.xhtml')");
	}
	
	public void editGroupLoan() 
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		ss.setAttribute("selectedLoan", selectedLoan);
		
		RequestContext.getCurrentInstance().execute("window.open('editGroupLoan.xhtml')");
	}
	
	
	
	public void printSanctionLoan()
    {
	   	HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        ss.setAttribute("selectedLoan",selectedLoan);
        
        if(printOne==true)
        {
        	 RequestContext.getCurrentInstance().execute("window.open('printFormatOfSanctionNote.xhtml')");
        }
        
        
        
        if(printTwo==true)
        {
           RequestContext.getCurrentInstance().execute("window.open('printSanctionLetter.xhtml')");
        }
        
        if(printThree==true)
        {
            RequestContext.getCurrentInstance().execute("window.open('printRepaymentSchedule.xhtml')");
        }
        
        if(printFour==true)
        {
            RequestContext.getCurrentInstance().execute("window.open('printRepayment.xhtml')");
        }
        
        if(printFive==true)
        {
            RequestContext.getCurrentInstance().execute("window.open('printDpn.xhtml')");
        }
        
        if(printSix==true)
        {
            RequestContext.getCurrentInstance().execute("window.open('printAgreementForm.xhtml')");
        }
        
        if(printSeven==true)
        {
            RequestContext.getCurrentInstance().execute("window.open('printDisbursementLetter.xhtml')");
        }
        if(printEight==true)
        {
            RequestContext.getCurrentInstance().execute("window.open('printDpnHindi.xhtml')");
        }
        
        if(printNine==true)
        {
            RequestContext.getCurrentInstance().execute("window.open('printWelcomeLetter.xhtml')");
        }
        
        if(printTenth==true)
        {
            RequestContext.getCurrentInstance().execute("window.open('printLoanApplicationForm.xhtml')");
            
            UserInfo selecteduser=new UserInfo();
            
            selecteduser=obj.allShareHoldersForView(selectedLoan.getShareHolderNo(), selecteduser);
            selecteduser=obj.allShareHoldersForView(selecteduser.getShareHolderNo(),selecteduser);
            System.out.println("center code in user is:"+selecteduser.getCenterCode());
    		ss.setAttribute("selecteduserForPrintRegForm",selecteduser);
        }
        
        if(printEleventh==true)
        {
            RequestContext.getCurrentInstance().execute("window.open('printLoanForm.xhtml')");
        }
        
        if(printInsuranseFormat == true)
        {
        	if (Double.valueOf(selectedLoan.getInsuranceFee()) == 0)
        	{
        		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Insurance Fee is Zero"));
        		return;
			}
        	
        	UserInfo selecteduser=new UserInfo();
            selecteduser=obj.allShareHoldersForView(selectedLoan.getShareHolderNo(), selecteduser);
            selecteduser=obj.allShareHoldersForView(selecteduser.getShareHolderNo(),selecteduser);
    		ss.setAttribute("selecteduserForPrintRegForm",selecteduser);
        	
        	RequestContext.getCurrentInstance().execute("window.open('printInsuranceCertificate.xhtml')");
        	RequestContext.getCurrentInstance().execute("window.open('printInsuranceCertificateHindi.xhtml')");
        }
    }
	
	public void printCheque() {
		  
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        ss.setAttribute("selectedLoan",selectedLoan);
        
		 RequestContext.getCurrentInstance().execute("window.open('printChequeEntry.xhtml')");
		  
		 
		ss.setAttribute("checkDetailList", chqList);
	}
	
	public void printAgreementForm()
    {
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        ss.setAttribute("selectedLoan",selectedLoan);
        RequestContext.getCurrentInstance().execute("window.open('printAgreementForm.xhtml')");
    }
	
	
	public void chqDetatilSubmit() {
		
		DataBaseMethodRahul dbr = new DataBaseMethodRahul();
		String cusID = selectedLoan.getShareHolderNo();
		System.out.println("shareHolder :"+selectedLoan.getShareHolderNo());
		System.out.println("name:"+selectedLoan.getApplicantName());
		System.out.println("loanNo :"+selectedLoan.getLoanNo());
		
	    dbr.GLchqueDetailEntry(selectedLoan.getLoanNo(),selectedLoan.getApplicantName(), chqFrom, chqNo, bankName, bankIfsc, bankBranch,cusID,bankAccNo);
	   
	   
	    chqNo = null ;	
	    bankName = null	;	
	    bankIfsc =   null;	
 	    bankBranch = null ;
	    
	}
	
	

	
	
	public LoanInfo getSelectedLoan() {
		return selectedLoan;
	}

	public void setSelectedLoan(LoanInfo selectedLoan) {
		this.selectedLoan = selectedLoan;
	}

	public ArrayList<LoanInfo> getgrpList() {
		return grpList;
	}

	public void setgrpList(ArrayList<LoanInfo> grpList) {
		this.grpList = grpList;
	}

	public String getCenter() {
		return center;
	}

	public void setCenter(String center) {
		this.center = center;
	}

	public ArrayList<SelectItem> getCenterList() {
		return centerList;
	}

	public void setCenterList(ArrayList<SelectItem> centerList) {
		this.centerList = centerList;
	}

	public Date getEmiDate() {
		return emiDate;
	}

	public void setEmiDate(Date emiDate) {
		this.emiDate = emiDate;
	}

	public int getTotalLoan() {
		return totalLoan;
	}

	public void setTotalLoan(int totalLoan) {
		this.totalLoan = totalLoan;
	}

	public ArrayList<LoanInfo> getGrpList() {
		return grpList;
	}

	public void setGrpList(ArrayList<LoanInfo> grpList) {
		this.grpList = grpList;
	}

	public ArrayList<LoanInfo> getMultiSelectList() {
		return multiSelectList;
	}

	public void setMultiSelectList(ArrayList<LoanInfo> multiSelectList) {
		this.multiSelectList = multiSelectList;
	}

	public String getNewDsaCode() {
		return newDsaCode;
	}

	public void setNewDsaCode(String newDsaCode) {
		this.newDsaCode = newDsaCode;
	}

	public ArrayList<SelectItem> getDsaList() {
		return dsaList;
	}

	public void setDsaList(ArrayList<SelectItem> dsaList) {
		this.dsaList = dsaList;
	}

	public boolean isPrintOne() {
		return printOne;
	}

	public void setPrintOne(boolean printOne) {
		this.printOne = printOne;
	}

	public boolean isPrintTwo() {
		return printTwo;
	}

	public void setPrintTwo(boolean printTwo) {
		this.printTwo = printTwo;
	}

	public boolean isPrintThree() {
		return printThree;
	}

	public void setPrintThree(boolean printThree) {
		this.printThree = printThree;
	}

	public boolean isPrintFour() {
		return printFour;
	}

	public void setPrintFour(boolean printFour) {
		this.printFour = printFour;
	}

	public boolean isPrintFive() {
		return printFive;
	}

	public void setPrintFive(boolean printFive) {
		this.printFive = printFive;
	}

	public boolean isPrintSix() {
		return printSix;
	}

	public void setPrintSix(boolean printSix) {
		this.printSix = printSix;
	}

	public boolean isPrintSeven() {
		return printSeven;
	}

	public void setPrintSeven(boolean printSeven) {
		this.printSeven = printSeven;
	}

	public boolean isPrintEight() {
		return printEight;
	}

	public void setPrintEight(boolean printEight) {
		this.printEight = printEight;
	}

	public boolean isPrintNine() {
		return printNine;
	}

	public void setPrintNine(boolean printNine) {
		this.printNine = printNine;
	}

	public boolean isPrintTenth() {
		return printTenth;
	}

	public void setPrintTenth(boolean printTenth) {
		this.printTenth = printTenth;
	}

	public boolean isPrintEleventh() {
		return printEleventh;
	}

	public void setPrintEleventh(boolean printEleventh) {
		this.printEleventh = printEleventh;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public Date getChargesDate() {
		return chargesDate;
	}

	public void setChargesDate(Date chargesDate) {
		this.chargesDate = chargesDate;
	}

	public int getChargesAmount() {
		return chargesAmount;
	}

	public void setChargesAmount(int chargesAmount) {
		this.chargesAmount = chargesAmount;
	}

	public ArrayList<BorrowerLenderInfo> getManualChargesList() {
		return manualChargesList;
	}

	public void setManualChargesList(ArrayList<BorrowerLenderInfo> manualChargesList) {
		this.manualChargesList = manualChargesList;
	}

	public BorrowerLenderInfo getSelectedRow() {
		return selectedRow;
	}

	public void setSelectedRow(BorrowerLenderInfo selectedRow) {
		this.selectedRow = selectedRow;
	}

	public DataBaseMethods getObj() {
		return obj;
	}

	public void setObj(DataBaseMethods obj) {
		this.obj = obj;
	}

	public String getChqFrom() {
		return chqFrom;
	}

	public void setChqFrom(String chqFrom) {
		this.chqFrom = chqFrom;
	}

	public String getChqNo() {
		return chqNo;
	}

	public void setChqNo(String chqNo) {
		this.chqNo = chqNo;
	}

	public String getChqName() {
		return chqName;
	}

	public void setChqName(String chqName) {
		this.chqName = chqName;
	}

	

	public String getBankBranch() {
		return bankBranch;
	}

	public void setBankBranch(String bankBranch) {
		this.bankBranch = bankBranch;
	}

	public String getBankName() {
		return bankName;
	}

	public void setBankName(String bankName) {
		this.bankName = bankName;
	}

	public String getBankIfsc() {
		return bankIfsc;
	}

	public void setBankIfsc(String bankIfsc) {
		this.bankIfsc = bankIfsc;
	}

	public ArrayList<ChequeDetailsEntryInfo> getChqList() {
		return chqList;
	}

	public void setChqList(ArrayList<ChequeDetailsEntryInfo> chqList) {
		this.chqList = chqList;
	}

	public String getBankAccNo() {
		return bankAccNo;
	}

	public void setBankAccNo(String bankAccNo) {
		this.bankAccNo = bankAccNo;
	}

	public String getCompanyName() {
		return companyName;
	}

	public void setCompanyName(String companyName) {
		this.companyName = companyName;
	}

	public boolean isPrintInsuranseFormat() {
		return printInsuranseFormat;
	}

	public void setPrintInsuranseFormat(boolean printInsuranseFormat) {
		this.printInsuranseFormat = printInsuranseFormat;
	}

	
	

	

}
