package main_package;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;

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;

import org.primefaces.context.RequestContext;

@ManagedBean(name="update")
@ViewScoped
public class EditViewDeleteUserBean implements Serializable 
{
	DataBaseMethods obj=new DataBaseMethods();
	private static final long serialVersionUID = 1L;
	String id, searchDeposit;
	ArrayList<UserInfo> list,matureList,returnList, multiSelectList;
	UserInfo selectedUser;
	String searchType, dsaCode, newDsaCode, accountStatus;
	boolean show;
	ArrayList<SelectItem>  dsaList, depositNoList;

	public EditViewDeleteUserBean() 
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		 int branchId = (int) ss.getAttribute("branchId");
		Connection conn=DataBaseConnection.JAVAConnection();
		dsaList=obj.allDsaList(branchId,"",conn);
		
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public void depositNoListMethod()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		 int branchId = (int) ss.getAttribute("branchId");
		depositNoList=obj.depositNoListSchemeWise(searchType, branchId);
	}
	
	public void searchData()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		 int branchId = (int) ss.getAttribute("branchId");
		list=obj.showUserSelectedType(searchType, dsaCode,accountStatus, searchDeposit,branchId);
		//matureList=obj.showMatureScheme(searchType);
		//returnList= obj.showReturnScheme(searchType);
		if(list.size()>0)
		{
			show=true;
		}
		else
		{
			show=false;
			FacesContext fc=FacesContext.getCurrentInstance();
			fc.addMessage(null, new FacesMessage("No Record found..."));
		}
	}
	
	public void updateDsa()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		 int branchId = (int) ss.getAttribute("branchId");
		if(multiSelectList.size()>0)
		{
			int count=0;
			Connection conn=DataBaseConnection.JAVAConnection();
			for(UserInfo uu: multiSelectList)
			{
				if(!uu.getDsaCode().equals(newDsaCode))
				{
					count+=obj.updateDsaInDepositAccount(uu.getId(), newDsaCode, conn);
				}
			}
			
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(count+" Account Has been Successfully Tranfer in the Code of "+newDsaCode));
			list=obj.showUserSelectedType(searchType, dsaCode,accountStatus, searchDeposit,branchId);
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Dear User, Please Select at least one deposit Account for Transfer"));
		}
	}
	
	public void editDetails()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		ss.setAttribute("selected", selectedUser);
		
		RequestContext.getCurrentInstance().execute("window.open('editUserInfo.xhtml')");
	}
	
	public void viewDetails()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		ss.setAttribute("selectedUser", selectedUser);
		ss.setAttribute("pageName","editViewDeleteUser.xhtml");
		
		RequestContext.getCurrentInstance().execute("window.open('viewUserInfo.xhtml')");
	}
	
	public void printDetails()
	{
		String arr[]=obj.firstReceiptNoOfRdFd(selectedUser.getUser_no()).split("<`>"); 
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		ss.setAttribute("shareHolderNo", selectedUser.getShareHolderNo());
		ss.setAttribute("frequency",selectedUser.getFrequency());
		ss.setAttribute("office_application_no",selectedUser.getOffice_application_no());
		ss.setAttribute("scheme_type",selectedUser.getScheme_type());
		ss.setAttribute("rd_fdNo",selectedUser.getUser_no());
		ss.setAttribute("maturityDate",selectedUser.getStrMatDate());
		ss.setAttribute("date",selectedUser.getOffice_date());
		ss.setAttribute("issue_date",selectedUser.getOffice_date());
		if(selectedUser.getScheme_type().equalsIgnoreCase("MIS"))
		{
			ss.setAttribute("maturity_value",selectedUser.getInstallment_amount());
		}
		else
		{
			ss.setAttribute("maturity_value",selectedUser.getMaturity_value());
		}
		ss.setAttribute("receipt_no",arr[0]);
		ss.setAttribute("installment_amount",selectedUser.getInstallment_amount());
		ss.setAttribute("plan_no",selectedUser.getPlan_no());
		ss.setAttribute("id",Integer.parseInt(arr[1]));
		RequestContext.getCurrentInstance().execute("window.open('printRD_FDForm.xhtml')");
		RequestContext.getCurrentInstance().execute("window.open('printRD_FDForm2.xhtml')");
		
		HeaderBean hh=new HeaderBean();
		if(hh.getName().equalsIgnoreCase("JANJANI NIDHI LIMITED"))
		{
			RequestContext.getCurrentInstance().execute("window.open('printRD_FDForm2_janjani1.xhtml')");
			RequestContext.getCurrentInstance().execute("window.open('printRD_FDForm2_janjani2.xhtml')");
		}
		else if(hh.getName().equalsIgnoreCase("DEVKRIPA MICROFINANCE FOUNDATION"))
		{
			RequestContext.getCurrentInstance().execute("window.open('printRD_FDFormGolden.xhtml')");
		}
	}
	
	public void delete()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		 int branchId = (int) ss.getAttribute("branchId");
		if(selectedUser.getPaidAmount()>0)
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Deposit No. "+selectedUser.getUser_no() +" Can't Delete. "+selectedUser.getPaidAmount()+" Rs. is Deposit in this Deposit No. "+selectedUser.getUser_no()+" Name: "+selectedUser.getFirst_applicantName()));
		}
		else
		{
			int i=obj.deletePolicy(selectedUser.getUser_no());
			if(i>0)
			{
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Policy No. "+selectedUser.getUser_no()+" Has Been Delete Sucessfully."));
				list=obj.showUserSelectedType(searchType,dsaCode,accountStatus, searchDeposit,branchId);
			}
			else
			{
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(""));
			}
		}
	}
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	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 getSearchType() {
		return searchType;
	}

	public void setSearchType(String searchType) {
		this.searchType = searchType;
	}

	public boolean isShow() {
		return show;
	}

	public void setShow(boolean show) {
		this.show = show;
	}

	public ArrayList<UserInfo> getMatureList() {
		return matureList;
	}

	public void setMatureList(ArrayList<UserInfo> matureList) {
		this.matureList = matureList;
	}

	public ArrayList<UserInfo> getReturnList() {
		return returnList;
	}

	public void setReturnList(ArrayList<UserInfo> returnList) {
		this.returnList = returnList;
	}

	public String getDsaCode() {
		return dsaCode;
	}

	public void setDsaCode(String dsaCode) {
		this.dsaCode = dsaCode;
	}

	public ArrayList<SelectItem> getDsaList() {
		return dsaList;
	}

	public void setDsaList(ArrayList<SelectItem> dsaList) {
		this.dsaList = dsaList;
	}

	public ArrayList<UserInfo> getMultiSelectList() {
		return multiSelectList;
	}

	public void setMultiSelectList(ArrayList<UserInfo> multiSelectList) {
		this.multiSelectList = multiSelectList;
	}

	public String getNewDsaCode() {
		return newDsaCode;
	}

	public void setNewDsaCode(String newDsaCode) {
		this.newDsaCode = newDsaCode;
	}

	public String getSearchDeposit() {
		return searchDeposit;
	}

	public void setSearchDeposit(String searchDeposit) {
		this.searchDeposit = searchDeposit;
	}

	public ArrayList<SelectItem> getDepositNoList() {
		return depositNoList;
	}

	public void setDepositNoList(ArrayList<SelectItem> depositNoList) {
		this.depositNoList = depositNoList;
	}

	public String getAccountStatus() {
		return accountStatus;
	}

	public void setAccountStatus(String accountStatus) {
		this.accountStatus = accountStatus;
	}
	
}
