package report_package;

import java.io.Serializable;
import java.sql.Connection;
import java.text.DecimalFormat;
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 main_package.DataBaseMethods;
import main_package.DataBaseConnection;
import main_package.DsaListInfo;

@ManagedBean(name="dsaWiseCommissionReport")
@ViewScoped
public class DsaWiseCommissionReportBean implements Serializable
{
	ArrayList<SelectItem> dsaList, typeList;
	Date startDate=new Date(), endDate=new Date();
	String dsaCode, searchType, dsaName;
	boolean show;
	ArrayList<DsaListInfo> dlist;
	DsaListInfo selectedRow=new DsaListInfo();
	double totalAmount;
	
	public DsaWiseCommissionReportBean() 
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss.getAttribute("branchId");
		startDate.setDate(1);
		Connection conn=DataBaseConnection.JAVAConnection();
		dsaList=new DataBaseMethods().allDsaList(branchId,"",conn);
		try {
			conn.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	public void checkTypeList()
	{
		Connection conn=DataBaseConnection.JAVAConnection();
		typeList=new DataBaseMethods().dsaCommissionTypeList(dsaCode,conn);
		dsaName=new DataBaseMethods().dsaNameById(dsaCode,conn);
		try {
			conn.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	
	public void search()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss.getAttribute("branchId");
		Connection conn=DataBaseConnection.JAVAConnection();
		if(startDate.before(endDate) || startDate.equals(endDate))
		{
			if(dsaList.size()>0)
			{
				dlist=new DataBaseMethods().dsaCommissionListDateWise(branchId,dsaCode, startDate, endDate, searchType,conn);
				if(dlist.size()>0)
				{
					show=true;
					totalAmount=0;
					for(DsaListInfo ff: dlist)
					{
						totalAmount+=ff.getCommission_value();
					}
					totalAmount=Double.parseDouble(new DecimalFormat("##.##").format(totalAmount));
				}
				else
				{
					show=false;
					FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("No Record Found."));
				}
			}
			else
			{
				show=false;
			}
			try {
				conn.close();
			} catch (Exception e) 
			{
				// TODO: handle exception
			}
		}
		else
		{
			show=false;
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Please Select Valid Date Selection."));
		}
		
	}
	
	public void delete()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss.getAttribute("branchId");
		Connection conn=DataBaseConnection.JAVAConnection();
		int i=new DataBaseMethods().deleteFromDsaCommissionTableById(selectedRow.getId());
		if(i>0)
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Delete Sucessfully."));
			dlist=new DataBaseMethods().dsaCommissionListDateWise(branchId,dsaCode, startDate, endDate, searchType,conn);
			if(dlist.size()>0)
			{
				show=true;
			}
			else
			{
				show=false;
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("No Record Found."));
			}
			try {
				conn.close();
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
		else
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Some Error Occured!!!"));
		}
	}

	public ArrayList<SelectItem> getDsaList() {
		return dsaList;
	}

	public void setDsaList(ArrayList<SelectItem> dsaList) {
		this.dsaList = dsaList;
	}

	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 getDsaCode() {
		return dsaCode;
	}

	public void setDsaCode(String dsaCode) {
		this.dsaCode = dsaCode;
	}

	public String getSearchType() {
		return searchType;
	}

	public void setSearchType(String searchType) {
		this.searchType = searchType;
	}

	public boolean isShow() {
		return show;
	}

	public void setShow(boolean show) {
		this.show = show;
	}

	public ArrayList<DsaListInfo> getDlist() {
		return dlist;
	}

	public void setDlist(ArrayList<DsaListInfo> dlist) {
		this.dlist = dlist;
	}

	public ArrayList<SelectItem> getTypeList() {
		return typeList;
	}

	public void setTypeList(ArrayList<SelectItem> typeList) {
		this.typeList = typeList;
	}

	public String getDsaName() {
		return dsaName;
	}

	public void setDsaName(String dsaName) {
		this.dsaName = dsaName;
	}

	public DsaListInfo getSelectedRow() {
		return selectedRow;
	}

	public void setSelectedRow(DsaListInfo selectedRow) {
		this.selectedRow = selectedRow;
	}

	public double getTotalAmount() {
		return totalAmount;
	}

	public void setTotalAmount(double totalAmount) {
		this.totalAmount = totalAmount;
	}
	
}
