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

/**
 *
 * @author Imam Bhoim
 */
@ViewScoped
@Named(value = "pemdaBeanCtrl")
public class PemdaCtrl implements Serializable {
    
    ReferensiPemda pemda;
    ReferensiPemda obj;
    
    private List<ReferensiPemda> list, listprovinsi, listkode, list2, filterPemda;
    
    String kd;
    @EJB
    RefPemdaEjbHome pemdaEjbHome;
    
    @PostConstruct
    public void init() {
        
        list = pemdaEjbHome.findAll();
        list2 = pemdaEjbHome.findByKodeWilayahId("-");

        //obj = periodeEjbHome.findByKode("22");
        //periode = periodeEjbHome.createNew();
    }
    
    public void add() {
        
        pemda = pemdaEjbHome.createNew();
        listprovinsi = pemdaEjbHome.findAllProvinsi("__.00");
        
    }
    
    public void addWilayah() {

        //wilayah = wilayahEjbHome.createNew();
        obj = new ReferensiPemda();
        list2 = pemdaEjbHome.findByKodeWilayahId("-");
        
    }
    
    public String save() {

        // simpan
//        int idprov = Integer.parseInt(pemda.getId_provinsi().trim());
//        int id = Integer.parseInt(pemda.getId().trim());
//
//        if (idprov > 9) {
//            pemda.setId("" + idprov + "." + pemda.getId());
//        } else {
//            pemda.setId("0" + idprov + "." + pemda.getId());
//        }
        String idprov = pemda.getId_provinsi().trim();
        
        String a = pemda.getId();
        if (a.equalsIgnoreCase("1")) {
            pemda.setId("01");
        } else if (a.equalsIgnoreCase("2")) {
            pemda.setId("02");
        } else if (a.equalsIgnoreCase("3")) {
            pemda.setId("03");
        } else if (a.equalsIgnoreCase("4")) {
            pemda.setId("04");
        } else if (a.equalsIgnoreCase("5")) {
            pemda.setId("05");
        } else if (a.equalsIgnoreCase("6")) {
            pemda.setId("06");
        } else if (a.equalsIgnoreCase("7")) {
            pemda.setId("07");
        } else if (a.equalsIgnoreCase("8")) {
            pemda.setId("08");
        } else if (a.equalsIgnoreCase("9")) {
            pemda.setId("09");
        } else {
            pemda.setId(pemda.getId());
        }
        
        ReferensiPemda rf = new ReferensiPemda();
        
        String kd = idprov + "." + pemda.getId();
        
        rf = pemdaEjbHome.findByKodeId(kd);
        try {
            if (rf == null) {
                
                System.out.println("INSERT");
                pemda.setId(kd);
                pemda.setId_provinsi(idprov);
                
                pemdaEjbHome.save(pemda);
                
                PrimeFaces.current().executeScript("PF('wg_entryform').hide()");
                PrimeFaces.current().ajax().update(":master:myTabView:dtpemda");
                PrimeFaces.current().ajax().update(":master:myTabView:dtwilayah");
                list = pemdaEjbHome.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() {

//        int idprov = Integer.parseInt(obj.getId_provinsi().trim());
//
//        if (idprov > 9) {
//            obj.setId("" + idprov + "." + obj.getId());
//        } else {
//            obj.setId("0" + idprov + "." + obj.getId());
//        }
        obj.setId(obj.getId_provinsi() + "." + obj.getId());
        
        pemdaEjbHome.update(obj);
        
        PrimeFaces.current().executeScript("PF('wg_entryform_ubah').hide()");
        PrimeFaces.current().ajax().update(":master:myTabView:dtpemda");
        PrimeFaces.current().ajax().update(":master:myTabView:dtwilayah");
        
        list = pemdaEjbHome.findAll();

        // this.list = service.findAll();
        return "index?faces-redirect=true";
    }
    
    public void edit(String kode) {
        //obj = service.findByID(Integer.parseInt(id));
        obj = pemdaEjbHome.findByKodeId(kode);
        int idprov = Integer.parseInt(obj.getId_provinsi().trim());
        String b = obj.getId();
        String idambil = b.substring(3, 5);
        
        obj.setId(idambil);

//        if (idprov > 9) {
//            listprovinsi = pemdaEjbHome.findAllProvinsi(obj.getId());
//             System.out.println("Lebih 9 "+obj.getNama_pemda());
//        } else {
//            listprovinsi = pemdaEjbHome.findAllProvinsi(obj.getId());
//             System.out.println("kurang 10 "+obj.getNama_pemda());
//        }
    }
    
    public String updateWilayah() {
        
        obj.setId_wilayah(kd);
        
        pemdaEjbHome.updateWilayah(obj);
        
        PrimeFaces.current().executeScript("PF('wg_entryform2').hide()");
        PrimeFaces.current().ajax().update(":master:myTabView:dtpemda");
        PrimeFaces.current().ajax().update(":master:myTabView:dtwilayah");
        list2 = pemdaEjbHome.findByKodeWilayahId(obj.getId_wilayah());
        
        return "index?faces-redirect=true";
    }
    
    public void delete(String kode) {
        //obj = service.findByID(Integer.parseInt(id));
        //obj = periodeEjbHome.findByKode(kode);
        pemdaEjbHome.delete(kode);
        PrimeFaces.current().ajax().update(":master:myTabView:dtpemda");
        PrimeFaces.current().ajax().update(":master:myTabView:dtwilayah");
        list = pemdaEjbHome.findAll();
    }
    
    public void deleteWilayah(String kode) {
        //obj = service.findByID(Integer.parseInt(id));
        //obj = periodeEjbHome.findByKode(kode);
        System.out.println("A " + kode);
        
        obj = new ReferensiPemda();
        
        obj.setId_wilayah("-");
        obj.setId(kode);

        //pemdaEjbHome.delete(kode);
        pemdaEjbHome.updateWilayah(obj);
        PrimeFaces.current().ajax().update(":master:myTabView:dtpemda");
        PrimeFaces.current().ajax().update(":master:myTabView:dtwilayah");
        list = pemdaEjbHome.findAll();
    }
    
    public void listkode(String kdwil) {
        System.out.println("Wilayah Id : " + kdwil);
        listkode = pemdaEjbHome.findByKodeWilayahId(kdwil);
        
        kd = kdwil;
        
    }
    
    public ReferensiPemda getPemda() {
        return pemda;
    }
    
    public void setPemda(ReferensiPemda pemda) {
        this.pemda = pemda;
    }
    
    public ReferensiPemda getObj() {
        return obj;
    }
    
    public void setObj(ReferensiPemda obj) {
        this.obj = obj;
    }
    
    public List<ReferensiPemda> getList() {
        return list;
    }
    
    public void setList(List<ReferensiPemda> list) {
        this.list = list;
    }
    
    public RefPemdaEjbHome getPemdaEjbHome() {
        return pemdaEjbHome;
    }
    
    public void setPemdaEjbHome(RefPemdaEjbHome pemdaEjbHome) {
        this.pemdaEjbHome = pemdaEjbHome;
    }
    
    public List<ReferensiPemda> getListprovinsi() {
        return listprovinsi;
    }
    
    public void setListprovinsi(List<ReferensiPemda> listprovinsi) {
        this.listprovinsi = listprovinsi;
    }
    
    public List<ReferensiPemda> getListkode() {
        return listkode;
    }
    
    public void setListkode(List<ReferensiPemda> listkode) {
        this.listkode = listkode;
    }
    
    public List<ReferensiPemda> getList2() {
        return list2;
    }
    
    public void setList2(List<ReferensiPemda> list2) {
        this.list2 = list2;
    }
    
    public String getKd() {
        return kd;
    }
    
    public void setKd(String kd) {
        this.kd = kd;
    }
    
    public List<ReferensiPemda> getFilterPemda() {
        return filterPemda;
    }
    
    public void setFilterPemda(List<ReferensiPemda> filterPemda) {
        this.filterPemda = filterPemda;
    }
    
}
