package main_package;

import java.io.Serializable;
import java.sql.Connection;
import java.text.ParseException;
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;

@ManagedBean(name="payMultipleSavingDeposit")
@ViewScoped
public class PayMultipleSavingDepositBean implements Serializable
{
	DataBaseMethods dbm=new DataBaseMethods();
	ArrayList<SelectItem> centerList;
	ArrayList<SavingAccountInfo> svAccList, multiSelectList, confirmationList, paidList;
	String center, centerName;
	Date paidDate;
	double totalAmount;
	boolean show, showConfirmTable, showPaidTable;
	
	public PayMultipleSavingDepositBean() 
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss.getAttribute("branchId");
		Connection conn=DataBaseConnection.JAVAConnection();
		
		centerList=dbm.allcenter(branchId,"all",conn);
		paidDate=new Date();
		
		try {
			conn.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	public void svAccListByCenter()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss.getAttribute("branchId");
		Connection conn=DataBaseConnection.JAVAConnection();
		
		svAccList=dbm.savingAccountListByCenter(branchId,center,conn);
		if(svAccList.size()>0)
		{
			show=true;
			centerName=dbm.centerNameCodeById(center, conn);
		}
		else
		{
			show=false;
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("No Record Found!!! "));
		}
       
		try {
			conn.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	public void confirmation()
	{
		confirmationList= new ArrayList<>();
		int sno=1;
		showConfirmTable=true;
		totalAmount=0;
		for(int i=0;i<multiSelectList.size();i++)
		{
			totalAmount=totalAmount+multiSelectList.get(i).getAmount();
			if(multiSelectList.get(i).getAmount()>0)
			{  
				multiSelectList.get(i).setSno(sno++);
				confirmationList.add(multiSelectList.get(i));
			}
		}
	}
	
	public void insertAmount()
	{
		Connection conn =DataBaseConnection.JAVAConnection();
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId =(int) ss.getAttribute("branchId");
		
		paidList=new ArrayList<>();
		for(SavingAccountInfo ff: confirmationList)
		{
			int	n = dbm.userDeposit(branchId,ff.getAccountNo(), String.valueOf(ff.getAmount()), paidDate,"Cash", "", paidDate,
					"","approved","N/A","Saving Amount",ff.getDsacode());
			if (n >= 1) 
			{
				double preAmount=dbm.acconuntHolderAmountByAccountNo(ff.getAccountNo());
				Double newAmount=preAmount+ff.getAmount();
				dbm.updateSavingAmountByAccountNo(ff.getAccountNo(),newAmount);
				dbm.increaseCompanyCapitalAmount(branchId,ff.getAmount(),conn);
				
				paidList.add(ff);
			}
		}
		
		showConfirmTable=false;
		showPaidTable=true;
		show=false;
		
		int k=1;
		for (SavingAccountInfo mm :paidList)
		{
			mm.setSno(k++);
			totalAmount +=mm.getAmount();
		}
		
		try {
			conn.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	public ArrayList<SelectItem> getCenterList() {
		return centerList;
	}

	public void setCenterList(ArrayList<SelectItem> centerList) {
		this.centerList = centerList;
	}

	public ArrayList<SavingAccountInfo> getSvAccList() {
		return svAccList;
	}

	public void setSvAccList(ArrayList<SavingAccountInfo> svAccList) {
		this.svAccList = svAccList;
	}

	public String getCenter() {
		return center;
	}

	public void setCenter(String center) {
		this.center = center;
	}

	public Date getPaidDate() {
		return paidDate;
	}

	public void setPaidDate(Date paidDate) {
		this.paidDate = paidDate;
	}

	public boolean isShow() {
		return show;
	}

	public void setShow(boolean show) {
		this.show = show;
	}

	public ArrayList<SavingAccountInfo> getMultiSelectList() {
		return multiSelectList;
	}

	public void setMultiSelectList(ArrayList<SavingAccountInfo> multiSelectList) {
		this.multiSelectList = multiSelectList;
	}

	public double getTotalAmount() {
		return totalAmount;
	}

	public void setTotalAmount(double totalAmount) {
		this.totalAmount = totalAmount;
	}

	public String getCenterName() {
		return centerName;
	}

	public void setCenterName(String centerName) {
		this.centerName = centerName;
	}

	public ArrayList<SavingAccountInfo> getConfirmationList() {
		return confirmationList;
	}

	public void setConfirmationList(ArrayList<SavingAccountInfo> confirmationList) {
		this.confirmationList = confirmationList;
	}

	public ArrayList<SavingAccountInfo> getPaidList() {
		return paidList;
	}

	public void setPaidList(ArrayList<SavingAccountInfo> paidList) {
		this.paidList = paidList;
	}

	public boolean isShowConfirmTable() {
		return showConfirmTable;
	}

	public void setShowConfirmTable(boolean showConfirmTable) {
		this.showConfirmTable = showConfirmTable;
	}

	public boolean isShowPaidTable() {
		return showPaidTable;
	}

	public void setShowPaidTable(boolean showPaidTable) {
		this.showPaidTable = showPaidTable;
	}
}
