/*
 * 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.Serializable;
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 javax.servlet.http.HttpSession;
import org.primefaces.PrimeFaces;

import sikd.ikonsol.ejb.session.BaganAkunStandarEjbHome;
import sikd.ikonsol.ejb.session.JenisBasEjbHome;
import sikd.ikonsol.model.BaganAkunStandar;
import sikd.ikonsol.model.JenisBas;

/**
 *
 * @author Imam Bhoim
 */
@ViewScoped
@Named(value = "baganAkunStandarBeanCtrl")
public class BaganAkunStandarCtrl implements Serializable {

    BaganAkunStandar akunstandar;
    BaganAkunStandar obj;
    private List<BaganAkunStandar> list, listlevel1,
            listlevel2, listlevel3, listlevel4, listlevel5, listJenisBas;

    private int kdlevel_1;
    private int kdlevel_2;
    private int kdlevel_3;
    private String namalevel3;
    private double saldonormal;

    JenisBas jb;

    private String idItem = null;

    @EJB
    BaganAkunStandarEjbHome baganakunstandarEjbHome;

    @EJB
    JenisBasEjbHome jbasEjbHome;

    @PostConstruct
    public void init() {

//        list = baganakunstandarEjbHome.findAll();
//
//        listlevel4 = baganakunstandarEjbHome.findAllLevel4();
//        listlevel5 = baganakunstandarEjbHome.findAllLevel5();
        akunstandar = baganakunstandarEjbHome.createNew();

        FacesContext context = FacesContext.getCurrentInstance();
        idItem = (String) context.getExternalContext().getSessionMap().get("IDITEM");

        HttpSession session = (HttpSession) context.getExternalContext()
                .getSession(true);

        System.out.println("Bagan " + idItem);
        listJenisBas = baganakunstandarEjbHome.findAllByJenisBas(Integer.valueOf(idItem));

        jb = jbasEjbHome.findByID(Integer.valueOf(idItem));

    }

    public void add() {

        akunstandar = baganakunstandarEjbHome.createNew();

        listlevel1 = baganakunstandarEjbHome.findAllLevel1(Integer.valueOf(idItem));

        System.out.println("AAABB");
        //listlevel2 = baganakunstandarEjbHome.findAllLevel2();
    }

