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

import com.arrebol.taxiservicios.beans.Datatable;
import com.arrebol.taxiservicios.beans.TaxiGenericBean;
import com.arrebol.taxiservicios.controller.sp.report.ReleaseController;
import com.arrebol.taxiservicios.model.sp.report.ReportReleaseResource;
import java.io.Serializable;
import java.text.SimpleDateFormat;
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
@ViewScoped
public class ReleaseBean 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 {
                if(getKardex() != null)
                    getKardex().clear();
                
                setKardex(getController().fillDatatable(DATE_FORMAT.format(getGenericStartDate()), DATE_FORMAT.format(getGenericEndDate()), getLoggedUser().getLocation().getId()));
                
            }
        } 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(ReleaseBean.class);
    ReleaseController controller;
    private Date startDate;
    private Date endDate;
    private List<ReportReleaseResource> kardex;
    
    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
    
    @PostConstruct
    public void init() {
        logger.info("init");
        try {
            loadBundlePropertyFile();
            setGenericUpDates(true);
            setController(new ReleaseController());
            setKardex(getController().fillDatatable(DATE_FORMAT.format( getGenericStartDate()),
                    DATE_FORMAT.format(getGenericEndDate()), getLoggedUser().getLocation().getId()));
            
            logger.info("ReleaseResource loaded");
        } catch (Exception e) {
            logger.error("init", e);
        }
    }
    
    public ReleaseController getController() {
        return controller;
    }

    public void setController(ReleaseController 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<ReportReleaseResource> getKardex() {
        return kardex;
    }

    public void setKardex(List<ReportReleaseResource> kardex) {
        this.kardex = kardex;
    }
    
}
