package com.fp.frontend.controller.armas.funcionalidad;

import java.util.ArrayList;
import java.util.HashMap;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import com.fp.dto.AbstractDataTransport;
import com.fp.frontend.controller.AbstractController;
import com.fp.frontend.controller.ReportController;
import com.fp.frontend.controller.pcustomer.PersonDetailController;
import com.fp.frontend.helper.MessageHelper;
import com.fp.persistence.pgeneral.safe.TsafeUserDetail;

/**
 *
 * @author David Cruz
 *
 */
@ManagedBean
@ViewScoped
public class ReporteArmasPrevImportacionController extends AbstractController<AbstractDataTransport> {

    private static final long serialVersionUID = -6726028201723692500L;

    @ManagedProperty(value = "#{reportController}")
    private ReportController reportController;
    private TsafeUserDetail userDetail;

    public ReporteArmasPrevImportacionController() throws Exception {
        super(AbstractDataTransport.class);
        // TODO Auto-generated constructor stub
    }

    @PostConstruct
    public void init() {
        beanalias = "REPORTE";
        lrecord = new ArrayList<>();
        userDetail = (TsafeUserDetail) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("TSAFEUSERDETAIL");
    }

    @Override
    protected void querydatabase() {
        // TODO Auto-generated method stub

    }

    public void generarReporte() {
        String path = "armas/reports/reporteArmasPrevImportacion";
        HashMap<String, Object> parameters = new HashMap<>();
        parameters.put("pathLogoIzquierda", "repo:/maia/1/image/comandoconjunto");
        parameters.put("pathLogoDerecha", "repo:/maia/1/image/selloarmas");
        try {
            //Usuario logueado en la aplicacion
            String nombreUsuario;
            if (userDetail.getPk().getPersoncode() != null) {
                nombreUsuario = PersonDetailController.find(userDetail.getPk().getPersoncode().toString()).getName();
                parameters.put("personcode", userDetail.getPk().getPersoncode());
            } else {
                nombreUsuario = "";
            }
            parameters.put("usuario", nombreUsuario);
            String format = "xlsx";
            String filename = "permisosprevimportacion";
            this.reportController.executeXls(path, parameters, format, filename, this.getLoginController());
        } catch (Exception ex) {
            MessageHelper.setMessageError(ex);
        }
    }

    public ReportController getReportController() {
        return reportController;
    }

    public void setReportController(ReportController reportController) {
        this.reportController = reportController;
    }

}
