/*
 * 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 sikd.ikonsol.ctrl;

import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.Iterator;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import javax.servlet.http.HttpServletRequest;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.primefaces.PrimeFaces;
import org.primefaces.event.FileUploadEvent;
import sikd.ikonsol.ejb.session.BaganAkunStandarEjbHome;
import sikd.ikonsol.ejb.session.JenisBasEjbHome;
import sikd.ikonsol.model.BaganAkunStandar;
import sikd.ikonsol.model.JenisBas;

/**
 *
 * @author Imam BhoimA
 */
@ViewScoped
@Named(value = "jenisBasBeanCtrl")
public class JenisBasBeanCtrl implements Serializable {

    JenisBas jbas;
    JenisBas obj;
    BaganAkunStandar bas;
    private List<JenisBas> list;

    @EJB
    JenisBasEjbHome jbasEjbHome;

    @EJB
    BaganAkunStandarEjbHome basEjbHome;

    @PostConstruct
    public void init() {

        list = jbasEjbHome.findAll();

        bas = new BaganAkunStandar();
        //obj = periodeEjbHome.findByKode("22");
        //periode = periodeEjbHome.createNew();
    }

    public void add() {

        jbas = jbasEjbHome.createNew();

    }

    public String save() {

        // simpan
        jbasEjbHome.save(jbas);

        PrimeFaces.current().executeScript("PF('wg_entryform').hide()");
        PrimeFaces.current().ajax().update(":master:dtjbas");

        list = jbasEjbHome.findAll();
        //this.list = service.findAll();
        return "index?faces-redirect=true";
    }

    public String update() {

        jbasEjbHome.update(obj);

        PrimeFaces.current().executeScript("PF('wg_entryform_ubah').hide()");
        PrimeFaces.current().ajax().update(":master:dtjbas");

        list = jbasEjbHome.findAll();
        // this.list = service.findAll();
        return "index?faces-redirect=true";
    }

    public void edit(int id) {
        //obj = service.findByID(Integer.parseInt(id));
        obj = jbasEjbHome.findByID(id);

    }

    public void upload(int id) {
        //obj = service.findByID(Integer.parseInt(id));
        obj = jbasEjbHome.findByID(id);

    }

    public void delete(int id) {
        //obj = service.findByID(Integer.parseInt(id));
        //obj = periodeEjbHome.findByKode(kode);
        jbasEjbHome.delete(id);
        PrimeFaces.current().ajax().update(":master:dtjbas");
        list = jbasEjbHome.findAll();
    }

    public void handleFileUpload(FileUploadEvent event) {
        if (event.getFile().equals(null)) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "File is null", null));
            System.out.println("importKosong");
        }

        InputStream file;
        XSSFWorkbook workbook = null;
        try {
            file = event.getFile().getInputstream();
            workbook = new XSSFWorkbook(file);
            System.out.println("importDapetfile " + file.toString());
        } catch (IOException e) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error reading file" + e, null));
        }

        XSSFSheet sheet = workbook.getSheetAt(0);

        Iterator<Row> rowIterator = sheet.iterator();

        rowIterator.next();
        while (rowIterator.hasNext()) {
            Row row = rowIterator.next();

            Iterator<Cell> cellIterator = row.cellIterator();

            int kdlvl1 = (int) row.getCell(0).getNumericCellValue();

            int kdlvl2 = (int) row.getCell(1).getNumericCellValue();

            int kdlvl3 = (int) row.getCell(2).getNumericCellValue();

            int kdlvl4 = (int) row.getCell(3).getNumericCellValue();

            int kdlvl5 = (int) row.getCell(4).getNumericCellValue();

            String namaakun = row.getCell(5).getStringCellValue().trim();
            
            String dk = row.getCell(6).getStringCellValue().trim();

            
            String kd4 = String.valueOf(kdlvl4).trim();
            String kd5 = String.valueOf(kdlvl5).trim();
            
            System.out.println(kd4 + " " + kd5 + " " + namaakun);
            
            
            kd4 = kd4.replaceAll("\0", "");
            kd5 = kd5.replaceAll("\0", "");
            namaakun = namaakun.replaceAll("\0", "");
            dk = dk.replaceAll("\0", "");
            
            bas.setKodeLevel1(kdlvl1);
            bas.setKodeLevel2(kdlvl2);
            bas.setKodeLevel3(kdlvl3);
            bas.setKodeLevel4(kd4);
            bas.setKodeLevel5(kd5);
            bas.setNama(namaakun);
            bas.setDk(dk);
            bas.setIdbas(obj.getId());

            basEjbHome.saveImport(bas);

        }
        FacesMessage msg = new FacesMessage("Sukses", event.getFile().getFileName() + " Upload ");
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    public String bas(String id) {
        this.saveToSession(id);

        FacesContext context = FacesContext.getCurrentInstance();
        System.out.println("Bas " + id);
        HttpServletRequest oriquest
                = (HttpServletRequest) context.getExternalContext().getRequest();
        String nexurl = "/ikonsol/referensi/baganakunstandar.sikd";
        try {
            context.getExternalContext()
                    .redirect(oriquest.getContextPath() + nexurl);// "/logbook/view.xhtml");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "view?faces-redirect=true";

    }

    private void saveToSession(String idx) {
        FacesContext context = FacesContext.getCurrentInstance();
        context.getExternalContext().getSessionMap()
                .put("IDITEM", idx);
        System.out.println("Session id " + idx);

    }

    public JenisBas getJbas() {
        return jbas;
    }

    public void setJbas(JenisBas jbas) {
        this.jbas = jbas;
    }

    public JenisBas getObj() {
        return obj;
    }

    public void setObj(JenisBas obj) {
        this.obj = obj;
    }

    public List<JenisBas> getList() {
        return list;
    }

    public void setList(List<JenisBas> list) {
        this.list = list;
    }

    public JenisBasEjbHome getJbasEjbHome() {
        return jbasEjbHome;
    }

    public void setJbasEjbHome(JenisBasEjbHome jbasEjbHome) {
        this.jbasEjbHome = jbasEjbHome;
    }

    public BaganAkunStandar getBas() {
        return bas;
    }

    public void setBas(BaganAkunStandar bas) {
        this.bas = bas;
    }

}
