/*
 * 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.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import org.primefaces.PrimeFaces;
import sikd.ikonsol.ejb.session.AkunResiprokalEjbHome;
import sikd.ikonsol.ejb.session.HeaderResiprokalEjbHome;
import sikd.ikonsol.ejb.session.KategoriPemdaEjbHome;
import sikd.ikonsol.model.AkunResiprokal;
import sikd.ikonsol.model.HeaderResiprokal;
import sikd.ikonsol.model.KategoriPemda;

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

    AkunResiprokal akunres;
    AkunResiprokal obj;
    AkunResiprokal obj2;

    HeaderResiprokal hd;
    private List<AkunResiprokal> list, listkode;

    private List<HeaderResiprokal> listheaderres;
    private List<KategoriPemda> listkatpemda;

    private int idbasakunres = 0;

    private int headerid = 0;

    @EJB
    HeaderResiprokalEjbHome headerResiprokalEjbHome;

    @EJB
    AkunResiprokalEjbHome akunResiprokalEjbHome;

    @EJB
    KategoriPemdaEjbHome katPemdaEjbHome;

    @PostConstruct
    public void init() {

        list = akunResiprokalEjbHome.findAll();
    }

    public void add() {

        akunres = akunResiprokalEjbHome.createNew();
//        listjlapor = headerResiprokalEjbHome.findAllJLaporan();
//        System.out.println(listjlapor.size() + " AAA");
//        listjtransfer = headerResiprokalEjbHome.findAllJtransfer();
//        System.out.println(listjtransfer.size() + " BBB ");

        listheaderres = headerResiprokalEjbHome.findAll();
        listkatpemda = katPemdaEjbHome.findAll();

    }

    public String save() {

        AkunResiprokal ar = new AkunResiprokal();
//        System.out.println(" Header ID " + headerid + " idbas " + idbasakunres);
//        System.out.println("A1 " + akunres.getKdlevel1());
//        System.out.println("A2 " + akunres.getKdlevel2());
//        System.out.println("A3 " + akunres.getKdlevel3());
//        System.out.println("A4 " + akunres.getKdlevel4());
//        System.out.println("A5 " + akunres.getKdlevel5());
//        System.out.println("idbas " + idbasakunres);
//        System.out.println("header res " + headerid);
        ar = akunResiprokalEjbHome.findByKodeAkunHeader(akunres.getKdlevel1(), akunres.getKdlevel2(),
                akunres.getKdlevel3(), akunres.getKdlevel4(), akunres.getKdlevel5(), idbasakunres,
                headerid);
        try {
            if (ar == null) {

                akunres.setHeader_id_resipro(headerid);
                akunres.setIdbas(idbasakunres);

                String kdrek4 = akunres.getKdlevel4();
                String kdrek5 = akunres.getKdlevel5();

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

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

                akunres.setKdlevel4(kdrek4);
                akunres.setKdlevel5(kdrek5);

                akunResiprokalEjbHome.save(akunres);
                System.out.println("SIMPAN 3");

                PrimeFaces.current().executeScript("PF('wg_entryform1').hide()");
                PrimeFaces.current().ajax().update(":master:dtheaderres");

                list = akunResiprokalEjbHome.findAll();
            } else {
                System.out.println("SIMPAN 4");
                FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO,
                        "Info", "Kode Akun Resiprokal Sudah Ada");
                PrimeFaces.current().dialog().showMessageDynamic(message);
            }
        } catch (Exception ex) {
            System.out.println("GAGAL!");
            ex.printStackTrace();
//            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 Resiprokal Sudah Ada");
            PrimeFaces.current().dialog().showMessageDynamic(message);
        }
        //this.list = service.findAll();
        return "index?faces-redirect=true";
    }

    public String update() {

        AkunResiprokal ar = new AkunResiprokal();
        AkunResiprokal ar2 = new AkunResiprokal();

        ar = akunResiprokalEjbHome.findByKodeAkunHeader(obj.getKdlevel1(), obj.getKdlevel2(),
                obj.getKdlevel3(), obj.getKdlevel4(), obj.getKdlevel5(),
                idbasakunres, obj.getHeader_id_resipro());

        ar2 = akunResiprokalEjbHome.findByKodeAkunHeaderLengkap(obj.getKdlevel1(), obj.getKdlevel2(),
                obj.getKdlevel3(), obj.getKdlevel4(), obj.getKdlevel5(),
                idbasakunres, obj.getHeader_id_resipro(), obj.getDk(), obj.getId_kategori_pemda(),
                obj.getFlag());
//
//        System.out.println("OBJ1 " + obj.getKdlevel1() + " AR" + ar.getKdlevel1());
//        System.out.println("OBJ1 " + obj.getKdlevel2() + " AR" + ar.getKdlevel2());
//        System.out.println("OBJ1 " + obj.getKdlevel3() + " AR" + ar.getKdlevel3());
//        System.out.println("OBJ1 " + obj.getKdlevel4() + " AR" + ar.getKdlevel4());
//        System.out.println("OBJ1 " + obj.getKdlevel5() + " AR" + ar.getKdlevel5());
//        System.out.println("OBJ1 " + headerid + " AR" + ar.getHeader_id_resipro());
//        System.out.println("OBJ1 " + idbasakunres + " AR" + ar.getIdbas());

        try {
            if (ar == null) {

                String kdrek4 = obj.getKdlevel4();
                String kdrek5 = obj.getKdlevel5();

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

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

                obj.setKdlevel4(kdrek4);
                obj.setKdlevel5(kdrek5);

                akunResiprokalEjbHome.update(obj);
                System.out.println("SIMPAN 3");

                PrimeFaces.current().executeScript("PF('wg_entryform1_ubah').hide()");
                PrimeFaces.current().ajax().update(":master:dtheaderres");

                list = akunResiprokalEjbHome.findAll();

            } else {

                if (ar2 == null) {

                    String kdrek4 = obj.getKdlevel4();
                    String kdrek5 = obj.getKdlevel5();

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

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

                    obj.setKdlevel4(kdrek4);
                    obj.setKdlevel5(kdrek5);

                    akunResiprokalEjbHome.update(obj);
                    System.out.println("SIMPAN 4");

                    PrimeFaces.current().executeScript("PF('wg_entryform1_ubah').hide()");
                    PrimeFaces.current().ajax().update(":master:dtheaderres");

                    list = akunResiprokalEjbHome.findAll();
                } else {
                    System.out.println("SIMPAN 5");
                    FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO,
                            "Info", "Kode Akun Resiprokal Sudah Ada");
                    PrimeFaces.current().dialog().showMessageDynamic(message);
                }
            }
        } catch (Exception ex) {
            System.out.println("GAGAL!");
            ex.printStackTrace();
//            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 Resiprokal Sudah Ada");
            PrimeFaces.current().dialog().showMessageDynamic(message);
        }
        return "index?faces-redirect=true";
    }

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

        listheaderres = headerResiprokalEjbHome.findAll();
        listkatpemda = katPemdaEjbHome.findAll();
    }

    public void delete(int id) {
        akunResiprokalEjbHome.delete(id);
        PrimeFaces.current().ajax().update(":master:dtheaderres");
        list = akunResiprokalEjbHome.findAll();
    }

    public ArrayList<String> pasanganData() {

        ArrayList<String> numbers = new ArrayList<String>();

        numbers.add("1");
        numbers.add("2");
        numbers.add("3");
        numbers.add("4");
        numbers.add("5");
        numbers.add("8");
        return (numbers);
    }

    public void listkode(int idheader) {

        listkode = akunResiprokalEjbHome.findByIdHeader(idheader);

        hd = headerResiprokalEjbHome.findByID(idheader);

        headerid = idheader;
        idbasakunres = hd.getIdbas();
    }

    public AkunResiprokal getAkunres() {
        return akunres;
    }

    public void setAkunres(AkunResiprokal akunres) {
        this.akunres = akunres;
    }

    public AkunResiprokal getObj() {
        return obj;
    }

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

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

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

    public List<HeaderResiprokal> getListheaderres() {
        return listheaderres;
    }

    public void setListheaderres(List<HeaderResiprokal> listheaderres) {
        this.listheaderres = listheaderres;
    }

    public List<KategoriPemda> getListkatpemda() {
        return listkatpemda;
    }

    public void setListkatpemda(List<KategoriPemda> listkatpemda) {
        this.listkatpemda = listkatpemda;
    }

    public HeaderResiprokalEjbHome getHeaderResiprokalEjbHome() {
        return headerResiprokalEjbHome;
    }

    public void setHeaderResiprokalEjbHome(HeaderResiprokalEjbHome headerResiprokalEjbHome) {
        this.headerResiprokalEjbHome = headerResiprokalEjbHome;
    }

    public AkunResiprokalEjbHome getAkunResiprokalEjbHome() {
        return akunResiprokalEjbHome;
    }

    public void setAkunResiprokalEjbHome(AkunResiprokalEjbHome akunResiprokalEjbHome) {
        this.akunResiprokalEjbHome = akunResiprokalEjbHome;
    }

    public KategoriPemdaEjbHome getKatPemdaEjbHome() {
        return katPemdaEjbHome;
    }

    public void setKatPemdaEjbHome(KategoriPemdaEjbHome katPemdaEjbHome) {
        this.katPemdaEjbHome = katPemdaEjbHome;
    }

    public List<AkunResiprokal> getListkode() {
        return listkode;
    }

    public void setListkode(List<AkunResiprokal> listkode) {
        this.listkode = listkode;
    }

    public HeaderResiprokal getHd() {
        return hd;
    }

    public void setHd(HeaderResiprokal hd) {
        this.hd = hd;
    }

    public int getIdbasakunres() {
        return idbasakunres;
    }

    public void setIdbasakunres(int idbasakunres) {
        this.idbasakunres = idbasakunres;
    }

    public int getHeaderid() {
        return headerid;
    }

    public void setHeaderid(int headerid) {
        this.headerid = headerid;
    }

    public AkunResiprokal getObj2() {
        return obj2;
    }

    public void setObj2(AkunResiprokal obj2) {
        this.obj2 = obj2;
    }

}
