package main_package;

import java.io.Serializable;
import java.sql.Connection;
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.servlet.http.HttpSession;

@ManagedBean(name = "ptpReport")
@ViewScoped
public class PTPHistoryBean implements Serializable{
		
	 int branchId;
	 String ptpMemberNo, ptpLoanNo, ptpRemarks,ptpEmiNo,ptpCreatedBy,status,userId;
	 Date startDate=new Date();
	 Date endDate=new Date();
	 Date ptpDate;
	 boolean show;
	 ArrayList<PTPInfo> ptpList,ptpNewList;
	 DataBaseMethods obj = new DataBaseMethods();
	 DataBaseMethodsRaviK dbrk = new DataBaseMethodsRaviK();
	 
	 
	 //for new records
	 private int selectedPtpId;
	 private double newPtpAmount;
	 private Date newPtpDate;
	 private Date oldPtpDate;
	 private String newPtpRemarks,newptpStatus;
	 boolean newPtpCreated;
	 
	 boolean keptshow,brokenshow;
	 
	 public PTPHistoryBean() {
		 HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		 branchId = (int) ss.getAttribute("branchId");
		 userId = obj.userId();
		 status = "pending";
		 search();
		 
	 }
	 
		public void search() {
				show = true;
				keptshow = false;
				brokenshow = false;	
				Connection conn = DataBaseConnection.JAVAConnection();
				try {
	
				switch (status) {
					case "Kept":
						keptshow=true;
						break;
					case "Broken":
						brokenshow=true;
						break;
					default:
						System.out.println("this is invalid approach");
						break;
					}
					
					
					ptpList = dbrk.showPtpReport(startDate, endDate, status, branchId, conn);
					System.out.println("this is size of list "+ptpList.size());

				} catch (Exception e) {

					e.printStackTrace();

				} finally {

					try {
						if (conn != null) {
							conn.close();
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
		}
		
		public void prepareNewPtp(PTPInfo selectedPtp) {

			if (selectedPtp == null) {
				return;
			}
			if (!"Broken".equalsIgnoreCase(selectedPtp.getPtpStatus())) {
				return;
			}

			selectedPtpId = selectedPtp.getId();

			ptpMemberNo = selectedPtp.getShareholderNo();
			ptpLoanNo = selectedPtp.getLoanNo();
			ptpEmiNo = selectedPtp.getPtpEmiNo();

			ptpEmiAmt = selectedPtp.getAmount();
			ptpAmount = selectedPtp.getPtpAmount();
			newptpStatus = selectedPtp.getPtpStatus();
			oldPtpDate = selectedPtp.getPtpDate();

			newPtpDate = null;
			newPtpAmount = ptpAmount;
			newPtpRemarks = "";
			
			 System.out.println("========== SET VALUES ==========");
			    System.out.println("Selected PTP ID  : " + selectedPtpId);
			    System.out.println("Member No        : " + ptpMemberNo);
			    System.out.println("Loan No          : " + ptpLoanNo);
			    System.out.println("EMI No           : " + ptpEmiNo);
			    System.out.println("EMI Amount       : " + ptpEmiAmt);
			    System.out.println("PTP Amount       : " + ptpAmount);
			    System.out.println("New PTP Status   : " + newptpStatus);
			    System.out.println("Old PTP Date     : " + oldPtpDate);
			    System.out.println("New PTP Date     : " + newPtpDate);
			    System.out.println("New PTP Amount   : " + newPtpAmount);
			    System.out.println("New PTP Remarks  : [" + newPtpRemarks + "]");

		}
		
		

		public void saveNewPtp() {
			Connection conn = null;
			try {
				conn = DataBaseConnection.JAVAConnection();

				int result = dbrk.addPtpRecordForBroken(selectedPtpId,ptpMemberNo, ptpLoanNo, ptpEmiAmt, newPtpAmount, oldPtpDate,
						     ptpEmiNo, newPtpDate, newptpStatus, newPtpRemarks, userId, true, branchId, conn);

				if (result > 0) {			
					search();
					FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,
							 "New PTP record created successfully.", "Success"));

				} else {

					FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,
							"New PTP record could not be created.", "Error"));
				}

				org.primefaces.context.RequestContext.getCurrentInstance().addCallbackParam("ptpSaved", result > 0);

			} catch (Exception e) {
				e.printStackTrace();

				org.primefaces.context.RequestContext.getCurrentInstance().addCallbackParam("ptpSaved", false);
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,
						"An error occurred while creating the new PTP.", "Error"));

			} finally {
				try {
					if (conn != null)
						conn.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}

//		public boolean checkbutton() {
//			boolean buttonDisable;
//			try {
//				
//			} catch (Exception e) {
//				// TODO: handle exception
//			}
//			return ;
//		}
		
		
		
		
	 
	 //Getter and Setter
	 public String getPtpMemberNo() {
		return ptpMemberNo;
	}
	public void setPtpMemberNo(String ptpMemberNo) {
		this.ptpMemberNo = ptpMemberNo;
	}
	public String getPtpLoanNo() {
		return ptpLoanNo;
	}
	public void setPtpLoanNo(String ptpLoanNo) {
		this.ptpLoanNo = ptpLoanNo;
	}
	public String getPtpRemarks() {
		return ptpRemarks;
	}
	public void setPtpRemarks(String ptpRemarks) {
		this.ptpRemarks = ptpRemarks;
	}
	public String getPtpEmiNo() {
		return ptpEmiNo;
	}
	public void setPtpEmiNo(String ptpEmiNo) {
		this.ptpEmiNo = ptpEmiNo;
	}
	public String getPtpCreatedBy() {
		return ptpCreatedBy;
	}
	public void setPtpCreatedBy(String ptpCreatedBy) {
		this.ptpCreatedBy = ptpCreatedBy;
	}
	public Date getStartDate() {
		return startDate;
	}
	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}
	public Date getEndDate() {
		return endDate;
	}
	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}
	public Date getPtpDate() {
		return ptpDate;
	}
	public void setPtpDate(Date ptpDate) {
		this.ptpDate = ptpDate;
	}
	public double getPtpEmiAmt() {
		return ptpEmiAmt;
	}
	public void setPtpEmiAmt(double ptpEmiAmt) {
		this.ptpEmiAmt = ptpEmiAmt;
	}
	public double getPtpAmount() {
		return ptpAmount;
	}
	public void setPtpAmount(double ptpAmount) {
		this.ptpAmount = ptpAmount;
	}
	double ptpEmiAmt, ptpAmount;

	public int getBranchId() {
		return branchId;
	}
	public void setBranchId(int branchId) {
		this.branchId = branchId;
	}
	public boolean isShow() {
		return show;
	}
	public void setShow(boolean show) {
		this.show = show;
	}
	public String getStatus() {
		return status;
	}
	public void setStatus(String status) {
		this.status = status;
	}
	public ArrayList<PTPInfo> getPtpList() {
		return ptpList;
	}
	public void setPtpList(ArrayList<PTPInfo> ptpList) {
		this.ptpList = ptpList;
	}

	public ArrayList<PTPInfo> getPtpNewList() {
		return ptpNewList;
	}

	public void setPtpNewList(ArrayList<PTPInfo> ptpNewList) {
		this.ptpNewList = ptpNewList;
	}

	public int getSelectedPtpId() {
		return selectedPtpId;
	}

	public void setSelectedPtpId(int selectedPtpId) {
		this.selectedPtpId = selectedPtpId;
	}

	public double getNewPtpAmount() {
		return newPtpAmount;
	}

	public void setNewPtpAmount(double newPtpAmount) {
		this.newPtpAmount = newPtpAmount;
	}

	public Date getNewPtpDate() {
		return newPtpDate;
	}

	public void setNewPtpDate(Date newPtpDate) {
		this.newPtpDate = newPtpDate;
	}

	public Date getOldPtpDate() {
		return oldPtpDate;
	}

	public void setOldPtpDate(Date oldPtpDate) {
		this.oldPtpDate = oldPtpDate;
	}

	public String getNewPtpRemarks() {
		return newPtpRemarks;
	}

	public void setNewPtpRemarks(String newPtpRemarks) {
		this.newPtpRemarks = newPtpRemarks;
	}

	public String getNewptpStatus() {
		return newptpStatus;
	}

	public void setNewptpStatus(String newptpStatus) {
		this.newptpStatus = newptpStatus;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public boolean isNewPtpCreated() {
		return newPtpCreated;
	}

	public void setNewPtpCreated(boolean newPtpCreated) {
		this.newPtpCreated = newPtpCreated;
	}

	public boolean isKeptshow() {
		return keptshow;
	}

	public void setKeptshow(boolean keptshow) {
		this.keptshow = keptshow;
	}

	public boolean isBrokenshow() {
		return brokenshow;
	}

	public void setBrokenshow(boolean brokenshow) {
		this.brokenshow = brokenshow;
	}			

}
