package main_package;

import java.io.Serializable;
import java.sql.Connection;
import java.text.SimpleDateFormat;
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.servlet.http.HttpSession;

import org.primefaces.context.RequestContext;

import other_package.DataBaseMethodsOther;
import other_package.ReportInfo;

@ManagedBean(name="manageSavingAccountTransaction")
@ViewScoped
public class ManageSavingAccountTransactionBean implements Serializable
{
	boolean show;
	String searchType, sDate, eDate, searchMode, accno;
	ArrayList<UserInfo> finalList;
	ArrayList<ReportInfo> rlist;
	ReportInfo selectedData;
	UserInfo selectedRow;
	boolean showIndv, showAll, showIndvList;
	int totalEntry;
	double totalAmount;
	List<String> list;
	DataBaseMethods obj=new DataBaseMethods();
	
	public void searchReport()
	{
		if(searchMode.equalsIgnoreCase("all"))
		{
			searchData();
			show=true;
			showIndvList=false;
		}
		else
		{
			if(accno.contains("/"))
			{
				accno=accno.substring(0,accno.indexOf("/"));
			}
			rlist=new DataBaseMethodsOther().savingAccountDepositEntry(accno);
			totalEntry=rlist.size();
			totalAmount=0;
			if(totalEntry>0)
			{
				for(ReportInfo rr: rlist)
				{
					totalAmount+=rr.getDebitAmount();
				}
			}
			show=false;
			showIndvList=true;
		}
	}
	
	public void checkOptions()
	{
		if(searchMode.equalsIgnoreCase("all"))
		{
			showIndv=false;
			showAll=true;
		}
		else
		{
			showIndv=true;
			showAll=false;
		}
	}
	
	public void printVoucherDetail()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		ss.setAttribute("voucherNo", "");
		ss.setAttribute("expDate", selectedRow.getEntryDate());
		ss.setAttribute("amount", String.valueOf(selectedRow.getAmount()));
		if(searchType.equals("withdrwal"))
		{
			ss.setAttribute("vchtype", "Debit");
		}
		else
		{
			ss.setAttribute("vchtype", "Credit");
		}
		ss.setAttribute("payTo", selectedRow.getAccountNo()+" - "+selectedRow.getFirst_applicantName());
		ss.setAttribute("being", "Saving "+searchType);
		ss.setAttribute("payMode", selectedRow.getPayMode());
		
