/*
 * Arrebol Consultancy copyright.
 * 
 * This code belongs to Arrebol Consultancy
 * its use, redistribution or modification are prohibited 
 * without written authorization from Arrebol Consultancy.
 */
package com.arrebol.apc.web.beans.admin;

import com.arrebol.apc.controller.admin.EndorsementController;
import com.arrebol.apc.model.catalog.People;
import com.arrebol.apc.model.loan.Loan;
import com.arrebol.apc.web.beans.Datatable;
import com.arrebol.apc.web.beans.GenericBean;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import org.primefaces.event.ReorderEvent;
import org.primefaces.event.RowEditEvent;

/**
 *
 * @author Oscar Armando Vargas Cardenas <oscar.vargas@arrebol.com.mx>
 */
@Named("endorsementDetailManager")
@ViewScoped
public class EndorsementDetailBean extends GenericBean implements Serializable, Datatable {
    
    public People getEndorsementDetail(String peopleId){
        return endorsementCtrl.findPeopleById(peopleId);
    }
    
    public List<Loan> getLoanByPeople(String peopleId){
        return endorsementCtrl.findLoanByEndorsement(peopleId);
    }
    
    public void editPeople()
    {
      if(endorsement.getBirthdate() == null){
         showMessage(FacesMessage.SEVERITY_FATAL, "Campo obligatorio", "La fecha de cumpleaños es obligatoria"); 
         return;
      }
      if(endorsement.getFirstName() == null){
         showMessage(FacesMessage.SEVERITY_FATAL, "Campo obligatorio", "El primer nombre es obligatoria"); 
         return;
      }
      if(endorsement.getLastName() == null){
         showMessage(FacesMessage.SEVERITY_FATAL, "Campo obligatorio", "El apellido paterno es obligatoria"); 
         return;
      }
      if(endorsement.getMiddleName() == null){
         showMessage(FacesMessage.SEVERITY_FATAL, "Campo obligatorio", "El apellido materno es obligatoria"); 
         return;
      }
      if(endorsement.getPhoneHome()== null){
         showMessage(FacesMessage.SEVERITY_FATAL, "Campo obligatorio", "El teléfono de casa es obligatoria"); 
         return;
      }
      if(endorsement.getAddressHome()== null){
         showMessage(FacesMessage.SEVERITY_FATAL, "Campo obligatorio", "El domicilio personal es obligatoria"); 
         return;
      }
      if(endorsementCtrl.updateByPeopleId(endorsement))
        {
            showMessage(FacesMessage.SEVERITY_INFO, "Aval modificado", "Se modificó correctamente.");
        }  
    }

    @Override
    public void editRow(RowEditEvent event) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void onRowCancel(RowEditEvent event) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void onRowReorder(ReorderEvent event) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void addRow() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void deleteRow() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    public EndorsementController getCustomerCtrl() {
        return endorsementCtrl;
    }

    public void setEndorsementCtrl(EndorsementController endorsementCtrl) {
        this.endorsementCtrl = endorsementCtrl;
    }

    public People getEndorsement() {
        return endorsement;
    }

    public void setEndorsement(People endorsement) {
        this.endorsement = endorsement;
    }

    public String getEndorsementId() {
        return endorsementId;
    }

    public void setEndorsementId(String endorsementId) {
        this.endorsementId = endorsementId;
    }

    public List<Loan> getLoan() {
        return loan;
    }

    public void setLoan(List<Loan> loan) {
        this.loan = loan;
    }
    
    private EndorsementController endorsementCtrl;
    private People endorsement;
    private String endorsementId;
    private List<Loan> loan;
    
    @PostConstruct
    public void init() {
        endorsementCtrl = new EndorsementController();
        setEndorsementId(externalContext().getRequestParameterMap().get("form:dtEndorsement_selection"));
        endorsement = getEndorsementDetail(getEndorsementId());
        loan = getLoanByPeople(getEndorsementId());
    
    }
    
}
