package main_package;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
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;

@ManagedBean(name="nachMandateRegForm")
@ViewScoped
public class NachMandateRegFormBean implements Serializable
{
	public List<String> list;
	String shareHolderNo, utilityCode, mandateType, umrnNo, accountNo, accountType, ifscCode, bankName, collectionType, reference1, reference2;
	String frequency, untillCancel="Y", name, mobileNo, phoneNo, mailId;
	int amount;
	Date mandateDate, debitStartDate, debitEndDate;
	UserInfo selectedUser;
	boolean show;
	DataBaseMethods dbm=new DataBaseMethods();
	
	public NachMandateRegFormBean() 
	{
		Connection conn=DataBaseConnection.JAVAConnection();
		reference1=dbm.checkSMessageApiDetailFromTable("nach_no","", conn);
		
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public List<String> completeText(String name)
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss.getAttribute("branchId");
		list=new DataBaseAutoCompAndDuplicate().shareHolderAllNo(branchId,name);
     	return list;
	}
	
	public void shareHolderDetails()
	{
		Connection conn=DataBaseConnection.JAVAConnection();

		if(shareHolderNo.contains("/"))
		{
			shareHolderNo=shareHolderNo.substring(0,shareHolderNo.indexOf("/"));
		}
		selectedUser=dbm.userInfoByShareHolderNo(shareHolderNo, conn);
		if(selectedUser!=null)
		{
			boolean check=dbm.checkNachDetailByShareHolderNo(shareHolderNo, conn);
			if(check==true)
			{
				show=false;
				FacesContext fc=FacesContext.getCurrentInstance();
				fc.addMessage(null, new FacesMessage(selectedUser.getFirst_applicantName()+" NACH Mandate Detail Already Added. Please Visit Manage Mandate Reg. Detail."));
			}
			else
			{
				show=true;
				bankName=selectedUser.getBank_name();
				accountNo=selectedUser.getBank_accountNo();
				name=selectedUser.getFirst_applicantName();
				mobileNo=selectedUser.getContactNo();
				reference2=selectedUser.getShareHolderNo();
			}
		}
		else
		{
			show=false;
			FacesContext fc=FacesContext.getCurrentInstance();
			fc.addMessage(null, new FacesMessage("Invalid Share Holder No"));
		}
		
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public String addDetail()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss.getAttribute("branchId");
		if(untillCancel.equalsIgnoreCase("Y"))
		{
			debitEndDate=null;
		}
		int i=dbm.addNachMandateRegDetail(utilityCode, mandateType, umrnNo, mandateDate, accountNo, accountType, ifscCode, bankName, collectionType, amount, reference1, 
				reference2, frequency, debitStartDate, debitEndDate, untillCancel, 	name, phoneNo, mobileNo, mailId, shareHolderNo, dbm.userId(),branchId);
		if(i>0)
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Mandate Detail of "+name+" is Added Successfully"));
			return "nachMandateRegForm";
		}
		else
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Error Occured!!!"));
			return "";
		}
	}
	
	
	
	public List<String> getList() {
		return list;
	}

	public void setList(List<String> list) {
		this.list = list;
	}

	public String getShareHolderNo() {
		return shareHolderNo;
	}

	public void setShareHolderNo(String shareHolderNo) {
		this.shareHolderNo = shareHolderNo;
	}

	public String getUtilityCode() {
		return utilityCode;
	}

	public void setUtilityCode(String utilityCode) {
		this.utilityCode = utilityCode;
	}

	public String getFrequency() {
		return frequency;
	}

	public void setFrequency(String frequency) {
		this.frequency = frequency;
	}

	public String getUntillCancel() {
		return untillCancel;
	}

	public void setUntillCancel(String untillCancel) {
		this.untillCancel = untillCancel;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getMobileNo() {
		return mobileNo;
	}

	public void setMobileNo(String mobileNo) {
		this.mobileNo = mobileNo;
	}

	public String getPhoneNo() {
		return phoneNo;
	}

	public void setPhoneNo(String phoneNo) {
		this.phoneNo = phoneNo;
	}

	public String getMailId() {
		return mailId;
	}

	public void setMailId(String mailId) {
		this.mailId = mailId;
	}

	public int getAmount() {
		return amount;
	}

	public void setAmount(int amount) {
		this.amount = amount;
	}

	public Date getMandateDate() {
		return mandateDate;
	}

	public void setMandateDate(Date mandateDate) {
		this.mandateDate = mandateDate;
	}

	public Date getDebitStartDate() {
		return debitStartDate;
	}

	public void setDebitStartDate(Date debitStartDate) {
		this.debitStartDate = debitStartDate;
	}

	public Date getDebitEndDate() {
		return debitEndDate;
	}

	public void setDebitEndDate(Date debitEndDate) {
		this.debitEndDate = debitEndDate;
	}

	public UserInfo getSelectedUser() {
		return selectedUser;
	}

	public void setSelectedUser(UserInfo selectedUser) {
		this.selectedUser = selectedUser;
	}

	public boolean isShow() {
		return show;
	}

	public void setShow(boolean show) {
		this.show = show;
	}

	public String getReference1() {
		return reference1;
	}

	public void setReference1(String reference1) {
		this.reference1 = reference1;
	}

	public String getReference2() {
		return reference2;
	}

	public void setReference2(String reference2) {
		this.reference2 = reference2;
	}

	public String getMandateType() {
		return mandateType;
	}

	public void setMandateType(String mandateType) {
		this.mandateType = mandateType;
	}

	public String getUmrnNo() {
		return umrnNo;
	}

	public void setUmrnNo(String umrnNo) {
		this.umrnNo = umrnNo;
	}

	public String getAccountNo() {
		return accountNo;
	}

	public void setAccountNo(String accountNo) {
		this.accountNo = accountNo;
	}

	public String getAccountType() {
		return accountType;
	}

	public void setAccountType(String accountType) {
		this.accountType = accountType;
	}

	public String getIfscCode() {
		return ifscCode;
	}

	public void setIfscCode(String ifscCode) {
		this.ifscCode = ifscCode;
	}

	public String getBankName() {
		return bankName;
	}

	public void setBankName(String bankName) {
		this.bankName = bankName;
	}

	public String getCollectionType() {
		return collectionType;
	}

	public void setCollectionType(String collectionType) {
		this.collectionType = collectionType;
	}
	
}
