package main_package;

import java.io.Serializable;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

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;

@ManagedBean(name = "dsawise")
@ViewScoped

public class DsaWiseCollectionBean implements Serializable
{
	private static final long serialVersionUID = 1L;
	
	int branchId;
	String branchUserId, reportType, loanType, listType, systemType, selectedUser=null,  webType;
	String dsaCode, userName;
	Double totalAmount;
	
	String userid;
	boolean show;
	
	
	Date startDate=new Date(), endDate=new Date();
	
	
    ArrayList<dsaWiseInfo> list;
	ArrayList<SelectItem> loanTypeList, dsaList, centerList;
	//new list select menu
	private ArrayList<SelectItem> appUserList;
	private ArrayList<SelectItem> webUserList;
	
	
	DataBaseMethods obj=new DataBaseMethods(); 
	DataBaseMethodsRaviK dbr = new DataBaseMethodsRaviK();

	

	public DsaWiseCollectionBean() 
	{	
		
		Connection conn = DataBaseConnection.JAVAConnection();
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		branchUserId = (String) ss.getAttribute("branchUserId");
		branchId = (int) ss.getAttribute("branchId");		
		appUserList = dbr.userAppList(branchId);
		
	}	
	
	
	
	
	public void search()
	{
	    HttpSession ss = (HttpSession) FacesContext.getCurrentInstance()
	                   .getExternalContext()
	                   .getSession(false);

	    Connection conn = DataBaseConnection.JAVAConnection();

	    int branchId = (int) ss.getAttribute("branchId");
	    
	    try
	    {
	        String uid = "";

	        if ("1".equalsIgnoreCase(systemType))  
	        {
	            if ("masterAdmin".equalsIgnoreCase(webType))
	            {
	                uid = dbr.userIdByuserType(webType, conn);
	            }
	            else
	            {
	                uid = selectedUser;
	            }

	            System.out.println("System Type  : " + systemType);
	            System.out.println("Web Type     : " + webType);
	            System.out.println("SelectedUser : " + selectedUser);
	            System.out.println("UID Passed   : " + uid);

	            list = dbr.webWiseCollection(startDate, endDate, reportType, webType, uid, branchId);
	            System.out.println("List Size in Bean = " + list.size());
	        }
	        else 
	        {
	            if (selectedUser != null && !selectedUser.trim().isEmpty())
	            {
	                userName = selectedUser;
	            }

	            list = dbr.appWiseCollection(startDate, endDate,
	                    reportType, userName, branchId);
	        }

	        if (list != null && !list.isEmpty())
	        {
	            totalAmount = 0.0;

	            for (dsaWiseInfo info : list)
	            {
	                totalAmount += info.getAmount();
	            }

	            show = true;

	            FacesContext.getCurrentInstance().addMessage(null,
	                    new FacesMessage(FacesMessage.SEVERITY_INFO,"Record Found Successfully", ""));
	        }
	        else
	        {
	            show = false;

	            FacesContext.getCurrentInstance().addMessage(null,
	                    new FacesMessage(FacesMessage.SEVERITY_WARN,"No Record Found", ""));
	        }
	    }
	    catch (Exception e)
	    {
	        e.printStackTrace();

	        show = false;

	        FacesContext.getCurrentInstance().addMessage(null,
	                new FacesMessage(FacesMessage.SEVERITY_ERROR,"Something went wrong", ""));
	    }
	    finally
	    {
	        try
	        {
	            if (conn != null)
	                conn.close();
	        }
	        catch (Exception e)
	        {
	            e.printStackTrace();
	        }
	    }
	}
	
	
	public void search2()
	{
	    HttpSession ss = (HttpSession) FacesContext.getCurrentInstance()
			           .getExternalContext()
			           .getSession(false);
	    
	    Connection conn = DataBaseConnection.JAVAConnection();
	    
        int branchId = (int) ss.getAttribute("branchId");
        System.out.println("Web Type : " + webType);
        System.out.println("Selected User : " + selectedUser);
        System.out.println("User Id : " + userid);
	    try
	    {   	
	    	String uid = "";
		   
	    	if("1".equalsIgnoreCase(systemType)) {
	    		
	    		if (!"masterAdmin".equalsIgnoreCase(webType)
	    		        && selectedUser != null
	    		        && !selectedUser.trim().isEmpty()) {
	    		           userName = selectedUser;}	
    			
	    		list = dbr.webWiseCollection(startDate, endDate, reportType, webType, userid, branchId);
	    		
	    	}
	    	else {
	    		if (selectedUser != null && !selectedUser.trim().isEmpty()) {
	    		    userName = selectedUser;}	 
	    		list = dbr.appWiseCollection(startDate, endDate, reportType, userName,  branchId);
			}
	    	
	        if (list != null && !list.isEmpty())
	        {	
	        	totalAmount = 0.0;

	            for (dsaWiseInfo info : list)
	            {
	                totalAmount += info.getAmount();
	            }

	            show = true;

	            FacesContext.getCurrentInstance().addMessage(null,
	                         new FacesMessage(FacesMessage.SEVERITY_INFO, "Record Found Successfully", ""));
	        }
	        else
	        {
	            show = false;

	            FacesContext.getCurrentInstance().addMessage(null,
	                         new FacesMessage(FacesMessage.SEVERITY_WARN, "No Record Found", ""));
	        }
	    }
	    catch (Exception e)
	    {
	        e.printStackTrace();

	        show = false;

	       FacesContext.getCurrentInstance().addMessage(null,
	                new FacesMessage(FacesMessage.SEVERITY_ERROR,
	                        "Something went wrong", ""));
	    }
}
	
	
	public void changeSystemType() {
	    selectedUser = null;
	 //   webType = null;
	   
	}

	
	//GETTER AND SETTER
	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public int getBranchId() {
		return branchId;
	}

