package group_head_package;

import java.io.Serializable;
import java.sql.Connection;
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.servlet.http.HttpSession;

import main_package.DataBaseConnection;

@ManagedBean(name="viewDeleteFixedDepositExpense")
@ViewScoped

public class ViewDeleteFixedDepositExpenseBean implements Serializable
{
	ArrayList<IncomeList>dList;
    IncomeList selectedExpense;
    Date startDate=new Date(),endDate=new Date();
    public ViewDeleteFixedDepositExpenseBean()
    {
    	HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss.getAttribute("branchId");
    	startDate=new Date();
    	startDate.setDate(1);
    	endDate=new Date();
    	Connection conn=DataBaseConnection.JAVAConnection();
    	dList=new  DataBaseIncomeExpense().allFixedDepositExpenseList(branchId,startDate,endDate,conn);
    	try {
			conn.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
    }
    
	public void searchExpense()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss.getAttribute("branchId");
		Connection conn=DataBaseConnection.JAVAConnection();
		dList=new  DataBaseIncomeExpense().allFixedDepositExpenseList(branchId,startDate,endDate,conn);
		try {
			conn.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	public void deleteSelectedExpense()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss.getAttribute("branchId");
		int i=new DataBaseIncomeExpense().deleteFixedDepositExpense(selectedExpense);
		if(i>=1)
		{
			Connection conn=DataBaseConnection.JAVAConnection();
			dList=new DataBaseIncomeExpense().allFixedDepositExpenseList(branchId,startDate, endDate,conn);
			FacesContext fc=FacesContext.getCurrentInstance();
			try {
				conn.close();
			} catch (Exception e) {
				// TODO: handle exception
			}
			fc.addMessage(null, new FacesMessage("Record Deleted successfully"));
		}
		else
		{
			FacesContext fc=FacesContext.getCurrentInstance();
			fc.addMessage(null, new FacesMessage("An error found, try again"));
		}
	}

	public ArrayList<IncomeList> getdList() {
		return dList;
	}

	public void setdList(ArrayList<IncomeList> dList) {
		this.dList = dList;
	}

	public IncomeList getSelectedExpense() {
		return selectedExpense;
	}

	public void setSelectedExpense(IncomeList selectedExpense) {
		this.selectedExpense = selectedExpense;
	}

	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;
	}


}
