/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.arrebol.apc.web.beans.admin;

import com.arrebol.apc.controller.system.employee.EmployeeController;
import com.arrebol.apc.model.core.HumanResource;
import com.arrebol.apc.model.enums.HumanResourceStatus;
import com.arrebol.apc.web.beans.Datatable;
import com.arrebol.apc.web.beans.GenericBean;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.primefaces.event.ReorderEvent;
import org.primefaces.event.RowEditEvent;

/**
 *
 * @author Oscar
 */
@Named("employeeListManager")
@ViewScoped
public class EmployeeListBean extends GenericBean implements Serializable, Datatable {

   public void editarIndetificadorDeFolios() {
      try {
         getSelectedHumanResource().setUltimaModificacionFolio(new Date());
         getSelectedHumanResource().setUsuarioUltimaModificacionFolio(getLoggedUser().getUser());
         getController().updateHumanResource(getSelectedHumanResource());
         logger.info("Se actualizó el identificador de folios correctamente");
         showMessage(FacesMessage.SEVERITY_INFO, "Registro actualizado", "Se actualizó el identificador de folios correctamente");
         setSelectedHumanResource(null);
         fillDatatableEmployee();
      } catch (Exception e) {
         logger.error("editarIndetificadorDeFolios(): " + e);
         showMessage(FacesMessage.SEVERITY_ERROR, "Error", "Ocurrió un error al tratar de editar el identificador de  folios");
      }
   }

   public List<HumanResource> fillDatatableEmployee() {
      return getController().findEmployeesByType(getLoggedUser().getOffice(), HumanResourceStatus.ENEBLED, getLoggedUser().getUser().getHumanResource().getId());
   }

   @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) {
      showMessage(FacesMessage.SEVERITY_INFO, "Registro Movido", "De columna: " + (event.getFromIndex() + 1) + " a columna: " + (event.getToIndex() + 1));
   }

   @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 EmployeeController getController() {
      return controller;
   }

   public void setController(EmployeeController controller) {
      this.controller = controller;
   }

   public List<HumanResource> getHumanResource() {
      return humanResource;
   }

   public void setHumanResource(List<HumanResource> humanResource) {
      this.humanResource = humanResource;
   }

   public HumanResource getSelectedHumanResource() {
      return selectedHumanResource;
   }

   public void setSelectedHumanResource(HumanResource selectedHumanResource) {
      this.selectedHumanResource = selectedHumanResource;
   }

   final Logger logger = LogManager.getLogger(EmployeeListBean.class);

   private EmployeeController controller;
   private List<HumanResource> humanResource;
   private HumanResource selectedHumanResource;

   @PostConstruct()
   public void init() {
      loadBundlePropertyFile();
      controller = new EmployeeController();
      setHumanResource(fillDatatableEmployee());
   }

}
