package report_package;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
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.DataBaseMethods;
import main_package.DataBaseMethodsHemant;
import main_package.UserInfo;

import org.primefaces.context.RequestContext;

@ManagedBean(name="fileCharge")
@ViewScoped

public class FileChargeReportBean implements Serializable
{
	ArrayList<UserInfo> fileChargeGrpList,fileChargeVhclList,fileChargePrprtyList,fileChargePrsnlList,incomeList;
	double totalAmount;
	int totalEntries;
	Date startDate=new Date(),endDate=new Date();
	String reportType;
	boolean show=false;
	DataBaseReportMethods obj1=new DataBaseReportMethods();
	int branchId;
	String branchUserId;
	List<SelectItem> branchList = new ArrayList<SelectItem>();
	DataBaseMethodsHemant dbmh = new DataBaseMethodsHemant();
    boolean disAll;
    String userType;
	
	public FileChargeReportBean() 
	{
		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= dbmh.disAllByCompanyNameUserType(userType,cc);

		try {
			con.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	
	public void showReport()
	{
		
		if(branchId != 0)
		{
			Connection conn=DataBaseConnection.JAVAConnection();
			if(((startDate!=null && endDate!=null) && (endDate.after(startDate))))
			{
				show=true;
				incomeList=new ArrayList<>();
				totalAmount=0;
				fileChargePrsnlList=obj1.fileChargePrsnlLoanListForIncomeReport(branchId,startDate,endDate, reportType,conn);
				fileChargePrprtyList=obj1.fileChargePrprtyLoanListForIncomeReport(branchId,startDate,endDate, reportType,conn);
				fileChargeVhclList=obj1.fileChargeVhclLoanListForIncomeReport(branchId,startDate,endDate, reportType,conn);
				fileChargeGrpList=obj1.fileChargeGrpLoanListForIncomeReport(branchId,startDate,endDate,reportType,conn);
				
				incomeList.addAll(fileChargePrsnlList);
				incomeList.addAll(fileChargePrprtyList);
				incomeList.addAll(fileChargeVhclList);
				incomeList.addAll(fileChargeGrpList);
				
				totalEntries=incomeList.size();
				int i=1;
				for(UserInfo ll:incomeList)
				{
					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)
		{
			//String st="All";
			HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
			ss.setAttribute("finalList", incomeList);
			ss.setAttribute("totalAmount", totalAmount);
			ss.setAttribute("totalEntry", totalEntries);
			ss.setAttribute("reportType", reportType);
			//ss.setAttribute("identity", st);
			RequestContext.getCurrentInstance().execute("window.open('printFileChargeReport.xhtml')");
		}
		else
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Nothing To Print.Please Check!"));
		}
	}
	
	public ArrayList<UserInfo> getIncomeList() {
		return incomeList;
	}

	public void setIncomeList(ArrayList<UserInfo> incomeList) {
		this.incomeList = incomeList;
	}

	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;
	}
	
}
