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 java.util.List;

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 report_package.LoanSpecialReportBean.ColumnModel;
import main_package.DataBaseConnection;
import main_package.DataBaseMethods;
import main_package.DataBaseMethodsHemant;
import main_package.LoanInfo;
import main_package.UserInfo;

@ManagedBean(name="branchReport")
@ViewScoped

public class BranchReportBean implements Serializable
{
	Date startDate=new Date(), endDate=new Date();
	String searchType, dsaCode;
	ArrayList<SelectItem> dsaList;
	boolean showData;
	ArrayList<UserInfo> list;
	ArrayList<DynamicColumnInfo> columns;
	ArrayList<SelectItem> dateList;
	int branchId;
	String branchUserId;
	List<SelectItem> branchList = new ArrayList<SelectItem>();
	SimpleDateFormat sf=new SimpleDateFormat("dd/MM/yyyy");
	SimpleDateFormat sdfForDb=new SimpleDateFormat("yyyy-MM-dd"); 
	DataBaseMethods db=new DataBaseMethods();
	DataBaseMethodsHemant dbmh= new DataBaseMethodsHemant();
	String userType;
	boolean disAll;
	
	public BranchReportBean() 
	{
		Connection conn=DataBaseConnection.JAVAConnection();
		
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		branchUserId = (String) ss.getAttribute("branchUserId");
		branchList = new DataBaseMethods().branchList(branchUserId,conn);
		startDate.setDate(1);
		String cc=(String) ss.getAttribute("companyName");
		userType= (String) ss.getAttribute("type");


	    disAll= dbmh.disAllByCompanyNameUserType(userType,cc);

		dsaList=db.allDsaList(branchId,"Loan",conn);
		
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void searchReport()
	{
		Connection conn=DataBaseConnection.JAVAConnection();
		showData=true;
		
		if(searchType.equalsIgnoreCase("Daily Loan"))
		{
			list=db.dailyLoanListForBranchReport(searchType,dsaCode, branchId, conn);
		}
		else if(searchType.equalsIgnoreCase("Daily Deposit"))
		{
			list=db.dailyDepositListForBranchReport(searchType, dsaCode, branchId, conn);
		}
		
		
		dateList=new ArrayList<>();
		
		while(!startDate.equals(endDate))
		{
			SelectItem ss=new SelectItem();
			ss.setLabel(sf.format(startDate));
			ss.setValue(sdfForDb.format(startDate));
			dateList.add(ss);
			
			startDate.setDate(startDate.getDate()+1);
			System.out.println("date: "+startDate);
		}
		
		//System.out.println(itemList.size()+"========="+list.size());
		columns = new ArrayList<DynamicColumnInfo>(); 
		
		db.calculateDailyLoanAndDDReceiveAmount(dateList, list, conn);
		
		for(SelectItem tt: dateList)
		{
			DynamicColumnInfo cc=new DynamicColumnInfo();
			
			//System.out.println("Label: "+tt.getLabel());
			cc.setLabel(tt.getLabel());
			cc.setValue(String.valueOf(tt.getValue()));
			
			columns.add(cc);
		}
		
		
		
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	static public class DynamicColumnInfo implements Serializable 
	{
		String label, value;

		public String getLabel() {
			return label;
		}

		public void setLabel(String label) {
			this.label = label;
		}

		public String getValue() {
			return value;
		}

		public void setValue(String value) {
			this.value = value;
		}
	}

	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 getSearchType() {
		return searchType;
	}

	public void setSearchType(String searchType) {
		this.searchType = searchType;
	}

	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;
	}

	public boolean isShowData() {
		return showData;
	}

	public void setShowData(boolean showData) {
		this.showData = showData;
	}

	public ArrayList<UserInfo> getList() {
		return list;
	}

	public void setList(ArrayList<UserInfo> list) {
		this.list = list;
	}

	public ArrayList<DynamicColumnInfo> getColumns() {
		return columns;
	}

	public void setColumns(ArrayList<DynamicColumnInfo> columns) {
		this.columns = columns;
	}

	public ArrayList<SelectItem> getDateList() {
		return dateList;
	}

	public void setDateList(ArrayList<SelectItem> dateList) {
		this.dateList = dateList;
	}

	public SimpleDateFormat getSf() {
		return sf;
	}

	public void setSf(SimpleDateFormat sf) {
		this.sf = sf;
	}

	public SimpleDateFormat getSdfForDb() {
		return sdfForDb;
	}

	public void setSdfForDb(SimpleDateFormat sdfForDb) {
		this.sdfForDb = sdfForDb;
	}

	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;
	}
}
