package other_package;

import java.io.IOException;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import main_package.DataBaseConnection;
import main_package.DataBaseMethods;
import main_package.UserInfo;

@ManagedBean(name="parSaving")
@ViewScoped
public class ParticularSavingAccountBean implements Serializable 
{
	private static final long serialVersionUID = 1L;
	String shareId,name,contact;
	ArrayList<UserInfo> list=new ArrayList<>();
	UserInfo selectedUser;
	
	public ParticularSavingAccountBean()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		shareId=(String) ss.getAttribute("shareId");
		list=new DataBaseMethodsOther().allSavingAccountBean(shareId);
		
		Connection conn=DataBaseConnection.JAVAConnection();
		UserInfo info=new DataBaseMethods().userInfoByShareHolderNo(shareId, conn);
		name=info.getFirst_applicantName();
		contact=info.getContactNo();
		
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	// This Method Shows the Detail of Selected Saving Account No.
	public void viewDetails() throws IOException
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		ss.setAttribute("selectedUser", selectedUser);
		ss.setAttribute("pageName","particularSaveAccount.xhtml");
		FacesContext.getCurrentInstance().getExternalContext().redirect("viewSavingAccount.xhtml");
	}
	
	// This Method Return on Deposit Page For Deposit Amount in Selected Saving Account No.
	public void depositAmount() throws IOException
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		ss.setAttribute("selectedUser", selectedUser);
		ss.setAttribute("pageName","particularSaveAccount");
		FacesContext.getCurrentInstance().getExternalContext().redirect("userDeposit.xhtml");
	}
	
	// This Method Return on Withdraw Page For Withdraw Amount in Selected Saving Account No.
	public void withdrawalAmount() throws IOException
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		ss.setAttribute("selectedUserWithdraw", selectedUser);
		ss.setAttribute("pageName","particularSaveAccount");
		FacesContext.getCurrentInstance().getExternalContext().redirect("userWithdrawl.xhtml");
	}
	
	// This Method Return on Mini-Statement Page where We Can Check Saving Account Statement.
	public void miniStatement() throws IOException
	{
		Date startDate=new Date();
		Date endDate=new Date();
		startDate.setDate(1);
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		ss.setAttribute("selectedUser", selectedUser);
		ss.setAttribute("pageName","particularSaveAccount");
		ss.setAttribute("svStartDate", startDate);
		ss.setAttribute("svEndDate", endDate);
		
		FacesContext.getCurrentInstance().getExternalContext().redirect("miniStatement.xhtml");
	}
	
	public ArrayList<UserInfo> getList() {
		return list;
	}
	public void setList(ArrayList<UserInfo> list) {
		this.list = list;
	}
	public UserInfo getSelectedUser() {
		return selectedUser;
	}
	public void setSelectedUser(UserInfo selectedUser) {
		this.selectedUser = selectedUser;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getContact() {
		return contact;
	}
	public void setContact(String contact) {
		this.contact = contact;
	}
}