		RequestContext.getCurrentInstance().execute("window.open('printVoucher.xhtml')");

	}
			
	private void searchData() 
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId =(int) ss.getAttribute("branchId");
		Date dt=new Date();
		int month=dt.getMonth();
		int year=dt.getYear()+1900;
		if(month>=0 && month<=2)
		{
			sDate=year+"-01-01";
			eDate=year+"-03-31";
		}
		else if(month>=3 && month<=5)
		{
			sDate=year+"-04-01";
			eDate=year+"-06-30";
		}
		else if(month>=6 && month<=8)
		{
			sDate=year+"-07-01";
			eDate=year+"-09-30";
		}
		else if(month>=9 && month<=11)
		{
			sDate=year+"-10-01";
			eDate=year+"-12-31";
		}
		
		finalList=obj.userSavingAccountTransactionList(branchId,searchType, sDate, eDate);
		totalAmount=0;
		if(finalList.size()>0)
		{
			for(UserInfo uu:finalList)
			{
				totalAmount+=uu.getAmount();
			}
			show=true;
		}
		totalEntry=finalList.size();
	}
	
	public List<String> completeText(String name)
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId =(int) ss.getAttribute("branchId");
		list=new DataBaseAutoCompAndDuplicate().userAccountNoForSavingAccount(branchId,name, obj.userId());
     	return list;
	}

	public void printReport()
	{
		
	}
	
	public void deleteDataIndividual()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId =(int) ss.getAttribute("branchId");
		Connection conn=DataBaseConnection.JAVAConnection();
		double cashBalance=obj.remainingCompanyCapitalAmount(branchId,conn);
		double preAmount=obj.acconuntHolderAmountByAccountNo(accno);
		//System.out.println(cashBalance+"=="+preAmount+"==="+accno);
		if(preAmount>=selectedData.getDebitAmount())
		{
			if(selectedData.getPayMode().equalsIgnoreCase("Cash"))
		    {	
				if(cashBalance>=selectedData.getDebitAmount())
				{
					int i=obj.updateUserSavingAccountTransactionStatus(selectedData.getId(), "deposit");
					if(i>0)
					{
						FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Transaction Delete Sucessfully"));
						Double newAmount=preAmount-selectedData.getDebitAmount();
						obj.updateSavingAmountByAccountNo(accno,newAmount);
						obj.increaseSharedCompanyAmount(branchId,selectedData.getDebitAmount(),conn);
						searchReport();
					}
					else
					{
						FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Error Occured!!!"));
					}
				}
		    }	
		}
	}
	
	public void deleteData()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId =(int) ss.getAttribute("branchId");
		Connection conn=DataBaseConnection.JAVAConnection();
		double cashBalance=obj.remainingCompanyCapitalAmount(branchId,conn);
		double preAmount=obj.acconuntHolderAmountByAccountNo(selectedRow.getAccountNo());
		if(searchType.equalsIgnoreCase("deposit"))
		{
			if(preAmount>=selectedRow.getAmount())
			{
				if(selectedRow.getPayMode().equalsIgnoreCase("Cash"))
			    {	
					if(cashBalance>=selectedRow.getAmount())
					{
						int i=obj.updateUserSavingAccountTransactionStatus(selectedRow.getId(), searchType);
						if(i>0)
						{
							FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Transaction Delete Sucessfully"));
							Double newAmount=preAmount-selectedRow.getAmount();
							obj.updateSavingAmountByAccountNo(selectedRow.getAccountNo(),newAmount);
							obj.increaseSharedCompanyAmount(branchId,selectedRow.getAmount(),conn);
							searchData();
						}
						else
						{
							FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Error Occured!!!"));
						}
					}
			    }	
			}
		}
		else
		{
			if(selectedRow.getPayMode().equalsIgnoreCase("Cash"))
		    {	
				int i=obj.updateUserSavingAccountTransactionStatus(selectedRow.getId(), searchType);
				if(i>0)
				{
					FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Transaction Delete Sucessfully"));
					Double newAmount=preAmount+selectedRow.getAmount();
					obj.updateSavingAmountByAccountNo(selectedRow.getAccountNo(),newAmount);
					obj.increaseCompanyCapitalAmount(branchId,selectedRow.getAmount(),conn);
					searchData();	
				}
				else
				{
					FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Error Occured!!!"));
				}
		    }	
		}
		
	}

	public boolean isShow() {
		return show;
	}

	public void setShow(boolean show) {
		this.show = show;
	}

	public String getSearchType() {
		return searchType;
	}

	public void setSearchType(String searchType) {
		this.searchType = searchType;
	}

	public ArrayList<UserInfo> getFinalList() {
		return finalList;
	}

	public void setFinalList(ArrayList<UserInfo> finalList) {
		this.finalList = finalList;
	}

	
	public int getTotalEntry() {
		return totalEntry;
	}

	public void setTotalEntry(int totalEntry) {
		this.totalEntry = totalEntry;
	}

	public double getTotalAmount() {
		return totalAmount;
	}

	public void setTotalAmount(double totalAmount) {
		this.totalAmount = totalAmount;
	}

	public UserInfo getSelectedRow() {
		return selectedRow;
	}

	public void setSelectedRow(UserInfo selectedRow) {
		this.selectedRow = selectedRow;
	}

	public String getsDate() {
		return sDate;
	}

	public void setsDate(String sDate) {
		this.sDate = sDate;
	}

	public String geteDate() {
		return eDate;
	}

	public void seteDate(String eDate) {
		this.eDate = eDate;
	}

	public String getSearchMode() {
		return searchMode;
	}

	public void setSearchMode(String searchMode) {
		this.searchMode = searchMode;
	}

	public boolean isShowIndv() {
		return showIndv;
	}

	public void setShowIndv(boolean showIndv) {
		this.showIndv = showIndv;
	}

	public boolean isShowAll() {
		return showAll;
	}

	public void setShowAll(boolean showAll) {
		this.showAll = showAll;
	}

	public String getAccno() {
		return accno;
	}

	public void setAccno(String accno) {
		this.accno = accno;
	}

	public List<String> getList() {
		return list;
	}

	public void setList(List<String> list) {
		this.list = list;
	}

	public ReportInfo getSelectedData() {
		return selectedData;
	}

	public void setSelectedData(ReportInfo selectedData) {
		this.selectedData = selectedData;
	}

	public ArrayList<ReportInfo> getRlist() {
		return rlist;
	}

	public void setRlist(ArrayList<ReportInfo> rlist) {
		this.rlist = rlist;
	}

	public boolean isShowIndvList() {
		return showIndvList;
	}

	public void setShowIndvList(boolean showIndvList) {
		this.showIndvList = showIndvList;
	}
	
}
