/*
 * 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.taxiservicios.beans.admin;

import com.arrebol.taxiservicios.beans.Datatable;
import com.arrebol.taxiservicios.beans.TaxiGenericBean;
import com.arrebol.taxiservicios.controller.stocktaking.HistoryUpdateKmController;
import com.arrebol.taxiservicios.model.stocktaking.view.HistoryUpdateKm;
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("historyUpdateKmBean")
@ViewScoped
public class HistoryUpdateKmBean extends TaxiGenericBean implements Serializable, Datatable {

    public void fillDataTableBetweenDates() {
        try {
            if (getGenericStartDate().after(getGenericEndDate())) {
                showMessage(FacesMessage.SEVERITY_ERROR, getBundlePropertyFile().getString("generic.start.date"), getBundlePropertyFile().getString("generic.end.date.error"));
            } else {
                setHistory(getController().fillDataTableBetweenDates(getGenericStartDate(), getGenericEndDate()));
            }
        } catch (Exception e) {
            logger.error("", e);
        }
    }
    
    @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.
    }
    
    final Logger logger = LogManager.getLogger(HistoryUpdateKmBean.class);
    HistoryUpdateKmController controller;
    private Date startDate;
    private Date endDate;
    List<HistoryUpdateKm> history;
    
    @PostConstruct
    public void init() {
        logger.info("init");
        try {
            loadBundlePropertyFile();
            setGenericUpDates(true);
            setController(new HistoryUpdateKmController());
            setHistory(getController().fillDataTableBetweenDates(getGenericStartDate(),
                    getGenericEndDate()));

            logger.info("HistoryUpdateKm loaded");
        } catch (Exception e) {
            logger.error("init", e);
        }
    }

    public HistoryUpdateKmController getController() {
        return controller;
    }

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

    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 List<HistoryUpdateKm> getHistory() {
        return history;
    }

    public void setHistory(List<HistoryUpdateKm> history) {
        this.history = history;
    }
    
    
}
