package report_package;

import java.io.Serializable;
import java.sql.Connection;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

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 main_package.DataBaseMethods;
import main_package.DataBaseMethodsHemant;
import main_package.DataBaseConnection;
import main_package.LoanEmiReceiptInfo;
import main_package.LoanInfo;

import org.primefaces.context.RequestContext;

@ManagedBean(name="completeLoanReport")
@ViewScoped
public class CompleteLoanReportBean implements Serializable
{
	ArrayList<LoanInfo> list;
	String loanType, searchBase;
	double totalamount, totalPrincipalPaid, totalIntrestPaid, totalPrincipalBalance, totalPaid;
	int totalLoan;
	Date startDate=new Date(), endDate=new Date();
	boolean show;
	LoanInfo selectedLoan;
	DataBaseMethods db = new DataBaseMethods();
	DataBaseReportMethods obj1=new DataBaseReportMethods();
	DataBaseMethodsHemant dbmh = new DataBaseMethodsHemant();
	int branchId;
	String branchUserId;
	List<SelectItem> branchList = new ArrayList<SelectItem>();
	boolean disAll;
	String userType;
	ArrayList<SelectItem> loanTypeList;
	
	
	
	public CompleteLoanReportBean()
	{
		Connection con = DataBaseConnection.JAVAConnection();
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		branchUserId = (String) ss.getAttribute("branchUserId");
		branchList = new DataBaseMethods().branchList(branchUserId,con);
		
		String cc=(String) ss.getAttribute("companyName");

		userType= (String) ss.getAttribute("type");

		
	    disAll= dbmh.disAllByCompanyNameUserType(userType,cc);

		try {
			con.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
	
	}
	
	public void searchReport()
	{
		if(branchId != 0)
		{
			Connection conn=DataBaseConnection.JAVAConnection();
			
			if ("All".equalsIgnoreCase(loanType)) 
			{
			    loanTypeList = db.loanTypeList(conn);

			    list = new ArrayList<>();

			    for (SelectItem item : loanTypeList) 
			    {
			        String actualLoanType = item.getValue().toString();
			        List<LoanInfo> tempList = obj1.completeLoanStatusList(branchId, actualLoanType, searchBase, startDate,
			                                            				  endDate, conn);

			        if (tempList != null && !tempList.isEmpty()) 
			        {
			            list.addAll(tempList);
			        }
			    }
			}
			else 
			{
				list = obj1.completeLoanStatusList(branchId,loanType, searchBase,startDate, endDate,conn);
			}
			
			if(list.size()>0)
			{
				show=true;
				totalLoan=list.size();
			}
			else
			{
				show=false;
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("No Record Found"));
			}
			try {
				conn.close();
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
		else
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Select Branch"));
		}
		
	}

	public void viewDetails() 
	{
		selectedLoan = new DataBaseReportMethods().loanCloserList(selectedLoan.getLoanNo(), selectedLoan);
	}

	
	 public void printDetails()
	 {
	    LoanEmiReceiptInfo loanReceipt_info=new DataBaseMethods().loanCloserReceiptInfo(selectedLoan.getLoanNo());
        HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        ss.setAttribute("shareHolderNo", selectedLoan.getShareHolderNo());
        ss.setAttribute("loanNo",selectedLoan.getLoanNo());
        ss.setAttribute("receipt_no",loanReceipt_info.getReceipt_no());
        ss.setAttribute("installment_amount",totalamount);
        ss.setAttribute("loanType", loanType);
        ss.setAttribute("id", Integer.parseInt(loanReceipt_info.getReceipt_no()));
      	ss.setAttribute("receipt_date",new SimpleDateFormat("dd/MM/yyyy").format(loanReceipt_info.getPay_date()));
        ss.setAttribute("instlmnt_no","Loan Closure");
        ss.setAttribute("penalty", 0.0);
		ss.setAttribute("bounceCharge", 0.0);

        RequestContext.getCurrentInstance().execute("window.open('loanPayReceipt.xhtml')");
	 }
	 
	public void printDetailsNoc()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		ss.setAttribute("selected", selectedLoan);
		ss.setAttribute("loanType", loanType);
		RequestContext.getCurrentInstance().execute("window.open('printLoanNOC.xhtml')");
		RequestContext.getCurrentInstance().execute("window.open('printNoc.xhtml')");
	}	
 
	public ArrayList<LoanInfo> getList() {
		return list;
	}

	public void setList(ArrayList<LoanInfo> list) {
		this.list = list;
	}

	public int getTotalLoan() {
		return totalLoan;
	}

	public void setTotalLoan(int totalLoan) {
		this.totalLoan = totalLoan;
	}


	public String getLoanType() {
		return loanType;
	}


	public void setLoanType(String loanType) {
		this.loanType = loanType;
	}


	public double getTotalamount() {
		return totalamount;
	}


	public void setTotalamount(double totalamount) {
		this.totalamount = totalamount;
	}


	public double getTotalPrincipalPaid() {
		return totalPrincipalPaid;
	}


	public void setTotalPrincipalPaid(double totalPrincipalPaid) {
		this.totalPrincipalPaid = totalPrincipalPaid;
	}


	public double getTotalIntrestPaid() {
		return totalIntrestPaid;
	}


	public void setTotalIntrestPaid(double totalIntrestPaid) {
		this.totalIntrestPaid = totalIntrestPaid;
	}


	public double getTotalPrincipalBalance() {
		return totalPrincipalBalance;
	}


	public void setTotalPrincipalBalance(double totalPrincipalBalance) {
		this.totalPrincipalBalance = totalPrincipalBalance;
	}


	public double getTotalPaid() {
		return totalPaid;
	}


	public void setTotalPaid(double totalPaid) {
		this.totalPaid = totalPaid;
	}


	public boolean isShow() {
		return show;
	}


	public void setShow(boolean show) {
		this.show = show;
	}

	public LoanInfo getSelectedLoan() {
		return selectedLoan;
	}

	public void setSelectedLoan(LoanInfo selectedLoan) {
		this.selectedLoan = selectedLoan;
	}

	public int getBranchId() {
		return branchId;
	}

	public void setBranchId(int branchId) {
		this.branchId = branchId;
	}

	public List<SelectItem> getBranchList() {
		return branchList;
	}

	public void setBranchList(List<SelectItem> branchList) {
		this.branchList = branchList;
	}

	public String getSearchBase() {
		return searchBase;
	}

	public void setSearchBase(String searchBase) {
		this.searchBase = searchBase;
	}

	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public boolean isDisAll() {
		return disAll;
	}

	public void setDisAll(boolean disAll) {
		this.disAll = disAll;
	}

	public String getUserType() {
		return userType;
	}

	public void setUserType(String userType) {
		this.userType = userType;
	}
	
	
}