/*
 * 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.Calendar;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import org.primefaces.PrimeFaces;
import sikd.ikonsol.ejb.session.InformasiEjbHome;
import sikd.ikonsol.model.Informasi;

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

    Informasi informasi, overview, disclaimer;
    Informasi obj;
    private List<Informasi> listInfoApp, listDokApp, listPeraturan;

    private Date tglversi;
    private Date tgldok;
    private Date tglperaturan;

    @EJB
    InformasiEjbHome informasiEjbHome;

    @PostConstruct
    public void init() {

        listInfoApp = informasiEjbHome.findAllInfoApp();
        listDokApp = informasiEjbHome.findAllDokApp();
        listPeraturan = informasiEjbHome.findAllPeraturanApp();

        overview = informasiEjbHome.findByIDOverview(1);

        disclaimer = informasiEjbHome.findByIDDisclaimer(1);

        //obj = periodeEjbHome.findByKode("22");
        //periode = periodeEjbHome.createNew();
        tglversi = Calendar.getInstance().getTime();
        tgldok = Calendar.getInstance().getTime();
        tglperaturan = Calendar.getInstance().getTime();
    }

    public Informasi getInformasi() {
        return informasi;
    }

    public void setInformasi(Informasi informasi) {
        this.informasi = informasi;
    }

    public Informasi getObj() {
        return obj;
    }

    public void addVersi() {

        //periode = periodeEjbHome.createNew();
        informasi = informasiEjbHome.createNewInfoApp();
    }

    public void addDok() {

        //periode = periodeEjbHome.createNew();
        informasi = informasiEjbHome.createNewDokApp();
    }

    public void addPeraturan() {

        //periode = periodeEjbHome.createNew();
        informasi = informasiEjbHome.createNewPeraturanApp();
    }
//

    public String saveVersi() {

        informasi.setTglversi(new java.sql.Date(tglversi.getTime()));

        informasiEjbHome.saveInfoApp(informasi);

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

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

    public String saveDok() {

        informasi.setTgldok(new java.sql.Date(tgldok.getTime()));

        informasiEjbHome.saveDokApp(informasi);

        PrimeFaces.current().executeScript("PF('wg_entryform2').hide()");
        PrimeFaces.current().ajax().update(":master2:dtdok");

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

    public String savePeraturan() {

        informasi.setTglper(new java.sql.Date(tglperaturan.getTime()));

        informasiEjbHome.savePeraturanApp(informasi);

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

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

    public void editVersi(int id) {
        //obj = service.findByID(Integer.parseInt(id));
        obj = informasiEjbHome.findByIDInfoApp(id);

        tglversi = (Date) obj.getTglversi();

    }

    public void editDok(int id) {
        //obj = service.findByID(Integer.parseInt(id));
        obj = informasiEjbHome.findByIDDokApp(id);
        tgldok = (Date) obj.getTgldok();
    }

    public void editPer(int id) {
        //obj = service.findByID(Integer.parseInt(id));
        obj = informasiEjbHome.findByIDPeraturanApp(id);
        tglperaturan = (Date) obj.getTglper();
    }

//
    public String updateVersi() {

         obj.setTglversi(new java.sql.Date(tglversi.getTime()));
        
        informasiEjbHome.updateInfoApp(obj);

        PrimeFaces.current().executeScript("PF('wg_entryformubah').hide()");
        PrimeFaces.current().ajax().update(":master:dtinfo");

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

    public String updateDok() {

        
        obj.setTgldok(new java.sql.Date(tgldok.getTime()));

        
        informasiEjbHome.updateDokApp(obj);

        PrimeFaces.current().executeScript("PF('wg_entryform2').hide()");
        PrimeFaces.current().ajax().update(":master2:dtdok");

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

    public String updatePer() {

         obj.setTglper(new java.sql.Date(tglperaturan.getTime()));

        
        informasiEjbHome.updatePeraturanApp(obj);

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

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

    public void deleteVersi(int id) {
        //obj = service.findByID(Integer.parseInt(id));
        //obj = periodeEjbHome.findByKode(kode);
        informasiEjbHome.deleteInfoApp(id);
        PrimeFaces.current().ajax().update(":master:dtinfo");
        listInfoApp = informasiEjbHome.findAllInfoApp();
    }

    public void deleteDok(int id) {
        //obj = service.findByID(Integer.parseInt(id));
        //obj = periodeEjbHome.findByKode(kode);
        informasiEjbHome.deleteDokApp(id);
        PrimeFaces.current().ajax().update(":master2:dtdok");
        listDokApp = informasiEjbHome.findAllDokApp();
    }

    public void deletePer(int id) {
        //obj = service.findByID(Integer.parseInt(id));
        //obj = periodeEjbHome.findByKode(kode);
        informasiEjbHome.deletePeraturanApp(id);
        PrimeFaces.current().ajax().update(":master:dtper");
        listPeraturan = informasiEjbHome.findAllPeraturanApp();
    }
//
//    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 setObj(Informasi obj) {
        this.obj = obj;
    }

    public List<Informasi> getListInfoApp() {
        return listInfoApp;
    }

    public void setListInfoApp(List<Informasi> listInfoApp) {
        this.listInfoApp = listInfoApp;
    }

    public List<Informasi> getListDokApp() {
        return listDokApp;
    }

    public void setListDokApp(List<Informasi> listDokApp) {
        this.listDokApp = listDokApp;
    }

    public List<Informasi> getListPeraturan() {
        return listPeraturan;
    }

    public void setListPeraturan(List<Informasi> listPeraturan) {
        this.listPeraturan = listPeraturan;
    }

    public Date getTglversi() {
        return tglversi;
    }

    public void setTglversi(Date tglversi) {
        this.tglversi = tglversi;
    }

    public Date getTgldok() {
        return tgldok;
    }

    public void setTgldok(Date tgldok) {
        this.tgldok = tgldok;
    }

    public Date getTglperaturan() {
        return tglperaturan;
    }

    public void setTglperaturan(Date tglperaturan) {
        this.tglperaturan = tglperaturan;
    }

    public Informasi getOverview() {
        return overview;
    }

    public void setOverview(Informasi overview) {
        this.overview = overview;
    }

    public Informasi getDisclaimer() {
        return disclaimer;
    }

    public void setDisclaimer(Informasi disclaimer) {
        this.disclaimer = disclaimer;
    }

}