    public String save() {

        // simpan
        BaganAkunStandar ba = new BaganAkunStandar();

        ba = baganakunstandarEjbHome.findByKodeLvl12(ba.getKodeLevel1(),
                ba.getKodeLevel2(), ba.getKodeLevel3(), ba.getIdbas());
        try {
            if (ba == null) {
                akunstandar.setIdbas(Integer.valueOf(idItem));

                baganakunstandarEjbHome.save(akunstandar);

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

                //list = baganakunstandarEjbHome.findAll();
                listJenisBas = baganakunstandarEjbHome.findAllByJenisBas(Integer.valueOf(idItem));

            } else {
                System.out.println("SIMPAN 4");
                FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO,
                        "Info", "Kode Akun Sudah Ada");
                PrimeFaces.current().dialog().showMessageDynamic(message);
            }
        } catch (Exception ex) {
//            System.out.println("GAGAL!");
//            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Info!", "Kode Sudah Ada");
//            FacesContext.getCurrentInstance().addMessage(null, message);
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO,
                    "Info", "Kode Akun Sudah Ada");
            PrimeFaces.current().dialog().showMessageDynamic(message);
        }

        return "index?faces-redirect=true";
        // simpan
    }

    public String saveLevel4() {

        // simpan
        BaganAkunStandar ba = new BaganAkunStandar();

        ba = baganakunstandarEjbHome.findByKodeLvl123(ba.getKodeLevel1(),
                ba.getKodeLevel2(), ba.getKodeLevel3(), ba.getKodeLevel4(), ba.getIdbas());
        try {
            if (ba == null) {
                akunstandar.setIdbas(Integer.valueOf(idItem));

                String kdrek4 = akunstandar.getKodeLevel4().trim();

                if (kdrek4.equalsIgnoreCase("1")) {
                    kdrek4 = "01";
                } else if (kdrek4.equalsIgnoreCase("2")) {
                    kdrek4 = "02";
                } else if (kdrek4.equalsIgnoreCase("3")) {
                    kdrek4 = "03";
                } else if (kdrek4.equalsIgnoreCase("4")) {
                    kdrek4 = "04";
                } else if (kdrek4.equalsIgnoreCase("5")) {
                    kdrek4 = "05";
                } else if (kdrek4.equalsIgnoreCase("6")) {
                    kdrek4 = "06";
                } else if (kdrek4.equalsIgnoreCase("7")) {
                    kdrek4 = "07";
                } else if (kdrek4.equalsIgnoreCase("8")) {
                    kdrek4 = "08";
                } else if (kdrek4.equalsIgnoreCase("9")) {
                    kdrek4 = "09";
                } else {
                    kdrek4 = kdrek4;
                }

                akunstandar.setKodeLevel4(kdrek4);

                baganakunstandarEjbHome.saveLevel4(akunstandar);

                PrimeFaces.current().executeScript("PF('wg_entryform4').hide()");
                PrimeFaces.current().ajax().update(":master:dtbaganakunstandar");

                // list = baganakunstandarEjbHome.findAll();
                listJenisBas = baganakunstandarEjbHome.findAllByJenisBas(Integer.valueOf(idItem));

            } else {
                System.out.println("SIMPAN 4");
                FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO,
                        "Info", "Kode Akun Sudah Ada");
                PrimeFaces.current().dialog().showMessageDynamic(message);
            }
        } catch (Exception ex) {
//            System.out.println("GAGAL!");
//            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Info!", "Kode Sudah Ada");
//            FacesContext.getCurrentInstance().addMessage(null, message);
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO,
                    "Info", "Kode Akun Sudah Ada");
            PrimeFaces.current().dialog().showMessageDynamic(message);
        }

        return "index?faces-redirect=true";
        // simpan
    }

    public String saveLevel5() {

        // simpan
        BaganAkunStandar ba = new BaganAkunStandar();

        ba = baganakunstandarEjbHome.findByKodeLvl1234(ba.getKodeLevel1(),
                ba.getKodeLevel2(), ba.getKodeLevel3(), ba.getKodeLevel4(),
                ba.getKodeLevel5(), ba.getIdbas());
        try {
            if (ba == null) {
                akunstandar.setIdbas(Integer.valueOf(idItem));

                String kdrek4 = akunstandar.getKodeLevel4().trim();
                String kdrek5 = akunstandar.getKodeLevel5().trim();

                if (kdrek4.equalsIgnoreCase("1")) {
                    kdrek4 = "01";
                } else if (kdrek4.equalsIgnoreCase("2")) {
                    kdrek4 = "02";
                } else if (kdrek4.equalsIgnoreCase("3")) {
                    kdrek4 = "03";
                } else if (kdrek4.equalsIgnoreCase("4")) {
                    kdrek4 = "04";
                } else if (kdrek4.equalsIgnoreCase("5")) {
                    kdrek4 = "05";
                } else if (kdrek4.equalsIgnoreCase("6")) {
                    kdrek4 = "06";
                } else if (kdrek4.equalsIgnoreCase("7")) {
                    kdrek4 = "07";
                } else if (kdrek4.equalsIgnoreCase("8")) {
                    kdrek4 = "08";
                } else if (kdrek4.equalsIgnoreCase("9")) {
                    kdrek4 = "09";
                } else {
                    kdrek4 = kdrek4;
                }

                if (kdrek5.equalsIgnoreCase("1")) {
                    kdrek5 = "01";
                } else if (kdrek5.equalsIgnoreCase("2")) {
                    kdrek5 = "02";
                } else if (kdrek5.equalsIgnoreCase("3")) {
                    kdrek5 = "03";
                } else if (kdrek5.equalsIgnoreCase("4")) {
                    kdrek5 = "04";
                } else if (kdrek5.equalsIgnoreCase("5")) {
                    kdrek5 = "05";
                } else if (kdrek5.equalsIgnoreCase("6")) {
                    kdrek5 = "06";
                } else if (kdrek5.equalsIgnoreCase("7")) {
                    kdrek5 = "07";
                } else if (kdrek5.equalsIgnoreCase("8")) {
                    kdrek5 = "08";
                } else if (kdrek5.equalsIgnoreCase("9")) {
                    kdrek5 = "09";
                } else {
                    kdrek5 = kdrek5;
                }

                akunstandar.setKodeLevel4(kdrek4);
                akunstandar.setKodeLevel5(kdrek5);

                baganakunstandarEjbHome.saveLevel5(akunstandar);

                PrimeFaces.current().executeScript("PF('wg_entryform5').hide()");
                PrimeFaces.current().ajax().update(":master:dtbaganakunstandar");

                // list = baganakunstandarEjbHome.findAll();
                listJenisBas = baganakunstandarEjbHome.findAllByJenisBas(Integer.valueOf(idItem));

            } else {
                System.out.println("SIMPAN 4");
                FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO,
                        "Info", "Kode Akun Sudah Ada");
                PrimeFaces.current().dialog().showMessageDynamic(message);
            }
        } catch (Exception ex) {
//            System.out.println("GAGAL!");
//            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Info!", "Kode Sudah Ada");
//            FacesContext.getCurrentInstance().addMessage(null, message);
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO,
                    "Info", "Kode Akun Sudah Ada");
            PrimeFaces.current().dialog().showMessageDynamic(message);
        }

        return "index?faces-redirect=true";
        // simpan
    }

    public String update() {

        baganakunstandarEjbHome.update(obj);

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

        //list = baganakunstandarEjbHome.findAll();
        listJenisBas = baganakunstandarEjbHome.findAllByJenisBas(Integer.valueOf(idItem));
// this.list = service.findAll();
        return "index?faces-redirect=true";
    }

    public void edit(int kdlvl1, int kdlvl2, int kdlvl3, String kdlvl4, String kdlvl5, int idbas) {
        //obj = service.findByID(Integer.parseInt(id));
        obj = baganakunstandarEjbHome.findByKodeLvl1234(kdlvl1, kdlvl2, kdlvl3, kdlvl4, kdlvl5, idbas);

//        listlevel1 = baganakunstandarEjbHome.findByLevel1(kdlvl1, idbas);
//        listlevel2 = baganakunstandarEjbHome.findByLevel2(kdlvl1, kdlvl2, idbas);
//        listlevel3 = baganakunstandarEjbHome.findByLevel2(kdlvl1, kdlvl2, idbas);
//        listlevel4 = baganakunstandarEjbHome.findByLevel2(kdlvl1, kdlvl2, idbas);
    }

    public void delete(int kdlvl1, int kdlvl2, int kdlvl3, String kdlvl4, String kdlvl5, int idbas) {
        //obj = service.findByID(Integer.parseInt(id));
        //obj = periodeEjbHome.findByKode(kode);
        //obj = baganakunstandarEjbHome.findByKodeLvl1234(kdlvl1, kdlvl2, kdlvl3, kdlvl4, kdlvl5, idbas);
        baganakunstandarEjbHome.delete(kdlvl1, kdlvl2, kdlvl3, kdlvl4, kdlvl5, idbas);
        PrimeFaces.current().ajax().update(":master:dtbaganakunstandar");

        //list = baganakunstandarEjbHome.findAll();
        listJenisBas = baganakunstandarEjbHome.findAllByJenisBas(Integer.valueOf(idItem));

    }

    public void onDatas() {
        if (akunstandar != null) {
            System.out.println("ZXZXZXZX");
            listlevel2 = baganakunstandarEjbHome.findAllLevel2ByLevel1(akunstandar.getKodeLevel1(), Integer.valueOf(idItem));
            System.out.println("DATA ADA");
            System.out.println(akunstandar.getKodeLevel1());
        }
    }

    public void onDatas2() {
        if (akunstandar != null) {
            listlevel3 = baganakunstandarEjbHome.findAllLevel3ByLevel2(akunstandar.getKodeLevel2(), Integer.valueOf(idItem));
        }

    }

    public void onDatas3() {
        if (akunstandar != null) {
            listlevel4 = baganakunstandarEjbHome.findAllLevel4ByLevel3(akunstandar.getKodeLevel3(), Integer.valueOf(idItem));
        }

    }

    public String URLBas() {

        return "baganakunstandar?faces-redirect=true";
    }

    public BaganAkunStandar getAkunstandar() {
        return akunstandar;
    }

    public void setAkunstandar(BaganAkunStandar akunstandar) {
        this.akunstandar = akunstandar;
    }

    public BaganAkunStandar getObj() {
        return obj;
    }

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

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

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

    public int getKdlevel_1() {
        return kdlevel_1;
    }

    public void setKdlevel_1(int kdlevel_1) {
        this.kdlevel_1 = kdlevel_1;
    }

    public int getKdlevel_2() {
        return kdlevel_2;
    }

    public void setKdlevel_2(int kdlevel_2) {
        this.kdlevel_2 = kdlevel_2;
    }

    public int getKdlevel_3() {
        return kdlevel_3;
    }

    public void setKdlevel_3(int kdlevel_3) {
        this.kdlevel_3 = kdlevel_3;
    }

    public String getNamalevel3() {
        return namalevel3;
    }

    public void setNamalevel3(String namalevel3) {
        this.namalevel3 = namalevel3;
    }

    public double getSaldonormal() {
        return saldonormal;
    }

    public void setSaldonormal(double saldonormal) {
        this.saldonormal = saldonormal;
    }

    public BaganAkunStandarEjbHome getBaganakunstandarEjbHome() {
        return baganakunstandarEjbHome;
    }

    public void setBaganakunstandarEjbHome(BaganAkunStandarEjbHome baganakunstandarEjbHome) {
        this.baganakunstandarEjbHome = baganakunstandarEjbHome;
    }

    public List<BaganAkunStandar> getListlevel1() {
        return listlevel1;
    }

    public void setListlevel1(List<BaganAkunStandar> listlevel1) {
        this.listlevel1 = listlevel1;
    }

    public List<BaganAkunStandar> getListlevel2() {
        return listlevel2;
    }

    public void setListlevel2(List<BaganAkunStandar> listlevel2) {
        this.listlevel2 = listlevel2;
    }

    public List<BaganAkunStandar> getListlevel4() {
        return listlevel4;
    }

    public void setListlevel4(List<BaganAkunStandar> listlevel4) {
        this.listlevel4 = listlevel4;
    }

    public List<BaganAkunStandar> getListlevel5() {
        return listlevel5;
    }

    public void setListlevel5(List<BaganAkunStandar> listlevel5) {
        this.listlevel5 = listlevel5;
    }

    public List<BaganAkunStandar> getListJenisBas() {
        return listJenisBas;
    }

    public void setListJenisBas(List<BaganAkunStandar> listJenisBas) {
        this.listJenisBas = listJenisBas;
    }

    public String getIdItem() {
        return idItem;
    }

    public void setIdItem(String idItem) {
        this.idItem = idItem;
    }

    public JenisBas getJb() {
        return jb;
    }

    public void setJb(JenisBas jb) {
        this.jb = jb;
    }

    public List<BaganAkunStandar> getListlevel3() {
        return listlevel3;
    }

    public void setListlevel3(List<BaganAkunStandar> listlevel3) {
        this.listlevel3 = listlevel3;
    }

}
