package report_package;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
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 main_package.DataBaseMethods;

@ManagedBean(name="instantBorrowReport")
@ViewScoped
public class InstantBorrowReportBean implements Serializable
{

	Date startDate=new Date(),endDate=new Date();
	ArrayList<SelectItem> blist;
	ArrayList<BorrowerLenderInfo> list, list1, list2;
	String selectedName, name, sDate, eDate, balanceType;
	boolean show;
	int totalCredit, totalDebit, balance;
	DataBaseReportMethods obj1=new DataBaseReportMethods();
	public InstantBorrowReportBean() 
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss.getAttribute("branchId");
		startDate.setMonth(startDate.getMonth()-1);
		blist=new DataBaseMethods().allBorrowerOrLenderList(branchId,"borrower");
	}
	
	public void view()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss.getAttribute("branchId");
		list=list1=list2=new ArrayList<>();
		list1=obj1.instantBorrowReport(branchId,startDate, endDate, selectedName, "credit");
		list2=obj1.instantBorrowReport(branchId,startDate, endDate, selectedName, "debit");
		
		
		
		list.addAll(list1);
		list.addAll(list2);
		
		sDate=new SimpleDateFormat("dd/MM/yyyy").format(startDate);
		eDate=new SimpleDateFormat("dd/MM/yyyy").format(endDate);
		name=new DataBaseMethods().borrowerLenderNameById(selectedName);
		
		if(list.size()>0)
		{
			show=true;
			Collections.sort(list);

			totalCredit=totalDebit=balance=0;
			for(int i=0;i<list.size();i++)
			{
				list.get(i).setSno(i+1);
				balance=(balance+Integer.parseInt(list.get(i).getCredit()))-Integer.parseInt(list.get(i).getDebit());
				list.get(i).setBalance(balance);
				
			}
			
			for(BorrowerLenderInfo ll:list)
			{
				totalDebit+=Integer.parseInt(ll.getDebit());
				totalCredit+=Integer.parseInt(ll.getCredit());
			}
			balance=totalCredit-totalDebit;
			
			if(balance<0)
			{
				balanceType="Debit";
			}
			else
			{
				balanceType="Credit";
			}
			//// System.out.println("dr: "+totalDebit+"cr: "+totalCredit+"bal: "+balance);
			
		}
		else
		{
			show=false;
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("No Record Found"));
		}
	}

	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 getSelectedName() {
		return selectedName;
	}

	public void setSelectedName(String selectedName) {
		this.selectedName = selectedName;
	}

	public ArrayList<SelectItem> getBlist() {
		return blist;
	}

	public void setBlist(ArrayList<SelectItem> blist) {
		this.blist = blist;
	}

	public ArrayList<BorrowerLenderInfo> getList() {
		return list;
	}

	public void setList(ArrayList<BorrowerLenderInfo> list) {
		this.list = list;
	}

	public ArrayList<BorrowerLenderInfo> getList1() {
		return list1;
	}

	public void setList1(ArrayList<BorrowerLenderInfo> list1) {
		this.list1 = list1;
	}

	public ArrayList<BorrowerLenderInfo> getList2() {
		return list2;
	}

	public void setList2(ArrayList<BorrowerLenderInfo> list2) {
		this.list2 = list2;
	}

	public boolean isShow() {
		return show;
	}

	public void setShow(boolean show) {
		this.show = show;
	}

	public int getTotalCredit() {
		return totalCredit;
	}

	public void setTotalCredit(int totalCredit) {
		this.totalCredit = totalCredit;
	}

	public int getTotalDebit() {
		return totalDebit;
	}

	public void setTotalDebit(int totalDebit) {
		this.totalDebit = totalDebit;
	}

	public int getBalance() {
		return balance;
	}

	public void setBalance(int balance) {
		this.balance = balance;
	}

	public String getsDate() {
		return sDate;
	}

	public void setsDate(String sDate) {
		this.sDate = sDate;
	}

	public String geteDate() {
		return eDate;
	}

	public void seteDate(String eDate) {
		this.eDate = eDate;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getBalanceType() {
		return balanceType;
	}

	public void setBalanceType(String balanceType) {
		this.balanceType = balanceType;
	}
	
	
	
}
