package report_package;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

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.UserInfo;

@ManagedBean(name="centerMeetingReport")
@ViewScoped
public class CenterMeetingReportBean implements Serializable
{
	Date startDate=new Date(), endDate=new Date();
	boolean showSingle, showCustom;
	ArrayList<SelectItem> emiList, dsaList;
	SimpleDateFormat sf=new SimpleDateFormat("dd/MMM/yyyy");
	ArrayList<UserInfo> list;
	String searchType="Custom Date", dsaCode;
	DataBaseMethods dbm=new DataBaseMethods();
	
	public CenterMeetingReportBean() 
	{
		Connection conn=DataBaseConnection.JAVAConnection();
		
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss.getAttribute("branchId");
		
		dsaList=dbm.allDsaList(branchId,"Loan",conn);
		
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	public void searchReportData()
	{
		if(searchType.equalsIgnoreCase("Custom Date"))
		{
			showSingle=false;
			showCustom=true;
		}
		else
		{
			showSingle=true;
			showCustom=false;
		}
	}
	
	public void searchData()
	{
		HttpSession ss0=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss0.getAttribute("branchId");
		
		if(searchType.equalsIgnoreCase("Custom Date"))
		{
			showSingle=false;
			showCustom=true;
			int sno=1;
			list=new ArrayList<>();
			while(startDate.before(endDate) || startDate.equals(endDate))
			{
				
				UserInfo uu=new UserInfo();
				
				uu.setSno(sno++);
				uu.setStrPayOutDate(sf.format(startDate));
				
				String typeMsg="";
				emiList=dbm.centerListForCenterMeeting(branchId,startDate,dsaCode);
			   	 if(emiList.size()>0)
			   	 {
			   		 int count=1;
			   		 for(SelectItem ss: emiList)
			       	 {
		       			 typeMsg+=count+". "+ss.getLabel()+ System.lineSeparator();
		       			 count++;
			       	 }
			   	 }
			   	
			   	uu.setDayOfWeek(new SimpleDateFormat("EE").format(startDate));
			   	uu.setDetail(typeMsg);
			   	list.add(uu);
			   	 
				startDate.setDate(startDate.getDate()+1);
			}
		}
		else
		{
			showSingle=true;
			showCustom=false;
			list=dbm.centerListForCenterMeeting2(branchId,startDate);
		}
	}
	
	
	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<SelectItem> getEmiList() {
		return emiList;
	}


	public void setEmiList(ArrayList<SelectItem> emiList) {
		this.emiList = emiList;
	}


	public ArrayList<UserInfo> getList() {
		return list;
	}


	public void setList(ArrayList<UserInfo> list) {
		this.list = list;
	}


	public boolean isShowSingle() {
		return showSingle;
	}


	public void setShowSingle(boolean showSingle) {
		this.showSingle = showSingle;
	}


	public boolean isShowCustom() {
		return showCustom;
	}


	public void setShowCustom(boolean showCustom) {
		this.showCustom = showCustom;
	}


	public String getSearchType() {
		return searchType;
	}


	public void setSearchType(String searchType) {
		this.searchType = searchType;
	}


	public ArrayList<SelectItem> getDsaList() {
		return dsaList;
	}


	public void setDsaList(ArrayList<SelectItem> dsaList) {
		this.dsaList = dsaList;
	}


	public String getDsaCode() {
		return dsaCode;
	}


	public void setDsaCode(String dsaCode) {
		this.dsaCode = dsaCode;
	}


	
	
}
