/*
 * 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.context.FacesContext;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import org.primefaces.PrimeFaces;

import sikd.ikonsol.model.Periode;
import sikd.ikonsol.ejb.session.PeriodeEjbHome;

/**
 *
 * @author User
 */
@ViewScoped
@Named(value = "periodeBeanCtrl")
public class PeriodeBeanCtrl implements Serializable {

    Periode periode;
    Periode obj;
    private List<Periode> list, listexp;

    private String kode;
    private String nama;
    private int status;

    @EJB
    PeriodeEjbHome periodeEjbHome;

    @PostConstruct
    public void init() {

        list = periodeEjbHome.findAll();

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

    public void add() {

        periode = periodeEjbHome.createNew();

    }

    public String save() {

        Periode a = new Periode();
        try {
            a = periodeEjbHome.findByKode(a.getKode());
            if (a == null) {
                periodeEjbHome.save(periode);

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

                list = periodeEjbHome.findAll();
                //this.list = service.findAll();
            }
        } catch (Exception ex) {
            System.out.println("GAGAL!");
//            FacesContext.getCurrentInstance().addMessage(null, 
//                    new FacesMessage(FacesMessage.SEVERITY_ERROR, "Info!", "Kode Sudah Ada"));
//            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 Sudah Ada");
            PrimeFaces.current().dialog().showMessageDynamic(message);
        }

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

    public String update() {

        periodeEjbHome.update(obj);

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

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

    public void edit(String kode) {
        //obj = service.findByID(Integer.parseInt(id));
        obj = periodeEjbHome.findByKode(kode);

    }

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

    }

    public void validateInput() {

    }

    public void listkode(String kode) {
        listexp = periodeEjbHome.findAllKode(kode);
    }

    public Periode getPeriode() {
        return periode;
    }

    public void setPeriode(Periode periode) {
        this.periode = periode;
    }

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

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

    public String getKode() {
        return kode;
    }

    public void setKode(String kode) {
        this.kode = kode;
    }

    public String getNama() {
        return nama;
    }

    public void setNama(String nama) {
        this.nama = nama;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public PeriodeEjbHome getPeriodeEjbHome() {
        return periodeEjbHome;
    }

    public void setPeriodeEjbHome(PeriodeEjbHome periodeEjbHome) {
        this.periodeEjbHome = periodeEjbHome;
    }

    public Periode getObj() {
        return obj;
    }

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

    public List<Periode> getListexp() {
        return listexp;
    }

    public void setListexp(List<Periode> listexp) {
        this.listexp = listexp;
    }

}
