/*
 * 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.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.RefPemdaEjbHome;
import sikd.ikonsol.ejb.session.RefWilayahEjbHome;
import sikd.ikonsol.model.ReferensiPemda;
import sikd.ikonsol.model.ReferensiWilayah;

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

    ReferensiWilayah wilayah;
    ReferensiWilayah obj;

    private List<ReferensiWilayah> list;

    private List<ReferensiPemda> list2;

    @EJB
    RefWilayahEjbHome wilayahEjbHome;

    @EJB
    RefPemdaEjbHome pemdaEjbHome;

    @PostConstruct
    public void init() {

        list = wilayahEjbHome.findAll();

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

    public void add() {

        wilayah = wilayahEjbHome.createNew();
        list2 = pemdaEjbHome.findByKodeWilayahId("-");

    }

    public String save() {

        String kdwil = wilayah.getKode().trim();

        ReferensiWilayah rf = new ReferensiWilayah();

        rf = wilayahEjbHome.findByKode(kdwil);

        try {
            if (rf == null) {

                System.out.println("INSERT");
                wilayah.setKode(kdwil);
                // simpan
                wilayahEjbHome.save(wilayah);

                PrimeFaces.current().executeScript("PF('wg_entryform1').hide()");
                PrimeFaces.current().ajax().update(":master:myTabView:dtwilayah");
                list = wilayahEjbHome.findAll();
            } else {
                FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO,
                        "Info", "Kode 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");
            PrimeFaces.current().dialog().showMessageDynamic(message);
        }

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

    public String update() {

        wilayahEjbHome.update(obj);

        PrimeFaces.current().executeScript("PF('wg_entryform_ubah1').hide()");
        PrimeFaces.current().ajax().update(":master:myTabView:dtwilayah");

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

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

    }

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

    public ReferensiWilayah getWilayah() {
        return wilayah;
    }

    public void setWilayah(ReferensiWilayah wilayah) {
        this.wilayah = wilayah;
    }

    public ReferensiWilayah getObj() {
        return obj;
    }

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

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

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

    public RefWilayahEjbHome getWilayahEjbHome() {
        return wilayahEjbHome;
    }

    public void setWilayahEjbHome(RefWilayahEjbHome wilayahEjbHome) {
        this.wilayahEjbHome = wilayahEjbHome;
    }

    public List<ReferensiPemda> getList2() {
        return list2;
    }

    public void setList2(List<ReferensiPemda> list2) {
        this.list2 = list2;
    }

}
