package report_package;

import java.io.Serializable;
import java.sql.Connection;
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.DataBaseConnection;
import main_package.DataBaseMethodTaheer;
import main_package.DataBaseMethods;
import main_package.LoanInfo;

import org.primefaces.context.RequestContext;

@ManagedBean(name="preEMIReportBean")
@ViewScoped

public class PreEMIChargeReportBean implements Serializable
{
	int branchId, totalEntries;
	
	double totalAmount;
	
	String userType, reportType, loanType, branchUserId, status;
	
	Date startDate = new Date(), endDate = new Date();
	
	boolean disAll, show = false;
	
	List<SelectItem> branchList = new ArrayList<SelectItem>();
	
	ArrayList<LoanInfo> preEMIChargeList, reportList;
	
	DataBaseMethodTaheer dbmt = new DataBaseMethodTaheer();
    
	
	
	public PreEMIChargeReportBean() 
	{
		startDate.setMonth(startDate.getMonth()-1);
		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 = dbmt.disAllByCompanyNameUserType(userType,cc);

		try 
		{
			con.close();
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
	}
	
	
	
	public void showReport()
	{
		if(branchId != 0)
		{
			Connection conn = DataBaseConnection.JAVAConnection();
			if((startDate != null && endDate != null) && (endDate.after(startDate)))
			{
				show = true;
				reportList = new ArrayList<>();
				totalAmount = 0;
				
				preEMIChargeList = dbmt.fetchActiveLoanListByLoanType(loanType, branchId, status, conn);
				
				reportList.addAll(preEMIChargeList);
				
				totalEntries = reportList.size();
				int i = 1;
				for(LoanInfo ll : reportList)
				{
					ll.setSno(i++);
					totalAmount = totalAmount+(ll.getAmount());
				}
			}
			else
			{
				show = false;
				FacesContext fc = FacesContext.getCurrentInstance();
				fc.addMessage(null, new FacesMessage("Please enter the dates properly!"));
			}
		}
    	else
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Select Branch"));
		}
	}
	
	
	
	public void printReport()
	{
		if(totalEntries > 0)
		{
			HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
			ss.setAttribute("finalList", reportList);
			ss.setAttribute("totalAmount", totalAmount);
			ss.setAttribute("totalEntry", totalEntries);
			ss.setAttribute("reportType", reportType);

			RequestContext.getCurrentInstance().execute("window.open('printFileChargeReport.xhtml')");
		}
		else
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Nothing To Print.Please Check!"));
		}
	}
	
	

	public double getTotalAmount() {
		return totalAmount;
	}

	public void setTotalAmount(double totalAmount) {
		this.totalAmount = totalAmount;
	}

	public int getTotalEntries() {
		return totalEntries;
	}

	public void setTotalEntries(int totalEntries) {
		this.totalEntries = totalEntries;
	}

	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 String getReportType() {
		return reportType;
	}

	public void setReportType(String reportType) {
		this.reportType = reportType;
	}

	public boolean isShow() {
		return show;
	}

	public void setShow(boolean show) {
		this.show = show;
	}
	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 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;
	}



	public ArrayList<LoanInfo> getPreEMIChargeList() {
		return preEMIChargeList;
	}



	public void setPreEMIChargeList(ArrayList<LoanInfo> preEMIChargeList) {
		this.preEMIChargeList = preEMIChargeList;
	}



	public ArrayList<LoanInfo> getReportList() {
		return reportList;
	}



	public void setReportList(ArrayList<LoanInfo> reportList) {
		this.reportList = reportList;
	}



	public String getLoanType() {
		return loanType;
	}



	public void setLoanType(String loanType) {
		this.loanType = loanType;
	}



	public String getStatus() {
		return status;
	}



	public void setStatus(String status) {
		this.status = status;
	}
	
}