package main_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.faces.model.SelectItem;
import javax.servlet.http.HttpSession;

import org.primefaces.context.RequestContext;

@ManagedBean(name="memberRegListByApp")
@ViewScoped
public class MemberRegListByAppBean implements Serializable
{
	Date startDate=new Date(), endDate=new Date();
	ArrayList<SelectItem>  dsaList;
	ArrayList<UserInfo> list=new ArrayList<>();
	String userId, dsaCode;
	DataBaseMethods dbm=new DataBaseMethods();
	int total;
	UserInfo selectedRow;
	
	public MemberRegListByAppBean() 
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId =(int) ss.getAttribute("branchId");
		Connection con = DataBaseConnection.JAVAConnection();
		startDate.setDate(1);
//		userId=dbm.userId();
//		list=dbm.memberRegListByApp(userId, startDate, endDate,branchId);
//		total=list.size();
		
		
		dsaList=new DataBaseMethods().allDsaList(branchId,"Loan",con);
		try {
			con.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	public void searchData()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId =(int) ss.getAttribute("branchId");
		Connection con = DataBaseConnection.JAVAConnection();
		
		if(dsaCode.equalsIgnoreCase("all"))
		{
			userId="all";
		}
		else
		{
			userId=dbm.empUserIdByDsaCode(dsaCode, con);
		}
		
		
		try {
			con.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		list=dbm.memberRegListByApp(userId, startDate, endDate,branchId);
		total=list.size();
	}
	
	public void viewDocuments()
	{
		selectedRow=dbm.appMemberDocForView(selectedRow.getId(), selectedRow);
		RequestContext.getCurrentInstance().execute("PF('docDlg').show();");
		RequestContext.getCurrentInstance().update("docForm");
	}
	
	public void printDetail()
	{
		selectedRow=dbm.appMemberDocForView(selectedRow.getId(), selectedRow);
		
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		ss.setAttribute("selectedAppFormRow", selectedRow);
		
		RequestContext.getCurrentInstance().execute("window.open('printAppForm.xhtml')");
	}


	public void dataOnNextPage()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId =(int) ss.getAttribute("branchId");
		ss.setAttribute("selectedMemberRow", selectedRow);
		
		RequestContext.getCurrentInstance().execute("window.open('memberRegistration.xhtml')");
		dbm.updateStatusInRegTable("active", selectedRow.getId());
		list=dbm.memberRegListByApp(userId, startDate, endDate,branchId);
	}
	
	public void delete()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId =(int) ss.getAttribute("branchId");
		int i=dbm.updateStatusInRegTable("inactive", selectedRow.getId());
		if(i>0)
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Delete Sucessfully"));
			list=dbm.memberRegListByApp(userId, startDate, endDate,branchId);
		}
		else
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Error in Deletion !!!!!!!!!!!!"));
		}
	}
	
	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 ArrayList<UserInfo> getList() {
		return list;
	}

	public void setList(ArrayList<UserInfo> list) {
		this.list = list;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public UserInfo getSelectedRow() {
		return selectedRow;
	}

	public void setSelectedRow(UserInfo selectedRow) {
		this.selectedRow = selectedRow;
	}

	public String getDsaCode() {
		return dsaCode;
	}

	public void setDsaCode(String dsaCode) {
		this.dsaCode = dsaCode;
	}

	public ArrayList<SelectItem> getDsaList() {
		return dsaList;
	}

	public void setDsaList(ArrayList<SelectItem> dsaList) {
		this.dsaList = dsaList;
	}
	
	
}