	public void setBranchId(int branchId) {
		this.branchId = branchId;
	}

	public String getBranchUserId() {
		return branchUserId;
	}

	public void setBranchUserId(String branchUserId) {
		this.branchUserId = branchUserId;
	}

	public String getReportType() {
		return reportType;
	}

	public void setReportType(String reportType) {
		this.reportType = reportType;
	}

	public String getDsaCode() {
		return dsaCode;
	}

	public void setDsaCode(String dsaCode) {
		this.dsaCode = dsaCode;
	}

	public Boolean getShow() {
		return show;
	}

	public void setShow(Boolean show) {
		this.show = show;
	}

	

	public DataBaseMethodsRaviK getDbr() {
		return dbr;
	}

	public void setDbr(DataBaseMethodsRaviK dbr) {
		this.dbr = dbr;
	}

	public ArrayList<SelectItem> getLoanTypeList() {
		return loanTypeList;
	}

	public void setLoanTypeList(ArrayList<SelectItem> loanTypeList) {
		this.loanTypeList = loanTypeList;
	}

	public ArrayList<SelectItem> getDsaList() {
		return dsaList;
	}

	public void setDsaList(ArrayList<SelectItem> dsaList) {
		this.dsaList = dsaList;
	}

	public ArrayList<SelectItem> getCenterList() {
		return centerList;
	}

	public void setCenterList(ArrayList<SelectItem> centerList) {
		this.centerList = centerList;
	}

	public DataBaseMethods getObj() {
		return obj;
	}

	public void setObj(DataBaseMethods obj) {
		this.obj = obj;
	}

	public String getLoanType() {
		return loanType;
	}

	public void setLoanType(String loanType) {
		this.loanType = loanType;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public ArrayList<dsaWiseInfo> getList() {
		return list;
	}

	public void setList(ArrayList<dsaWiseInfo> list) {
		this.list = list;
	}



	public String getListType() {
		return listType;
	}



	public void setListType(String listType) {
		this.listType = listType;
	}



	public void setShow(boolean show) {
		this.show = show;
	}



	public String getSystemType() {
		return systemType;
	}



	public void setSystemType(String systemType) {
		this.systemType = systemType;
	}



	public String getSelectedUser() {
		return selectedUser;
	}



	public void setSelectedUser(String selectedUser) {
		this.selectedUser = selectedUser;
	}



	public String getUserName() {
		return userName;
	}



	public void setUserName(String userName) {
		this.userName = userName;
	}



	public Double getTotalAmount() {
		return totalAmount;
	}



	public void setTotalAmount(Double totalAmount) {
		this.totalAmount = totalAmount;
	}



	public String getWebType() {
		return webType;
	}



	public void setWebType(String webType) {
		this.webType = webType;
	}



	public String getUserid() {
		return userid;
	}



	public void setUserid(String userid) {
		this.userid = userid;
	}







	public ArrayList<SelectItem> getWebUserList() {
		return webUserList;
	}



	public void setWebUserList(ArrayList<SelectItem> webUserList) {
		this.webUserList = webUserList;
	}



	public ArrayList<SelectItem> getAppUserList() {
		return appUserList;
	}



	public void setAppUserList(ArrayList<SelectItem> appUserList) {
		this.appUserList = appUserList;
	}

}