/*
 * 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.dashboard;

import com.arrebol.apc.controller.admin.CustomerController;
import com.arrebol.apc.controller.admin.LoanController;
import com.arrebol.apc.model.catalog.People;
import com.arrebol.apc.model.loan.Loan;
import com.arrebol.apc.model.loan.LoanDetails;
import com.arrebol.apc.web.beans.Datatable;
import com.arrebol.apc.web.beans.GenericBean;
import java.io.Serializable;
import java.util.ArrayList;
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(value = "customerWithoutLoanDetailManager")
@ViewScoped
public class CustomerWithoutLoanDetailBean extends GenericBean implements Serializable, Datatable {

    public List<LoanDetails> getDetails(String id) {
        try {
            loanDetails = loanCtrl.getLoanDetailsbyId(id);

        } catch (Exception e) {
        }
        return null == loanDetails ? new ArrayList<>() : loanDetails;
    }

    public People getCustomerDetail(String peopleId) {
        return customerCtrl.findPeopleById(peopleId);
    }

    public List<Loan> getLoanByPeople(String peopleId) {
        return customerCtrl.findLoanByCustomer(peopleId);
    }

    public void editPeople() {
        if (customer.getBirthdate() == null) {
            showMessage(FacesMessage.SEVERITY_FATAL, "Campo obligatorio", "La fecha de cumpleaños es obligatoria");
            return;
        }
        if (customer.getFirstName() == null) {
            showMessage(FacesMessage.SEVERITY_FATAL, "Campo obligatorio", "El primer nombre es obligatoria");
            return;
        }
        if (customer.getLastName() == null) {
            showMessage(FacesMessage.SEVERITY_FATAL, "Campo obligatorio", "El apellido paterno es obligatoria");
            return;
        }
        if (customer.getMiddleName() == null) {
            showMessage(FacesMessage.SEVERITY_FATAL, "Campo obligatorio", "El apellido materno es obligatoria");
            return;
        }
        if (customer.getPhoneHome() == null) {
            showMessage(FacesMessage.SEVERITY_FATAL, "Campo obligatorio", "El teléfono de casa es obligatoria");
            return;
        }
        if (customer.getAddressHome() == null) {
            showMessage(FacesMessage.SEVERITY_FATAL, "Campo obligatorio", "El domicilio personal es obligatoria");
            return;
        }
        if (customerCtrl.updateByPeopleId(customer)) {
            showMessage(FacesMessage.SEVERITY_INFO, "Cliente modificado", "Se modificó correctamente.");
        }
    }

    @Override
    public void editRow(RowEditEvent event) {

    }

    @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 CustomerController getCustomerCtrl() {
        return customerCtrl;
    }

    public void setCustomerCtrl(CustomerController customerCtrl) {
        this.customerCtrl = customerCtrl;
    }

    public People getCustomer() {
        return customer;
    }

    public void setCustomer(People customer) {
        this.customer = customer;
    }

    public String getCustomerId() {
        return customerId;
    }

    public void setCustomerId(String customerId) {
        this.customerId = customerId;
    }

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

    public void setLoan(List<Loan> loan) {
        this.loan = loan;
    }

    public LoanController getLoanCtrl() {
        return loanCtrl;
    }

    public void setLoanCtrl(LoanController loanCtrl) {
        this.loanCtrl = loanCtrl;
    }

    public List<LoanDetails> getLoanDetails() {
        return loanDetails;
    }

    public void setLoanDetails(List<LoanDetails> loanDetails) {
        this.loanDetails = loanDetails;
    }

    private CustomerController customerCtrl;
    private LoanController loanCtrl;
    private People customer;
    private String customerId;
    private List<Loan> loan;
    private List<LoanDetails> loanDetails;

    @PostConstruct
    public void init() {
        customerCtrl = new CustomerController();
        loanCtrl = new LoanController();
        setCustomerId(externalContext().getRequestParameterMap().get("selectedId"));
        customer = getCustomerDetail(getCustomerId());
        loan = getLoanByPeople(getCustomerId());

    }

}
